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

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