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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. ENDPOINT_MESSAGE_RECEIVED
  13. } from '../base/conference/actionTypes';
  14. import { getCurrentConference } from '../base/conference/functions';
  15. import { getURLWithoutParamsNormalized } from '../base/connection/utils';
  16. import { hideDialog } from '../base/dialog/actions';
  17. import { isDialogOpen } from '../base/dialog/functions';
  18. import { getLocalizedDateFormatter } from '../base/i18n/dateUtil';
  19. import { translateToHTML } from '../base/i18n/functions';
  20. import i18next from '../base/i18n/i18next';
  21. import { browser } from '../base/lib-jitsi-meet';
  22. import { pinParticipant, raiseHand, raiseHandClear } from '../base/participants/actions';
  23. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  24. import StateListenerRegistry from '../base/redux/StateListenerRegistry';
  25. import { SET_REDUCED_UI } from '../base/responsive-ui/actionTypes';
  26. import { LOWER_HAND_MESSAGE } from '../base/tracks/constants';
  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 { showStartRecordingNotification } from '../recording/actions';
  40. import { showSalesforceNotification } from '../salesforce/actions';
  41. import { setToolboxEnabled } from '../toolbox/actions.any';
  42. import { DISMISS_CALENDAR_NOTIFICATION } from './actionTypes';
  43. import { dismissCalendarNotification } from './actions';
  44. import { IFRAME_DISABLED_TIMEOUT_MINUTES, IFRAME_EMBED_ALLOWED_LOCATIONS } from './constants';
  45. let intervalID: any;
  46. MiddlewareRegistry.register(store => next => action => {
  47. const result = next(action);
  48. switch (action.type) {
  49. case CONFERENCE_JOINED: {
  50. _conferenceJoined(store);
  51. break;
  52. }
  53. case SET_REDUCED_UI: {
  54. _setReducedUI(store);
  55. break;
  56. }
  57. case DISMISS_CALENDAR_NOTIFICATION:
  58. case CONFERENCE_LEFT:
  59. case CONFERENCE_FAILED: {
  60. clearInterval(intervalID);
  61. intervalID = null;
  62. break;
  63. }
  64. case ENDPOINT_MESSAGE_RECEIVED: {
  65. const { participant, data } = action;
  66. const { dispatch } = store;
  67. if (data.name === LOWER_HAND_MESSAGE && participant.isModerator()) {
  68. dispatch(raiseHand(false));
  69. }
  70. break;
  71. }
  72. }
  73. return result;
  74. });
  75. /**
  76. * Set up state change listener to perform maintenance tasks when the conference
  77. * is left or failed, close all dialogs and unpin any pinned participants.
  78. */
  79. StateListenerRegistry.register(
  80. state => getCurrentConference(state),
  81. (conference, { dispatch, getState }, prevConference) => {
  82. const { authRequired, membersOnly, passwordRequired }
  83. = getState()['features/base/conference'];
  84. if (conference !== prevConference) {
  85. // Unpin participant, in order to avoid the local participant
  86. // remaining pinned, since it's not destroyed across runs.
  87. dispatch(pinParticipant(null));
  88. // Clear raised hands.
  89. dispatch(raiseHandClear());
  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. dispatch(showStartRecordingNotification());
  142. _checkIframe(getState(), dispatch);
  143. }
  144. /**
  145. * Additional checks for embedding in iframe.
  146. *
  147. * @param {IReduxState} state - The current state of the app.
  148. * @param {Function} dispatch - The Redux dispatch function.
  149. * @private
  150. * @returns {void}
  151. */
  152. function _checkIframe(state: IReduxState, dispatch: IStore['dispatch']) {
  153. let allowIframe = false;
  154. if (document.referrer === '' && browser.isElectron()) {
  155. // no iframe
  156. allowIframe = true;
  157. } else {
  158. try {
  159. allowIframe = IFRAME_EMBED_ALLOWED_LOCATIONS.includes(new URL(document.referrer).hostname);
  160. } catch (e) {
  161. // wrong URL in referrer
  162. }
  163. }
  164. if (inIframe() && state['features/base/config'].disableIframeAPI && !browser.isElectron()
  165. && !isVpaasMeeting(state) && !allowIframe) {
  166. // show sticky notification and redirect in 5 minutes
  167. const { locationURL } = state['features/base/connection'];
  168. let translationKey = 'notify.disabledIframe';
  169. const hostname = locationURL?.hostname ?? '';
  170. let domain = '';
  171. const mapping: Record<string, string> = {
  172. '8x8.vc': 'https://jaas.8x8.vc',
  173. 'meet.jit.si': 'https://jitsi.org/jaas'
  174. };
  175. const jaasDomain = mapping[hostname];
  176. if (jaasDomain) {
  177. translationKey = 'notify.disabledIframeSecondary';
  178. domain = hostname;
  179. }
  180. dispatch(showWarningNotification({
  181. description: translateToHTML(
  182. i18next.t.bind(i18next),
  183. translationKey,
  184. {
  185. domain,
  186. jaasDomain,
  187. timeout: IFRAME_DISABLED_TIMEOUT_MINUTES
  188. }
  189. )
  190. }, NOTIFICATION_TIMEOUT_TYPE.STICKY));
  191. setTimeout(() => {
  192. // redirect to the promotional page
  193. dispatch(redirectToStaticPage('static/close3.html', `#jitsi_meet_external_api_id=${API_ID}`));
  194. }, IFRAME_DISABLED_TIMEOUT_MINUTES * 60 * 1000);
  195. }
  196. }
  197. /**
  198. * Periodically checks if there is an event in the calendar for which we
  199. * need to show a notification.
  200. *
  201. * @param {Store} store - The redux store in which the specified {@code action}
  202. * is being dispatched.
  203. * @private
  204. * @returns {void}
  205. */
  206. function _maybeDisplayCalendarNotification({ dispatch, getState }: IStore) {
  207. const state = getState();
  208. const calendarEnabled = isCalendarEnabled(state);
  209. const { events: eventList } = state['features/calendar-sync'];
  210. const { locationURL } = state['features/base/connection'];
  211. const { reducedUI } = state['features/base/responsive-ui'];
  212. const currentConferenceURL
  213. = locationURL ? getURLWithoutParamsNormalized(locationURL) : '';
  214. const ALERT_MILLISECONDS = 5 * 60 * 1000;
  215. const now = Date.now();
  216. let eventToShow;
  217. if (!calendarEnabled && reducedUI) {
  218. return;
  219. }
  220. if (eventList?.length) {
  221. for (const event of eventList) {
  222. const eventURL
  223. = event?.url && getURLWithoutParamsNormalized(new URL(event.url));
  224. if (eventURL && eventURL !== currentConferenceURL) {
  225. // @ts-ignore
  226. if ((!eventToShow && event.startDate > now && event.startDate < now + ALERT_MILLISECONDS)
  227. // @ts-ignore
  228. || (event.startDate < now && event.endDate > now)) {
  229. eventToShow = event;
  230. }
  231. }
  232. }
  233. }
  234. _calendarNotification(
  235. {
  236. dispatch,
  237. getState
  238. }, eventToShow
  239. );
  240. }
  241. /**
  242. * Calendar notification.
  243. *
  244. * @param {Store} store - The redux store in which the specified {@code action}
  245. * is being dispatched.
  246. * @param {eventToShow} eventToShow - Next or ongoing event.
  247. * @private
  248. * @returns {void}
  249. */
  250. function _calendarNotification({ dispatch, getState }: IStore, eventToShow: any) {
  251. const state = getState();
  252. const { locationURL } = state['features/base/connection'];
  253. const currentConferenceURL
  254. = locationURL ? getURLWithoutParamsNormalized(locationURL) : '';
  255. const now = Date.now();
  256. if (!eventToShow) {
  257. return;
  258. }
  259. const customActionNameKey = [ 'notify.joinMeeting', 'notify.dontRemindMe' ];
  260. const customActionType = [ BUTTON_TYPES.PRIMARY, BUTTON_TYPES.DESTRUCTIVE ];
  261. const customActionHandler = [ () => batch(() => {
  262. dispatch(hideNotification(CALENDAR_NOTIFICATION_ID));
  263. if (eventToShow?.url && (eventToShow.url !== currentConferenceURL)) {
  264. dispatch(appNavigate(eventToShow.url));
  265. }
  266. }), () => dispatch(dismissCalendarNotification()) ];
  267. const description
  268. = getLocalizedDateFormatter(eventToShow.startDate).fromNow();
  269. const icon = NOTIFICATION_ICON.WARNING;
  270. const title = (eventToShow.startDate < now) && (eventToShow.endDate > now)
  271. ? `${i18n.t('calendarSync.ongoingMeeting')}: \n${eventToShow.title}`
  272. : `${i18n.t('calendarSync.nextMeeting')}: \n${eventToShow.title}`;
  273. const uid = CALENDAR_NOTIFICATION_ID;
  274. dispatch(showNotification({
  275. customActionHandler,
  276. customActionNameKey,
  277. customActionType,
  278. description,
  279. icon,
  280. maxLines: 1,
  281. title,
  282. uid
  283. }, NOTIFICATION_TIMEOUT_TYPE.STICKY));
  284. }