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

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