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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 { id } = participant;
  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 (
  121. setStateProperty(
  122. state,
  123. 'pinned',
  124. state.id === action.participant.id));
  125. }
  126. return state;
  127. }
  128. /**
  129. * Listen for actions which add, remove, or update the set of participants in
  130. * the conference.
  131. *
  132. * @param {Participant[]} state - List of participants to be modified.
  133. * @param {Object} action - Action object.
  134. * @param {string} action.type - Type of action.
  135. * @param {Participant} action.participant - Information about participant to be
  136. * added/removed/modified.
  137. * @returns {Participant[]}
  138. */
  139. ReducerRegistry.register('features/base/participants', (state = [], action) => {
  140. switch (action.type) {
  141. case PARTICIPANT_JOINED:
  142. return [ ...state, _participant(undefined, action) ];
  143. case PARTICIPANT_LEFT:
  144. return state.filter(p => p.id !== action.participant.id);
  145. case DOMINANT_SPEAKER_CHANGED:
  146. case PARTICIPANT_ID_CHANGED:
  147. case PARTICIPANT_UPDATED:
  148. case PIN_PARTICIPANT:
  149. return state.map(p => _participant(p, action));
  150. default:
  151. return state;
  152. }
  153. });