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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. local,
  71. pinned,
  72. role
  73. } = participant;
  74. let { avatarID, id, name } = participant;
  75. // avatarID
  76. //
  77. // TODO Get the avatarID of the local participant from localStorage.
  78. if (!avatarID && local) {
  79. avatarID = randomHexString(32);
  80. }
  81. // id
  82. //
  83. // XXX The situation of not having an ID for a remote participant should
  84. // not happen. Maybe we should raise an error in this case or generate a
  85. // random ID.
  86. if (!id && local) {
  87. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  88. }
  89. // name
  90. if (!name) {
  91. // TODO Get the from config and/or localized.
  92. name = local ? 'me' : 'Fellow Jitster';
  93. }
  94. return {
  95. avatarID,
  96. avatarURL,
  97. connectionStatus,
  98. dominantSpeaker: dominantSpeaker || false,
  99. email,
  100. id,
  101. local: local || false,
  102. name,
  103. pinned: pinned || false,
  104. role: role || PARTICIPANT_ROLE.NONE
  105. };
  106. }
  107. case PARTICIPANT_UPDATED: {
  108. const participant = action.participant; // eslint-disable-line no-shadow
  109. const { local } = participant;
  110. let { id } = participant;
  111. if (!id && local) {
  112. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  113. }
  114. if (state.id === id) {
  115. const newState = { ...state };
  116. for (const key in participant) {
  117. if (participant.hasOwnProperty(key)
  118. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  119. === -1) {
  120. newState[key] = participant[key];
  121. }
  122. }
  123. return newState;
  124. }
  125. break;
  126. }
  127. case PIN_PARTICIPANT:
  128. // Currently, only one pinned participant is allowed.
  129. return set(state, 'pinned', state.id === action.participant.id);
  130. }
  131. return state;
  132. }
  133. /**
  134. * Listen for actions which add, remove, or update the set of participants in
  135. * the conference.
  136. *
  137. * @param {Participant[]} state - List of participants to be modified.
  138. * @param {Object} action - Action object.
  139. * @param {string} action.type - Type of action.
  140. * @param {Participant} action.participant - Information about participant to be
  141. * added/removed/modified.
  142. * @returns {Participant[]}
  143. */
  144. ReducerRegistry.register('features/base/participants', (state = [], action) => {
  145. switch (action.type) {
  146. case PARTICIPANT_JOINED:
  147. return [ ...state, _participant(undefined, action) ];
  148. case PARTICIPANT_LEFT:
  149. return state.filter(p => p.id !== action.participant.id);
  150. case DOMINANT_SPEAKER_CHANGED:
  151. case PARTICIPANT_ID_CHANGED:
  152. case PARTICIPANT_UPDATED:
  153. case PIN_PARTICIPANT:
  154. return state.map(p => _participant(p, action));
  155. default:
  156. return state;
  157. }
  158. });