Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

reducer.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. * These properties should not be bulk assigned when updating a particular
  31. * @see Participant.
  32. * @type {string[]}
  33. */
  34. const PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE
  35. = [ 'dominantSpeaker', 'id', 'local', 'pinned' ];
  36. /**
  37. * Listen for actions which add, remove, or update the set of participants in
  38. * the conference.
  39. *
  40. * @param {Participant[]} state - List of participants to be modified.
  41. * @param {Object} action - Action object.
  42. * @param {string} action.type - Type of action.
  43. * @param {Participant} action.participant - Information about participant to be
  44. * added/removed/modified.
  45. * @returns {Participant[]}
  46. */
  47. ReducerRegistry.register('features/base/participants', (state = [], action) => {
  48. switch (action.type) {
  49. case DOMINANT_SPEAKER_CHANGED:
  50. case PARTICIPANT_ID_CHANGED:
  51. case PARTICIPANT_UPDATED:
  52. case PIN_PARTICIPANT:
  53. return state.map(p => _participant(p, action));
  54. case PARTICIPANT_JOINED:
  55. return [ ...state, _participantJoined(action) ];
  56. case PARTICIPANT_LEFT:
  57. return state.filter(p => p.id !== action.participant.id);
  58. }
  59. return state;
  60. });
  61. /**
  62. * Reducer function for a single participant.
  63. *
  64. * @param {Participant|undefined} state - Participant to be modified.
  65. * @param {Object} action - Action object.
  66. * @param {string} action.type - Type of action.
  67. * @param {Participant} action.participant - Information about participant to be
  68. * added/modified.
  69. * @param {JitsiConference} action.conference - Conference instance.
  70. * @private
  71. * @returns {Participant}
  72. */
  73. function _participant(state: Object = {}, action) {
  74. switch (action.type) {
  75. case DOMINANT_SPEAKER_CHANGED:
  76. // Only one dominant speaker is allowed.
  77. return (
  78. set(state, 'dominantSpeaker', state.id === action.participant.id));
  79. case PARTICIPANT_ID_CHANGED:
  80. if (state.id === action.oldValue) {
  81. return {
  82. ...state,
  83. id: action.newValue
  84. };
  85. }
  86. break;
  87. case PARTICIPANT_UPDATED: {
  88. const { participant } = action; // eslint-disable-line no-shadow
  89. let { id } = participant;
  90. const { local } = participant;
  91. if (!id && local) {
  92. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  93. }
  94. if (state.id === id) {
  95. const newState = { ...state };
  96. for (const key in participant) {
  97. if (participant.hasOwnProperty(key)
  98. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  99. === -1) {
  100. newState[key] = participant[key];
  101. }
  102. }
  103. return newState;
  104. }
  105. break;
  106. }
  107. case PIN_PARTICIPANT:
  108. // Currently, only one pinned participant is allowed.
  109. return set(state, 'pinned', state.id === action.participant.id);
  110. }
  111. return state;
  112. }
  113. /**
  114. * Reduces a specific redux action of type {@link PARTICIPANT_JOINED} in the
  115. * feature base/participants.
  116. *
  117. * @param {Action} action - The redux action of type {@code PARTICIPANT_JOINED}
  118. * to reduce.
  119. * @private
  120. * @returns {Object} The new participant derived from the payload of the
  121. * specified {@code action} to be added into the redux state of the feature
  122. * base/participants after the reduction of the specified
  123. * {@code action}.
  124. */
  125. function _participantJoined({ participant }) {
  126. const {
  127. avatarURL,
  128. connectionStatus,
  129. dominantSpeaker,
  130. email,
  131. isBot,
  132. local,
  133. name,
  134. pinned,
  135. role
  136. } = participant;
  137. let { avatarID, conference, id } = participant;
  138. if (local) {
  139. // avatarID
  140. //
  141. // TODO Get the avatarID of the local participant from localStorage.
  142. avatarID || (avatarID = randomHexString(32));
  143. // conference
  144. //
  145. // XXX The local participant is not identified in association with a
  146. // JitsiConference because it is identified by the very fact that it is
  147. // the local participant.
  148. conference = undefined;
  149. // id
  150. id || (id = LOCAL_PARTICIPANT_DEFAULT_ID);
  151. }
  152. return {
  153. avatarID,
  154. avatarURL,
  155. conference,
  156. connectionStatus,
  157. dominantSpeaker: dominantSpeaker || false,
  158. email,
  159. id,
  160. isBot,
  161. local: local || false,
  162. name,
  163. pinned: pinned || false,
  164. role: role || PARTICIPANT_ROLE.NONE
  165. };
  166. }