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

middleware.js 9.4KB

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