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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // @flow
  2. import { APP_WILL_MOUNT } from '../base/app';
  3. import { CONFERENCE_WILL_LEAVE, SET_ROOM } from '../base/conference';
  4. import { JITSI_CONFERENCE_URL_KEY } from '../base/conference/constants';
  5. import { addKnownDomains } from '../base/known-domains';
  6. import { MiddlewareRegistry } from '../base/redux';
  7. import { parseURIString } from '../base/util';
  8. import { RECENT_LIST_ENABLED } from './featureFlag';
  9. import { _storeCurrentConference, _updateConferenceDuration } from './actions';
  10. /**
  11. * used in order to get the device because there is a different way to get the
  12. * location URL on web and on native
  13. */
  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 (RECENT_LIST_ENABLED) {
  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. let locationURL;
  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 guranteed 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. return next(action);
  97. }
  98. /**
  99. * Checks if there is a current conference (upon SET_ROOM action), and saves it
  100. * if necessary.
  101. *
  102. * @param {Store} store - The redux store.
  103. * @param {Dispatch} next - The redux {@code dispatch} function.
  104. * @param {Action} action - The redux action {@link SET_ROOM}.
  105. * @private
  106. * @returns {*} The result returned by {@code next(action)}.
  107. */
  108. function _setRoom({ dispatch, getState }, next, action) {
  109. if (action.room) {
  110. const { locationURL } = getState()['features/base/connection'];
  111. if (locationURL) {
  112. dispatch(_storeCurrentConference(locationURL));
  113. // Whatever domain the feature recent-list knows about, the app as a
  114. // whole should know about.
  115. //
  116. // XXX Technically, _storeCurrentConference could be turned into an
  117. // asynchronous action creator which dispatches both
  118. // _STORE_CURRENT_CONFERENCE and addKnownDomains but...
  119. dispatch(addKnownDomains(locationURL.host));
  120. }
  121. }
  122. return next(action);
  123. }