選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

reducer.js 6.8KB

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