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

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