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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. 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 => getCurrentConference(state),
  111. /* listener */ (conference, { dispatch, getState }) => {
  112. for (const p of getState()['features/base/participants']) {
  113. !p.local
  114. && (!conference || p.conference !== conference)
  115. && dispatch(participantLeft(p.id, p.conference));
  116. }
  117. });
  118. /**
  119. * Reset the ID of the local participant to
  120. * {@link LOCAL_PARTICIPANT_DEFAULT_ID}. Such a reset is deemed possible only if
  121. * the local participant and, respectively, her ID is not involved in a
  122. * conference which is still of interest to the user and, consequently, the app.
  123. * For example, a conference which is in the process of leaving is no longer of
  124. * interest the user, is unrecoverable from the perspective of the user and,
  125. * consequently, the app.
  126. */
  127. StateListenerRegistry.register(
  128. /* selector */ state => state['features/base/conference'],
  129. /* listener */ ({ leaving }, { dispatch, getState }) => {
  130. const state = getState();
  131. const localParticipant = getLocalParticipant(state);
  132. let id;
  133. if (!localParticipant
  134. || (id = localParticipant.id)
  135. === LOCAL_PARTICIPANT_DEFAULT_ID) {
  136. // The ID of the local participant has been reset already.
  137. return;
  138. }
  139. // The ID of the local may be reset only if it is not in use.
  140. const dispatchLocalParticipantIdChanged
  141. = forEachConference(
  142. state,
  143. conference =>
  144. conference === leaving || conference.myUserId() !== id);
  145. dispatchLocalParticipantIdChanged
  146. && dispatch(
  147. localParticipantIdChanged(LOCAL_PARTICIPANT_DEFAULT_ID));
  148. });
  149. /**
  150. * Initializes the local participant and signals that it joined.
  151. *
  152. * @private
  153. * @param {Store} store - The redux store.
  154. * @param {Dispatch} next - The redux dispatch function to dispatch the
  155. * specified action to the specified store.
  156. * @param {Action} action - The redux action which is being dispatched
  157. * in the specified store.
  158. * @private
  159. * @returns {Object} The value returned by {@code next(action)}.
  160. */
  161. function _localParticipantJoined({ getState, dispatch }, next, action) {
  162. const result = next(action);
  163. const settings = getState()['features/base/settings'];
  164. dispatch(localParticipantJoined({
  165. avatarID: settings.avatarID,
  166. avatarURL: settings.avatarURL,
  167. email: settings.email,
  168. name: settings.displayName
  169. }));
  170. return result;
  171. }
  172. /**
  173. * Plays sounds when participants join/leave conference.
  174. *
  175. * @param {Store} store - The redux store.
  176. * @param {Action} action - The redux action. Should be either
  177. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  178. * @private
  179. * @returns {void}
  180. */
  181. function _maybePlaySounds({ getState, dispatch }, action) {
  182. const state = getState();
  183. const { startAudioMuted } = state['features/base/config'];
  184. // We're not playing sounds for local participant
  185. // nor when the user is joining past the "startAudioMuted" limit.
  186. // The intention there was to not play user joined notification in big
  187. // conferences where 100th person is joining.
  188. if (!action.participant.local
  189. && (!startAudioMuted
  190. || getParticipantCount(state) < startAudioMuted)) {
  191. if (action.type === PARTICIPANT_JOINED) {
  192. const { presence } = action.participant;
  193. // The sounds for the poltergeist are handled by features/invite.
  194. if (presence !== INVITED && presence !== CALLING) {
  195. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  196. }
  197. } else if (action.type === PARTICIPANT_LEFT) {
  198. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  199. }
  200. }
  201. }
  202. /**
  203. * Notifies the feature base/participants that the action
  204. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  205. * within a specific redux store.
  206. *
  207. * @param {Store} store - The redux store in which the specified {@code action}
  208. * is being dispatched.
  209. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  210. * specified {@code action} in the specified {@code store}.
  211. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  212. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  213. * {@code store}.
  214. * @private
  215. * @returns {Object} The value returned by {@code next(action)}.
  216. */
  217. function _participantJoinedOrUpdated({ getState }, next, action) {
  218. const { participant: { id, local, raisedHand } } = action;
  219. // Send an external update of the local participant's raised hand state
  220. // if a new raised hand state is defined in the action.
  221. if (typeof raisedHand !== 'undefined') {
  222. if (local) {
  223. const { conference } = getState()['features/base/conference'];
  224. conference
  225. && conference.setLocalParticipantProperty(
  226. 'raisedHand',
  227. raisedHand);
  228. }
  229. if (typeof APP === 'object') {
  230. if (local) {
  231. APP.UI.onLocalRaiseHandChanged(raisedHand);
  232. APP.UI.setLocalRaisedHandStatus(raisedHand);
  233. } else {
  234. const remoteParticipant = getParticipantById(getState(), id);
  235. remoteParticipant
  236. && APP.UI.setRaisedHandStatus(
  237. remoteParticipant.id,
  238. remoteParticipant.name,
  239. raisedHand);
  240. }
  241. }
  242. }
  243. // Notify external listeners of potential avatarURL changes.
  244. if (typeof APP === 'object') {
  245. const oldAvatarURL = getAvatarURLByParticipantId(getState(), id);
  246. // Allow the redux update to go through and compare the old avatar
  247. // to the new avatar and emit out change events if necessary.
  248. const result = next(action);
  249. const newAvatarURL = getAvatarURLByParticipantId(getState(), id);
  250. if (oldAvatarURL !== newAvatarURL) {
  251. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  252. APP.UI.refreshAvatarDisplay(currentKnownId, newAvatarURL);
  253. APP.API.notifyAvatarChanged(currentKnownId, newAvatarURL);
  254. }
  255. return result;
  256. }
  257. return next(action);
  258. }
  259. /**
  260. * Registers sounds related with the participants feature.
  261. *
  262. * @param {Store} store - The redux store.
  263. * @private
  264. * @returns {void}
  265. */
  266. function _registerSounds({ dispatch }) {
  267. dispatch(
  268. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  269. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  270. }
  271. /**
  272. * Unregisters sounds related with the participants feature.
  273. *
  274. * @param {Store} store - The redux store.
  275. * @private
  276. * @returns {void}
  277. */
  278. function _unregisterSounds({ dispatch }) {
  279. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  280. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  281. }