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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // @flow
  2. import i18n from 'i18next';
  3. import { batch } from 'react-redux';
  4. import UIEvents from '../../../../service/UI/UIEvents';
  5. import { approveParticipant } from '../../av-moderation/actions';
  6. import { toggleE2EE } from '../../e2ee/actions';
  7. import { MAX_MODE } from '../../e2ee/constants';
  8. import {
  9. NOTIFICATION_TIMEOUT_TYPE,
  10. RAISE_HAND_NOTIFICATION_ID,
  11. showNotification
  12. } from '../../notifications';
  13. import { isForceMuted } from '../../participants-pane/functions';
  14. import { CALLING, INVITED } from '../../presence-status';
  15. import { RAISE_HAND_SOUND_ID } from '../../reactions/constants';
  16. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app';
  17. import {
  18. CONFERENCE_WILL_JOIN,
  19. forEachConference,
  20. getCurrentConference
  21. } from '../conference';
  22. import { getDisableRemoveRaisedHandOnFocus } from '../config/functions.any';
  23. import { JitsiConferenceEvents } from '../lib-jitsi-meet';
  24. import { MEDIA_TYPE } from '../media';
  25. import { MiddlewareRegistry, StateListenerRegistry } from '../redux';
  26. import { playSound, registerSound, unregisterSound } from '../sounds';
  27. import {
  28. DOMINANT_SPEAKER_CHANGED,
  29. GRANT_MODERATOR,
  30. KICK_PARTICIPANT,
  31. LOCAL_PARTICIPANT_AUDIO_LEVEL_CHANGED,
  32. LOCAL_PARTICIPANT_RAISE_HAND,
  33. MUTE_REMOTE_PARTICIPANT,
  34. PARTICIPANT_DISPLAY_NAME_CHANGED,
  35. PARTICIPANT_JOINED,
  36. PARTICIPANT_LEFT,
  37. PARTICIPANT_UPDATED,
  38. RAISE_HAND_UPDATED
  39. } from './actionTypes';
  40. import {
  41. localParticipantIdChanged,
  42. localParticipantJoined,
  43. localParticipantLeft,
  44. participantLeft,
  45. participantUpdated,
  46. raiseHand,
  47. raiseHandUpdateQueue,
  48. setLoadableAvatarUrl
  49. } from './actions';
  50. import {
  51. LOCAL_PARTICIPANT_DEFAULT_ID,
  52. LOWER_HAND_AUDIO_LEVEL,
  53. PARTICIPANT_JOINED_SOUND_ID,
  54. PARTICIPANT_LEFT_SOUND_ID
  55. } from './constants';
  56. import {
  57. getDominantSpeakerParticipant,
  58. getFirstLoadableAvatarUrl,
  59. getLocalParticipant,
  60. getParticipantById,
  61. getParticipantCount,
  62. getParticipantDisplayName,
  63. getRaiseHandsQueue,
  64. getRemoteParticipants,
  65. hasRaisedHand,
  66. isLocalParticipantModerator
  67. } from './functions';
  68. import { PARTICIPANT_JOINED_FILE, PARTICIPANT_LEFT_FILE } from './sounds';
  69. declare var APP: Object;
  70. /**
  71. * Middleware that captures CONFERENCE_JOINED and CONFERENCE_LEFT actions and
  72. * updates respectively ID of local participant.
  73. *
  74. * @param {Store} store - The redux store.
  75. * @returns {Function}
  76. */
  77. MiddlewareRegistry.register(store => next => action => {
  78. switch (action.type) {
  79. case APP_WILL_MOUNT:
  80. _registerSounds(store);
  81. return _localParticipantJoined(store, next, action);
  82. case APP_WILL_UNMOUNT:
  83. _unregisterSounds(store);
  84. return _localParticipantLeft(store, next, action);
  85. case CONFERENCE_WILL_JOIN:
  86. store.dispatch(localParticipantIdChanged(action.conference.myUserId()));
  87. break;
  88. case DOMINANT_SPEAKER_CHANGED: {
  89. // Lower hand through xmpp when local participant becomes dominant speaker.
  90. const { id } = action.participant;
  91. const state = store.getState();
  92. const participant = getLocalParticipant(state);
  93. const isLocal = participant && participant.id === id;
  94. if (isLocal && hasRaisedHand(participant) && !getDisableRemoveRaisedHandOnFocus(state)) {
  95. store.dispatch(raiseHand(false));
  96. }
  97. break;
  98. }
  99. case LOCAL_PARTICIPANT_AUDIO_LEVEL_CHANGED: {
  100. const state = store.getState();
  101. const participant = getDominantSpeakerParticipant(state);
  102. if (
  103. participant
  104. && participant.local
  105. && hasRaisedHand(participant)
  106. && action.level > LOWER_HAND_AUDIO_LEVEL
  107. && !getDisableRemoveRaisedHandOnFocus(state)
  108. ) {
  109. store.dispatch(raiseHand(false));
  110. }
  111. break;
  112. }
  113. case GRANT_MODERATOR: {
  114. const { conference } = store.getState()['features/base/conference'];
  115. conference.grantOwner(action.id);
  116. break;
  117. }
  118. case KICK_PARTICIPANT: {
  119. const { conference } = store.getState()['features/base/conference'];
  120. conference.kickParticipant(action.id);
  121. break;
  122. }
  123. case LOCAL_PARTICIPANT_RAISE_HAND: {
  124. const { raisedHandTimestamp } = action;
  125. const localId = getLocalParticipant(store.getState())?.id;
  126. store.dispatch(participantUpdated({
  127. // XXX Only the local participant is allowed to update without
  128. // stating the JitsiConference instance (i.e. participant property
  129. // `conference` for a remote participant) because the local
  130. // participant is uniquely identified by the very fact that there is
  131. // only one local participant.
  132. id: localId,
  133. local: true,
  134. raisedHandTimestamp
  135. }));
  136. store.dispatch(raiseHandUpdateQueue({
  137. id: localId,
  138. raisedHandTimestamp
  139. }));
  140. if (typeof APP !== 'undefined') {
  141. APP.API.notifyRaiseHandUpdated(localId, raisedHandTimestamp);
  142. }
  143. break;
  144. }
  145. case MUTE_REMOTE_PARTICIPANT: {
  146. const { conference } = store.getState()['features/base/conference'];
  147. conference.muteParticipant(action.id, action.mediaType);
  148. break;
  149. }
  150. // TODO Remove this middleware when the local display name update flow is
  151. // fully brought into redux.
  152. case PARTICIPANT_DISPLAY_NAME_CHANGED: {
  153. if (typeof APP !== 'undefined') {
  154. const participant = getLocalParticipant(store.getState());
  155. if (participant && participant.id === action.id) {
  156. APP.UI.emitEvent(UIEvents.NICKNAME_CHANGED, action.name);
  157. }
  158. }
  159. break;
  160. }
  161. case RAISE_HAND_UPDATED: {
  162. const { participant } = action;
  163. let queue = getRaiseHandsQueue(store.getState());
  164. if (participant.raisedHandTimestamp) {
  165. queue.push({
  166. id: participant.id,
  167. raisedHandTimestamp: participant.raisedHandTimestamp
  168. });
  169. // sort the queue before adding to store.
  170. queue = queue.sort(({ raisedHandTimestamp: a }, { raisedHandTimestamp: b }) => a - b);
  171. } else {
  172. // no need to sort on remove value.
  173. queue = queue.filter(({ id }) => id !== participant.id);
  174. }
  175. action.queue = queue;
  176. break;
  177. }
  178. case PARTICIPANT_JOINED: {
  179. _maybePlaySounds(store, action);
  180. return _participantJoinedOrUpdated(store, next, action);
  181. }
  182. case PARTICIPANT_LEFT:
  183. _maybePlaySounds(store, action);
  184. break;
  185. case PARTICIPANT_UPDATED:
  186. return _participantJoinedOrUpdated(store, next, action);
  187. }
  188. return next(action);
  189. });
  190. /**
  191. * Syncs the redux state features/base/participants up with the redux state
  192. * features/base/conference by ensuring that the former does not contain remote
  193. * participants no longer relevant to the latter. Introduced to address an issue
  194. * with multiplying thumbnails in the filmstrip.
  195. */
  196. StateListenerRegistry.register(
  197. /* selector */ state => getCurrentConference(state),
  198. /* listener */ (conference, { dispatch, getState }) => {
  199. batch(() => {
  200. for (const [ id, p ] of getRemoteParticipants(getState())) {
  201. (!conference || p.conference !== conference)
  202. && dispatch(participantLeft(id, p.conference, p.isReplaced));
  203. }
  204. });
  205. });
  206. /**
  207. * Reset the ID of the local participant to
  208. * {@link LOCAL_PARTICIPANT_DEFAULT_ID}. Such a reset is deemed possible only if
  209. * the local participant and, respectively, her ID is not involved in a
  210. * conference which is still of interest to the user and, consequently, the app.
  211. * For example, a conference which is in the process of leaving is no longer of
  212. * interest the user, is unrecoverable from the perspective of the user and,
  213. * consequently, the app.
  214. */
  215. StateListenerRegistry.register(
  216. /* selector */ state => state['features/base/conference'],
  217. /* listener */ ({ leaving }, { dispatch, getState }) => {
  218. const state = getState();
  219. const localParticipant = getLocalParticipant(state);
  220. let id;
  221. if (!localParticipant
  222. || (id = localParticipant.id)
  223. === LOCAL_PARTICIPANT_DEFAULT_ID) {
  224. // The ID of the local participant has been reset already.
  225. return;
  226. }
  227. // The ID of the local may be reset only if it is not in use.
  228. const dispatchLocalParticipantIdChanged
  229. = forEachConference(
  230. state,
  231. conference =>
  232. conference === leaving || conference.myUserId() !== id);
  233. dispatchLocalParticipantIdChanged
  234. && dispatch(
  235. localParticipantIdChanged(LOCAL_PARTICIPANT_DEFAULT_ID));
  236. });
  237. /**
  238. * Registers listeners for participant change events.
  239. */
  240. StateListenerRegistry.register(
  241. state => state['features/base/conference'].conference,
  242. (conference, store) => {
  243. if (conference) {
  244. const propertyHandlers = {
  245. 'e2ee.enabled': (participant, value) => _e2eeUpdated(store, conference, participant.getId(), value),
  246. 'features_e2ee': (participant, value) =>
  247. store.dispatch(participantUpdated({
  248. conference,
  249. id: participant.getId(),
  250. e2eeSupported: value
  251. })),
  252. 'features_jigasi': (participant, value) =>
  253. store.dispatch(participantUpdated({
  254. conference,
  255. id: participant.getId(),
  256. isJigasi: value
  257. })),
  258. 'features_screen-sharing': (participant, value) => // eslint-disable-line no-unused-vars
  259. store.dispatch(participantUpdated({
  260. conference,
  261. id: participant.getId(),
  262. features: { 'screen-sharing': true }
  263. })),
  264. 'raisedHand': (participant, value) =>
  265. _raiseHandUpdated(store, conference, participant.getId(), value),
  266. 'region': (participant, value) =>
  267. store.dispatch(participantUpdated({
  268. conference,
  269. id: participant.getId(),
  270. region: value
  271. })),
  272. 'remoteControlSessionStatus': (participant, value) =>
  273. store.dispatch(participantUpdated({
  274. conference,
  275. id: participant.getId(),
  276. remoteControlSessionStatus: value
  277. }))
  278. };
  279. // update properties for the participants that are already in the conference
  280. conference.getParticipants().forEach(participant => {
  281. Object.keys(propertyHandlers).forEach(propertyName => {
  282. const value = participant.getProperty(propertyName);
  283. if (value !== undefined) {
  284. propertyHandlers[propertyName](participant, value);
  285. }
  286. });
  287. });
  288. // We joined a conference
  289. conference.on(
  290. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  291. (participant, propertyName, oldValue, newValue) => {
  292. if (propertyHandlers.hasOwnProperty(propertyName)) {
  293. propertyHandlers[propertyName](participant, newValue);
  294. }
  295. });
  296. } else {
  297. const localParticipantId = getLocalParticipant(store.getState).id;
  298. // We left the conference, the local participant must be updated.
  299. _e2eeUpdated(store, conference, localParticipantId, false);
  300. _raiseHandUpdated(store, conference, localParticipantId, 0);
  301. }
  302. }
  303. );
  304. /**
  305. * Handles a E2EE enabled status update.
  306. *
  307. * @param {Store} store - The redux store.
  308. * @param {Object} conference - The conference for which we got an update.
  309. * @param {string} participantId - The ID of the participant from which we got an update.
  310. * @param {boolean} newValue - The new value of the E2EE enabled status.
  311. * @returns {void}
  312. */
  313. function _e2eeUpdated({ getState, dispatch }, conference, participantId, newValue) {
  314. const e2eeEnabled = newValue === 'true';
  315. const { e2ee = {} } = getState()['features/base/config'];
  316. dispatch(participantUpdated({
  317. conference,
  318. id: participantId,
  319. e2eeEnabled
  320. }));
  321. if (e2ee.externallyManagedKey) {
  322. return;
  323. }
  324. const { maxMode } = getState()['features/e2ee'] || {};
  325. if (maxMode !== MAX_MODE.THRESHOLD_EXCEEDED || !e2eeEnabled) {
  326. dispatch(toggleE2EE(e2eeEnabled));
  327. }
  328. }
  329. /**
  330. * Initializes the local participant and signals that it joined.
  331. *
  332. * @private
  333. * @param {Store} store - The redux store.
  334. * @param {Dispatch} next - The redux dispatch function to dispatch the
  335. * specified action to the specified store.
  336. * @param {Action} action - The redux action which is being dispatched
  337. * in the specified store.
  338. * @private
  339. * @returns {Object} The value returned by {@code next(action)}.
  340. */
  341. function _localParticipantJoined({ getState, dispatch }, next, action) {
  342. const result = next(action);
  343. const settings = getState()['features/base/settings'];
  344. dispatch(localParticipantJoined({
  345. avatarURL: settings.avatarURL,
  346. email: settings.email,
  347. name: settings.displayName
  348. }));
  349. return result;
  350. }
  351. /**
  352. * Signals that the local participant has left.
  353. *
  354. * @param {Store} store - The redux store.
  355. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  356. * specified {@code action} into the specified {@code store}.
  357. * @param {Action} action - The redux action which is being dispatched in the
  358. * specified {@code store}.
  359. * @private
  360. * @returns {Object} The value returned by {@code next(action)}.
  361. */
  362. function _localParticipantLeft({ dispatch }, next, action) {
  363. const result = next(action);
  364. dispatch(localParticipantLeft());
  365. return result;
  366. }
  367. /**
  368. * Plays sounds when participants join/leave conference.
  369. *
  370. * @param {Store} store - The redux store.
  371. * @param {Action} action - The redux action. Should be either
  372. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  373. * @private
  374. * @returns {void}
  375. */
  376. function _maybePlaySounds({ getState, dispatch }, action) {
  377. const state = getState();
  378. const { startAudioMuted } = state['features/base/config'];
  379. const { soundsParticipantJoined: joinSound, soundsParticipantLeft: leftSound } = state['features/base/settings'];
  380. // We're not playing sounds for local participant
  381. // nor when the user is joining past the "startAudioMuted" limit.
  382. // The intention there was to not play user joined notification in big
  383. // conferences where 100th person is joining.
  384. if (!action.participant.local
  385. && (!startAudioMuted
  386. || getParticipantCount(state) < startAudioMuted)) {
  387. const { isReplacing, isReplaced } = action.participant;
  388. if (action.type === PARTICIPANT_JOINED) {
  389. if (!joinSound) {
  390. return;
  391. }
  392. const { presence } = action.participant;
  393. // The sounds for the poltergeist are handled by features/invite.
  394. if (presence !== INVITED && presence !== CALLING && !isReplacing) {
  395. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  396. }
  397. } else if (action.type === PARTICIPANT_LEFT && !isReplaced && leftSound) {
  398. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  399. }
  400. }
  401. }
  402. /**
  403. * Notifies the feature base/participants that the action
  404. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  405. * within a specific redux store.
  406. *
  407. * @param {Store} store - The redux store in which the specified {@code action}
  408. * is being dispatched.
  409. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  410. * specified {@code action} in the specified {@code store}.
  411. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  412. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  413. * {@code store}.
  414. * @private
  415. * @returns {Object} The value returned by {@code next(action)}.
  416. */
  417. function _participantJoinedOrUpdated(store, next, action) {
  418. const { dispatch, getState } = store;
  419. const { participant: { avatarURL, email, id, local, name, raisedHandTimestamp } } = action;
  420. // Send an external update of the local participant's raised hand state
  421. // if a new raised hand state is defined in the action.
  422. if (typeof raisedHandTimestamp !== 'undefined') {
  423. if (local) {
  424. const { conference } = getState()['features/base/conference'];
  425. const rHand = parseInt(raisedHandTimestamp, 10);
  426. // Send raisedHand signalling only if there is a change
  427. if (conference && rHand !== getLocalParticipant(getState()).raisedHandTimestamp) {
  428. conference.setLocalParticipantProperty('raisedHand', rHand);
  429. }
  430. }
  431. }
  432. // Allow the redux update to go through and compare the old avatar
  433. // to the new avatar and emit out change events if necessary.
  434. const result = next(action);
  435. // Only run this if the config is populated, otherwise we preload external resources
  436. // even if disableThirdPartyRequests is set to true in config
  437. if (Object.keys(getState()['features/base/config']).length) {
  438. const { disableThirdPartyRequests } = getState()['features/base/config'];
  439. if (!disableThirdPartyRequests && (avatarURL || email || id || name)) {
  440. const participantId = !id && local ? getLocalParticipant(getState()).id : id;
  441. const updatedParticipant = getParticipantById(getState(), participantId);
  442. getFirstLoadableAvatarUrl(updatedParticipant, store)
  443. .then(urlData => {
  444. dispatch(setLoadableAvatarUrl(participantId, urlData?.src, urlData?.isUsingCORS));
  445. });
  446. }
  447. }
  448. // Notify external listeners of potential avatarURL changes.
  449. if (typeof APP === 'object') {
  450. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  451. // Force update of local video getting a new id.
  452. APP.UI.refreshAvatarDisplay(currentKnownId);
  453. }
  454. return result;
  455. }
  456. /**
  457. * Handles a raise hand status update.
  458. *
  459. * @param {Function} dispatch - The Redux dispatch function.
  460. * @param {Object} conference - The conference for which we got an update.
  461. * @param {string} participantId - The ID of the participant from which we got an update.
  462. * @param {boolean} newValue - The new value of the raise hand status.
  463. * @returns {void}
  464. */
  465. function _raiseHandUpdated({ dispatch, getState }, conference, participantId, newValue) {
  466. let raisedHandTimestamp;
  467. switch (newValue) {
  468. case undefined:
  469. case 'false':
  470. raisedHandTimestamp = 0;
  471. break;
  472. case 'true':
  473. raisedHandTimestamp = Date.now();
  474. break;
  475. default:
  476. raisedHandTimestamp = parseInt(newValue, 10);
  477. }
  478. const state = getState();
  479. dispatch(participantUpdated({
  480. conference,
  481. id: participantId,
  482. raisedHandTimestamp
  483. }));
  484. dispatch(raiseHandUpdateQueue({
  485. id: participantId,
  486. raisedHandTimestamp
  487. }));
  488. if (typeof APP !== 'undefined') {
  489. APP.API.notifyRaiseHandUpdated(participantId, raisedHandTimestamp);
  490. }
  491. const isModerator = isLocalParticipantModerator(state);
  492. const participant = getParticipantById(state, participantId);
  493. let shouldDisplayAllowAction = false;
  494. if (isModerator) {
  495. shouldDisplayAllowAction = isForceMuted(participant, MEDIA_TYPE.AUDIO, state)
  496. || isForceMuted(participant, MEDIA_TYPE.VIDEO, state);
  497. }
  498. const action = shouldDisplayAllowAction ? {
  499. customActionNameKey: [ 'notify.allowAction' ],
  500. customActionHandler: [ () => dispatch(approveParticipant(participantId)) ]
  501. } : {};
  502. if (raisedHandTimestamp) {
  503. let notificationTitle;
  504. const participantName = getParticipantDisplayName(state, participantId);
  505. const { raisedHandsQueue } = state['features/base/participants'];
  506. if (raisedHandsQueue.length > 1) {
  507. const raisedHands = raisedHandsQueue.length - 1;
  508. notificationTitle = i18n.t('notify.raisedHands', {
  509. participantName,
  510. raisedHands
  511. });
  512. } else {
  513. notificationTitle = participantName;
  514. }
  515. dispatch(showNotification({
  516. titleKey: 'notify.somebody',
  517. title: notificationTitle,
  518. descriptionKey: 'notify.raisedHand',
  519. concatText: true,
  520. uid: RAISE_HAND_NOTIFICATION_ID,
  521. ...action
  522. }, shouldDisplayAllowAction ? NOTIFICATION_TIMEOUT_TYPE.MEDIUM : NOTIFICATION_TIMEOUT_TYPE.SHORT));
  523. dispatch(playSound(RAISE_HAND_SOUND_ID));
  524. }
  525. }
  526. /**
  527. * Registers sounds related with the participants feature.
  528. *
  529. * @param {Store} store - The redux store.
  530. * @private
  531. * @returns {void}
  532. */
  533. function _registerSounds({ dispatch }) {
  534. dispatch(
  535. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  536. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  537. }
  538. /**
  539. * Unregisters sounds related with the participants feature.
  540. *
  541. * @param {Store} store - The redux store.
  542. * @private
  543. * @returns {void}
  544. */
  545. function _unregisterSounds({ dispatch }) {
  546. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  547. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  548. }