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.ts 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. import i18n from 'i18next';
  2. import { batch } from 'react-redux';
  3. // @ts-expect-error
  4. import UIEvents from '../../../../service/UI/UIEvents';
  5. import { IStore } from '../../app/types';
  6. import { approveParticipant } from '../../av-moderation/actions';
  7. import { UPDATE_BREAKOUT_ROOMS } from '../../breakout-rooms/actionTypes';
  8. import { getBreakoutRooms } from '../../breakout-rooms/functions';
  9. import { toggleE2EE } from '../../e2ee/actions';
  10. import { MAX_MODE } from '../../e2ee/constants';
  11. import { showNotification } from '../../notifications/actions';
  12. import {
  13. LOCAL_RECORDING_NOTIFICATION_ID,
  14. NOTIFICATION_TIMEOUT_TYPE,
  15. RAISE_HAND_NOTIFICATION_ID
  16. } from '../../notifications/constants';
  17. import { isForceMuted } from '../../participants-pane/functions';
  18. import { CALLING, INVITED } from '../../presence-status/constants';
  19. import { RAISE_HAND_SOUND_ID } from '../../reactions/constants';
  20. import { RECORDING_OFF_SOUND_ID, RECORDING_ON_SOUND_ID } from '../../recording/constants';
  21. import { APP_WILL_MOUNT, APP_WILL_UNMOUNT } from '../app/actionTypes';
  22. import { CONFERENCE_WILL_JOIN } from '../conference/actionTypes';
  23. import { forEachConference, getCurrentConference } from '../conference/functions';
  24. import { IJitsiConference } from '../conference/reducer';
  25. import { SET_CONFIG } from '../config/actionTypes';
  26. import { getDisableRemoveRaisedHandOnFocus } from '../config/functions.any';
  27. import { JitsiConferenceEvents } from '../lib-jitsi-meet';
  28. import { MEDIA_TYPE } from '../media/constants';
  29. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  30. import StateListenerRegistry from '../redux/StateListenerRegistry';
  31. import { playSound, registerSound, unregisterSound } from '../sounds/actions';
  32. import {
  33. DOMINANT_SPEAKER_CHANGED,
  34. GRANT_MODERATOR,
  35. KICK_PARTICIPANT,
  36. LOCAL_PARTICIPANT_AUDIO_LEVEL_CHANGED,
  37. LOCAL_PARTICIPANT_RAISE_HAND,
  38. MUTE_REMOTE_PARTICIPANT,
  39. OVERWRITE_PARTICIPANTS_NAMES,
  40. OVERWRITE_PARTICIPANT_NAME,
  41. PARTICIPANT_DISPLAY_NAME_CHANGED,
  42. PARTICIPANT_JOINED,
  43. PARTICIPANT_LEFT,
  44. PARTICIPANT_UPDATED,
  45. RAISE_HAND_UPDATED,
  46. SET_LOCAL_PARTICIPANT_RECORDING_STATUS
  47. } from './actionTypes';
  48. import {
  49. localParticipantIdChanged,
  50. localParticipantJoined,
  51. localParticipantLeft,
  52. overwriteParticipantName,
  53. participantLeft,
  54. participantUpdated,
  55. raiseHand,
  56. raiseHandUpdateQueue,
  57. setLoadableAvatarUrl
  58. } from './actions';
  59. import {
  60. LOCAL_PARTICIPANT_DEFAULT_ID,
  61. LOWER_HAND_AUDIO_LEVEL,
  62. PARTICIPANT_JOINED_SOUND_ID,
  63. PARTICIPANT_LEFT_SOUND_ID
  64. } from './constants';
  65. import {
  66. getDominantSpeakerParticipant,
  67. getFirstLoadableAvatarUrl,
  68. getLocalParticipant,
  69. getParticipantById,
  70. getParticipantCount,
  71. getParticipantDisplayName,
  72. getRaiseHandsQueue,
  73. getRemoteParticipants,
  74. hasRaisedHand,
  75. isLocalParticipantModerator,
  76. isScreenShareParticipant,
  77. isWhiteboardParticipant
  78. } from './functions';
  79. import logger from './logger';
  80. import { PARTICIPANT_JOINED_FILE, PARTICIPANT_LEFT_FILE } from './sounds';
  81. import { FakeParticipant, IJitsiParticipant } from './types';
  82. import './subscriber';
  83. /**
  84. * Middleware that captures CONFERENCE_JOINED and CONFERENCE_LEFT actions and
  85. * updates respectively ID of local participant.
  86. *
  87. * @param {Store} store - The redux store.
  88. * @returns {Function}
  89. */
  90. MiddlewareRegistry.register(store => next => action => {
  91. switch (action.type) {
  92. case APP_WILL_MOUNT:
  93. _registerSounds(store);
  94. return _localParticipantJoined(store, next, action);
  95. case APP_WILL_UNMOUNT:
  96. _unregisterSounds(store);
  97. return _localParticipantLeft(store, next, action);
  98. case CONFERENCE_WILL_JOIN:
  99. store.dispatch(localParticipantIdChanged(action.conference.myUserId()));
  100. break;
  101. case DOMINANT_SPEAKER_CHANGED: {
  102. // Lower hand through xmpp when local participant becomes dominant speaker.
  103. const { id } = action.participant;
  104. const state = store.getState();
  105. const participant = getLocalParticipant(state);
  106. const dominantSpeaker = getDominantSpeakerParticipant(state);
  107. const isLocal = participant && participant.id === id;
  108. if (isLocal && dominantSpeaker?.id !== id
  109. && hasRaisedHand(participant)
  110. && !getDisableRemoveRaisedHandOnFocus(state)) {
  111. store.dispatch(raiseHand(false));
  112. }
  113. break;
  114. }
  115. case LOCAL_PARTICIPANT_AUDIO_LEVEL_CHANGED: {
  116. const state = store.getState();
  117. const participant = getDominantSpeakerParticipant(state);
  118. if (
  119. participant?.local
  120. && hasRaisedHand(participant)
  121. && action.level > LOWER_HAND_AUDIO_LEVEL
  122. && !getDisableRemoveRaisedHandOnFocus(state)
  123. ) {
  124. store.dispatch(raiseHand(false));
  125. }
  126. break;
  127. }
  128. case GRANT_MODERATOR: {
  129. const { conference } = store.getState()['features/base/conference'];
  130. conference?.grantOwner(action.id);
  131. break;
  132. }
  133. case KICK_PARTICIPANT: {
  134. const { conference } = store.getState()['features/base/conference'];
  135. conference?.kickParticipant(action.id);
  136. break;
  137. }
  138. case LOCAL_PARTICIPANT_RAISE_HAND: {
  139. const { raisedHandTimestamp } = action;
  140. const localId = getLocalParticipant(store.getState())?.id;
  141. store.dispatch(participantUpdated({
  142. // XXX Only the local participant is allowed to update without
  143. // stating the JitsiConference instance (i.e. participant property
  144. // `conference` for a remote participant) because the local
  145. // participant is uniquely identified by the very fact that there is
  146. // only one local participant.
  147. id: localId ?? '',
  148. local: true,
  149. raisedHandTimestamp
  150. }));
  151. store.dispatch(raiseHandUpdateQueue({
  152. id: localId ?? '',
  153. raisedHandTimestamp
  154. }));
  155. if (typeof APP !== 'undefined') {
  156. APP.API.notifyRaiseHandUpdated(localId, raisedHandTimestamp);
  157. }
  158. break;
  159. }
  160. case SET_CONFIG: {
  161. const result = next(action);
  162. const state = store.getState();
  163. const { deploymentInfo } = state['features/base/config'];
  164. // if there userRegion set let's use it for the local participant
  165. if (deploymentInfo?.userRegion) {
  166. const localId = getLocalParticipant(state)?.id;
  167. if (localId) {
  168. store.dispatch(participantUpdated({
  169. id: localId,
  170. local: true,
  171. region: deploymentInfo.userRegion
  172. }));
  173. }
  174. }
  175. return result;
  176. }
  177. case SET_LOCAL_PARTICIPANT_RECORDING_STATUS: {
  178. const state = store.getState();
  179. const { recording, onlySelf } = action;
  180. const localId = getLocalParticipant(state)?.id;
  181. const { localRecording } = state['features/base/config'];
  182. if (localRecording?.notifyAllParticipants && !onlySelf && localId) {
  183. store.dispatch(participantUpdated({
  184. // XXX Only the local participant is allowed to update without
  185. // stating the JitsiConference instance (i.e. participant property
  186. // `conference` for a remote participant) because the local
  187. // participant is uniquely identified by the very fact that there is
  188. // only one local participant.
  189. id: localId,
  190. local: true,
  191. localRecording: recording
  192. }));
  193. }
  194. break;
  195. }
  196. case MUTE_REMOTE_PARTICIPANT: {
  197. const { conference } = store.getState()['features/base/conference'];
  198. conference?.muteParticipant(action.id, action.mediaType);
  199. break;
  200. }
  201. // TODO Remove this middleware when the local display name update flow is
  202. // fully brought into redux.
  203. case PARTICIPANT_DISPLAY_NAME_CHANGED: {
  204. if (typeof APP !== 'undefined') {
  205. const participant = getLocalParticipant(store.getState());
  206. if (participant && participant.id === action.id) {
  207. APP.UI.emitEvent(UIEvents.NICKNAME_CHANGED, action.name);
  208. }
  209. }
  210. break;
  211. }
  212. case RAISE_HAND_UPDATED: {
  213. const { participant } = action;
  214. let queue = getRaiseHandsQueue(store.getState());
  215. if (participant.raisedHandTimestamp) {
  216. queue.push({
  217. id: participant.id,
  218. raisedHandTimestamp: participant.raisedHandTimestamp
  219. });
  220. // sort the queue before adding to store.
  221. queue = queue.sort(({ raisedHandTimestamp: a }, { raisedHandTimestamp: b }) => a - b);
  222. } else {
  223. // no need to sort on remove value.
  224. queue = queue.filter(({ id }) => id !== participant.id);
  225. }
  226. action.queue = queue;
  227. break;
  228. }
  229. case PARTICIPANT_JOINED: {
  230. // Do not play sounds when a screenshare or whiteboard participant tile is created for screenshare.
  231. (!isScreenShareParticipant(action.participant)
  232. && !isWhiteboardParticipant(action.participant)
  233. ) && _maybePlaySounds(store, action);
  234. return _participantJoinedOrUpdated(store, next, action);
  235. }
  236. case PARTICIPANT_LEFT: {
  237. // Do not play sounds when a tile for screenshare or whiteboard is removed.
  238. (!isScreenShareParticipant(action.participant)
  239. && !isWhiteboardParticipant(action.participant)
  240. ) && _maybePlaySounds(store, action);
  241. break;
  242. }
  243. case PARTICIPANT_UPDATED:
  244. return _participantJoinedOrUpdated(store, next, action);
  245. case OVERWRITE_PARTICIPANTS_NAMES: {
  246. const { participantList } = action;
  247. if (!Array.isArray(participantList)) {
  248. logger.error('Overwrite names failed. Argument is not an array.');
  249. return;
  250. }
  251. batch(() => {
  252. participantList.forEach(p => {
  253. store.dispatch(overwriteParticipantName(p.id, p.name));
  254. });
  255. });
  256. break;
  257. }
  258. case OVERWRITE_PARTICIPANT_NAME: {
  259. const { dispatch, getState } = store;
  260. const state = getState();
  261. const { id, name } = action;
  262. let breakoutRoom = false, identifier = id;
  263. if (id.indexOf('@') !== -1) {
  264. identifier = id.slice(id.indexOf('/') + 1);
  265. breakoutRoom = true;
  266. action.id = identifier;
  267. }
  268. if (breakoutRoom) {
  269. const rooms = getBreakoutRooms(state);
  270. const roomCounter = state['features/breakout-rooms'].roomCounter;
  271. const newRooms: any = {};
  272. Object.entries(rooms).forEach(([ key, r ]) => {
  273. const participants = r?.participants || {};
  274. const jid = Object.keys(participants).find(p =>
  275. p.slice(p.indexOf('/') + 1) === identifier);
  276. if (jid) {
  277. newRooms[key] = {
  278. ...r,
  279. participants: {
  280. ...participants,
  281. [jid]: {
  282. ...participants[jid],
  283. displayName: name
  284. }
  285. }
  286. };
  287. } else {
  288. newRooms[key] = r;
  289. }
  290. });
  291. dispatch({
  292. type: UPDATE_BREAKOUT_ROOMS,
  293. rooms,
  294. roomCounter,
  295. updatedNames: true
  296. });
  297. } else {
  298. dispatch(participantUpdated({
  299. id: identifier,
  300. name
  301. }));
  302. }
  303. break;
  304. }
  305. }
  306. return next(action);
  307. });
  308. /**
  309. * Syncs the redux state features/base/participants up with the redux state
  310. * features/base/conference by ensuring that the former does not contain remote
  311. * participants no longer relevant to the latter. Introduced to address an issue
  312. * with multiplying thumbnails in the filmstrip.
  313. */
  314. StateListenerRegistry.register(
  315. /* selector */ state => getCurrentConference(state),
  316. /* listener */ (conference, { dispatch, getState }) => {
  317. batch(() => {
  318. for (const [ id, p ] of getRemoteParticipants(getState())) {
  319. (!conference || p.conference !== conference)
  320. && dispatch(participantLeft(id, p.conference, {
  321. isReplaced: p.isReplaced
  322. }));
  323. }
  324. });
  325. });
  326. /**
  327. * Reset the ID of the local participant to
  328. * {@link LOCAL_PARTICIPANT_DEFAULT_ID}. Such a reset is deemed possible only if
  329. * the local participant and, respectively, her ID is not involved in a
  330. * conference which is still of interest to the user and, consequently, the app.
  331. * For example, a conference which is in the process of leaving is no longer of
  332. * interest the user, is unrecoverable from the perspective of the user and,
  333. * consequently, the app.
  334. */
  335. StateListenerRegistry.register(
  336. /* selector */ state => state['features/base/conference'],
  337. /* listener */ ({ leaving }, { dispatch, getState }) => {
  338. const state = getState();
  339. const localParticipant = getLocalParticipant(state);
  340. let id: string;
  341. if (!localParticipant
  342. || (id = localParticipant.id)
  343. === LOCAL_PARTICIPANT_DEFAULT_ID) {
  344. // The ID of the local participant has been reset already.
  345. return;
  346. }
  347. // The ID of the local may be reset only if it is not in use.
  348. const dispatchLocalParticipantIdChanged
  349. = forEachConference(
  350. state,
  351. conference =>
  352. conference === leaving || conference.myUserId() !== id);
  353. dispatchLocalParticipantIdChanged
  354. && dispatch(
  355. localParticipantIdChanged(LOCAL_PARTICIPANT_DEFAULT_ID));
  356. });
  357. /**
  358. * Registers listeners for participant change events.
  359. */
  360. StateListenerRegistry.register(
  361. state => state['features/base/conference'].conference,
  362. (conference, store) => {
  363. if (conference) {
  364. const propertyHandlers: {
  365. [key: string]: Function;
  366. } = {
  367. 'e2ee.enabled': (participant: IJitsiParticipant, value: string) =>
  368. _e2eeUpdated(store, conference, participant.getId(), value),
  369. 'features_e2ee': (participant: IJitsiParticipant, value: boolean) =>
  370. store.dispatch(participantUpdated({
  371. conference,
  372. id: participant.getId(),
  373. e2eeSupported: value
  374. })),
  375. 'features_jigasi': (participant: IJitsiParticipant, value: boolean) =>
  376. store.dispatch(participantUpdated({
  377. conference,
  378. id: participant.getId(),
  379. fakeParticipant: value ? FakeParticipant.Jigasi : undefined
  380. })),
  381. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  382. 'features_screen-sharing': (participant: IJitsiParticipant, value: string) =>
  383. store.dispatch(participantUpdated({
  384. conference,
  385. id: participant.getId(),
  386. features: { 'screen-sharing': true }
  387. })),
  388. 'localRecording': (participant: IJitsiParticipant, value: string) =>
  389. _localRecordingUpdated(store, conference, participant.getId(), value),
  390. 'raisedHand': (participant: IJitsiParticipant, value: string) =>
  391. _raiseHandUpdated(store, conference, participant.getId(), value),
  392. 'region': (participant: IJitsiParticipant, value: string) =>
  393. store.dispatch(participantUpdated({
  394. conference,
  395. id: participant.getId(),
  396. region: value
  397. })),
  398. 'remoteControlSessionStatus': (participant: IJitsiParticipant, value: boolean) =>
  399. store.dispatch(participantUpdated({
  400. conference,
  401. id: participant.getId(),
  402. remoteControlSessionStatus: value
  403. }))
  404. };
  405. // update properties for the participants that are already in the conference
  406. conference.getParticipants().forEach((participant: any) => {
  407. Object.keys(propertyHandlers).forEach(propertyName => {
  408. const value = participant.getProperty(propertyName);
  409. if (value !== undefined) {
  410. propertyHandlers[propertyName as keyof typeof propertyHandlers](participant, value);
  411. }
  412. });
  413. });
  414. // We joined a conference
  415. conference.on(
  416. JitsiConferenceEvents.PARTICIPANT_PROPERTY_CHANGED,
  417. (participant: IJitsiParticipant, propertyName: string, oldValue: string, newValue: string) => {
  418. if (propertyHandlers.hasOwnProperty(propertyName)) {
  419. propertyHandlers[propertyName](participant, newValue);
  420. }
  421. });
  422. } else {
  423. const localParticipantId = getLocalParticipant(store.getState)?.id;
  424. // We left the conference, the local participant must be updated.
  425. _e2eeUpdated(store, conference, localParticipantId ?? '', false);
  426. _raiseHandUpdated(store, conference, localParticipantId ?? '', 0);
  427. }
  428. }
  429. );
  430. /**
  431. * Handles a E2EE enabled status update.
  432. *
  433. * @param {Store} store - The redux store.
  434. * @param {Object} conference - The conference for which we got an update.
  435. * @param {string} participantId - The ID of the participant from which we got an update.
  436. * @param {boolean} newValue - The new value of the E2EE enabled status.
  437. * @returns {void}
  438. */
  439. function _e2eeUpdated({ getState, dispatch }: IStore, conference: IJitsiConference,
  440. participantId: string, newValue: string | boolean) {
  441. const e2eeEnabled = newValue === 'true';
  442. const { e2ee = {} } = getState()['features/base/config'];
  443. dispatch(participantUpdated({
  444. conference,
  445. id: participantId,
  446. e2eeEnabled
  447. }));
  448. if (e2ee.externallyManagedKey) {
  449. return;
  450. }
  451. const { maxMode } = getState()['features/e2ee'] || {};
  452. if (maxMode !== MAX_MODE.THRESHOLD_EXCEEDED || !e2eeEnabled) {
  453. dispatch(toggleE2EE(e2eeEnabled));
  454. }
  455. }
  456. /**
  457. * Initializes the local participant and signals that it joined.
  458. *
  459. * @private
  460. * @param {Store} store - The redux store.
  461. * @param {Dispatch} next - The redux dispatch function to dispatch the
  462. * specified action to the specified store.
  463. * @param {Action} action - The redux action which is being dispatched
  464. * in the specified store.
  465. * @private
  466. * @returns {Object} The value returned by {@code next(action)}.
  467. */
  468. function _localParticipantJoined({ getState, dispatch }: IStore, next: Function, action: any) {
  469. const result = next(action);
  470. const settings = getState()['features/base/settings'];
  471. // @ts-ignore
  472. dispatch(localParticipantJoined({
  473. avatarURL: settings.avatarURL,
  474. email: settings.email,
  475. name: settings.displayName
  476. }));
  477. return result;
  478. }
  479. /**
  480. * Signals that the local participant has left.
  481. *
  482. * @param {Store} store - The redux store.
  483. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  484. * specified {@code action} into the specified {@code store}.
  485. * @param {Action} action - The redux action which is being dispatched in the
  486. * specified {@code store}.
  487. * @private
  488. * @returns {Object} The value returned by {@code next(action)}.
  489. */
  490. function _localParticipantLeft({ dispatch }: IStore, next: Function, action: any) {
  491. const result = next(action);
  492. dispatch(localParticipantLeft());
  493. return result;
  494. }
  495. /**
  496. * Plays sounds when participants join/leave conference.
  497. *
  498. * @param {Store} store - The redux store.
  499. * @param {Action} action - The redux action. Should be either
  500. * {@link PARTICIPANT_JOINED} or {@link PARTICIPANT_LEFT}.
  501. * @private
  502. * @returns {void}
  503. */
  504. function _maybePlaySounds({ getState, dispatch }: IStore, action: any) {
  505. const state = getState();
  506. const { startAudioMuted } = state['features/base/config'];
  507. const { soundsParticipantJoined: joinSound, soundsParticipantLeft: leftSound } = state['features/base/settings'];
  508. // We're not playing sounds for local participant
  509. // nor when the user is joining past the "startAudioMuted" limit.
  510. // The intention there was to not play user joined notification in big
  511. // conferences where 100th person is joining.
  512. if (!action.participant.local
  513. && (!startAudioMuted
  514. || getParticipantCount(state) < startAudioMuted)) {
  515. const { isReplacing, isReplaced } = action.participant;
  516. if (action.type === PARTICIPANT_JOINED) {
  517. if (!joinSound) {
  518. return;
  519. }
  520. const { presence } = action.participant;
  521. // The sounds for the poltergeist are handled by features/invite.
  522. if (presence !== INVITED && presence !== CALLING && !isReplacing) {
  523. dispatch(playSound(PARTICIPANT_JOINED_SOUND_ID));
  524. }
  525. } else if (action.type === PARTICIPANT_LEFT && !isReplaced && leftSound) {
  526. dispatch(playSound(PARTICIPANT_LEFT_SOUND_ID));
  527. }
  528. }
  529. }
  530. /**
  531. * Notifies the feature base/participants that the action
  532. * {@code PARTICIPANT_JOINED} or {@code PARTICIPANT_UPDATED} is being dispatched
  533. * within a specific redux store.
  534. *
  535. * @param {Store} store - The redux store in which the specified {@code action}
  536. * is being dispatched.
  537. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  538. * specified {@code action} in the specified {@code store}.
  539. * @param {Action} action - The redux action {@code PARTICIPANT_JOINED} or
  540. * {@code PARTICIPANT_UPDATED} which is being dispatched in the specified
  541. * {@code store}.
  542. * @private
  543. * @returns {Object} The value returned by {@code next(action)}.
  544. */
  545. function _participantJoinedOrUpdated(store: IStore, next: Function, action: any) {
  546. const { dispatch, getState } = store;
  547. const { overwrittenNameList } = store.getState()['features/base/participants'];
  548. const { participant: {
  549. avatarURL,
  550. email,
  551. id,
  552. local,
  553. localRecording,
  554. name,
  555. raisedHandTimestamp
  556. } } = action;
  557. // Send an external update of the local participant's raised hand state
  558. // if a new raised hand state is defined in the action.
  559. if (typeof raisedHandTimestamp !== 'undefined') {
  560. if (local) {
  561. const { conference } = getState()['features/base/conference'];
  562. const rHand = parseInt(raisedHandTimestamp, 10);
  563. // Send raisedHand signalling only if there is a change
  564. if (conference && rHand !== getLocalParticipant(getState())?.raisedHandTimestamp) {
  565. conference.setLocalParticipantProperty('raisedHand', rHand);
  566. }
  567. }
  568. }
  569. if (overwrittenNameList[id]) {
  570. action.participant.name = overwrittenNameList[id];
  571. }
  572. // Send an external update of the local participant's local recording state
  573. // if a new local recording state is defined in the action.
  574. if (typeof localRecording !== 'undefined') {
  575. if (local) {
  576. const conference = getCurrentConference(getState);
  577. // Send localRecording signalling only if there is a change
  578. if (conference
  579. && localRecording !== getLocalParticipant(getState())?.localRecording) {
  580. conference.setLocalParticipantProperty('localRecording', localRecording);
  581. }
  582. }
  583. }
  584. // Allow the redux update to go through and compare the old avatar
  585. // to the new avatar and emit out change events if necessary.
  586. const result = next(action);
  587. // Only run this if the config is populated, otherwise we preload external resources
  588. // even if disableThirdPartyRequests is set to true in config
  589. if (Object.keys(getState()['features/base/config']).length) {
  590. const { disableThirdPartyRequests } = getState()['features/base/config'];
  591. if (!disableThirdPartyRequests && (avatarURL || email || id || name)) {
  592. const participantId = !id && local ? getLocalParticipant(getState())?.id : id;
  593. const updatedParticipant = getParticipantById(getState(), participantId);
  594. getFirstLoadableAvatarUrl(updatedParticipant ?? { id: '' }, store)
  595. .then((urlData?: { isUsingCORS: boolean; src: string; }) => {
  596. dispatch(setLoadableAvatarUrl(participantId, urlData?.src ?? '', Boolean(urlData?.isUsingCORS)));
  597. });
  598. }
  599. }
  600. // Notify external listeners of potential avatarURL changes.
  601. if (typeof APP === 'object') {
  602. const currentKnownId = local ? APP.conference.getMyUserId() : id;
  603. // Force update of local video getting a new id.
  604. APP.UI.refreshAvatarDisplay(currentKnownId);
  605. }
  606. return result;
  607. }
  608. /**
  609. * Handles a local recording status update.
  610. *
  611. * @param {Function} dispatch - The Redux dispatch function.
  612. * @param {Object} conference - The conference for which we got an update.
  613. * @param {string} participantId - The ID of the participant from which we got an update.
  614. * @param {boolean} newValue - The new value of the local recording status.
  615. * @returns {void}
  616. */
  617. function _localRecordingUpdated({ dispatch, getState }: IStore, conference: IJitsiConference,
  618. participantId: string, newValue: string) {
  619. const state = getState();
  620. dispatch(participantUpdated({
  621. conference,
  622. id: participantId,
  623. localRecording: newValue
  624. }));
  625. const participantName = getParticipantDisplayName(state, participantId);
  626. dispatch(showNotification({
  627. titleKey: 'notify.somebody',
  628. title: participantName,
  629. descriptionKey: newValue ? 'notify.localRecordingStarted' : 'notify.localRecordingStopped',
  630. uid: LOCAL_RECORDING_NOTIFICATION_ID
  631. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  632. dispatch(playSound(newValue ? RECORDING_ON_SOUND_ID : RECORDING_OFF_SOUND_ID));
  633. }
  634. /**
  635. * Handles a raise hand status update.
  636. *
  637. * @param {Function} dispatch - The Redux dispatch function.
  638. * @param {Object} conference - The conference for which we got an update.
  639. * @param {string} participantId - The ID of the participant from which we got an update.
  640. * @param {boolean} newValue - The new value of the raise hand status.
  641. * @returns {void}
  642. */
  643. function _raiseHandUpdated({ dispatch, getState }: IStore, conference: IJitsiConference,
  644. participantId: string, newValue: string | number) {
  645. let raisedHandTimestamp;
  646. switch (newValue) {
  647. case undefined:
  648. case 'false':
  649. raisedHandTimestamp = 0;
  650. break;
  651. case 'true':
  652. raisedHandTimestamp = Date.now();
  653. break;
  654. default:
  655. raisedHandTimestamp = parseInt(`${newValue}`, 10);
  656. }
  657. const state = getState();
  658. dispatch(participantUpdated({
  659. conference,
  660. id: participantId,
  661. raisedHandTimestamp
  662. }));
  663. dispatch(raiseHandUpdateQueue({
  664. id: participantId,
  665. raisedHandTimestamp
  666. }));
  667. if (typeof APP !== 'undefined') {
  668. APP.API.notifyRaiseHandUpdated(participantId, raisedHandTimestamp);
  669. }
  670. const isModerator = isLocalParticipantModerator(state);
  671. const participant = getParticipantById(state, participantId);
  672. let shouldDisplayAllowAction = false;
  673. if (isModerator) {
  674. shouldDisplayAllowAction = isForceMuted(participant, MEDIA_TYPE.AUDIO, state)
  675. || isForceMuted(participant, MEDIA_TYPE.VIDEO, state);
  676. }
  677. const action = shouldDisplayAllowAction ? {
  678. customActionNameKey: [ 'notify.allowAction' ],
  679. customActionHandler: [ () => dispatch(approveParticipant(participantId)) ]
  680. } : {};
  681. if (raisedHandTimestamp) {
  682. let notificationTitle;
  683. const participantName = getParticipantDisplayName(state, participantId);
  684. const { raisedHandsQueue } = state['features/base/participants'];
  685. if (raisedHandsQueue.length > 1) {
  686. const raisedHands = raisedHandsQueue.length - 1;
  687. notificationTitle = i18n.t('notify.raisedHands', {
  688. participantName,
  689. raisedHands
  690. });
  691. } else {
  692. notificationTitle = participantName;
  693. }
  694. dispatch(showNotification({
  695. titleKey: 'notify.somebody',
  696. title: notificationTitle,
  697. descriptionKey: 'notify.raisedHand',
  698. concatText: true,
  699. uid: RAISE_HAND_NOTIFICATION_ID,
  700. ...action
  701. }, shouldDisplayAllowAction ? NOTIFICATION_TIMEOUT_TYPE.MEDIUM : NOTIFICATION_TIMEOUT_TYPE.SHORT));
  702. dispatch(playSound(RAISE_HAND_SOUND_ID));
  703. }
  704. }
  705. /**
  706. * Registers sounds related with the participants feature.
  707. *
  708. * @param {Store} store - The redux store.
  709. * @private
  710. * @returns {void}
  711. */
  712. function _registerSounds({ dispatch }: IStore) {
  713. dispatch(
  714. registerSound(PARTICIPANT_JOINED_SOUND_ID, PARTICIPANT_JOINED_FILE));
  715. dispatch(registerSound(PARTICIPANT_LEFT_SOUND_ID, PARTICIPANT_LEFT_FILE));
  716. }
  717. /**
  718. * Unregisters sounds related with the participants feature.
  719. *
  720. * @param {Store} store - The redux store.
  721. * @private
  722. * @returns {void}
  723. */
  724. function _unregisterSounds({ dispatch }: IStore) {
  725. dispatch(unregisterSound(PARTICIPANT_JOINED_SOUND_ID));
  726. dispatch(unregisterSound(PARTICIPANT_LEFT_SOUND_ID));
  727. }