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

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