您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

reducer.js 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // @flow
  2. import { randomHexString } from 'js-utils/random';
  3. import { ReducerRegistry, set } from '../redux';
  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. // XXX Do not allow collisions in the IDs of the local
  77. // participant and a remote participant cause the removal of
  78. // the local participant when the remote participant's
  79. // removal is requested.
  80. && p.conference === conference
  81. && (conference || p.local)));
  82. }
  83. }
  84. return state;
  85. });
  86. /**
  87. * Reducer function for a single participant.
  88. *
  89. * @param {Participant|undefined} state - Participant to be modified.
  90. * @param {Object} action - Action object.
  91. * @param {string} action.type - Type of action.
  92. * @param {Participant} action.participant - Information about participant to be
  93. * added/modified.
  94. * @param {JitsiConference} action.conference - Conference instance.
  95. * @private
  96. * @returns {Participant}
  97. */
  98. function _participant(state: Object = {}, action) {
  99. switch (action.type) {
  100. case DOMINANT_SPEAKER_CHANGED:
  101. // Only one dominant speaker is allowed.
  102. return (
  103. set(state, 'dominantSpeaker', state.id === action.participant.id));
  104. case PARTICIPANT_ID_CHANGED: {
  105. // A participant is identified by an id-conference pair. Only the local
  106. // participant is with an undefined conference.
  107. const { conference } = action;
  108. if (state.id === action.oldValue
  109. && state.conference === conference
  110. && (conference || state.local)) {
  111. return {
  112. ...state,
  113. id: action.newValue
  114. };
  115. }
  116. break;
  117. }
  118. case PARTICIPANT_UPDATED: {
  119. const { participant } = action; // eslint-disable-line no-shadow
  120. let { id } = participant;
  121. const { local } = participant;
  122. if (!id && local) {
  123. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  124. }
  125. if (state.id === id) {
  126. const newState = { ...state };
  127. for (const key in participant) {
  128. if (participant.hasOwnProperty(key)
  129. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  130. === -1) {
  131. newState[key] = participant[key];
  132. }
  133. }
  134. return newState;
  135. }
  136. break;
  137. }
  138. case PIN_PARTICIPANT:
  139. // Currently, only one pinned participant is allowed.
  140. return set(state, 'pinned', state.id === action.participant.id);
  141. }
  142. return state;
  143. }
  144. /**
  145. * Reduces a specific redux action of type {@link PARTICIPANT_JOINED} in the
  146. * feature base/participants.
  147. *
  148. * @param {Action} action - The redux action of type {@code PARTICIPANT_JOINED}
  149. * to reduce.
  150. * @private
  151. * @returns {Object} The new participant derived from the payload of the
  152. * specified {@code action} to be added into the redux state of the feature
  153. * base/participants after the reduction of the specified
  154. * {@code action}.
  155. */
  156. function _participantJoined({ participant }) {
  157. const {
  158. avatarURL,
  159. botType,
  160. connectionStatus,
  161. dominantSpeaker,
  162. email,
  163. isFakeParticipant,
  164. local,
  165. name,
  166. pinned,
  167. presence,
  168. role
  169. } = participant;
  170. let { avatarID, conference, id } = participant;
  171. if (local) {
  172. // avatarID
  173. //
  174. // TODO Get the avatarID of the local participant from localStorage.
  175. avatarID || (avatarID = randomHexString(32));
  176. // conference
  177. //
  178. // XXX The local participant is not identified in association with a
  179. // JitsiConference because it is identified by the very fact that it is
  180. // the local participant.
  181. conference = undefined;
  182. // id
  183. id || (id = LOCAL_PARTICIPANT_DEFAULT_ID);
  184. }
  185. return {
  186. avatarID,
  187. avatarURL,
  188. botType,
  189. conference,
  190. connectionStatus,
  191. dominantSpeaker: dominantSpeaker || false,
  192. email,
  193. id,
  194. isFakeParticipant,
  195. local: local || false,
  196. name,
  197. pinned: pinned || false,
  198. presence,
  199. role: role || PARTICIPANT_ROLE.NONE
  200. };
  201. }