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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import jwtDecode from 'jwt-decode';
  2. import {
  3. CONFERENCE_FAILED,
  4. CONFERENCE_LEFT,
  5. CONFERENCE_WILL_LEAVE,
  6. SET_ROOM
  7. } from '../base/conference';
  8. import { SET_CONFIG } from '../base/config';
  9. import { SET_LOCATION_URL } from '../base/connection';
  10. import { LIB_INIT_ERROR } from '../base/lib-jitsi-meet';
  11. import { PARTICIPANT_JOINED } from '../base/participants';
  12. import { MiddlewareRegistry } from '../base/redux';
  13. import { setCallOverlayVisible, setJWT } from './actions';
  14. import { SET_JWT } from './actionTypes';
  15. import { parseJWTFromURLParams } from './functions';
  16. /**
  17. * Middleware to parse token data upon setting a new room URL.
  18. *
  19. * @param {Store} store - The redux store.
  20. * @private
  21. * @returns {Function}
  22. */
  23. MiddlewareRegistry.register(store => next => action => {
  24. switch (action.type) {
  25. case CONFERENCE_FAILED:
  26. case CONFERENCE_LEFT:
  27. case CONFERENCE_WILL_LEAVE:
  28. case LIB_INIT_ERROR:
  29. case PARTICIPANT_JOINED:
  30. case SET_ROOM:
  31. return _maybeSetCallOverlayVisible(store, next, action);
  32. case SET_CONFIG:
  33. case SET_LOCATION_URL:
  34. // XXX The JSON Web Token (JWT) is not the only piece of state that we
  35. // have decided to store in the feature jwt, there is isGuest as well
  36. // which depends on the states of the features base/config and jwt. So
  37. // the JSON Web Token comes from the conference/room's URL and isGuest
  38. // needs a recalculation upon SET_CONFIG as well.
  39. return _setConfigOrLocationURL(store, next, action);
  40. case SET_JWT:
  41. return _setJWT(store, next, action);
  42. }
  43. return next(action);
  44. });
  45. /**
  46. * Notifies the feature jwt that a specific {@code action} is being dispatched
  47. * within a specific redux {@code store} which may have an effect on the
  48. * visiblity of (the) {@code CallOverlay}.
  49. *
  50. * @param {Store} store - The redux store in which the specified {@code action}
  51. * is being dispatched.
  52. * @param {Dispatch} next - The redux dispatch function to dispatch the
  53. * specified {@code action} to the specified {@code store}.
  54. * @param {Action} action - The redux action which is being dispatched in the
  55. * specified {@code store}.
  56. * @private
  57. * @returns {Object} The new state that is the result of the reduction of the
  58. * specified {@code action}.
  59. */
  60. function _maybeSetCallOverlayVisible({ dispatch, getState }, next, action) {
  61. const result = next(action);
  62. const state = getState();
  63. const stateFeaturesJWT = state['features/jwt'];
  64. let callOverlayVisible;
  65. if (stateFeaturesJWT.callee) {
  66. const { conference, leaving, room } = state['features/base/conference'];
  67. // XXX The CallOverlay is to be displayed/visible as soon as
  68. // possible including even before the conference is joined.
  69. if (room && (!conference || conference !== leaving)) {
  70. switch (action.type) {
  71. case CONFERENCE_FAILED:
  72. case CONFERENCE_LEFT:
  73. case CONFERENCE_WILL_LEAVE:
  74. case LIB_INIT_ERROR:
  75. // Because the CallOverlay is to be displayed/visible as soon as
  76. // possible even before the connection is established and/or the
  77. // conference is joined, it is very complicated to figure out
  78. // based on the current state alone. In order to reduce the
  79. // risks of displaying the CallOverly at inappropirate times, do
  80. // not even attempt to figure out based on the current state.
  81. // The (redux) actions listed above are also the equivalents of
  82. // the execution ponints at which APP.UI.hideRingOverlay() used
  83. // to be invoked.
  84. break;
  85. default: {
  86. // The CallOverlay it to no longer be displayed/visible as soon
  87. // as another participant joins.
  88. const participants = state['features/base/participants'];
  89. callOverlayVisible
  90. = Boolean(
  91. participants
  92. && participants.length === 1
  93. && participants[0].local);
  94. // However, the CallDialog is not to be displayed/visible again
  95. // after all remote participants leave.
  96. if (callOverlayVisible
  97. && stateFeaturesJWT.callOverlayVisible === false) {
  98. callOverlayVisible = false;
  99. }
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. dispatch(setCallOverlayVisible(callOverlayVisible));
  106. return result;
  107. }
  108. /**
  109. * Notifies the feature jwt that the action {@link SET_CONFIG} or
  110. * {@link SET_LOCATION_URL} is being dispatched within a specific redux
  111. * {@code store}.
  112. *
  113. * @param {Store} store - The redux store in which the specified {@code action}
  114. * is being dispatched.
  115. * @param {Dispatch} next - The redux dispatch function to dispatch the
  116. * specified {@code action} to the specified {@code store}.
  117. * @param {Action} action - The redux action {@code SET_CONFIG} or
  118. * {@code SET_LOCATION_URL} which is being dispatched in the specified
  119. * {@code store}.
  120. * @private
  121. * @returns {Object} The new state that is the result of the reduction of the
  122. * specified {@code action}.
  123. */
  124. function _setConfigOrLocationURL({ dispatch, getState }, next, action) {
  125. const result = next(action);
  126. const { locationURL } = getState()['features/base/connection'];
  127. let jwt;
  128. if (locationURL) {
  129. jwt = parseJWTFromURLParams(locationURL);
  130. }
  131. dispatch(setJWT(jwt));
  132. return result;
  133. }
  134. /**
  135. * Notifies the feature jwt that the action {@link SET_JWT} is being dispatched
  136. * within a specific redux {@code store}.
  137. *
  138. * @param {Store} store - The redux store in which the specified {@code action}
  139. * is being dispatched.
  140. * @param {Dispatch} next - The redux dispatch function to dispatch the
  141. * specified {@code action} to the specified {@code store}.
  142. * @param {Action} action - The redux action {@code SET_JWT} which is being
  143. * dispatched in the specified {@code store}.
  144. * @private
  145. * @returns {Object} The new state that is the result of the reduction of the
  146. * specified {@code action}.
  147. */
  148. function _setJWT(store, next, action) {
  149. // eslint-disable-next-line no-unused-vars
  150. const { jwt, type, ...actionPayload } = action;
  151. if (jwt && !Object.keys(actionPayload).length) {
  152. const {
  153. enableUserRolesBasedOnToken
  154. } = store.getState()['features/base/config'];
  155. action.isGuest = !enableUserRolesBasedOnToken;
  156. const jwtPayload = jwtDecode(jwt);
  157. if (jwtPayload) {
  158. const { context, iss } = jwtPayload;
  159. action.jwt = jwt;
  160. action.issuer = iss;
  161. if (context) {
  162. action.callee = context.callee;
  163. action.caller = context.user;
  164. action.group = context.group;
  165. action.server = context.server;
  166. }
  167. }
  168. }
  169. return _maybeSetCallOverlayVisible(store, next, action);
  170. }