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

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