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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { ReducerRegistry, setStateProperty } from '../redux';
  2. import {
  3. DOMINANT_SPEAKER_CHANGED,
  4. PARTICIPANT_ID_CHANGED,
  5. PARTICIPANT_JOINED,
  6. PARTICIPANT_LEFT,
  7. PARTICIPANT_UPDATED,
  8. PIN_PARTICIPANT
  9. } from './actionTypes';
  10. import {
  11. LOCAL_PARTICIPANT_DEFAULT_ID,
  12. PARTICIPANT_ROLE
  13. } from './constants';
  14. import { getAvatarURL } from './functions';
  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. const id = action.newValue;
  61. const newState = {
  62. ...state,
  63. id
  64. };
  65. if (!newState.avatar) {
  66. newState.avatar = getAvatarURL(newState);
  67. }
  68. return newState;
  69. }
  70. break;
  71. case PARTICIPANT_JOINED: {
  72. const participant = action.participant; // eslint-disable-line no-shadow
  73. const { avatar, dominantSpeaker, email, local, pinned, role }
  74. = participant;
  75. let { id, name } = participant;
  76. // id
  77. //
  78. // XXX The situation of not having an ID for a remote participant should
  79. // not happen. Maybe we should raise an error in this case or generate a
  80. // random ID.
  81. if (!id && local) {
  82. id = LOCAL_PARTICIPANT_DEFAULT_ID;
  83. }
  84. // name
  85. if (!name) {
  86. // TODO Get the from config and/or localized.
  87. name = local ? 'me' : 'Fellow Jitster';
  88. }
  89. const newState = {
  90. avatar,
  91. dominantSpeaker: dominantSpeaker || false,
  92. email,
  93. id,
  94. local: local || false,
  95. name,
  96. pinned: pinned || false,
  97. role: role || PARTICIPANT_ROLE.NONE
  98. };
  99. if (!newState.avatar) {
  100. newState.avatar = getAvatarURL(newState);
  101. }
  102. return newState;
  103. }
  104. case PARTICIPANT_UPDATED: {
  105. const participant = action.participant; // eslint-disable-line no-shadow
  106. const { id } = participant;
  107. if (state.id === id) {
  108. const newState = { ...state };
  109. for (const key in participant) {
  110. if (participant.hasOwnProperty(key)
  111. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  112. === -1) {
  113. newState[key] = participant[key];
  114. }
  115. }
  116. if (!newState.avatar) {
  117. newState.avatar = getAvatarURL(newState);
  118. }
  119. return newState;
  120. }
  121. break;
  122. }
  123. case PIN_PARTICIPANT:
  124. // Currently, only one pinned participant is allowed.
  125. return (
  126. setStateProperty(
  127. state,
  128. 'pinned',
  129. 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. });