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

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