Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

middleware.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // @flow
  2. import UIEvents from '../../../../service/UI/UIEvents';
  3. import { NOTIFICATION_TIMEOUT, showNotification } from '../../notifications';
  4. import { CALLING, INVITED } from '../../presence-status';
  5. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app';
  6. import {
  7. CONFERENCE_WILL_JOIN,
  8. forEachConference,
  9. getCurrentConference
  10. } from '../conference';
  11. import { JitsiConferenceEvents } from '../lib-jitsi-meet';
  12. import { MiddlewareRegistry, StateListenerRegistry } from '../redux';
  13. import { playSound, registerSound, unregisterSound } from '../sounds';
  14. import {
  15. localParticipantIdChanged,
  16. localParticipantJoined,
  17. localParticipantLeft,
  18. participantLeft,
  19. participantUpdated
  20. } from './actions';
  21. import {
  22. DOMINANT_SPEAKER_CHANGED,
  23. KICK_PARTICIPANT,
  24. MUTE_REMOTE_PARTICIPANT,
  25. PARTICIPANT_DISPLAY_NAME_CHANGED,
  26. PARTICIPANT_JOINED,
  27. PARTICIPANT_LEFT,
  28. PARTICIPANT_UPDATED
  29. } from './actionTypes';
  30. import {
  31. LOCAL_PARTICIPANT_DEFAULT_ID,
  32. PARTICIPANT_JOINED_SOUND_ID,
  33. PARTICIPANT_LEFT_SOUND_ID
  34. } from './constants';
  35. import {
  36. getAvatarURLByParticipantId,
  37. getLocalParticipant,
  38. getParticipantCount,
  39. getParticipantDisplayName
  40. } from './functions';
  41. import { PARTICIPANT_JOINED_FILE, PARTICIPANT_LEFT_FILE } from './sounds';
  42. declare var APP: Object;
  43. /**
  44. * Middleware that captures CONFERENCE_JOINED and CONFERENCE_LEFT actions and
  45. * updates respectively ID of local participant.
  46. *
  47. * @param {Store} store - The redux store.
  48. * @returns {Function}
  49. */
  50. MiddlewareRegistry.register(store => next => action => {
  51. switch (action.type) {
  52. case APP_WILL_MOUNT:
  53. _registerSounds(store);
  54. return _localParticipantJoined(store, next, action);
  55. case APP_WILL_UNMOUNT:
  56. _unregisterSounds(store);
  57. return _localParticipantLeft(store, next, action);
  58. case CONFERENCE_WILL_JOIN:
  59. store.dispatch(localParticipantIdChanged(action.conference.myUserId()));
  60. break;
  61. case DOMINANT_SPEAKER_CHANGED: {
  62. // Ensure the raised hand state is cleared for the dominant speaker.
  63. const { conference, id } = action.participant;
  64. const participant = getLocalParticipant(store.getState());
  65. participant
  66. && store.dispatch(participantUpdated({
  67. conference,
  68. id,
  69. local: participant.id === id,
  70. raisedHand: false
  71. }));
  72. break;
  73. }
  74. case KICK_PARTICIPANT: {
  75. const { conference } = store.getState()['features/base/conference'];
  76. conference.kickParticipant(action.id);
  77. break;
  78. }
  79. case MUTE_REMOTE_PARTICIPANT: {
  80. const { conference } = store.getState()['features/base/conference'];
  81. conference.muteParticipant(action.id);
  82. break;
  83. }
  84. // TODO Remove this middleware when the local display name update flow is
  85. // fully brought into redux.
  86. case PARTICIPANT_DISPLAY_NAME_CHANGED: {
  87. if (typeof APP !== 'undefined') {
  88. const participant = getLocalParticipant(store.getState());
  89. if (participant && participant.id === action.id) {
  90. APP.UI.emitEvent(UIEvents.NICKNAME_CHANGED, action.name);
  91. }
  92. }
  93. break;
  94. }
  95. case PARTICIPANT_JOINED: {
  96. _maybePlaySounds(store, action);
  97. return _participantJoinedOrUpdated(store, next, action);
  98. }
  99. case PARTICIPANT_LEFT:
  100. _maybePlaySounds(store, action);
  101. break;
  102. case PARTICIPANT_UPDATED:
  103. return _participantJoinedOrUpdated(store, next, action);
  104. }
  105. return next(action);
  106. });
  107. /**
  108. * Syncs the redux state features/base/participants up with the redux state
  109. * features/base/conference by ensuring that the former does not contain remote
  110. * participants no longer relevant to the latter. Introduced to address an issue
  111. * with multiplying thumbnails in the filmstrip.
  112. */
  113. StateListenerRegistry.register(
  114. /* selector */ state => getCurrentConference(state),
  115. /* listener */ (conference, { dispatch, getState }) => {
  116. for (const p of getState()['features/base/participants']) {
  117. !p.local
  118. && (!conference || p.conference !== conference)
  119. && dispatch(participantLeft(p.id, p.conference));
  120. }
  121. });
  122. /**
  123. * Reset the ID of the local participant to
  124. * {@link LOCAL_PARTICIPANT_DEFAULT_ID}. Such a reset is deemed possible only if
  125. * the local participant and, respectively, her ID is not involved in a
  126. * conference which is still of interest to the user and, consequently, the app.
  127. * For example, a conference which is in the process of leaving is no longer of
  128. * interest the user, is unrecoverable from the perspective of the user and,
  129. * consequently, the app.
  130. */
  131. StateListenerRegistry.register(
  132. /* selector */ state => state['features/base/conference'],
  133. /* listener */ ({ leaving }, { dispatch, getState }) => {
  134. const state = getState();
  135. const localParticipant = getLocalParticipant(state);
  136. let id;
  137. if (!localParticipant
  138. || (id = localParticipant.id)
  139. === LOCAL_PARTICIPANT_DEFAULT_ID) {
  140. // The ID of the local participant has been reset already.
  141. return;
  142. }
  143. // The ID of the local may be reset only if it is not in use.
  144. const dispatchLocalParticipantIdChanged
  145. = forEachConference(
  146. state,
  147. conference =>
  148. conference === leaving || conference.myUserId() !== id);
  149. dispatchLocalParticipantIdChanged
  150. && dispatch(
  151. localParticipantIdChanged(LOCAL_PARTICIPANT_DEFAULT_ID));
  152. });
  153. /**
  154. * Registers listeners for participant change events.
  155. */
  156. StateListenerRegistry.register(
  157. state => state['features/base/conference'].conference,
  158. (conference, store) => {
  159. if (conference) {
  160. // We joined a conference
  161. conference.on(
  162. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  163. (participant, propertyName, oldValue, newValue) => {
  164. switch (propertyName) {
  165. case 'features_screen-sharing':
  166. store.dispatch(participantUpdated({
  167. conference,
  168. id: participant.getId(),
  169. features: { 'screen-sharing': true }
  170. }));
  171. break;
  172. case 'raisedHand': {
  173. _raiseHandUpdated(
  174. store, conference, participant.getId(), newValue);
  175. break;
  176. }
  177. default:
  178. // Ignore for now.
  179. }
  180. });
  181. } else {
  182. // We left the conference, raise hand of the local participant must be updated.
  183. _raiseHandUpdated(
  184. store, conference, undefined, false);
  185. }
  186. }
  187. );
  188. /**
  189. * Initializes the local participant and signals that it joined.
  190. *
  191. * @private
  192. * @param {Store} store - The redux store.
  193. * @param {Dispatch} next - The redux dispatch function to dispatch the
  194. * specified action to the specified store.
  195. * @param {Action} action - The redux action which is being dispatched
  196. * in the specified store.
  197. * @private
  198. * @returns {Object} The value returned by {@code next(action)}.
  199. */
  200. function _localParticipantJoined({ getState, dispatch }, next, action) {
  201. const result = next(action);
  202. const settings = getState()['features/base/settings'];
  203. dispatch(localParticipantJoined({
  204. avatarID: settings.avatarID,
  205. avatarURL: settings.avatarURL,
  206. email: settings.email,
  207. name: settings.displayName
  208. }));
  209. return result;
  210. }
  211. /**
  212. * Signals that the local participant has left.
  213. *
  214. * @param {Store} store - The redux store.
  215. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  216. * specified {@code action} into the specified {@code store}.
  217. * @param {Action} action - The redux action which is being dispatched in the
  218. * specified {@code store}.
  219. * @private
  220. * @returns {Object} The value returned by {@code next(action)}.
  221. */
  222. function _localParticipantLeft({ dispatch }, next, action) {
  223. const result = next(action);
  224. dispatch(localParticipantLeft());
  225. return result;
  226. }
  227. /**
  228. * Plays sounds when participants join/leave conference.
  229. *
  230. * @param {Store} store - The redux store.
  231. * @param {Action} action - The redux action. Should be either
  232. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  233. * @private
  234. * @returns {void}
  235. */
  236. function _maybePlaySounds({ getState, dispatch }, action) {
  237. const state = getState();
  238. const { startAudioMuted } = state['features/base/config'];
  239. // We're not playing sounds for local participant
  240. // nor when the user is joining past the "startAudioMuted" limit.
  241. // The intention there was to not play user joined notification in big
  242. // conferences where 100th person is joining.
  243. if (!action.participant.local
  244. && (!startAudioMuted
  245. || getParticipantCount(state) < startAudioMuted)) {
  246. if (action.type === PARTICIPANT_JOINED) {
  247. const { presence } = action.participant;
  248. // The sounds for the poltergeist are handled by features/invite.
  249. if (presence !== INVITED && presence !== CALLING) {
  250. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  251. }
  252. } else if (action.type === PARTICIPANT_LEFT) {
  253. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  254. }
  255. }
  256. }
  257. /**
  258. * Notifies the feature base/participants that the action
  259. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  260. * within a specific redux store.
  261. *
  262. * @param {Store} store - The redux store in which the specified {@code action}
  263. * is being dispatched.
  264. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  265. * specified {@code action} in the specified {@code store}.
  266. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  267. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  268. * {@code store}.
  269. * @private
  270. * @returns {Object} The value returned by {@code next(action)}.
  271. */
  272. function _participantJoinedOrUpdated({ getState }, next, action) {
  273. const { participant: { id, local, raisedHand } } = action;
  274. // Send an external update of the local participant's raised hand state
  275. // if a new raised hand state is defined in the action.
  276. if (typeof raisedHand !== 'undefined') {
  277. if (local) {
  278. const { conference } = getState()['features/base/conference'];
  279. conference
  280. && conference.setLocalParticipantProperty(
  281. 'raisedHand',
  282. raisedHand);
  283. }
  284. }
  285. // Notify external listeners of potential avatarURL changes.
  286. if (typeof APP === 'object') {
  287. const oldAvatarURL = getAvatarURLByParticipantId(getState(), id);
  288. // Allow the redux update to go through and compare the old avatar
  289. // to the new avatar and emit out change events if necessary.
  290. const result = next(action);
  291. const newAvatarURL = getAvatarURLByParticipantId(getState(), id);
  292. if (oldAvatarURL !== newAvatarURL) {
  293. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  294. APP.UI.refreshAvatarDisplay(currentKnownId, newAvatarURL);
  295. APP.API.notifyAvatarChanged(currentKnownId, newAvatarURL);
  296. }
  297. return result;
  298. }
  299. return next(action);
  300. }
  301. /**
  302. * Handles a raise hand status update.
  303. *
  304. * @param {Function} dispatch - The Redux dispatch function.
  305. * @param {Object} conference - The conference for which we got an update.
  306. * @param {string?} participantId - The ID of the participant from which we got an update. If undefined,
  307. * we update the local participant.
  308. * @param {boolean} newValue - The new value of the raise hand status.
  309. * @returns {void}
  310. */
  311. function _raiseHandUpdated({ dispatch, getState }, conference, participantId, newValue) {
  312. const raisedHand = newValue === 'true';
  313. const pid = participantId || getLocalParticipant(getState()).id;
  314. dispatch(participantUpdated({
  315. conference,
  316. id: pid,
  317. raisedHand
  318. }));
  319. if (raisedHand) {
  320. dispatch(showNotification({
  321. titleArguments: {
  322. name: getParticipantDisplayName(getState, pid)
  323. },
  324. titleKey: 'notify.raisedHand'
  325. }, NOTIFICATION_TIMEOUT));
  326. }
  327. }
  328. /**
  329. * Registers sounds related with the participants feature.
  330. *
  331. * @param {Store} store - The redux store.
  332. * @private
  333. * @returns {void}
  334. */
  335. function _registerSounds({ dispatch }) {
  336. dispatch(
  337. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  338. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  339. }
  340. /**
  341. * Unregisters sounds related with the participants feature.
  342. *
  343. * @param {Store} store - The redux store.
  344. * @private
  345. * @returns {void}
  346. */
  347. function _unregisterSounds({ dispatch }) {
  348. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  349. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  350. }