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

middleware.js 8.3KB

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