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

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