您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.ts 30KB

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