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 9.1KB

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