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

middleware.js 11KB

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