您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.any.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 participants = state['features/base/participants'];
  346. const id = action.participant.id;
  347. const participantById = getParticipantById(participants, id);
  348. const pinnedParticipant = getPinnedParticipant(participants);
  349. const actionName = id ? ACTION_PINNED : ACTION_UNPINNED;
  350. const local
  351. = (participantById && participantById.local)
  352. || (!id && pinnedParticipant && pinnedParticipant.local);
  353. let participantIdForEvent;
  354. if (local) {
  355. participantIdForEvent = local;
  356. } else {
  357. participantIdForEvent
  358. = actionName === ACTION_PINNED ? id : pinnedParticipant && pinnedParticipant.id;
  359. }
  360. sendAnalytics(createPinnedEvent(
  361. actionName,
  362. participantIdForEvent,
  363. {
  364. local,
  365. 'participant_count': conference.getParticipantCount()
  366. }));
  367. return next(action);
  368. }
  369. /**
  370. * Requests the specified tones to be played.
  371. *
  372. * @param {Store} store - The redux store in which the specified {@code action}
  373. * is being dispatched.
  374. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  375. * specified {@code action} to the specified {@code store}.
  376. * @param {Action} action - The redux action {@code SEND_TONES} which is
  377. * being dispatched in the specified {@code store}.
  378. * @private
  379. * @returns {Object} The value returned by {@code next(action)}.
  380. */
  381. function _sendTones({ getState }, next, action) {
  382. const state = getState();
  383. const { conference } = state['features/base/conference'];
  384. if (conference) {
  385. const { duration, tones, pause } = action;
  386. conference.sendTones(tones, duration, pause);
  387. }
  388. return next(action);
  389. }
  390. /**
  391. * Notifies the feature base/conference that the action
  392. * {@code SET_ROOM} is being dispatched within a specific
  393. * redux store.
  394. *
  395. * @param {Store} store - The redux store in which the specified {@code action}
  396. * is being dispatched.
  397. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  398. * specified {@code action} to the specified {@code store}.
  399. * @param {Action} action - The redux action {@code SET_ROOM}
  400. * which is being dispatched in the specified {@code store}.
  401. * @private
  402. * @returns {Object} The value returned by {@code next(action)}.
  403. */
  404. function _setRoom({ dispatch, getState }, next, action) {
  405. const state = getState();
  406. const { subject } = state['features/base/config'];
  407. const { room } = action;
  408. if (room) {
  409. // Set the stored subject.
  410. dispatch(setSubject(subject));
  411. }
  412. return next(action);
  413. }
  414. /**
  415. * Synchronizes local tracks from state with local tracks in JitsiConference
  416. * instance.
  417. *
  418. * @param {Store} store - The redux store.
  419. * @param {Object} action - Action object.
  420. * @private
  421. * @returns {Promise}
  422. */
  423. function _syncConferenceLocalTracksWithState({ getState }, action) {
  424. const conference = getCurrentConference(getState);
  425. let promise;
  426. if (conference) {
  427. const track = action.track.jitsiTrack;
  428. if (action.type === TRACK_ADDED) {
  429. promise = _addLocalTracksToConference(conference, [ track ]);
  430. } else {
  431. promise = _removeLocalTracksFromConference(conference, [ track ]);
  432. }
  433. }
  434. return promise || Promise.resolve();
  435. }
  436. /**
  437. * Notifies the feature base/conference that the action {@code TRACK_ADDED}
  438. * or {@code TRACK_REMOVED} is being dispatched within a specific redux store.
  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 TRACK_ADDED} or
  445. * {@code TRACK_REMOVED} which is being dispatched in the specified
  446. * {@code store}.
  447. * @private
  448. * @returns {Object} The value returned by {@code next(action)}.
  449. */
  450. function _trackAddedOrRemoved(store, next, action) {
  451. const track = action.track;
  452. // TODO All track swapping should happen here instead of conference.js.
  453. // Since we swap the tracks for the web client in conference.js, ignore
  454. // presenter tracks here and do not add/remove them to/from the conference.
  455. if (track && track.local && track.mediaType !== MEDIA_TYPE.PRESENTER) {
  456. return (
  457. _syncConferenceLocalTracksWithState(store, action)
  458. .then(() => next(action)));
  459. }
  460. return next(action);
  461. }
  462. /**
  463. * Updates the conference object when the local participant is updated.
  464. *
  465. * @param {Store} store - The redux store in which the specified {@code action}
  466. * is being dispatched.
  467. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  468. * specified {@code action} to the specified {@code store}.
  469. * @param {Action} action - The redux action which is being dispatched in the
  470. * specified {@code store}.
  471. * @private
  472. * @returns {Object} The value returned by {@code next(action)}.
  473. */
  474. function _updateLocalParticipantInConference({ dispatch, getState }, next, action) {
  475. const { conference } = getState()['features/base/conference'];
  476. const { participant } = action;
  477. const result = next(action);
  478. const localParticipant = getLocalParticipant(getState);
  479. if (conference && participant.id === localParticipant.id) {
  480. if ('name' in participant) {
  481. conference.setDisplayName(participant.name);
  482. }
  483. if ('role' in participant && participant.role === PARTICIPANT_ROLE.MODERATOR) {
  484. const { pendingSubjectChange, subject } = getState()['features/base/conference'];
  485. // When the local user role is updated to moderator and we have a pending subject change
  486. // which was not reflected we need to set it (the first time we tried was before becoming moderator).
  487. if (typeof pendingSubjectChange !== 'undefined' && pendingSubjectChange !== subject) {
  488. dispatch(setSubject(pendingSubjectChange));
  489. }
  490. }
  491. }
  492. return result;
  493. }