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.js 5.2KB

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