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.any.ts 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. import i18n from 'i18next';
  2. import { AnyAction } from 'redux';
  3. // @ts-ignore
  4. import { MIN_ASSUMED_BANDWIDTH_BPS } from '../../../../modules/API/constants';
  5. import {
  6. ACTION_PINNED,
  7. ACTION_UNPINNED,
  8. createOfferAnswerFailedEvent,
  9. createPinnedEvent
  10. } from '../../analytics/AnalyticsEvents';
  11. import { sendAnalytics } from '../../analytics/functions';
  12. import { reloadNow } from '../../app/actions';
  13. import { IStore } from '../../app/types';
  14. import { removeLobbyChatParticipant } from '../../chat/actions.any';
  15. import { openDisplayNamePrompt } from '../../display-name/actions';
  16. import { showErrorNotification } from '../../notifications/actions';
  17. import { NOTIFICATION_TIMEOUT_TYPE } from '../../notifications/constants';
  18. import { hasDisplayName } from '../../prejoin/utils';
  19. import { stopLocalVideoRecording } from '../../recording/actions.any';
  20. import LocalRecordingManager from '../../recording/components/Recording/LocalRecordingManager';
  21. import { iAmVisitor } from '../../visitors/functions';
  22. import { overwriteConfig } from '../config/actions';
  23. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED } from '../connection/actionTypes';
  24. import { connect, connectionDisconnected, disconnect } from '../connection/actions';
  25. import { validateJwt } from '../jwt/functions';
  26. import { JitsiConferenceErrors, JitsiConnectionErrors } from '../lib-jitsi-meet';
  27. import { PARTICIPANT_UPDATED, PIN_PARTICIPANT } from '../participants/actionTypes';
  28. import { PARTICIPANT_ROLE } from '../participants/constants';
  29. import {
  30. getLocalParticipant,
  31. getParticipantById,
  32. getPinnedParticipant
  33. } from '../participants/functions';
  34. import MiddlewareRegistry from '../redux/MiddlewareRegistry';
  35. import { TRACK_ADDED, TRACK_REMOVED } from '../tracks/actionTypes';
  36. import { getLocalTracks } from '../tracks/functions.any';
  37. import {
  38. CONFERENCE_FAILED,
  39. CONFERENCE_JOINED,
  40. CONFERENCE_SUBJECT_CHANGED,
  41. CONFERENCE_WILL_LEAVE,
  42. P2P_STATUS_CHANGED,
  43. SEND_TONES,
  44. SET_ASSUMED_BANDWIDTH_BPS,
  45. SET_PENDING_SUBJECT_CHANGE,
  46. SET_ROOM
  47. } from './actionTypes';
  48. import {
  49. authStatusChanged,
  50. conferenceFailed,
  51. conferenceWillLeave,
  52. createConference,
  53. setLocalSubject,
  54. setSubject
  55. } from './actions';
  56. import { CONFERENCE_LEAVE_REASONS } from './constants';
  57. import {
  58. _addLocalTracksToConference,
  59. _removeLocalTracksFromConference,
  60. forEachConference,
  61. getCurrentConference,
  62. restoreConferenceOptions
  63. } from './functions';
  64. import logger from './logger';
  65. /**
  66. * Handler for before unload event.
  67. */
  68. let beforeUnloadHandler: ((e?: any) => void) | undefined;
  69. /**
  70. * Implements the middleware of the feature base/conference.
  71. *
  72. * @param {Store} store - The redux store.
  73. * @returns {Function}
  74. */
  75. MiddlewareRegistry.register(store => next => action => {
  76. switch (action.type) {
  77. case CONFERENCE_FAILED:
  78. return _conferenceFailed(store, next, action);
  79. case CONFERENCE_JOINED:
  80. return _conferenceJoined(store, next, action);
  81. case CONNECTION_ESTABLISHED:
  82. return _connectionEstablished(store, next, action);
  83. case CONNECTION_FAILED:
  84. return _connectionFailed(store, next, action);
  85. case CONFERENCE_SUBJECT_CHANGED:
  86. return _conferenceSubjectChanged(store, next, action);
  87. case CONFERENCE_WILL_LEAVE:
  88. _conferenceWillLeave(store);
  89. break;
  90. case P2P_STATUS_CHANGED:
  91. return _p2pStatusChanged(next, action);
  92. case PARTICIPANT_UPDATED:
  93. return _updateLocalParticipantInConference(store, next, action);
  94. case PIN_PARTICIPANT:
  95. return _pinParticipant(store, next, action);
  96. case SEND_TONES:
  97. return _sendTones(store, next, action);
  98. case SET_ROOM:
  99. return _setRoom(store, next, action);
  100. case TRACK_ADDED:
  101. case TRACK_REMOVED:
  102. return _trackAddedOrRemoved(store, next, action);
  103. case SET_ASSUMED_BANDWIDTH_BPS:
  104. return _setAssumedBandwidthBps(store, next, action);
  105. }
  106. return next(action);
  107. });
  108. /**
  109. * Makes sure to leave a failed conference in order to release any allocated
  110. * resources like peer connections, emit participant left events, etc.
  111. *
  112. * @param {Store} store - The redux store in which the specified {@code action}
  113. * is being dispatched.
  114. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  115. * specified {@code action} to the specified {@code store}.
  116. * @param {Action} action - The redux action {@code CONFERENCE_FAILED} which is
  117. * being dispatched in the specified {@code store}.
  118. * @private
  119. * @returns {Object} The value returned by {@code next(action)}.
  120. */
  121. function _conferenceFailed({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  122. const { conference, error } = action;
  123. const result = next(action);
  124. const { enableForcedReload } = getState()['features/base/config'];
  125. // Handle specific failure reasons.
  126. switch (error.name) {
  127. case JitsiConferenceErrors.CONFERENCE_RESTARTED: {
  128. if (enableForcedReload) {
  129. dispatch(showErrorNotification({
  130. description: 'Restart initiated because of a bridge failure',
  131. titleKey: 'dialog.sessionRestarted'
  132. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  133. }
  134. break;
  135. }
  136. case JitsiConferenceErrors.CONNECTION_ERROR: {
  137. const [ msg ] = error.params;
  138. dispatch(connectionDisconnected(getState()['features/base/connection'].connection));
  139. dispatch(showErrorNotification({
  140. descriptionArguments: { msg },
  141. descriptionKey: msg ? 'dialog.connectErrorWithMsg' : 'dialog.connectError',
  142. titleKey: 'connection.CONNFAIL'
  143. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  144. break;
  145. }
  146. case JitsiConferenceErrors.CONFERENCE_MAX_USERS: {
  147. dispatch(showErrorNotification({
  148. hideErrorSupportLink: true,
  149. descriptionKey: 'dialog.maxUsersLimitReached',
  150. titleKey: 'dialog.maxUsersLimitReachedTitle'
  151. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  152. // In case of max users(it can be from a visitor node), let's restore
  153. // oldConfig if any as we will be back to the main prosody.
  154. const newConfig = restoreConferenceOptions(getState);
  155. if (newConfig) {
  156. dispatch(overwriteConfig(newConfig)) // @ts-ignore
  157. .then(() => dispatch(conferenceWillLeave(conference)))
  158. .then(() => conference.leave())
  159. .then(() => dispatch(disconnect()))
  160. .then(() => dispatch(connect()))
  161. .then(() => {
  162. // FIXME: Workaround for the web version. To be removed once we get rid of conference.js
  163. if (typeof APP !== 'undefined') {
  164. const localTracks = getLocalTracks(getState()['features/base/tracks']);
  165. const jitsiTracks = localTracks.map((t: any) => t.jitsiTrack);
  166. APP.conference.startConference(jitsiTracks).catch(logger.error);
  167. }
  168. });
  169. }
  170. break;
  171. }
  172. case JitsiConferenceErrors.OFFER_ANSWER_FAILED:
  173. sendAnalytics(createOfferAnswerFailedEvent());
  174. break;
  175. }
  176. !error.recoverable
  177. && conference
  178. && conference.leave(CONFERENCE_LEAVE_REASONS.UNRECOVERABLE_ERROR).catch((reason: Error) => {
  179. // Even though we don't care too much about the failure, it may be
  180. // good to know that it happen, so log it (on the info level).
  181. logger.info('JitsiConference.leave() rejected with:', reason);
  182. });
  183. // FIXME: Workaround for the web version. Currently, the creation of the
  184. // conference is handled by /conference.js and appropriate failure handlers
  185. // are set there.
  186. if (typeof APP !== 'undefined') {
  187. _removeUnloadHandler(getState);
  188. }
  189. if (enableForcedReload && error?.name === JitsiConferenceErrors.CONFERENCE_RESTARTED) {
  190. dispatch(conferenceWillLeave(conference));
  191. dispatch(reloadNow());
  192. }
  193. return result;
  194. }
  195. /**
  196. * Does extra sync up on properties that may need to be updated after the
  197. * conference was joined.
  198. *
  199. * @param {Store} store - The redux store in which the specified {@code action}
  200. * is being dispatched.
  201. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  202. * specified {@code action} to the specified {@code store}.
  203. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
  204. * being dispatched in the specified {@code store}.
  205. * @private
  206. * @returns {Object} The value returned by {@code next(action)}.
  207. */
  208. function _conferenceJoined({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  209. const result = next(action);
  210. const { conference } = action;
  211. const { pendingSubjectChange } = getState()['features/base/conference'];
  212. const {
  213. disableBeforeUnloadHandlers = false,
  214. requireDisplayName
  215. } = getState()['features/base/config'];
  216. dispatch(removeLobbyChatParticipant(true));
  217. pendingSubjectChange && dispatch(setSubject(pendingSubjectChange));
  218. // FIXME: Very dirty solution. This will work on web only.
  219. // When the user closes the window or quits the browser, lib-jitsi-meet
  220. // handles the process of leaving the conference. This is temporary solution
  221. // that should cover the described use case as part of the effort to
  222. // implement the conferenceWillLeave action for web.
  223. beforeUnloadHandler = (e?: any) => {
  224. if (LocalRecordingManager.isRecordingLocally()) {
  225. dispatch(stopLocalVideoRecording());
  226. if (e) {
  227. e.preventDefault();
  228. e.returnValue = null;
  229. }
  230. }
  231. dispatch(conferenceWillLeave(conference));
  232. };
  233. if (!iAmVisitor(getState())) {
  234. // if a visitor is promoted back to main room and want to join an empty breakout room
  235. // we need to send iq to jicofo, so it can join/create the breakout room
  236. dispatch(overwriteConfig({ disableFocus: false }));
  237. }
  238. window.addEventListener(disableBeforeUnloadHandlers ? 'unload' : 'beforeunload', beforeUnloadHandler);
  239. if (requireDisplayName
  240. && !getLocalParticipant(getState)?.name
  241. && !conference.isHidden()) {
  242. dispatch(openDisplayNamePrompt({
  243. validateInput: hasDisplayName
  244. }));
  245. }
  246. return result;
  247. }
  248. /**
  249. * Notifies the feature base/conference that the action
  250. * {@code CONNECTION_ESTABLISHED} is being dispatched within a specific redux
  251. * store.
  252. *
  253. * @param {Store} store - The redux store in which the specified {@code action}
  254. * is being dispatched.
  255. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  256. * specified {@code action} to the specified {@code store}.
  257. * @param {Action} action - The redux action {@code CONNECTION_ESTABLISHED}
  258. * which is being dispatched in the specified {@code store}.
  259. * @private
  260. * @returns {Object} The value returned by {@code next(action)}.
  261. */
  262. async function _connectionEstablished({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  263. const result = next(action);
  264. const { tokenAuthUrl = false } = getState()['features/base/config'];
  265. // if there is token auth URL defined and local participant is using jwt
  266. // this means it is logged in when connection is established, so we can change the state
  267. if (tokenAuthUrl) {
  268. let email;
  269. if (getState()['features/base/jwt'].jwt) {
  270. email = getLocalParticipant(getState())?.email;
  271. }
  272. dispatch(authStatusChanged(true, email || ''));
  273. }
  274. // FIXME: Workaround for the web version. Currently, the creation of the
  275. // conference is handled by /conference.js.
  276. if (typeof APP === 'undefined') {
  277. dispatch(createConference());
  278. return result;
  279. }
  280. return result;
  281. }
  282. /**
  283. * Logs jwt validation errors from xmpp and from the client-side validator.
  284. *
  285. * @param {string} message - The error message from xmpp.
  286. * @param {string} errors - The detailed errors.
  287. * @returns {void}
  288. */
  289. function _logJwtErrors(message: string, errors: string) {
  290. message && logger.error(`JWT error: ${message}`);
  291. errors && logger.error('JWT parsing errors:', errors);
  292. }
  293. /**
  294. * Notifies the feature base/conference that the action
  295. * {@code CONNECTION_FAILED} is being dispatched within a specific redux
  296. * store.
  297. *
  298. * @param {Store} store - The redux store in which the specified {@code action}
  299. * is being dispatched.
  300. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  301. * specified {@code action} to the specified {@code store}.
  302. * @param {Action} action - The redux action {@code CONNECTION_FAILED} which is
  303. * being dispatched in the specified {@code store}.
  304. * @private
  305. * @returns {Object} The value returned by {@code next(action)}.
  306. */
  307. function _connectionFailed({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  308. const { connection, error } = action;
  309. const { jwt } = getState()['features/base/jwt'];
  310. if (jwt) {
  311. const errors: string = validateJwt(jwt).map((err: any) =>
  312. i18n.t(`dialog.tokenAuthFailedReason.${err.key}`, err.args))
  313. .join(' ');
  314. _logJwtErrors(error.message, errors);
  315. // do not show the notification when we will prompt the user
  316. // for username and password
  317. if (error.name === JitsiConnectionErrors.PASSWORD_REQUIRED) {
  318. dispatch(showErrorNotification({
  319. descriptionKey: errors ? 'dialog.tokenAuthFailedWithReasons' : 'dialog.tokenAuthFailed',
  320. descriptionArguments: { reason: errors },
  321. titleKey: 'dialog.tokenAuthFailedTitle'
  322. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  323. }
  324. }
  325. const result = next(action);
  326. _removeUnloadHandler(getState);
  327. forEachConference(getState, conference => {
  328. // TODO: revisit this
  329. // It feels that it would make things easier if JitsiConference
  330. // in lib-jitsi-meet would monitor it's connection and emit
  331. // CONFERENCE_FAILED when it's dropped. It has more knowledge on
  332. // whether it can recover or not. But because the reload screen
  333. // and the retry logic is implemented in the app maybe it can be
  334. // left this way for now.
  335. if (conference.getConnection() === connection) {
  336. // XXX Note that on mobile the error type passed to
  337. // connectionFailed is always an object with .name property.
  338. // This fact needs to be checked prior to enabling this logic on
  339. // web.
  340. const conferenceAction = conferenceFailed(conference, error.name);
  341. // Copy the recoverable flag if set on the CONNECTION_FAILED
  342. // action to not emit recoverable action caused by
  343. // a non-recoverable one.
  344. if (typeof error.recoverable !== 'undefined') {
  345. conferenceAction.error.recoverable = error.recoverable;
  346. }
  347. dispatch(conferenceAction);
  348. }
  349. return true;
  350. });
  351. return result;
  352. }
  353. /**
  354. * Notifies the feature base/conference that the action
  355. * {@code CONFERENCE_SUBJECT_CHANGED} is being dispatched within a specific
  356. * redux store.
  357. *
  358. * @param {Store} store - The redux store in which the specified {@code action}
  359. * is being dispatched.
  360. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  361. * specified {@code action} to the specified {@code store}.
  362. * @param {Action} action - The redux action {@code CONFERENCE_SUBJECT_CHANGED}
  363. * which is being dispatched in the specified {@code store}.
  364. * @private
  365. * @returns {Object} The value returned by {@code next(action)}.
  366. */
  367. function _conferenceSubjectChanged({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  368. const result = next(action);
  369. const { subject } = getState()['features/base/conference'];
  370. if (subject) {
  371. dispatch({
  372. type: SET_PENDING_SUBJECT_CHANGE,
  373. subject: undefined
  374. });
  375. }
  376. typeof APP === 'object' && APP.API.notifySubjectChanged(subject);
  377. return result;
  378. }
  379. /**
  380. * Notifies the feature base/conference that the action
  381. * {@code CONFERENCE_WILL_LEAVE} is being dispatched within a specific redux
  382. * store.
  383. *
  384. * @private
  385. * @param {Object} store - The redux store.
  386. * @returns {void}
  387. */
  388. function _conferenceWillLeave({ getState }: IStore) {
  389. _removeUnloadHandler(getState);
  390. }
  391. /**
  392. * Notifies the feature base/conference that the action {@code PIN_PARTICIPANT}
  393. * is being dispatched within a specific redux store. Pins the specified remote
  394. * participant in the associated conference, ignores the local participant.
  395. *
  396. * @param {Store} store - The redux store in which the specified {@code action}
  397. * is being dispatched.
  398. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  399. * specified {@code action} to the specified {@code store}.
  400. * @param {Action} action - The redux action {@code PIN_PARTICIPANT} which is
  401. * being dispatched in the specified {@code store}.
  402. * @private
  403. * @returns {Object} The value returned by {@code next(action)}.
  404. */
  405. function _pinParticipant({ getState }: IStore, next: Function, action: AnyAction) {
  406. const state = getState();
  407. const { conference } = state['features/base/conference'];
  408. if (!conference) {
  409. return next(action);
  410. }
  411. const id = action.participant.id;
  412. const participantById = getParticipantById(state, id);
  413. const pinnedParticipant = getPinnedParticipant(state);
  414. const actionName = id ? ACTION_PINNED : ACTION_UNPINNED;
  415. const local
  416. = participantById?.local
  417. || (!id && pinnedParticipant && pinnedParticipant.local);
  418. let participantIdForEvent;
  419. if (local) {
  420. participantIdForEvent = local;
  421. } else {
  422. participantIdForEvent
  423. = actionName === ACTION_PINNED ? id : pinnedParticipant?.id;
  424. }
  425. sendAnalytics(createPinnedEvent(
  426. actionName,
  427. participantIdForEvent,
  428. {
  429. local,
  430. 'participant_count': conference.getParticipantCount()
  431. }));
  432. return next(action);
  433. }
  434. /**
  435. * Removes the unload handler.
  436. *
  437. * @param {Function} getState - The redux getState function.
  438. * @returns {void}
  439. */
  440. function _removeUnloadHandler(getState: IStore['getState']) {
  441. if (typeof beforeUnloadHandler !== 'undefined') {
  442. const { disableBeforeUnloadHandlers = false } = getState()['features/base/config'];
  443. window.removeEventListener(disableBeforeUnloadHandlers ? 'unload' : 'beforeunload', beforeUnloadHandler);
  444. beforeUnloadHandler = undefined;
  445. }
  446. }
  447. /**
  448. * Requests the specified tones to be played.
  449. *
  450. * @param {Store} store - The redux store in which the specified {@code action}
  451. * is being dispatched.
  452. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  453. * specified {@code action} to the specified {@code store}.
  454. * @param {Action} action - The redux action {@code SEND_TONES} which is
  455. * being dispatched in the specified {@code store}.
  456. * @private
  457. * @returns {Object} The value returned by {@code next(action)}.
  458. */
  459. function _sendTones({ getState }: IStore, next: Function, action: AnyAction) {
  460. const state = getState();
  461. const { conference } = state['features/base/conference'];
  462. if (conference) {
  463. const { duration, tones, pause } = action;
  464. conference.sendTones(tones, duration, pause);
  465. }
  466. return next(action);
  467. }
  468. /**
  469. * Notifies the feature base/conference that the action
  470. * {@code SET_ROOM} is being dispatched within a specific
  471. * redux store.
  472. *
  473. * @param {Store} store - The redux store in which the specified {@code action}
  474. * is being dispatched.
  475. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  476. * specified {@code action} to the specified {@code store}.
  477. * @param {Action} action - The redux action {@code SET_ROOM}
  478. * which is being dispatched in the specified {@code store}.
  479. * @private
  480. * @returns {Object} The value returned by {@code next(action)}.
  481. */
  482. function _setRoom({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  483. const state = getState();
  484. const { localSubject, subject } = state['features/base/config'];
  485. const { room } = action;
  486. if (room) {
  487. // Set the stored subject.
  488. dispatch(setLocalSubject(localSubject ?? ''));
  489. dispatch(setSubject(subject ?? ''));
  490. }
  491. return next(action);
  492. }
  493. /**
  494. * Synchronizes local tracks from state with local tracks in JitsiConference
  495. * instance.
  496. *
  497. * @param {Store} store - The redux store.
  498. * @param {Object} action - Action object.
  499. * @private
  500. * @returns {Promise}
  501. */
  502. function _syncConferenceLocalTracksWithState({ getState }: IStore, action: AnyAction) {
  503. const state = getState();
  504. const conference = getCurrentConference(state);
  505. let promise;
  506. if (conference) {
  507. const track = action.track.jitsiTrack;
  508. if (action.type === TRACK_ADDED) {
  509. // If gUM is slow and tracks are created after the user has already joined the conference, avoid
  510. // adding the tracks to the conference if the user is a visitor.
  511. if (!iAmVisitor(state)) {
  512. promise = _addLocalTracksToConference(conference, [ track ]);
  513. }
  514. } else {
  515. promise = _removeLocalTracksFromConference(conference, [ track ]);
  516. }
  517. }
  518. return promise || Promise.resolve();
  519. }
  520. /**
  521. * Notifies the feature base/conference that the action {@code TRACK_ADDED}
  522. * or {@code TRACK_REMOVED} is being dispatched within a specific redux store.
  523. *
  524. * @param {Store} store - The redux store in which the specified {@code action}
  525. * is being dispatched.
  526. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  527. * specified {@code action} to the specified {@code store}.
  528. * @param {Action} action - The redux action {@code TRACK_ADDED} or
  529. * {@code TRACK_REMOVED} which is being dispatched in the specified
  530. * {@code store}.
  531. * @private
  532. * @returns {Object} The value returned by {@code next(action)}.
  533. */
  534. function _trackAddedOrRemoved(store: IStore, next: Function, action: AnyAction) {
  535. const track = action.track;
  536. // TODO All track swapping should happen here instead of conference.js.
  537. if (track?.local) {
  538. return (
  539. _syncConferenceLocalTracksWithState(store, action)
  540. .then(() => next(action)));
  541. }
  542. return next(action);
  543. }
  544. /**
  545. * Updates the conference object when the local participant is updated.
  546. *
  547. * @param {Store} store - The redux store in which the specified {@code action}
  548. * is being dispatched.
  549. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  550. * specified {@code action} to the specified {@code store}.
  551. * @param {Action} action - The redux action which is being dispatched in the
  552. * specified {@code store}.
  553. * @private
  554. * @returns {Object} The value returned by {@code next(action)}.
  555. */
  556. function _updateLocalParticipantInConference({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  557. const { conference } = getState()['features/base/conference'];
  558. const { participant } = action;
  559. const result = next(action);
  560. const localParticipant = getLocalParticipant(getState);
  561. if (conference && participant.id === localParticipant?.id) {
  562. if ('name' in participant) {
  563. conference.setDisplayName(participant.name);
  564. }
  565. if ('role' in participant && participant.role === PARTICIPANT_ROLE.MODERATOR) {
  566. const { pendingSubjectChange, subject } = getState()['features/base/conference'];
  567. // When the local user role is updated to moderator and we have a pending subject change
  568. // which was not reflected we need to set it (the first time we tried was before becoming moderator).
  569. if (typeof pendingSubjectChange !== 'undefined' && pendingSubjectChange !== subject) {
  570. dispatch(setSubject(pendingSubjectChange));
  571. }
  572. }
  573. }
  574. return result;
  575. }
  576. /**
  577. * Notifies the external API that the action {@code P2P_STATUS_CHANGED}
  578. * is being dispatched within a specific redux store.
  579. *
  580. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  581. * specified {@code action} to the specified {@code store}.
  582. * @param {Action} action - The redux action {@code P2P_STATUS_CHANGED}
  583. * which is being dispatched in the specified {@code store}.
  584. * @private
  585. * @returns {Object} The value returned by {@code next(action)}.
  586. */
  587. function _p2pStatusChanged(next: Function, action: AnyAction) {
  588. const result = next(action);
  589. if (typeof APP !== 'undefined') {
  590. APP.API.notifyP2pStatusChanged(action.p2p);
  591. }
  592. return result;
  593. }
  594. /**
  595. * Notifies the feature base/conference that the action
  596. * {@code SET_ASSUMED_BANDWIDTH_BPS} is being dispatched within a specific
  597. * redux store.
  598. *
  599. * @param {Store} store - The redux store in which the specified {@code action}
  600. * is being dispatched.
  601. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  602. * specified {@code action} to the specified {@code store}.
  603. * @param {Action} action - The redux action {@code SET_ASSUMED_BANDWIDTH_BPS}
  604. * which is being dispatched in the specified {@code store}.
  605. * @private
  606. * @returns {Object} The value returned by {@code next(action)}.
  607. */
  608. function _setAssumedBandwidthBps({ getState }: IStore, next: Function, action: AnyAction) {
  609. const state = getState();
  610. const conference = getCurrentConference(state);
  611. const payload = Number(action.assumedBandwidthBps);
  612. const assumedBandwidthBps = isNaN(payload) || payload < MIN_ASSUMED_BANDWIDTH_BPS
  613. ? MIN_ASSUMED_BANDWIDTH_BPS
  614. : payload;
  615. if (conference) {
  616. conference.setAssumedBandwidthBps(assumedBandwidthBps);
  617. }
  618. return next(action);
  619. }