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 5.9KB

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