Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.any.ts 9.6KB

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