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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* global MD5 */
  2. import { ReducerRegistry } from '../redux';
  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. * @returns {Participant|undefined}
  47. */
  48. function participant(state, action) {
  49. switch (action.type) {
  50. case DOMINANT_SPEAKER_CHANGED:
  51. // Only one dominant speaker is allowed.
  52. return (
  53. _setStateProperty(
  54. state,
  55. 'dominantSpeaker',
  56. state.id === action.participant.id));
  57. case PARTICIPANT_ID_CHANGED:
  58. if (state.id === action.oldValue) {
  59. const id = action.newValue;
  60. return {
  61. ...state,
  62. id,
  63. avatar: state.avatar || _getAvatarURL(id, state.email)
  64. };
  65. }
  66. break;
  67. case PARTICIPANT_JOINED: {
  68. const participant = action.participant; // eslint-disable-line no-shadow
  69. // XXX The situation of not having an ID for a remote participant should
  70. // not happen. Maybe we should raise an error in this case or generate a
  71. // random ID.
  72. const id
  73. = participant.id
  74. || (participant.local && LOCAL_PARTICIPANT_DEFAULT_ID);
  75. const avatar
  76. = participant.avatar || _getAvatarURL(id, participant.email);
  77. // TODO Get these names from config/localized.
  78. const name
  79. = participant.name || (participant.local ? 'me' : 'Fellow Jitster');
  80. return {
  81. avatar,
  82. email: participant.email,
  83. id,
  84. local: participant.local || false,
  85. name,
  86. pinned: participant.pinned || false,
  87. role: participant.role || PARTICIPANT_ROLE.NONE,
  88. dominantSpeaker: participant.dominantSpeaker || false
  89. };
  90. }
  91. case PARTICIPANT_UPDATED:
  92. if (state.id === action.participant.id) {
  93. const newState = { ...state };
  94. for (const key in action.participant) {
  95. if (action.participant.hasOwnProperty(key)
  96. && PARTICIPANT_PROPS_TO_OMIT_WHEN_UPDATE.indexOf(key)
  97. === -1) {
  98. newState[key] = action.participant[key];
  99. }
  100. }
  101. if (!newState.avatar) {
  102. newState.avatar
  103. = _getAvatarURL(action.participant.id, newState.email);
  104. }
  105. return newState;
  106. }
  107. break;
  108. case PIN_PARTICIPANT:
  109. // Currently, only one pinned participant is allowed.
  110. return (
  111. _setStateProperty(
  112. state,
  113. 'pinned',
  114. state.id === action.participant.id));
  115. }
  116. return state;
  117. }
  118. /**
  119. * Listen for actions which add, remove, or update the set of participants in
  120. * the conference.
  121. *
  122. * @param {Participant[]} state - List of participants to be modified.
  123. * @param {Object} action - Action object.
  124. * @param {string} action.type - Type of action.
  125. * @param {Participant} action.participant - Information about participant to be
  126. * added/removed/modified.
  127. * @returns {Participant[]}
  128. */
  129. ReducerRegistry.register('features/base/participants', (state = [], action) => {
  130. switch (action.type) {
  131. case PARTICIPANT_JOINED:
  132. return [ ...state, participant(undefined, action) ];
  133. case PARTICIPANT_LEFT:
  134. return state.filter(p => p.id !== action.participant.id);
  135. case DOMINANT_SPEAKER_CHANGED:
  136. case PARTICIPANT_ID_CHANGED:
  137. case PARTICIPANT_UPDATED:
  138. case PIN_PARTICIPANT:
  139. return state.map(p => participant(p, action));
  140. default:
  141. return state;
  142. }
  143. });
  144. /**
  145. * Returns the URL of the image for the avatar of a particular participant
  146. * identified by their id and/or e-mail address.
  147. *
  148. * @param {string} participantId - Participant's id.
  149. * @param {string} [email] - Participant's email.
  150. * @returns {string} The URL of the image for the avatar of the participant
  151. * identified by the specified participantId and/or email.
  152. */
  153. function _getAvatarURL(participantId, email) {
  154. // TODO: Use disableThirdPartyRequests config.
  155. let avatarId = email || participantId;
  156. // If the ID looks like an email, we'll use gravatar. Otherwise, it's a
  157. // random avatar and we'll use the configured URL.
  158. const random = !avatarId || avatarId.indexOf('@') < 0;
  159. if (!avatarId) {
  160. avatarId = participantId;
  161. }
  162. // MD5 is provided by Strophe
  163. avatarId = MD5.hexdigest(avatarId.trim().toLowerCase());
  164. let urlPref = null;
  165. let urlSuf = null;
  166. if (random) {
  167. // TODO: Use RANDOM_AVATAR_URL_PREFIX from interface config.
  168. urlPref = 'https://robohash.org/';
  169. urlSuf = '.png?size=200x200';
  170. } else {
  171. urlPref = 'https://www.gravatar.com/avatar/';
  172. urlSuf = '?d=wavatar&size=200';
  173. }
  174. return urlPref + avatarId + urlSuf;
  175. }
  176. /**
  177. * Sets a specific property of a specific state to a specific value. Prevents
  178. * unnecessary state changes (when the specified <tt>value</tt> is equal to the
  179. * value of the specified <tt>property</tt> of the specified <tt>state</tt>).
  180. *
  181. * @param {Object} state - The (Redux) state from which a new state is to be
  182. * constructed by setting the specified <tt>property</tt> to the specified
  183. * <tt>value</tt>.
  184. * @param {string} property - The property of <tt>state</tt> which is to be
  185. * assigned the specified <tt>value</tt> (in the new state).
  186. * @param {*} value - The value to assign to the specified <tt>property</tt>.
  187. * @returns {Object} The specified <tt>state</tt> if the value of the specified
  188. * <tt>property</tt> equals the specified <tt>value/tt>; otherwise, a new state
  189. * constructed from the specified <tt>state</tt> by setting the specified
  190. * <tt>property</tt> to the specified <tt>value</tt>.
  191. */
  192. function _setStateProperty(state, property, value) {
  193. if (state[property] !== value) {
  194. return {
  195. ...state,
  196. [property]: value
  197. };
  198. }
  199. return state;
  200. }