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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import jwtDecode from 'jwt-decode';
  2. import {
  3. CONFERENCE_FAILED,
  4. CONFERENCE_LEFT,
  5. CONFERENCE_WILL_LEAVE,
  6. SET_ROOM
  7. } from '../conference';
  8. import { SET_CONFIG } from '../config';
  9. import { SET_LOCATION_URL } from '../connection';
  10. import { LIB_INIT_ERROR } from '../lib-jitsi-meet';
  11. import {
  12. getLocalParticipant,
  13. getParticipantCount,
  14. PARTICIPANT_JOINED
  15. } from '../participants';
  16. import { MiddlewareRegistry } from '../redux';
  17. import { setCallOverlayVisible, setJWT } from './actions';
  18. import { SET_JWT } from './actionTypes';
  19. import { parseJWTFromURLParams } from './functions';
  20. /**
  21. * Middleware to parse token data upon setting a new room URL.
  22. *
  23. * @param {Store} store - The redux store.
  24. * @private
  25. * @returns {Function}
  26. */
  27. MiddlewareRegistry.register(store => next => action => {
  28. switch (action.type) {
  29. case CONFERENCE_FAILED:
  30. case CONFERENCE_LEFT:
  31. case CONFERENCE_WILL_LEAVE:
  32. case LIB_INIT_ERROR:
  33. case PARTICIPANT_JOINED:
  34. case SET_ROOM:
  35. return _maybeSetCallOverlayVisible(store, next, action);
  36. case SET_CONFIG:
  37. case SET_LOCATION_URL:
  38. // XXX The JSON Web Token (JWT) is not the only piece of state that we
  39. // have decided to store in the feature jwt, there is isGuest as well
  40. // which depends on the states of the features base/config and jwt. So
  41. // the JSON Web Token comes from the conference/room's URL and isGuest
  42. // needs a recalculation upon SET_CONFIG as well.
  43. return _setConfigOrLocationURL(store, next, action);
  44. case SET_JWT:
  45. return _setJWT(store, next, action);
  46. }
  47. return next(action);
  48. });
  49. /**
  50. * Notifies the feature jwt that a specific {@code action} is being dispatched
  51. * within a specific redux {@code store} which may have an effect on the
  52. * visiblity of (the) {@code CallOverlay}.
  53. *
  54. * @param {Store} store - The redux store in which the specified {@code action}
  55. * is being dispatched.
  56. * @param {Dispatch} next - The redux dispatch function to dispatch the
  57. * specified {@code action} to the specified {@code store}.
  58. * @param {Action} action - The redux action which is being dispatched in the
  59. * specified {@code store}.
  60. * @private
  61. * @returns {Object} The new state that is the result of the reduction of the
  62. * specified {@code action}.
  63. */
  64. function _maybeSetCallOverlayVisible({ dispatch, getState }, next, action) {
  65. const result = next(action);
  66. const state = getState();
  67. const stateFeaturesJWT = state['features/base/jwt'];
  68. let callOverlayVisible;
  69. if (stateFeaturesJWT.callee) {
  70. const { conference, leaving, room } = state['features/base/conference'];
  71. // XXX The CallOverlay is to be displayed/visible as soon as
  72. // possible including even before the conference is joined.
  73. if (room && (!conference || conference !== leaving)) {
  74. switch (action.type) {
  75. case CONFERENCE_FAILED:
  76. case CONFERENCE_LEFT:
  77. case CONFERENCE_WILL_LEAVE:
  78. case LIB_INIT_ERROR:
  79. // Because the CallOverlay is to be displayed/visible as soon as
  80. // possible even before the connection is established and/or the
  81. // conference is joined, it is very complicated to figure out
  82. // based on the current state alone. In order to reduce the
  83. // risks of displaying the CallOverly at inappropirate times, do
  84. // not even attempt to figure out based on the current state.
  85. // The (redux) actions listed above are also the equivalents of
  86. // the execution ponints at which APP.UI.hideRingOverlay() used
  87. // to be invoked.
  88. break;
  89. default: {
  90. // The CallOverlay it to no longer be displayed/visible as soon
  91. // as another participant joins.
  92. callOverlayVisible = getParticipantCount(state) === 1
  93. && Boolean(getLocalParticipant(state));
  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. }