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

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