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

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