Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

middleware.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // @flow
  2. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../../app';
  3. import { CONFERENCE_WILL_JOIN, forEachConference } from '../conference';
  4. import { CALLING, INVITED } from '../../presence-status';
  5. import { MiddlewareRegistry, StateListenerRegistry } from '../redux';
  6. import UIEvents from '../../../../service/UI/UIEvents';
  7. import { playSound, registerSound, unregisterSound } from '../sounds';
  8. import {
  9. localParticipantIdChanged,
  10. localParticipantJoined,
  11. participantLeft,
  12. participantUpdated
  13. } from './actions';
  14. import {
  15. DOMINANT_SPEAKER_CHANGED,
  16. KICK_PARTICIPANT,
  17. MUTE_REMOTE_PARTICIPANT,
  18. PARTICIPANT_DISPLAY_NAME_CHANGED,
  19. PARTICIPANT_JOINED,
  20. PARTICIPANT_LEFT,
  21. PARTICIPANT_UPDATED
  22. } from './actionTypes';
  23. import {
  24. LOCAL_PARTICIPANT_DEFAULT_ID,
  25. PARTICIPANT_JOINED_SOUND_ID,
  26. PARTICIPANT_LEFT_SOUND_ID
  27. } from './constants';
  28. import {
  29. getAvatarURLByParticipantId,
  30. getLocalParticipant,
  31. getParticipantById,
  32. getParticipantCount
  33. } from './functions';
  34. import { PARTICIPANT_JOINED_FILE, PARTICIPANT_LEFT_FILE } from './sounds';
  35. declare var APP: Object;
  36. /**
  37. * Middleware that captures CONFERENCE_JOINED and CONFERENCE_LEFT actions and
  38. * updates respectively ID of local participant.
  39. *
  40. * @param {Store} store - The redux store.
  41. * @returns {Function}
  42. */
  43. MiddlewareRegistry.register(store => next => action => {
  44. switch (action.type) {
  45. case APP_WILL_MOUNT:
  46. _registerSounds(store);
  47. return _localParticipantJoined(store, next, action);
  48. case APP_WILL_UNMOUNT:
  49. _unregisterSounds(store);
  50. break;
  51. case CONFERENCE_WILL_JOIN:
  52. store.dispatch(localParticipantIdChanged(action.conference.myUserId()));
  53. break;
  54. case DOMINANT_SPEAKER_CHANGED: {
  55. // Ensure the raised hand state is cleared for the dominant speaker.
  56. const { conference, id } = action.participant;
  57. const participant = getLocalParticipant(store.getState());
  58. participant
  59. && store.dispatch(participantUpdated({
  60. conference,
  61. id,
  62. local: participant.id === id,
  63. raisedHand: false
  64. }));
  65. typeof APP === 'object' && APP.UI.markDominantSpeaker(id);
  66. break;
  67. }
  68. case KICK_PARTICIPANT: {
  69. const { conference } = store.getState()['features/base/conference'];
  70. conference.kickParticipant(action.id);
  71. break;
  72. }
  73. case MUTE_REMOTE_PARTICIPANT: {
  74. const { conference } = store.getState()['features/base/conference'];
  75. conference.muteParticipant(action.id);
  76. break;
  77. }
  78. // TODO Remove this middleware when the local display name update flow is
  79. // fully brought into redux.
  80. case PARTICIPANT_DISPLAY_NAME_CHANGED: {
  81. if (typeof APP !== 'undefined') {
  82. const participant = getLocalParticipant(store.getState());
  83. if (participant && participant.id === action.id) {
  84. APP.UI.emitEvent(UIEvents.NICKNAME_CHANGED, action.name);
  85. }
  86. }
  87. break;
  88. }
  89. case PARTICIPANT_JOINED:
  90. _maybePlaySounds(store, action);
  91. return _participantJoinedOrUpdated(store, next, action);
  92. case PARTICIPANT_LEFT:
  93. _maybePlaySounds(store, action);
  94. break;
  95. case PARTICIPANT_UPDATED:
  96. return _participantJoinedOrUpdated(store, next, action);
  97. }
  98. return next(action);
  99. });
  100. /**
  101. * Syncs the redux state features/base/participants up with the redux state
  102. * features/base/conference by ensuring that the former does not contain remote
  103. * participants no longer relevant to the latter. Introduced to address an issue
  104. * with multiplying thumbnails in the filmstrip.
  105. */
  106. StateListenerRegistry.register(
  107. /* selector */ state => {
  108. const { conference, joining } = state['features/base/conference'];
  109. return conference || joining;
  110. },
  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. }