Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

middleware.ts 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { AnyAction } from 'redux';
  2. import { IStore } from '../app/types';
  3. import { APP_WILL_MOUNT } from '../base/app/actionTypes';
  4. import { CONFERENCE_WILL_LEAVE, SET_ROOM } from '../base/conference/actionTypes';
  5. import { JITSI_CONFERENCE_URL_KEY } from '../base/conference/constants';
  6. import { addKnownDomains } from '../base/known-domains/actions';
  7. import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
  8. import { inIframe } from '../base/util/iframeUtils';
  9. import { parseURIString } from '../base/util/uri';
  10. import { _storeCurrentConference, _updateConferenceDuration } from './actions';
  11. import { isRecentListEnabled } from './functions';
  12. /**
  13. * Middleware that captures joined rooms so they can be saved into
  14. * {@code window.localStorage}.
  15. *
  16. * @param {Store} store - The redux store.
  17. * @returns {Function}
  18. */
  19. MiddlewareRegistry.register(store => next => action => {
  20. if (isRecentListEnabled()) {
  21. switch (action.type) {
  22. case APP_WILL_MOUNT:
  23. return _appWillMount(store, next, action);
  24. case CONFERENCE_WILL_LEAVE:
  25. return _conferenceWillLeave(store, next, action);
  26. case SET_ROOM:
  27. return _setRoom(store, next, action);
  28. }
  29. }
  30. return next(action);
  31. });
  32. /**
  33. * Notifies the feature recent-list that the redux action {@link APP_WILL_MOUNT}
  34. * is being dispatched in a specific redux store.
  35. *
  36. * @param {Store} store - The redux store in which the specified action is being
  37. * dispatched.
  38. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  39. * specified action to the specified store.
  40. * @param {Action} action - The redux action {@code APP_WILL_MOUNT} which is
  41. * being dispatched in the specified redux store.
  42. * @private
  43. * @returns {*} The result returned by {@code next(action)}.
  44. */
  45. function _appWillMount({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  46. const result = next(action);
  47. // It's an opportune time to transfer the feature recent-list's knowledge
  48. // about "known domains" (which is local to the feature) to the feature
  49. // base/known-domains (which is global to the app).
  50. //
  51. // XXX Since the feature recent-list predates the feature calendar-sync and,
  52. // consequently, the feature known-domains, it's possible for the feature
  53. // known-list to know of domains which the feature known-domains is yet to
  54. // discover.
  55. const knownDomains = [];
  56. for (const { conference } of getState()['features/recent-list']) {
  57. const uri = parseURIString(conference);
  58. let host;
  59. uri && (host = uri.host) && knownDomains.push(host);
  60. }
  61. knownDomains.length && dispatch(addKnownDomains(knownDomains));
  62. return result;
  63. }
  64. /**
  65. * Updates the duration of the last conference stored in the list.
  66. *
  67. * @param {Store} store - The redux store.
  68. * @param {Dispatch} next - The redux {@code dispatch} function.
  69. * @param {Action} action - The redux action {@link CONFERENCE_WILL_LEAVE}.
  70. * @private
  71. * @returns {*} The result returned by {@code next(action)}.
  72. */
  73. function _conferenceWillLeave({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  74. const { doNotStoreRoom } = getState()['features/base/config'];
  75. if (!doNotStoreRoom && !inIframe()) {
  76. let locationURL;
  77. /**
  78. * FIXME:
  79. * It is better to use action.conference[JITSI_CONFERENCE_URL_KEY]
  80. * in order to make sure we get the url the conference is leaving
  81. * from (i.e. The room we are leaving from) because if the order of events
  82. * is different, we cannot be guaranteed that the location URL in base
  83. * connection is the url we are leaving from... Not the one we are going to
  84. * (the latter happens on mobile -- if we use the web implementation);
  85. * however, the conference object on web does not have
  86. * JITSI_CONFERENCE_URL_KEY so we cannot call it and must use the other way.
  87. */
  88. if (typeof APP === 'undefined') {
  89. locationURL = action.conference[JITSI_CONFERENCE_URL_KEY];
  90. } else {
  91. locationURL = getState()['features/base/connection'].locationURL;
  92. }
  93. dispatch(
  94. _updateConferenceDuration(
  95. locationURL));
  96. }
  97. return next(action);
  98. }
  99. /**
  100. * Checks if there is a current conference (upon SET_ROOM action), and saves it
  101. * if necessary.
  102. *
  103. * @param {Store} store - The redux store.
  104. * @param {Dispatch} next - The redux {@code dispatch} function.
  105. * @param {Action} action - The redux action {@link SET_ROOM}.
  106. * @private
  107. * @returns {*} The result returned by {@code next(action)}.
  108. */
  109. function _setRoom({ dispatch, getState }: IStore, next: Function, action: AnyAction) {
  110. const { doNotStoreRoom } = getState()['features/base/config'];
  111. if (!doNotStoreRoom && !inIframe() && action.room) {
  112. const { locationURL } = getState()['features/base/connection'];
  113. if (locationURL) {
  114. dispatch(_storeCurrentConference(locationURL));
  115. // Whatever domain the feature recent-list knows about, the app as a
  116. // whole should know about.
  117. //
  118. // XXX Technically, _storeCurrentConference could be turned into an
  119. // asynchronous action creator which dispatches both
  120. // _STORE_CURRENT_CONFERENCE and addKnownDomains but...
  121. dispatch(addKnownDomains(locationURL.host));
  122. }
  123. }
  124. return next(action);
  125. }