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.

middleware.js 11KB

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