Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

reducer.js 5.5KB

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