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.1KB

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