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

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