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

middleware.ts 8.2KB

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