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

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