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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. } from './actions';
  19. import {
  20. DOMINANT_SPEAKER_CHANGED,
  21. KICK_PARTICIPANT,
  22. MUTE_REMOTE_PARTICIPANT,
  23. PARTICIPANT_DISPLAY_NAME_CHANGED,
  24. PARTICIPANT_JOINED,
  25. PARTICIPANT_LEFT,
  26. PARTICIPANT_UPDATED
  27. } from './actionTypes';
  28. import {
  29. LOCAL_PARTICIPANT_DEFAULT_ID,
  30. PARTICIPANT_JOINED_SOUND_ID,
  31. PARTICIPANT_LEFT_SOUND_ID
  32. } from './constants';
  33. import {
  34. getAvatarURLByParticipantId,
  35. getLocalParticipant,
  36. getParticipantById,
  37. getParticipantCount
  38. } from './functions';
  39. import { PARTICIPANT_JOINED_FILE, PARTICIPANT_LEFT_FILE } from './sounds';
  40. declare var APP: Object;
  41. /**
  42. * Middleware that captures CONFERENCE_JOINED and CONFERENCE_LEFT actions and
  43. * updates respectively ID of local participant.
  44. *
  45. * @param {Store} store - The redux store.
  46. * @returns {Function}
  47. */
  48. MiddlewareRegistry.register(store => next => action => {
  49. switch (action.type) {
  50. case APP_WILL_MOUNT:
  51. _registerSounds(store);
  52. return _localParticipantJoined(store, next, action);
  53. case APP_WILL_UNMOUNT:
  54. _unregisterSounds(store);
  55. return _localParticipantLeft(store, next, action);
  56. case CONFERENCE_WILL_JOIN:
  57. store.dispatch(localParticipantIdChanged(action.conference.myUserId()));
  58. break;
  59. case DOMINANT_SPEAKER_CHANGED: {
  60. // Ensure the raised hand state is cleared for the dominant speaker.
  61. const { conference, id } = action.participant;
  62. const participant = getLocalParticipant(store.getState());
  63. participant
  64. && store.dispatch(participantUpdated({
  65. conference,
  66. id,
  67. local: participant.id === id,
  68. raisedHand: false
  69. }));
  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. * Signals that the local participant has left.
  175. *
  176. * @param {Store} store - The redux store.
  177. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  178. * specified {@code action} into the specified {@code store}.
  179. * @param {Action} action - The redux action which is being dispatched in the
  180. * specified {@code store}.
  181. * @private
  182. * @returns {Object} The value returned by {@code next(action)}.
  183. */
  184. function _localParticipantLeft({ dispatch }, next, action) {
  185. const result = next(action);
  186. dispatch(localParticipantLeft());
  187. return result;
  188. }
  189. /**
  190. * Plays sounds when participants join/leave conference.
  191. *
  192. * @param {Store} store - The redux store.
  193. * @param {Action} action - The redux action. Should be either
  194. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  195. * @private
  196. * @returns {void}
  197. */
  198. function _maybePlaySounds({ getState, dispatch }, action) {
  199. const state = getState();
  200. const { startAudioMuted } = state['features/base/config'];
  201. // We're not playing sounds for local participant
  202. // nor when the user is joining past the "startAudioMuted" limit.
  203. // The intention there was to not play user joined notification in big
  204. // conferences where 100th person is joining.
  205. if (!action.participant.local
  206. && (!startAudioMuted
  207. || getParticipantCount(state) < startAudioMuted)) {
  208. if (action.type === PARTICIPANT_JOINED) {
  209. const { presence } = action.participant;
  210. // The sounds for the poltergeist are handled by features/invite.
  211. if (presence !== INVITED && presence !== CALLING) {
  212. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  213. }
  214. } else if (action.type === PARTICIPANT_LEFT) {
  215. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  216. }
  217. }
  218. }
  219. /**
  220. * Notifies the feature base/participants that the action
  221. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  222. * within a specific redux store.
  223. *
  224. * @param {Store} store - The redux store in which the specified {@code action}
  225. * is being dispatched.
  226. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  227. * specified {@code action} in the specified {@code store}.
  228. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  229. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  230. * {@code store}.
  231. * @private
  232. * @returns {Object} The value returned by {@code next(action)}.
  233. */
  234. function _participantJoinedOrUpdated({ getState }, next, action) {
  235. const { participant: { id, local, raisedHand } } = action;
  236. // Send an external update of the local participant's raised hand state
  237. // if a new raised hand state is defined in the action.
  238. if (typeof raisedHand !== 'undefined') {
  239. if (local) {
  240. const { conference } = getState()['features/base/conference'];
  241. conference
  242. && conference.setLocalParticipantProperty(
  243. 'raisedHand',
  244. raisedHand);
  245. }
  246. if (typeof APP === 'object') {
  247. if (local) {
  248. APP.UI.onLocalRaiseHandChanged(raisedHand);
  249. APP.UI.setLocalRaisedHandStatus(raisedHand);
  250. } else {
  251. const remoteParticipant = getParticipantById(getState(), id);
  252. remoteParticipant
  253. && APP.UI.setRaisedHandStatus(
  254. remoteParticipant.id,
  255. remoteParticipant.name,
  256. raisedHand);
  257. }
  258. }
  259. }
  260. // Notify external listeners of potential avatarURL changes.
  261. if (typeof APP === 'object') {
  262. const oldAvatarURL = getAvatarURLByParticipantId(getState(), id);
  263. // Allow the redux update to go through and compare the old avatar
  264. // to the new avatar and emit out change events if necessary.
  265. const result = next(action);
  266. const newAvatarURL = getAvatarURLByParticipantId(getState(), id);
  267. if (oldAvatarURL !== newAvatarURL) {
  268. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  269. APP.UI.refreshAvatarDisplay(currentKnownId, newAvatarURL);
  270. APP.API.notifyAvatarChanged(currentKnownId, newAvatarURL);
  271. }
  272. return result;
  273. }
  274. return next(action);
  275. }
  276. /**
  277. * Registers sounds related with the participants feature.
  278. *
  279. * @param {Store} store - The redux store.
  280. * @private
  281. * @returns {void}
  282. */
  283. function _registerSounds({ dispatch }) {
  284. dispatch(
  285. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  286. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  287. }
  288. /**
  289. * Unregisters sounds related with the participants feature.
  290. *
  291. * @param {Store} store - The redux store.
  292. * @private
  293. * @returns {void}
  294. */
  295. function _unregisterSounds({ dispatch }) {
  296. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  297. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  298. }