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

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