您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // @flow
  2. import UIEvents from '../../../../service/UI/UIEvents';
  3. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../../app';
  4. import {
  5. CONFERENCE_JOINED,
  6. CONFERENCE_LEFT
  7. } from '../conference';
  8. import { MiddlewareRegistry } from '../redux';
  9. import { playSound, registerSound, unregisterSound } from '../sounds';
  10. import {
  11. localParticipantIdChanged,
  12. localParticipantJoined,
  13. participantUpdated
  14. } from './actions';
  15. import {
  16. DOMINANT_SPEAKER_CHANGED,
  17. KICK_PARTICIPANT,
  18. MUTE_REMOTE_PARTICIPANT,
  19. PARTICIPANT_DISPLAY_NAME_CHANGED,
  20. PARTICIPANT_JOINED,
  21. PARTICIPANT_LEFT,
  22. PARTICIPANT_UPDATED
  23. } from './actionTypes';
  24. import {
  25. LOCAL_PARTICIPANT_DEFAULT_ID,
  26. PARTICIPANT_JOINED_SOUND_ID,
  27. PARTICIPANT_LEFT_SOUND_ID
  28. } from './constants';
  29. import {
  30. getAvatarURLByParticipantId,
  31. getLocalParticipant,
  32. getParticipantById,
  33. getParticipantCount
  34. } from './functions';
  35. import { PARTICIPANT_JOINED_FILE, PARTICIPANT_LEFT_FILE } from './sounds';
  36. declare var APP: Object;
  37. /**
  38. * Middleware that captures CONFERENCE_JOINED and CONFERENCE_LEFT actions and
  39. * updates respectively ID of local participant.
  40. *
  41. * @param {Store} store - Redux store.
  42. * @returns {Function}
  43. */
  44. MiddlewareRegistry.register(store => next => action => {
  45. const { conference } = store.getState()['features/base/conference'];
  46. if (action.type === PARTICIPANT_JOINED
  47. || action.type === PARTICIPANT_LEFT) {
  48. _maybePlaySounds(store, action);
  49. }
  50. switch (action.type) {
  51. case APP_WILL_MOUNT:
  52. _registerSounds(store);
  53. return _localParticipantJoined(store, next, action);
  54. case APP_WILL_UNMOUNT:
  55. _unregisterSounds(store);
  56. break;
  57. case CONFERENCE_JOINED:
  58. store.dispatch(localParticipantIdChanged(action.conference.myUserId()));
  59. break;
  60. case CONFERENCE_LEFT:
  61. store.dispatch(localParticipantIdChanged(LOCAL_PARTICIPANT_DEFAULT_ID));
  62. break;
  63. case DOMINANT_SPEAKER_CHANGED: {
  64. // Ensure the raised hand state is cleared for the dominant speaker.
  65. const participant = getLocalParticipant(store.getState());
  66. if (participant) {
  67. const local = participant.id === action.participant.id;
  68. store.dispatch(participantUpdated({
  69. id: action.participant.id,
  70. local,
  71. raisedHand: false
  72. }));
  73. }
  74. if (typeof APP === 'object') {
  75. APP.UI.markDominantSpeaker(action.participant.id);
  76. }
  77. break;
  78. }
  79. case KICK_PARTICIPANT:
  80. conference.kickParticipant(action.id);
  81. break;
  82. case MUTE_REMOTE_PARTICIPANT:
  83. conference.muteParticipant(action.id);
  84. break;
  85. // TODO Remove this middleware when the local display name update flow is
  86. // fully brought into redux.
  87. case PARTICIPANT_DISPLAY_NAME_CHANGED: {
  88. if (typeof APP !== 'undefined') {
  89. const participant = getLocalParticipant(store.getState());
  90. if (participant && participant.id === action.id) {
  91. APP.UI.emitEvent(UIEvents.NICKNAME_CHANGED, action.name);
  92. }
  93. }
  94. break;
  95. }
  96. case PARTICIPANT_JOINED:
  97. case PARTICIPANT_UPDATED: {
  98. const { participant } = action;
  99. const { id, local, raisedHand } = participant;
  100. // Send an external update of the local participant's raised hand state
  101. // if a new raised hand state is defined in the action.
  102. if (typeof raisedHand !== 'undefined') {
  103. if (local) {
  104. conference.setLocalParticipantProperty(
  105. 'raisedHand',
  106. raisedHand);
  107. }
  108. if (typeof APP === 'object') {
  109. if (local) {
  110. APP.UI.onLocalRaiseHandChanged(raisedHand);
  111. APP.UI.setLocalRaisedHandStatus(raisedHand);
  112. } else {
  113. const remoteParticipant
  114. = getParticipantById(store.getState(), id);
  115. remoteParticipant
  116. && APP.UI.setRaisedHandStatus(
  117. remoteParticipant.id,
  118. remoteParticipant.name,
  119. raisedHand);
  120. }
  121. }
  122. }
  123. // Notify external listeners of potential avatarURL changes.
  124. if (typeof APP === 'object') {
  125. const preUpdateAvatarURL
  126. = getAvatarURLByParticipantId(store.getState(), id);
  127. // Allow the redux update to go through and compare the old avatar
  128. // to the new avatar and emit out change events if necessary.
  129. const result = next(action);
  130. const postUpdateAvatarURL
  131. = getAvatarURLByParticipantId(store.getState(), id);
  132. if (preUpdateAvatarURL !== postUpdateAvatarURL) {
  133. const currentKnownId = local
  134. ? APP.conference.getMyUserId() : id;
  135. APP.UI.refreshAvatarDisplay(
  136. currentKnownId, postUpdateAvatarURL);
  137. APP.API.notifyAvatarChanged(
  138. currentKnownId, postUpdateAvatarURL);
  139. }
  140. return result;
  141. }
  142. break;
  143. }
  144. }
  145. return next(action);
  146. });
  147. /**
  148. * Initializes the local participant and signals that it joined.
  149. *
  150. * @private
  151. * @param {Store} store - The Redux store.
  152. * @param {Dispatch} next - The redux dispatch function to dispatch the
  153. * specified action to the specified store.
  154. * @param {Action} action - The redux action which is being dispatched
  155. * in the specified store.
  156. * @private
  157. * @returns {Object} The value returned by {@code next(action)}.
  158. */
  159. function _localParticipantJoined({ getState, dispatch }, next, action) {
  160. const result = next(action);
  161. const settings = getState()['features/base/settings'];
  162. const localParticipant = {
  163. avatarID: settings.avatarID,
  164. avatarURL: settings.avatarURL,
  165. email: settings.email,
  166. name: settings.displayName
  167. };
  168. dispatch(localParticipantJoined(localParticipant));
  169. return result;
  170. }
  171. /**
  172. * Plays sounds when participants join/leave conference.
  173. *
  174. * @param {Store} store - The Redux store.
  175. * @param {Action} action - The Redux action. Should be either
  176. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  177. * @private
  178. * @returns {void}
  179. */
  180. function _maybePlaySounds({ getState, dispatch }, action) {
  181. const state = getState();
  182. const { startAudioMuted } = state['features/base/config'];
  183. // We're not playing sounds for local participant
  184. // nor when the user is joining past the "startAudioMuted" limit.
  185. // The intention there was to not play user joined notification in big
  186. // conferences where 100th person is joining.
  187. if (!action.participant.local
  188. && (!startAudioMuted
  189. || getParticipantCount(state) < startAudioMuted)) {
  190. if (action.type === PARTICIPANT_JOINED) {
  191. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  192. } else if (action.type === PARTICIPANT_LEFT) {
  193. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  194. }
  195. }
  196. }
  197. /**
  198. * Registers sounds related with the participants feature.
  199. *
  200. * @param {Store} store - The Redux store.
  201. * @private
  202. * @returns {void}
  203. */
  204. function _registerSounds({ dispatch }) {
  205. dispatch(
  206. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  207. dispatch(
  208. registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  209. }
  210. /**
  211. * Unregisters sounds related with the participants feature.
  212. *
  213. * @param {Store} store - The Redux store.
  214. * @private
  215. * @returns {void}
  216. */
  217. function _unregisterSounds({ dispatch }) {
  218. dispatch(
  219. unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  220. dispatch(
  221. unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  222. }