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

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