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.ts 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. import i18n from 'i18next';
  2. import { batch } from 'react-redux';
  3. // @ts-expect-error
  4. import { API_ID } from '../../../modules/API/constants';
  5. import { appNavigate } from '../app/actions';
  6. import { redirectToStaticPage } from '../app/actions.any';
  7. import { IReduxState, IStore } from '../app/types';
  8. import {
  9. CONFERENCE_FAILED,
  10. CONFERENCE_JOINED,
  11. CONFERENCE_LEFT,
  12. KICKED_OUT
  13. } from '../base/conference/actionTypes';
  14. import { conferenceLeft } from '../base/conference/actions';
  15. import { getCurrentConference } from '../base/conference/functions';
  16. import { getURLWithoutParamsNormalized } from '../base/connection/utils';
  17. import { hideDialog } from '../base/dialog/actions';
  18. import { isDialogOpen } from '../base/dialog/functions';
  19. import { getLocalizedDateFormatter } from '../base/i18n/dateUtil';
  20. import { translateToHTML } from '../base/i18n/functions';
  21. import i18next from '../base/i18n/i18next';
  22. import { browser } from '../base/lib-jitsi-meet';
  23. import { pinParticipant } from '../base/participants/actions';
  24. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  25. import StateListenerRegistry from '../base/redux/StateListenerRegistry';
  26. import { SET_REDUCED_UI } from '../base/responsive-ui/actionTypes';
  27. import { BUTTON_TYPES } from '../base/ui/constants.any';
  28. import { inIframe } from '../base/util/iframeUtils';
  29. import { isCalendarEnabled } from '../calendar-sync/functions';
  30. import FeedbackDialog from '../feedback/components/FeedbackDialog';
  31. import { setFilmstripEnabled } from '../filmstrip/actions.any';
  32. import { isVpaasMeeting } from '../jaas/functions';
  33. import { hideNotification, showNotification, showWarningNotification } from '../notifications/actions';
  34. import {
  35. CALENDAR_NOTIFICATION_ID,
  36. NOTIFICATION_ICON,
  37. NOTIFICATION_TIMEOUT_TYPE
  38. } from '../notifications/constants';
  39. import { showSalesforceNotification } from '../salesforce/actions';
  40. import { setToolboxEnabled } from '../toolbox/actions.any';
  41. import { DISMISS_CALENDAR_NOTIFICATION } from './actionTypes';
  42. import { dismissCalendarNotification, notifyKickedOut } from './actions';
  43. import { IFRAME_DISABLED_TIMEOUT_MINUTES, IFRAME_EMBED_ALLOWED_LOCATIONS } from './constants';
  44. let intervalID: any;
  45. MiddlewareRegistry.register(store => next => action => {
  46. const result = next(action);
  47. switch (action.type) {
  48. case CONFERENCE_JOINED: {
  49. _conferenceJoined(store);
  50. break;
  51. }
  52. case SET_REDUCED_UI: {
  53. _setReducedUI(store);
  54. break;
  55. }
  56. case KICKED_OUT: {
  57. const { dispatch } = store;
  58. dispatch(notifyKickedOut(
  59. action.participant,
  60. () => {
  61. dispatch(conferenceLeft(action.conference));
  62. dispatch(appNavigate(undefined));
  63. }
  64. ));
  65. break;
  66. }
  67. case DISMISS_CALENDAR_NOTIFICATION:
  68. case CONFERENCE_LEFT:
  69. case CONFERENCE_FAILED: {
  70. clearInterval(intervalID);
  71. intervalID = null;
  72. break;
  73. }
  74. }
  75. return result;
  76. });
  77. /**
  78. * Set up state change listener to perform maintenance tasks when the conference
  79. * is left or failed, close all dialogs and unpin any pinned participants.
  80. */
  81. StateListenerRegistry.register(
  82. state => getCurrentConference(state),
  83. (conference, { dispatch, getState }, prevConference) => {
  84. const { authRequired, membersOnly, passwordRequired }
  85. = getState()['features/base/conference'];
  86. if (conference !== prevConference) {
  87. // Unpin participant, in order to avoid the local participant
  88. // remaining pinned, since it's not destroyed across runs.
  89. dispatch(pinParticipant(null));
  90. // XXX I wonder if there is a better way to do this. At this stage
  91. // we do know what dialogs we want to keep but the list of those
  92. // we want to hide is a lot longer. Thus we take a bit of a shortcut
  93. // and explicitly check.
  94. if (typeof authRequired === 'undefined'
  95. && typeof passwordRequired === 'undefined'
  96. && typeof membersOnly === 'undefined'
  97. && !isDialogOpen(getState(), FeedbackDialog)) {
  98. // Conference changed, left or failed... and there is no
  99. // pending authentication, nor feedback request, so close any
  100. // dialog we might have open.
  101. dispatch(hideDialog());
  102. }
  103. }
  104. });
  105. /**
  106. * Configures the UI. In reduced UI mode some components will
  107. * be hidden if there is no space to render them.
  108. *
  109. * @param {Store} store - The redux store in which the specified {@code action}
  110. * is being dispatched.
  111. * @private
  112. * @returns {void}
  113. */
  114. function _setReducedUI({ dispatch, getState }: IStore) {
  115. const { reducedUI } = getState()['features/base/responsive-ui'];
  116. dispatch(setToolboxEnabled(!reducedUI));
  117. dispatch(setFilmstripEnabled(!reducedUI));
  118. }
  119. /**
  120. * Does extra sync up on properties that may need to be updated after the
  121. * conference was joined.
  122. *
  123. * @param {Store} store - The redux store in which the specified {@code action}
  124. * is being dispatched.
  125. * @private
  126. * @returns {void}
  127. */
  128. function _conferenceJoined({ dispatch, getState }: IStore) {
  129. _setReducedUI({
  130. dispatch,
  131. getState
  132. });
  133. if (!intervalID) {
  134. intervalID = setInterval(() =>
  135. _maybeDisplayCalendarNotification({
  136. dispatch,
  137. getState
  138. }), 10 * 1000);
  139. }
  140. dispatch(showSalesforceNotification());
  141. _checkIframe(getState(), dispatch);
  142. }
  143. /**
  144. * Additional checks for embedding in iframe.
  145. *
  146. * @param {IReduxState} state - The current state of the app.
  147. * @param {Function} dispatch - The Redux dispatch function.
  148. * @private
  149. * @returns {void}
  150. */
  151. function _checkIframe(state: IReduxState, dispatch: IStore['dispatch']) {
  152. let allowIframe = false;
  153. if (document.referrer === '' && browser.isElectron()) {
  154. // no iframe
  155. allowIframe = true;
  156. } else {
  157. try {
  158. allowIframe = IFRAME_EMBED_ALLOWED_LOCATIONS.includes(new URL(document.referrer).hostname);
  159. } catch (e) {
  160. // wrong URL in referrer
  161. }
  162. }
  163. if (inIframe() && state['features/base/config'].disableIframeAPI && !browser.isElectron()
  164. && !isVpaasMeeting(state) && !allowIframe) {
  165. // show sticky notification and redirect in 5 minutes
  166. dispatch(showWarningNotification({
  167. description: translateToHTML(
  168. i18next.t.bind(i18next),
  169. 'notify.disabledIframe',
  170. {
  171. timeout: IFRAME_DISABLED_TIMEOUT_MINUTES
  172. }
  173. )
  174. }, NOTIFICATION_TIMEOUT_TYPE.STICKY));
  175. setTimeout(() => {
  176. // redirect to the promotional page
  177. dispatch(redirectToStaticPage('static/close3.html', `#jitsi_meet_external_api_id=${API_ID}`));
  178. }, IFRAME_DISABLED_TIMEOUT_MINUTES * 60 * 1000);
  179. }
  180. }
  181. /**
  182. * Periodically checks if there is an event in the calendar for which we
  183. * need to show a notification.
  184. *
  185. * @param {Store} store - The redux store in which the specified {@code action}
  186. * is being dispatched.
  187. * @private
  188. * @returns {void}
  189. */
  190. function _maybeDisplayCalendarNotification({ dispatch, getState }: IStore) {
  191. const state = getState();
  192. const calendarEnabled = isCalendarEnabled(state);
  193. const { events: eventList } = state['features/calendar-sync'];
  194. const { locationURL } = state['features/base/connection'];
  195. const { reducedUI } = state['features/base/responsive-ui'];
  196. const currentConferenceURL
  197. = locationURL ? getURLWithoutParamsNormalized(locationURL) : '';
  198. const ALERT_MILLISECONDS = 5 * 60 * 1000;
  199. const now = Date.now();
  200. let eventToShow;
  201. if (!calendarEnabled && reducedUI) {
  202. return;
  203. }
  204. if (eventList?.length) {
  205. for (const event of eventList) {
  206. const eventURL
  207. = event?.url && getURLWithoutParamsNormalized(new URL(event.url));
  208. if (eventURL && eventURL !== currentConferenceURL) {
  209. // @ts-ignore
  210. if ((!eventToShow && event.startDate > now && event.startDate < now + ALERT_MILLISECONDS)
  211. // @ts-ignore
  212. || (event.startDate < now && event.endDate > now)) {
  213. eventToShow = event;
  214. }
  215. }
  216. }
  217. }
  218. _calendarNotification(
  219. {
  220. dispatch,
  221. getState
  222. }, eventToShow
  223. );
  224. }
  225. /**
  226. * Calendar notification.
  227. *
  228. * @param {Store} store - The redux store in which the specified {@code action}
  229. * is being dispatched.
  230. * @param {eventToShow} eventToShow - Next or ongoing event.
  231. * @private
  232. * @returns {void}
  233. */
  234. function _calendarNotification({ dispatch, getState }: IStore, eventToShow: any) {
  235. const state = getState();
  236. const { locationURL } = state['features/base/connection'];
  237. const currentConferenceURL
  238. = locationURL ? getURLWithoutParamsNormalized(locationURL) : '';
  239. const now = Date.now();
  240. if (!eventToShow) {
  241. return;
  242. }
  243. const customActionNameKey = [ 'notify.joinMeeting', 'notify.dontRemindMe' ];
  244. const customActionType = [ BUTTON_TYPES.PRIMARY, BUTTON_TYPES.DESTRUCTIVE ];
  245. const customActionHandler = [ () => batch(() => {
  246. dispatch(hideNotification(CALENDAR_NOTIFICATION_ID));
  247. if (eventToShow?.url && (eventToShow.url !== currentConferenceURL)) {
  248. dispatch(appNavigate(eventToShow.url));
  249. }
  250. }), () => dispatch(dismissCalendarNotification()) ];
  251. const description
  252. = getLocalizedDateFormatter(eventToShow.startDate).fromNow();
  253. const icon = NOTIFICATION_ICON.WARNING;
  254. const title = (eventToShow.startDate < now) && (eventToShow.endDate > now)
  255. ? `${i18n.t('calendarSync.ongoingMeeting')}: \n${eventToShow.title}`
  256. : `${i18n.t('calendarSync.nextMeeting')}: \n${eventToShow.title}`;
  257. const uid = CALENDAR_NOTIFICATION_ID;
  258. dispatch(showNotification({
  259. customActionHandler,
  260. customActionNameKey,
  261. customActionType,
  262. description,
  263. icon,
  264. title,
  265. uid
  266. }, NOTIFICATION_TIMEOUT_TYPE.STICKY));
  267. }