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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { ReducerRegistry, set } from '../redux';
  2. import { randomHexString } from '../util';
  3. import {
  4. DOMINANT_SPEAKER_CHANGED,
  5. PARTICIPANT_ID_CHANGED,
  6. PARTICIPANT_JOINED,
  7. PARTICIPANT_LEFT,
  8. PARTICIPANT_UPDATED,
  9. PIN_PARTICIPANT
  10. } from './actionTypes';
  11. import {
  12. LOCAL_PARTICIPANT_DEFAULT_ID,
  13. PARTICIPANT_ROLE
  14. } from './constants';
  15. /**
  16. * Participant object.
  17. * @typedef {Object} Participant
  18. * @property {string} id - Participant ID.
  19. * @property {string} name - Participant name.
  20. * @property {string} avatar - Path to participant avatar if any.
  21. * @property {string} role - Participant role.
  22. * @property {boolean} local - If true, participant is local.
  23. * @property {boolean} pinned - If true, participant is currently a
  24. * "PINNED_ENDPOINT".
  25. * @property {boolean} dominantSpeaker - If this participant is the dominant
  26. * speaker in the (associated) conference, <tt>true</tt>; otherwise,
  27. * <tt>false</tt>.
  28. * @property {string} email - Participant email.
  29. */
  30. /**
  31. * These properties should not be bulk assigned when updating a particular
  32. * @see Participant.
  33. * @type {string[]}
  34. */
  35. const PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE
  36. = [ 'dominantSpeaker', 'id', 'local', 'pinned' ];
  37. /**
  38. * Reducer function for a single participant.
  39. *
  40. * @param {Participant|undefined} state - Participant 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/modified.
  45. * @param {JitsiConference} action.conference - Conference instance.
  46. * @private
  47. * @returns {Participant|undefined}
  48. */
  49. function _participant(state, action) {
  50. switch (action.type) {
  51. case DOMINANT_SPEAKER_CHANGED:
  52. // Only one dominant speaker is allowed.
  53. return (
  54. set(state, 'dominantSpeaker', state.id === action.participant.id));
  55. case PARTICIPANT_ID_CHANGED:
  56. if (state.id === action.oldValue) {
  57. return {
  58. ...state,
  59. id: action.newValue
  60. };
  61. }
  62. break;
  63. case PARTICIPANT_JOINED: {
  64. const participant = action.participant; // eslint-disable-line no-shadow
  65. const {
  66. avatarURL,
  67. connectionStatus,
  68. dominantSpeaker,
  69. email,
  70. isBot,
  71. local,
  72. pinned,
  73. role
  74. } = participant;
  75. let { avatarID, id, name } = participant;
  76. // avatarID
  77. //
  78. // TODO Get the avatarID of the local participant from localStorage.
  79. if (!avatarID && local) {
  80. avatarID = randomHexString(32);
  81. }
  82. // id
  83. //
  84. // XXX The situation of not having an ID for a remote participant should
  85. // not happen. Maybe we should raise an error in this case or generate a
  86. // random ID.
  87. if (!id && local) {
  88. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  89. }
  90. // name
  91. if (!name) {
  92. // TODO Get the from config and/or localized.
  93. name = local ? 'me' : 'Fellow Jitster';
  94. }
  95. return {
  96. avatarID,
  97. avatarURL,
  98. connectionStatus,
  99. dominantSpeaker: dominantSpeaker || false,
  100. email,
  101. id,
  102. isBot,
  103. local: local || false,
  104. name,
  105. pinned: pinned || false,
  106. role: role || PARTICIPANT_ROLE.NONE
  107. };
  108. }
  109. case PARTICIPANT_UPDATED: {
  110. const participant = action.participant; // eslint-disable-line no-shadow
  111. const { local } = participant;
  112. let { id } = participant;
  113. if (!id && local) {
  114. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  115. }
  116. if (state.id === id) {
  117. const newState = { ...state };
  118. for (const key in participant) {
  119. if (participant.hasOwnProperty(key)
  120. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  121. === -1) {
  122. newState[key] = participant[key];
  123. }
  124. }
  125. return newState;
  126. }
  127. break;
  128. }
  129. case PIN_PARTICIPANT:
  130. // Currently, only one pinned participant is allowed.
  131. return set(state, 'pinned', state.id === action.participant.id);
  132. }
  133. return state;
  134. }
  135. /**
  136. * Listen for actions which add, remove, or update the set of participants in
  137. * the conference.
  138. *
  139. * @param {Participant[]} state - List of participants to be modified.
  140. * @param {Object} action - Action object.
  141. * @param {string} action.type - Type of action.
  142. * @param {Participant} action.participant - Information about participant to be
  143. * added/removed/modified.
  144. * @returns {Participant[]}
  145. */
  146. ReducerRegistry.register('features/base/participants', (state = [], action) => {
  147. switch (action.type) {
  148. case PARTICIPANT_JOINED:
  149. return [ ...state, _participant(undefined, action) ];
  150. case PARTICIPANT_LEFT:
  151. return state.filter(p => p.id !== action.participant.id);
  152. case DOMINANT_SPEAKER_CHANGED:
  153. case PARTICIPANT_ID_CHANGED:
  154. case PARTICIPANT_UPDATED:
  155. case PIN_PARTICIPANT:
  156. return state.map(p => _participant(p, action));
  157. default:
  158. return state;
  159. }
  160. });