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.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. // @flow
  2. import { readyToClose } from '../../../features/mobile/external-api/actions';
  3. import {
  4. ACTION_PINNED,
  5. ACTION_UNPINNED,
  6. createOfferAnswerFailedEvent,
  7. createPinnedEvent,
  8. sendAnalytics
  9. } from '../../analytics';
  10. import { reloadNow } from '../../app/actions';
  11. import { removeLobbyChatParticipant } from '../../chat/actions.any';
  12. import { openDisplayNamePrompt } from '../../display-name';
  13. import {
  14. NOTIFICATION_TIMEOUT_TYPE,
  15. showErrorNotification
  16. } from '../../notifications';
  17. import { showSalesforceNotification } from '../../salesforce';
  18. import { CONNECTION_ESTABLISHED, CONNECTION_FAILED, connectionDisconnected } from '../connection';
  19. import { validateJwt } from '../jwt';
  20. import { JitsiConferenceErrors } from '../lib-jitsi-meet';
  21. import { MEDIA_TYPE } from '../media';
  22. import {
  23. getLocalParticipant,
  24. getParticipantById,
  25. getPinnedParticipant,
  26. PARTICIPANT_ROLE,
  27. PARTICIPANT_UPDATED,
  28. PIN_PARTICIPANT
  29. } from '../participants';
  30. import { MiddlewareRegistry } from '../redux';
  31. import { TRACK_ADDED, TRACK_REMOVED } from '../tracks';
  32. import {
  33. CONFERENCE_FAILED,
  34. CONFERENCE_JOINED,
  35. CONFERENCE_SUBJECT_CHANGED,
  36. CONFERENCE_WILL_LEAVE,
  37. SEND_TONES,
  38. SET_PENDING_SUBJECT_CHANGE,
  39. SET_ROOM
  40. } from './actionTypes';
  41. import {
  42. conferenceFailed,
  43. conferenceWillLeave,
  44. createConference,
  45. setLocalSubject,
  46. setSubject
  47. } from './actions';
  48. import { TRIGGER_READY_TO_CLOSE_REASONS } from './constants';
  49. import {
  50. _addLocalTracksToConference,
  51. _removeLocalTracksFromConference,
  52. forEachConference,
  53. getCurrentConference
  54. } from './functions';
  55. import logger from './logger';
  56. declare var APP: Object;
  57. /**
  58. * Handler for before unload event.
  59. */
  60. let beforeUnloadHandler;
  61. /**
  62. * Implements the middleware of the feature base/conference.
  63. *
  64. * @param {Store} store - The redux store.
  65. * @returns {Function}
  66. */
  67. MiddlewareRegistry.register(store => next => action => {
  68. switch (action.type) {
  69. case CONFERENCE_FAILED:
  70. return _conferenceFailed(store, next, action);
  71. case CONFERENCE_JOINED:
  72. return _conferenceJoined(store, next, action);
  73. case CONNECTION_ESTABLISHED:
  74. return _connectionEstablished(store, next, action);
  75. case CONNECTION_FAILED:
  76. return _connectionFailed(store, next, action);
  77. case CONFERENCE_SUBJECT_CHANGED:
  78. return _conferenceSubjectChanged(store, next, action);
  79. case CONFERENCE_WILL_LEAVE:
  80. _conferenceWillLeave(store);
  81. break;
  82. case PARTICIPANT_UPDATED:
  83. return _updateLocalParticipantInConference(store, next, action);
  84. case PIN_PARTICIPANT:
  85. return _pinParticipant(store, next, action);
  86. case SEND_TONES:
  87. return _sendTones(store, next, action);
  88. case SET_ROOM:
  89. return _setRoom(store, next, action);
  90. case TRACK_ADDED:
  91. case TRACK_REMOVED:
  92. return _trackAddedOrRemoved(store, next, action);
  93. }
  94. return next(action);
  95. });
  96. /**
  97. * Makes sure to leave a failed conference in order to release any allocated
  98. * resources like peer connections, emit participant left events, etc.
  99. *
  100. * @param {Store} store - The redux store in which the specified {@code action}
  101. * is being dispatched.
  102. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  103. * specified {@code action} to the specified {@code store}.
  104. * @param {Action} action - The redux action {@code CONFERENCE_FAILED} which is
  105. * being dispatched in the specified {@code store}.
  106. * @private
  107. * @returns {Object} The value returned by {@code next(action)}.
  108. */
  109. function _conferenceFailed({ dispatch, getState }, next, action) {
  110. const result = next(action);
  111. const { conference, error } = action;
  112. const { enableForcedReload } = getState()['features/base/config'];
  113. // Handle specific failure reasons.
  114. switch (error.name) {
  115. case JitsiConferenceErrors.CONFERENCE_DESTROYED: {
  116. const [ reason ] = error.params;
  117. dispatch(showErrorNotification({
  118. description: reason,
  119. titleKey: 'dialog.sessTerminated'
  120. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  121. if (TRIGGER_READY_TO_CLOSE_REASONS.includes(reason)) {
  122. if (typeof APP === undefined) {
  123. dispatch(readyToClose());
  124. } else {
  125. APP.API.notifyReadyToClose();
  126. }
  127. }
  128. break;
  129. }
  130. case JitsiConferenceErrors.CONFERENCE_RESTARTED: {
  131. if (enableForcedReload) {
  132. dispatch(showErrorNotification({
  133. description: 'Restart initiated because of a bridge failure',
  134. titleKey: 'dialog.sessionRestarted'
  135. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  136. }
  137. break;
  138. }
  139. case JitsiConferenceErrors.CONNECTION_ERROR: {
  140. const [ msg ] = error.params;
  141. dispatch(connectionDisconnected(getState()['features/base/connection'].connection));
  142. dispatch(showErrorNotification({
  143. descriptionArguments: { msg },
  144. descriptionKey: msg ? 'dialog.connectErrorWithMsg' : 'dialog.connectError',
  145. titleKey: 'connection.CONNFAIL'
  146. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  147. break;
  148. }
  149. case JitsiConferenceErrors.OFFER_ANSWER_FAILED:
  150. sendAnalytics(createOfferAnswerFailedEvent());
  151. break;
  152. }
  153. if (typeof APP === 'undefined') {
  154. !error.recoverable
  155. && conference
  156. && conference.leave().catch(reason => {
  157. // Even though we don't care too much about the failure, it may be
  158. // good to know that it happen, so log it (on the info level).
  159. logger.info('JitsiConference.leave() rejected with:', reason);
  160. });
  161. } else {
  162. // FIXME: Workaround for the web version. Currently, the creation of the
  163. // conference is handled by /conference.js and appropriate failure handlers
  164. // are set there.
  165. _removeUnloadHandler(getState);
  166. }
  167. if (enableForcedReload && error?.name === JitsiConferenceErrors.CONFERENCE_RESTARTED) {
  168. dispatch(conferenceWillLeave(conference));
  169. dispatch(reloadNow());
  170. }
  171. return result;
  172. }
  173. /**
  174. * Does extra sync up on properties that may need to be updated after the
  175. * conference was joined.
  176. *
  177. * @param {Store} store - The redux store in which the specified {@code action}
  178. * is being dispatched.
  179. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  180. * specified {@code action} to the specified {@code store}.
  181. * @param {Action} action - The redux action {@code CONFERENCE_JOINED} which is
  182. * being dispatched in the specified {@code store}.
  183. * @private
  184. * @returns {Object} The value returned by {@code next(action)}.
  185. */
  186. function _conferenceJoined({ dispatch, getState }, next, action) {
  187. const result = next(action);
  188. const { conference } = action;
  189. const { pendingSubjectChange } = getState()['features/base/conference'];
  190. const {
  191. disableBeforeUnloadHandlers = false,
  192. requireDisplayName
  193. } = getState()['features/base/config'];
  194. dispatch(removeLobbyChatParticipant(true));
  195. pendingSubjectChange && dispatch(setSubject(pendingSubjectChange));
  196. // FIXME: Very dirty solution. This will work on web only.
  197. // When the user closes the window or quits the browser, lib-jitsi-meet
  198. // handles the process of leaving the conference. This is temporary solution
  199. // that should cover the described use case as part of the effort to
  200. // implement the conferenceWillLeave action for web.
  201. beforeUnloadHandler = () => {
  202. dispatch(conferenceWillLeave(conference));
  203. };
  204. window.addEventListener(disableBeforeUnloadHandlers ? 'unload' : 'beforeunload', beforeUnloadHandler);
  205. if (requireDisplayName
  206. && !getLocalParticipant(getState)?.name
  207. && !conference.isHidden()) {
  208. dispatch(openDisplayNamePrompt(undefined));
  209. }
  210. dispatch(showSalesforceNotification());
  211. return result;
  212. }
  213. /**
  214. * Notifies the feature base/conference that the action
  215. * {@code CONNECTION_ESTABLISHED} is being dispatched within a specific redux
  216. * store.
  217. *
  218. * @param {Store} store - The redux store in which the specified {@code action}
  219. * is being dispatched.
  220. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  221. * specified {@code action} to the specified {@code store}.
  222. * @param {Action} action - The redux action {@code CONNECTION_ESTABLISHED}
  223. * which is being dispatched in the specified {@code store}.
  224. * @private
  225. * @returns {Object} The value returned by {@code next(action)}.
  226. */
  227. function _connectionEstablished({ dispatch }, next, action) {
  228. const result = next(action);
  229. // FIXME: Workaround for the web version. Currently, the creation of the
  230. // conference is handled by /conference.js.
  231. typeof APP === 'undefined' && dispatch(createConference());
  232. return result;
  233. }
  234. /**
  235. * Logs jwt validation errors from xmpp and from the client-side validator.
  236. *
  237. * @param {string} message -The error message from xmpp.
  238. * @param {Object} state - The redux state.
  239. * @returns {void}
  240. */
  241. function _logJwtErrors(message, state) {
  242. const { jwt } = state['features/base/jwt'];
  243. if (!jwt) {
  244. return;
  245. }
  246. const errorKeys = validateJwt(jwt);
  247. message && logger.error(`JWT error: ${message}`);
  248. errorKeys.length && logger.error('JWT parsing error:', errorKeys);
  249. }
  250. /**
  251. * Notifies the feature base/conference that the action
  252. * {@code CONNECTION_FAILED} is being dispatched within a specific redux
  253. * store.
  254. *
  255. * @param {Store} store - The redux store in which the specified {@code action}
  256. * is being dispatched.
  257. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  258. * specified {@code action} to the specified {@code store}.
  259. * @param {Action} action - The redux action {@code CONNECTION_FAILED} which is
  260. * being dispatched in the specified {@code store}.
  261. * @private
  262. * @returns {Object} The value returned by {@code next(action)}.
  263. */
  264. function _connectionFailed({ dispatch, getState }, next, action) {
  265. _logJwtErrors(action.error.message, getState());
  266. const result = next(action);
  267. _removeUnloadHandler(getState);
  268. // FIXME: Workaround for the web version. Currently, the creation of the
  269. // conference is handled by /conference.js and appropriate failure handlers
  270. // are set there.
  271. if (typeof APP === 'undefined') {
  272. const { connection } = action;
  273. const { error } = action;
  274. forEachConference(getState, conference => {
  275. // It feels that it would make things easier if JitsiConference
  276. // in lib-jitsi-meet would monitor it's connection and emit
  277. // CONFERENCE_FAILED when it's dropped. It has more knowledge on
  278. // whether it can recover or not. But because the reload screen
  279. // and the retry logic is implemented in the app maybe it can be
  280. // left this way for now.
  281. if (conference.getConnection() === connection) {
  282. // XXX Note that on mobile the error type passed to
  283. // connectionFailed is always an object with .name property.
  284. // This fact needs to be checked prior to enabling this logic on
  285. // web.
  286. const conferenceAction
  287. = conferenceFailed(conference, error.name);
  288. // Copy the recoverable flag if set on the CONNECTION_FAILED
  289. // action to not emit recoverable action caused by
  290. // a non-recoverable one.
  291. if (typeof error.recoverable !== 'undefined') {
  292. conferenceAction.error.recoverable = error.recoverable;
  293. }
  294. dispatch(conferenceAction);
  295. }
  296. return true;
  297. });
  298. }
  299. return result;
  300. }
  301. /**
  302. * Notifies the feature base/conference that the action
  303. * {@code CONFERENCE_SUBJECT_CHANGED} is being dispatched within a specific
  304. * redux store.
  305. *
  306. * @param {Store} store - The redux store in which the specified {@code action}
  307. * is being dispatched.
  308. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  309. * specified {@code action} to the specified {@code store}.
  310. * @param {Action} action - The redux action {@code CONFERENCE_SUBJECT_CHANGED}
  311. * which is being dispatched in the specified {@code store}.
  312. * @private
  313. * @returns {Object} The value returned by {@code next(action)}.
  314. */
  315. function _conferenceSubjectChanged({ dispatch, getState }, next, action) {
  316. const result = next(action);
  317. const { subject } = getState()['features/base/conference'];
  318. if (subject) {
  319. dispatch({
  320. type: SET_PENDING_SUBJECT_CHANGE,
  321. subject: undefined
  322. });
  323. }
  324. typeof APP === 'object' && APP.API.notifySubjectChanged(subject);
  325. return result;
  326. }
  327. /**
  328. * Notifies the feature base/conference that the action
  329. * {@code CONFERENCE_WILL_LEAVE} is being dispatched within a specific redux
  330. * store.
  331. *
  332. * @private
  333. * @param {Object} store - The redux store.
  334. * @returns {void}
  335. */
  336. function _conferenceWillLeave({ getState }: { getState: Function }) {
  337. _removeUnloadHandler(getState);
  338. }
  339. /**
  340. * Notifies the feature base/conference that the action {@code PIN_PARTICIPANT}
  341. * is being dispatched within a specific redux store. Pins the specified remote
  342. * participant in the associated conference, ignores the local participant.
  343. *
  344. * @param {Store} store - The redux store in which the specified {@code action}
  345. * is being dispatched.
  346. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  347. * specified {@code action} to the specified {@code store}.
  348. * @param {Action} action - The redux action {@code PIN_PARTICIPANT} which is
  349. * being dispatched in the specified {@code store}.
  350. * @private
  351. * @returns {Object} The value returned by {@code next(action)}.
  352. */
  353. function _pinParticipant({ getState }, next, action) {
  354. const state = getState();
  355. const { conference } = state['features/base/conference'];
  356. if (!conference) {
  357. return next(action);
  358. }
  359. const id = action.participant.id;
  360. const participantById = getParticipantById(state, id);
  361. const pinnedParticipant = getPinnedParticipant(state);
  362. const actionName = id ? ACTION_PINNED : ACTION_UNPINNED;
  363. const local
  364. = (participantById && participantById.local)
  365. || (!id && pinnedParticipant && pinnedParticipant.local);
  366. let participantIdForEvent;
  367. if (local) {
  368. participantIdForEvent = local;
  369. } else {
  370. participantIdForEvent
  371. = actionName === ACTION_PINNED ? id : pinnedParticipant && pinnedParticipant.id;
  372. }
  373. sendAnalytics(createPinnedEvent(
  374. actionName,
  375. participantIdForEvent,
  376. {
  377. local,
  378. 'participant_count': conference.getParticipantCount()
  379. }));
  380. return next(action);
  381. }
  382. /**
  383. * Removes the unload handler.
  384. *
  385. * @param {Function} getState - The redux getState function.
  386. * @returns {void}
  387. */
  388. function _removeUnloadHandler(getState) {
  389. if (typeof beforeUnloadHandler !== 'undefined') {
  390. const { disableBeforeUnloadHandlers = false } = getState()['features/base/config'];
  391. window.removeEventListener(disableBeforeUnloadHandlers ? 'unload' : 'beforeunload', beforeUnloadHandler);
  392. beforeUnloadHandler = undefined;
  393. }
  394. }
  395. /**
  396. * Requests the specified tones to be played.
  397. *
  398. * @param {Store} store - The redux store in which the specified {@code action}
  399. * is being dispatched.
  400. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  401. * specified {@code action} to the specified {@code store}.
  402. * @param {Action} action - The redux action {@code SEND_TONES} which is
  403. * being dispatched in the specified {@code store}.
  404. * @private
  405. * @returns {Object} The value returned by {@code next(action)}.
  406. */
  407. function _sendTones({ getState }, next, action) {
  408. const state = getState();
  409. const { conference } = state['features/base/conference'];
  410. if (conference) {
  411. const { duration, tones, pause } = action;
  412. conference.sendTones(tones, duration, pause);
  413. }
  414. return next(action);
  415. }
  416. /**
  417. * Notifies the feature base/conference that the action
  418. * {@code SET_ROOM} is being dispatched within a specific
  419. * redux store.
  420. *
  421. * @param {Store} store - The redux store in which the specified {@code action}
  422. * is being dispatched.
  423. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  424. * specified {@code action} to the specified {@code store}.
  425. * @param {Action} action - The redux action {@code SET_ROOM}
  426. * which is being dispatched in the specified {@code store}.
  427. * @private
  428. * @returns {Object} The value returned by {@code next(action)}.
  429. */
  430. function _setRoom({ dispatch, getState }, next, action) {
  431. const state = getState();
  432. const { localSubject, subject } = state['features/base/config'];
  433. const { room } = action;
  434. if (room) {
  435. // Set the stored subject.
  436. dispatch(setLocalSubject(localSubject));
  437. dispatch(setSubject(subject));
  438. }
  439. return next(action);
  440. }
  441. /**
  442. * Synchronizes local tracks from state with local tracks in JitsiConference
  443. * instance.
  444. *
  445. * @param {Store} store - The redux store.
  446. * @param {Object} action - Action object.
  447. * @private
  448. * @returns {Promise}
  449. */
  450. function _syncConferenceLocalTracksWithState({ getState }, action) {
  451. const conference = getCurrentConference(getState);
  452. let promise;
  453. if (conference) {
  454. const track = action.track.jitsiTrack;
  455. if (action.type === TRACK_ADDED) {
  456. promise = _addLocalTracksToConference(conference, [ track ]);
  457. } else {
  458. promise = _removeLocalTracksFromConference(conference, [ track ]);
  459. }
  460. }
  461. return promise || Promise.resolve();
  462. }
  463. /**
  464. * Notifies the feature base/conference that the action {@code TRACK_ADDED}
  465. * or {@code TRACK_REMOVED} is being dispatched within a specific redux store.
  466. *
  467. * @param {Store} store - The redux store in which the specified {@code action}
  468. * is being dispatched.
  469. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  470. * specified {@code action} to the specified {@code store}.
  471. * @param {Action} action - The redux action {@code TRACK_ADDED} or
  472. * {@code TRACK_REMOVED} which is being dispatched in the specified
  473. * {@code store}.
  474. * @private
  475. * @returns {Object} The value returned by {@code next(action)}.
  476. */
  477. function _trackAddedOrRemoved(store, next, action) {
  478. const track = action.track;
  479. // TODO All track swapping should happen here instead of conference.js.
  480. // Since we swap the tracks for the web client in conference.js, ignore
  481. // presenter tracks here and do not add/remove them to/from the conference.
  482. if (track && track.local && track.mediaType !== MEDIA_TYPE.PRESENTER) {
  483. return (
  484. _syncConferenceLocalTracksWithState(store, action)
  485. .then(() => next(action)));
  486. }
  487. return next(action);
  488. }
  489. /**
  490. * Updates the conference object when the local participant is updated.
  491. *
  492. * @param {Store} store - The redux store in which the specified {@code action}
  493. * is being dispatched.
  494. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  495. * specified {@code action} to the specified {@code store}.
  496. * @param {Action} action - The redux action which is being dispatched in the
  497. * specified {@code store}.
  498. * @private
  499. * @returns {Object} The value returned by {@code next(action)}.
  500. */
  501. function _updateLocalParticipantInConference({ dispatch, getState }, next, action) {
  502. const { conference } = getState()['features/base/conference'];
  503. const { participant } = action;
  504. const result = next(action);
  505. const localParticipant = getLocalParticipant(getState);
  506. if (conference && participant.id === localParticipant?.id) {
  507. if ('name' in participant) {
  508. conference.setDisplayName(participant.name);
  509. }
  510. if ('role' in participant && participant.role === PARTICIPANT_ROLE.MODERATOR) {
  511. const { pendingSubjectChange, subject } = getState()['features/base/conference'];
  512. // When the local user role is updated to moderator and we have a pending subject change
  513. // which was not reflected we need to set it (the first time we tried was before becoming moderator).
  514. if (typeof pendingSubjectChange !== 'undefined' && pendingSubjectChange !== subject) {
  515. dispatch(setSubject(pendingSubjectChange));
  516. }
  517. }
  518. }
  519. return result;
  520. }