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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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.raisedHand === 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. raisedHand: false
  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 { enabled } = action;
  112. const localId = getLocalParticipant(store.getState())?.id;
  113. store.dispatch(raiseHandUpdateQueue({
  114. id: localId,
  115. raisedHand: enabled
  116. }));
  117. store.dispatch(participantUpdated({
  118. // XXX Only the local participant is allowed to update without
  119. // stating the JitsiConference instance (i.e. participant property
  120. // `conference` for a remote participant) because the local
  121. // participant is uniquely identified by the very fact that there is
  122. // only one local participant.
  123. id: localId,
  124. local: true,
  125. raisedHand: enabled
  126. }));
  127. if (typeof APP !== 'undefined') {
  128. APP.API.notifyRaiseHandUpdated(localId, enabled);
  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. const queue = getRaiseHandsQueue(store.getState());
  151. if (participant.raisedHand) {
  152. queue.push(participant.id);
  153. action.queue = queue;
  154. } else {
  155. const filteredQueue = queue.filter(id => id !== participant.id);
  156. action.queue = filteredQueue;
  157. }
  158. break;
  159. }
  160. case PARTICIPANT_JOINED: {
  161. _maybePlaySounds(store, action);
  162. return _participantJoinedOrUpdated(store, next, action);
  163. }
  164. case PARTICIPANT_LEFT:
  165. _maybePlaySounds(store, action);
  166. break;
  167. case PARTICIPANT_UPDATED:
  168. return _participantJoinedOrUpdated(store, next, action);
  169. }
  170. return next(action);
  171. });
  172. /**
  173. * Syncs the redux state features/base/participants up with the redux state
  174. * features/base/conference by ensuring that the former does not contain remote
  175. * participants no longer relevant to the latter. Introduced to address an issue
  176. * with multiplying thumbnails in the filmstrip.
  177. */
  178. StateListenerRegistry.register(
  179. /* selector */ state => getCurrentConference(state),
  180. /* listener */ (conference, { dispatch, getState }) => {
  181. batch(() => {
  182. for (const [ id, p ] of getRemoteParticipants(getState())) {
  183. (!conference || p.conference !== conference)
  184. && dispatch(participantLeft(id, p.conference, p.isReplaced));
  185. }
  186. });
  187. });
  188. /**
  189. * Reset the ID of the local participant to
  190. * {@link LOCAL_PARTICIPANT_DEFAULT_ID}. Such a reset is deemed possible only if
  191. * the local participant and, respectively, her ID is not involved in a
  192. * conference which is still of interest to the user and, consequently, the app.
  193. * For example, a conference which is in the process of leaving is no longer of
  194. * interest the user, is unrecoverable from the perspective of the user and,
  195. * consequently, the app.
  196. */
  197. StateListenerRegistry.register(
  198. /* selector */ state => state['features/base/conference'],
  199. /* listener */ ({ leaving }, { dispatch, getState }) => {
  200. const state = getState();
  201. const localParticipant = getLocalParticipant(state);
  202. let id;
  203. if (!localParticipant
  204. || (id = localParticipant.id)
  205. === LOCAL_PARTICIPANT_DEFAULT_ID) {
  206. // The ID of the local participant has been reset already.
  207. return;
  208. }
  209. // The ID of the local may be reset only if it is not in use.
  210. const dispatchLocalParticipantIdChanged
  211. = forEachConference(
  212. state,
  213. conference =>
  214. conference === leaving || conference.myUserId() !== id);
  215. dispatchLocalParticipantIdChanged
  216. && dispatch(
  217. localParticipantIdChanged(LOCAL_PARTICIPANT_DEFAULT_ID));
  218. });
  219. /**
  220. * Registers listeners for participant change events.
  221. */
  222. StateListenerRegistry.register(
  223. state => state['features/base/conference'].conference,
  224. (conference, store) => {
  225. if (conference) {
  226. const propertyHandlers = {
  227. 'e2ee.enabled': (participant, value) => _e2eeUpdated(store, conference, participant.getId(), value),
  228. 'features_e2ee': (participant, value) =>
  229. store.dispatch(participantUpdated({
  230. conference,
  231. id: participant.getId(),
  232. e2eeSupported: value
  233. })),
  234. 'features_jigasi': (participant, value) =>
  235. store.dispatch(participantUpdated({
  236. conference,
  237. id: participant.getId(),
  238. isJigasi: value
  239. })),
  240. 'features_screen-sharing': (participant, value) => // eslint-disable-line no-unused-vars
  241. store.dispatch(participantUpdated({
  242. conference,
  243. id: participant.getId(),
  244. features: { 'screen-sharing': true }
  245. })),
  246. 'raisedHand': (participant, value) => _raiseHandUpdated(store, conference, participant.getId(), value),
  247. 'remoteControlSessionStatus': (participant, value) =>
  248. store.dispatch(participantUpdated({
  249. conference,
  250. id: participant.getId(),
  251. remoteControlSessionStatus: value
  252. }))
  253. };
  254. // update properties for the participants that are already in the conference
  255. conference.getParticipants().forEach(participant => {
  256. Object.keys(propertyHandlers).forEach(propertyName => {
  257. const value = participant.getProperty(propertyName);
  258. if (value !== undefined) {
  259. propertyHandlers[propertyName](participant, value);
  260. }
  261. });
  262. });
  263. // We joined a conference
  264. conference.on(
  265. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  266. (participant, propertyName, oldValue, newValue) => {
  267. if (propertyHandlers.hasOwnProperty(propertyName)) {
  268. propertyHandlers[propertyName](participant, newValue);
  269. }
  270. });
  271. } else {
  272. const localParticipantId = getLocalParticipant(store.getState).id;
  273. // We left the conference, the local participant must be updated.
  274. _e2eeUpdated(store, conference, localParticipantId, false);
  275. _raiseHandUpdated(store, conference, localParticipantId, false);
  276. }
  277. }
  278. );
  279. /**
  280. * Handles a E2EE enabled status update.
  281. *
  282. * @param {Store} store - The redux store.
  283. * @param {Object} conference - The conference for which we got an update.
  284. * @param {string} participantId - The ID of the participant from which we got an update.
  285. * @param {boolean} newValue - The new value of the E2EE enabled status.
  286. * @returns {void}
  287. */
  288. function _e2eeUpdated({ getState, dispatch }, conference, participantId, newValue) {
  289. const e2eeEnabled = newValue === 'true';
  290. const { maxMode } = getState()['features/e2ee'] || {};
  291. if (maxMode !== MAX_MODE.THRESHOLD_EXCEEDED || !e2eeEnabled) {
  292. dispatch(toggleE2EE(e2eeEnabled));
  293. }
  294. dispatch(participantUpdated({
  295. conference,
  296. id: participantId,
  297. e2eeEnabled
  298. }));
  299. }
  300. /**
  301. * Initializes the local participant and signals that it joined.
  302. *
  303. * @private
  304. * @param {Store} store - The redux store.
  305. * @param {Dispatch} next - The redux dispatch function to dispatch the
  306. * specified action to the specified store.
  307. * @param {Action} action - The redux action which is being dispatched
  308. * in the specified store.
  309. * @private
  310. * @returns {Object} The value returned by {@code next(action)}.
  311. */
  312. function _localParticipantJoined({ getState, dispatch }, next, action) {
  313. const result = next(action);
  314. const settings = getState()['features/base/settings'];
  315. dispatch(localParticipantJoined({
  316. avatarURL: settings.avatarURL,
  317. email: settings.email,
  318. name: settings.displayName
  319. }));
  320. return result;
  321. }
  322. /**
  323. * Signals that the local participant has left.
  324. *
  325. * @param {Store} store - The redux store.
  326. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  327. * specified {@code action} into the specified {@code store}.
  328. * @param {Action} action - The redux action which is being dispatched in the
  329. * specified {@code store}.
  330. * @private
  331. * @returns {Object} The value returned by {@code next(action)}.
  332. */
  333. function _localParticipantLeft({ dispatch }, next, action) {
  334. const result = next(action);
  335. dispatch(localParticipantLeft());
  336. return result;
  337. }
  338. /**
  339. * Plays sounds when participants join/leave conference.
  340. *
  341. * @param {Store} store - The redux store.
  342. * @param {Action} action - The redux action. Should be either
  343. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  344. * @private
  345. * @returns {void}
  346. */
  347. function _maybePlaySounds({ getState, dispatch }, action) {
  348. const state = getState();
  349. const { startAudioMuted } = state['features/base/config'];
  350. const { soundsParticipantJoined: joinSound, soundsParticipantLeft: leftSound } = state['features/base/settings'];
  351. // We're not playing sounds for local participant
  352. // nor when the user is joining past the "startAudioMuted" limit.
  353. // The intention there was to not play user joined notification in big
  354. // conferences where 100th person is joining.
  355. if (!action.participant.local
  356. && (!startAudioMuted
  357. || getParticipantCount(state) < startAudioMuted)) {
  358. const { isReplacing, isReplaced } = action.participant;
  359. if (action.type === PARTICIPANT_JOINED) {
  360. if (!joinSound) {
  361. return;
  362. }
  363. const { presence } = action.participant;
  364. // The sounds for the poltergeist are handled by features/invite.
  365. if (presence !== INVITED && presence !== CALLING && !isReplacing) {
  366. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  367. }
  368. } else if (action.type === PARTICIPANT_LEFT && !isReplaced && leftSound) {
  369. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  370. }
  371. }
  372. }
  373. /**
  374. * Notifies the feature base/participants that the action
  375. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  376. * within a specific redux store.
  377. *
  378. * @param {Store} store - The redux store in which the specified {@code action}
  379. * is being dispatched.
  380. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  381. * specified {@code action} in the specified {@code store}.
  382. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  383. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  384. * {@code store}.
  385. * @private
  386. * @returns {Object} The value returned by {@code next(action)}.
  387. */
  388. function _participantJoinedOrUpdated(store, next, action) {
  389. const { dispatch, getState } = store;
  390. const { participant: { avatarURL, email, id, local, name, raisedHand } } = action;
  391. // Send an external update of the local participant's raised hand state
  392. // if a new raised hand state is defined in the action.
  393. if (typeof raisedHand !== 'undefined') {
  394. if (local) {
  395. const { conference } = getState()['features/base/conference'];
  396. // Send raisedHand signalling only if there is a change
  397. if (conference && raisedHand !== getLocalParticipant(getState()).raisedHand) {
  398. conference.setLocalParticipantProperty('raisedHand', raisedHand);
  399. }
  400. }
  401. }
  402. // Allow the redux update to go through and compare the old avatar
  403. // to the new avatar and emit out change events if necessary.
  404. const result = next(action);
  405. // Only run this if the config is populated, otherwise we preload external resources
  406. // even if disableThirdPartyRequests is set to true in config
  407. if (Object.keys(getState()['features/base/config']).length) {
  408. const { disableThirdPartyRequests } = getState()['features/base/config'];
  409. if (!disableThirdPartyRequests && (avatarURL || email || id || name)) {
  410. const participantId = !id && local ? getLocalParticipant(getState()).id : id;
  411. const updatedParticipant = getParticipantById(getState(), participantId);
  412. getFirstLoadableAvatarUrl(updatedParticipant, store)
  413. .then(url => {
  414. dispatch(setLoadableAvatarUrl(participantId, url));
  415. });
  416. }
  417. }
  418. // Notify external listeners of potential avatarURL changes.
  419. if (typeof APP === 'object') {
  420. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  421. // Force update of local video getting a new id.
  422. APP.UI.refreshAvatarDisplay(currentKnownId);
  423. }
  424. return result;
  425. }
  426. /**
  427. * Handles a raise hand status update.
  428. *
  429. * @param {Function} dispatch - The Redux dispatch function.
  430. * @param {Object} conference - The conference for which we got an update.
  431. * @param {string} participantId - The ID of the participant from which we got an update.
  432. * @param {boolean} newValue - The new value of the raise hand status.
  433. * @returns {void}
  434. */
  435. function _raiseHandUpdated({ dispatch, getState }, conference, participantId, newValue) {
  436. const raisedHand = newValue === 'true';
  437. const state = getState();
  438. dispatch(participantUpdated({
  439. conference,
  440. id: participantId,
  441. raisedHand
  442. }));
  443. dispatch(raiseHandUpdateQueue({
  444. id: participantId,
  445. raisedHand
  446. }));
  447. if (typeof APP !== 'undefined') {
  448. APP.API.notifyRaiseHandUpdated(participantId, raisedHand);
  449. }
  450. const isModerator = isLocalParticipantModerator(state);
  451. const participant = getParticipantById(state, participantId);
  452. let shouldDisplayAllowAction = false;
  453. if (isModerator) {
  454. shouldDisplayAllowAction = isForceMuted(participant, MEDIA_TYPE.AUDIO, state)
  455. || isForceMuted(participant, MEDIA_TYPE.VIDEO, state);
  456. }
  457. const action = shouldDisplayAllowAction ? {
  458. customActionNameKey: 'notify.allowAction',
  459. customActionHandler: () => dispatch(approveParticipant(participantId))
  460. } : {};
  461. if (raisedHand) {
  462. dispatch(showNotification({
  463. titleKey: 'notify.somebody',
  464. title: getParticipantDisplayName(state, participantId),
  465. descriptionKey: 'notify.raisedHand',
  466. raiseHandNotification: true,
  467. concatText: true,
  468. ...action
  469. }, NOTIFICATION_TIMEOUT * (shouldDisplayAllowAction ? 2 : 1)));
  470. dispatch(playSound(RAISE_HAND_SOUND_ID));
  471. }
  472. }
  473. /**
  474. * Registers sounds related with the participants feature.
  475. *
  476. * @param {Store} store - The redux store.
  477. * @private
  478. * @returns {void}
  479. */
  480. function _registerSounds({ dispatch }) {
  481. dispatch(
  482. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  483. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  484. }
  485. /**
  486. * Unregisters sounds related with the participants feature.
  487. *
  488. * @param {Store} store - The redux store.
  489. * @private
  490. * @returns {void}
  491. */
  492. function _unregisterSounds({ dispatch }) {
  493. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  494. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  495. }