You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reducer.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // @flow
  2. import { ReducerRegistry, set } from '../redux';
  3. import { randomHexString } from '../util';
  4. import {
  5. DOMINANT_SPEAKER_CHANGED,
  6. PARTICIPANT_ID_CHANGED,
  7. PARTICIPANT_JOINED,
  8. PARTICIPANT_LEFT,
  9. PARTICIPANT_UPDATED,
  10. PIN_PARTICIPANT
  11. } from './actionTypes';
  12. import { LOCAL_PARTICIPANT_DEFAULT_ID, PARTICIPANT_ROLE } from './constants';
  13. /**
  14. * Participant object.
  15. * @typedef {Object} Participant
  16. * @property {string} id - Participant ID.
  17. * @property {string} name - Participant name.
  18. * @property {string} avatar - Path to participant avatar if any.
  19. * @property {string} role - Participant role.
  20. * @property {boolean} local - If true, participant is local.
  21. * @property {boolean} pinned - If true, participant is currently a
  22. * "PINNED_ENDPOINT".
  23. * @property {boolean} dominantSpeaker - If this participant is the dominant
  24. * speaker in the (associated) conference, {@code true}; otherwise,
  25. * {@code false}.
  26. * @property {string} email - Participant email.
  27. */
  28. declare var APP: Object;
  29. /**
  30. * The participant properties which cannot be updated through
  31. * {@link PARTICIPANT_UPDATED}. They either identify the participant or can only
  32. * be modified through property-dedicated actions.
  33. *
  34. * @type {string[]}
  35. */
  36. const PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE = [
  37. // The following properties identify the participant:
  38. 'conference',
  39. 'id',
  40. 'local',
  41. // The following properties can only be modified through property-dedicated
  42. // actions:
  43. 'dominantSpeaker',
  44. 'pinned'
  45. ];
  46. /**
  47. * Listen for actions which add, remove, or update the set of participants in
  48. * the conference.
  49. *
  50. * @param {Participant[]} state - List of participants to be modified.
  51. * @param {Object} action - Action object.
  52. * @param {string} action.type - Type of action.
  53. * @param {Participant} action.participant - Information about participant to be
  54. * added/removed/modified.
  55. * @returns {Participant[]}
  56. */
  57. ReducerRegistry.register('features/base/participants', (state = [], action) => {
  58. switch (action.type) {
  59. case DOMINANT_SPEAKER_CHANGED:
  60. case PARTICIPANT_ID_CHANGED:
  61. case PARTICIPANT_UPDATED:
  62. case PIN_PARTICIPANT:
  63. return state.map(p => _participant(p, action));
  64. case PARTICIPANT_JOINED:
  65. return [ ...state, _participantJoined(action) ];
  66. case PARTICIPANT_LEFT: {
  67. // XXX A remote participant is uniquely identified by their id in a
  68. // specific JitsiConference instance. The local participant is uniquely
  69. // identified by the very fact that there is only one local participant
  70. // (and the fact that the local participant "joins" at the beginning of
  71. // the app and "leaves" at the end of the app).
  72. const { conference, id } = action.participant;
  73. return state.filter(p =>
  74. !(
  75. p.id === id
  76. && (p.local
  77. || (conference && p.conference === conference))));
  78. }
  79. }
  80. return state;
  81. });
  82. /**
  83. * Reducer function for a single participant.
  84. *
  85. * @param {Participant|undefined} state - Participant to be modified.
  86. * @param {Object} action - Action object.
  87. * @param {string} action.type - Type of action.
  88. * @param {Participant} action.participant - Information about participant to be
  89. * added/modified.
  90. * @param {JitsiConference} action.conference - Conference instance.
  91. * @private
  92. * @returns {Participant}
  93. */
  94. function _participant(state: Object = {}, action) {
  95. switch (action.type) {
  96. case DOMINANT_SPEAKER_CHANGED:
  97. // Only one dominant speaker is allowed.
  98. return (
  99. set(state, 'dominantSpeaker', state.id === action.participant.id));
  100. case PARTICIPANT_ID_CHANGED:
  101. if (state.id === action.oldValue) {
  102. return {
  103. ...state,
  104. id: action.newValue
  105. };
  106. }
  107. break;
  108. case PARTICIPANT_UPDATED: {
  109. const { participant } = action; // eslint-disable-line no-shadow
  110. let { id } = participant;
  111. const { local } = participant;
  112. if (!id && local) {
  113. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  114. }
  115. if (state.id === id) {
  116. const newState = { ...state };
  117. for (const key in participant) {
  118. if (participant.hasOwnProperty(key)
  119. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  120. === -1) {
  121. newState[key] = participant[key];
  122. }
  123. }
  124. return newState;
  125. }
  126. break;
  127. }
  128. case PIN_PARTICIPANT:
  129. // Currently, only one pinned participant is allowed.
  130. return set(state, 'pinned', state.id === action.participant.id);
  131. }
  132. return state;
  133. }
  134. /**
  135. * Reduces a specific redux action of type {@link PARTICIPANT_JOINED} in the
  136. * feature base/participants.
  137. *
  138. * @param {Action} action - The redux action of type {@code PARTICIPANT_JOINED}
  139. * to reduce.
  140. * @private
  141. * @returns {Object} The new participant derived from the payload of the
  142. * specified {@code action} to be added into the redux state of the feature
  143. * base/participants after the reduction of the specified
  144. * {@code action}.
  145. */
  146. function _participantJoined({ participant }) {
  147. const {
  148. avatarURL,
  149. connectionStatus,
  150. dominantSpeaker,
  151. email,
  152. isBot,
  153. local,
  154. name,
  155. pinned,
  156. presence,
  157. role
  158. } = participant;
  159. let { avatarID, conference, id } = participant;
  160. if (local) {
  161. // avatarID
  162. //
  163. // TODO Get the avatarID of the local participant from localStorage.
  164. avatarID || (avatarID = randomHexString(32));
  165. // conference
  166. //
  167. // XXX The local participant is not identified in association with a
  168. // JitsiConference because it is identified by the very fact that it is
  169. // the local participant.
  170. conference = undefined;
  171. // id
  172. id || (id = LOCAL_PARTICIPANT_DEFAULT_ID);
  173. }
  174. return {
  175. avatarID,
  176. avatarURL,
  177. conference,
  178. connectionStatus,
  179. dominantSpeaker: dominantSpeaker || false,
  180. email,
  181. id,
  182. isBot,
  183. local: local || false,
  184. name,
  185. pinned: pinned || false,
  186. presence,
  187. role: role || PARTICIPANT_ROLE.NONE
  188. };
  189. }