您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. LOCAL_PARTICIPANT_DEFAULT_NAME,
  15. PARTICIPANT_JOINED,
  16. participantUpdated
  17. } from '../participants';
  18. import { MiddlewareRegistry } from '../redux';
  19. import { setCallOverlayVisible, setJWT } from './actions';
  20. import { SET_JWT } from './actionTypes';
  21. import { parseJWTFromURLParams } from './functions';
  22. /**
  23. * Middleware to parse token data upon setting a new room URL.
  24. *
  25. * @param {Store} store - The redux store.
  26. * @private
  27. * @returns {Function}
  28. */
  29. MiddlewareRegistry.register(store => next => action => {
  30. switch (action.type) {
  31. case CONFERENCE_FAILED:
  32. case CONFERENCE_LEFT:
  33. case CONFERENCE_WILL_LEAVE:
  34. case LIB_INIT_ERROR:
  35. case PARTICIPANT_JOINED:
  36. case SET_ROOM:
  37. return _maybeSetCallOverlayVisible(store, next, action);
  38. case SET_CONFIG:
  39. case SET_LOCATION_URL:
  40. // XXX The JSON Web Token (JWT) is not the only piece of state that we
  41. // have decided to store in the feature jwt, there is isGuest as well
  42. // which depends on the states of the features base/config and jwt. So
  43. // the JSON Web Token comes from the conference/room's URL and isGuest
  44. // needs a recalculation upon SET_CONFIG as well.
  45. return _setConfigOrLocationURL(store, next, action);
  46. case SET_JWT:
  47. return _setJWT(store, next, action);
  48. }
  49. return next(action);
  50. });
  51. /**
  52. * Notifies the feature jwt that a specific {@code action} is being dispatched
  53. * within a specific redux {@code store} which may have an effect on the
  54. * visiblity of (the) {@code CallOverlay}.
  55. *
  56. * @param {Store} store - The redux store in which the specified {@code action}
  57. * is being dispatched.
  58. * @param {Dispatch} next - The redux dispatch function to dispatch the
  59. * specified {@code action} to the specified {@code store}.
  60. * @param {Action} action - The redux action which is being dispatched in the
  61. * specified {@code store}.
  62. * @private
  63. * @returns {Object} The new state that is the result of the reduction of the
  64. * specified {@code action}.
  65. */
  66. function _maybeSetCallOverlayVisible({ dispatch, getState }, next, action) {
  67. const result = next(action);
  68. const state = getState();
  69. const stateFeaturesJWT = state['features/base/jwt'];
  70. let callOverlayVisible;
  71. if (stateFeaturesJWT.callee) {
  72. const { conference, leaving, room } = state['features/base/conference'];
  73. // XXX The CallOverlay is to be displayed/visible as soon as
  74. // possible including even before the conference is joined.
  75. if (room && (!conference || conference !== leaving)) {
  76. switch (action.type) {
  77. case CONFERENCE_FAILED:
  78. case CONFERENCE_LEFT:
  79. case CONFERENCE_WILL_LEAVE:
  80. case LIB_INIT_ERROR:
  81. // Because the CallOverlay is to be displayed/visible as soon as
  82. // possible even before the connection is established and/or the
  83. // conference is joined, it is very complicated to figure out
  84. // based on the current state alone. In order to reduce the
  85. // risks of displaying the CallOverly at inappropirate times, do
  86. // not even attempt to figure out based on the current state.
  87. // The (redux) actions listed above are also the equivalents of
  88. // the execution ponints at which APP.UI.hideRingOverlay() used
  89. // to be invoked.
  90. break;
  91. default: {
  92. // The CallOverlay it to no longer be displayed/visible as soon
  93. // as another participant joins.
  94. callOverlayVisible = getParticipantCount(state) === 1
  95. && Boolean(getLocalParticipant(state));
  96. // However, the CallDialog is not to be displayed/visible again
  97. // after all remote participants leave.
  98. if (callOverlayVisible
  99. && stateFeaturesJWT.callOverlayVisible === false) {
  100. callOverlayVisible = false;
  101. }
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. dispatch(setCallOverlayVisible(callOverlayVisible));
  108. return result;
  109. }
  110. /**
  111. * Converts 'context.user' JWT token structure to the format compatible with the
  112. * corresponding fields overridden in base/participants.
  113. *
  114. * @param {Object} user - The 'jwt.context.user' structure parsed from the JWT
  115. * token.
  116. * @returns {({
  117. * avatarURL: string?,
  118. * email: string?,
  119. * name: string?
  120. * })}
  121. * @private
  122. */
  123. function _normalizeCallerFields(user) {
  124. const { avatar, avatarUrl, email, name } = user;
  125. const caller = { };
  126. if (typeof (avatarUrl || avatar) === 'string') {
  127. caller.avatarURL = (avatarUrl || avatar).trim();
  128. }
  129. if (typeof email === 'string') {
  130. caller.email = email.trim();
  131. }
  132. if (typeof name === 'string') {
  133. caller.name = name.trim();
  134. }
  135. return Object.keys(caller).length ? caller : undefined;
  136. }
  137. /**
  138. * Eventually overwrites 'avatarURL', 'email' and 'name' fields with the values
  139. * from JWT token for the local participant stored in the 'base/participants'
  140. * Redux store by dispatching the participant updated action.
  141. *
  142. * @param {Store} store - The redux store.
  143. * @param {Object} caller - The "caller" structure parsed from 'context.user'
  144. * part of the JWT token and then normalized using
  145. * {@link _normalizeCallerFields}.
  146. * @returns {void}
  147. * @private
  148. */
  149. function _overwriteLocalParticipant({ dispatch, getState }, caller) {
  150. const { avatarURL, email, name } = caller;
  151. const localParticipant = getLocalParticipant(getState());
  152. if (localParticipant && (avatarURL || email || name)) {
  153. const newProperties = { id: localParticipant.id };
  154. if (avatarURL) {
  155. newProperties.avatarURL = avatarURL;
  156. }
  157. if (email) {
  158. newProperties.email = email;
  159. }
  160. if (name) {
  161. newProperties.name = name;
  162. }
  163. dispatch(participantUpdated(newProperties));
  164. }
  165. }
  166. /**
  167. * Will reset the values overridden by {@link _overwriteLocalParticipant}
  168. * by either clearing them or setting to default values. Only the values that
  169. * have not changed since the override happened will be restored.
  170. *
  171. * NOTE Once there is the possibility to edit and save participant properties,
  172. * this method should restore values from the storage instead.
  173. *
  174. * @param {Store} store - The Redux store.
  175. * @param {Object} caller - The 'caller' part of the JWT Redux state which tells
  176. * which local participant's fields's been overridden when the JWT token was
  177. * set.
  178. * @returns {void}
  179. * @private
  180. */
  181. function _resetLocalParticipantOverrides({ dispatch, getState }, caller) {
  182. const { avatarURL, name, email } = caller;
  183. const localParticipant = getLocalParticipant(getState());
  184. if (localParticipant && (avatarURL || name || email)) {
  185. const newProperties = { id: localParticipant.id };
  186. if (avatarURL === localParticipant.avatarURL) {
  187. newProperties.avatarURL = undefined;
  188. }
  189. if (name === localParticipant.name) {
  190. newProperties.name = LOCAL_PARTICIPANT_DEFAULT_NAME;
  191. }
  192. if (email === localParticipant.email) {
  193. newProperties.email = undefined;
  194. }
  195. dispatch(participantUpdated(newProperties));
  196. }
  197. }
  198. /**
  199. * Notifies the feature jwt that the action {@link SET_CONFIG} or
  200. * {@link SET_LOCATION_URL} is being dispatched within a specific redux
  201. * {@code store}.
  202. *
  203. * @param {Store} store - The redux store in which the specified {@code action}
  204. * is being dispatched.
  205. * @param {Dispatch} next - The redux dispatch function to dispatch the
  206. * specified {@code action} to the specified {@code store}.
  207. * @param {Action} action - The redux action {@code SET_CONFIG} or
  208. * {@code SET_LOCATION_URL} which is being dispatched in the specified
  209. * {@code store}.
  210. * @private
  211. * @returns {Object} The new state that is the result of the reduction of the
  212. * specified {@code action}.
  213. */
  214. function _setConfigOrLocationURL({ dispatch, getState }, next, action) {
  215. const result = next(action);
  216. const { locationURL } = getState()['features/base/connection'];
  217. let jwt;
  218. if (locationURL) {
  219. jwt = parseJWTFromURLParams(locationURL);
  220. }
  221. dispatch(setJWT(jwt));
  222. return result;
  223. }
  224. /**
  225. * Notifies the feature jwt that the action {@link SET_JWT} is being dispatched
  226. * within a specific redux {@code store}.
  227. *
  228. * @param {Store} store - The redux store in which the specified {@code action}
  229. * is being dispatched.
  230. * @param {Dispatch} next - The redux dispatch function to dispatch the
  231. * specified {@code action} to the specified {@code store}.
  232. * @param {Action} action - The redux action {@code SET_JWT} which is being
  233. * dispatched in the specified {@code store}.
  234. * @private
  235. * @returns {Object} The new state that is the result of the reduction of the
  236. * specified {@code action}.
  237. */
  238. function _setJWT(store, next, action) {
  239. // eslint-disable-next-line no-unused-vars
  240. const { jwt, type, ...actionPayload } = action;
  241. if (jwt && !Object.keys(actionPayload).length) {
  242. const {
  243. enableUserRolesBasedOnToken
  244. } = store.getState()['features/base/config'];
  245. action.isGuest = !enableUserRolesBasedOnToken;
  246. const jwtPayload = jwtDecode(jwt);
  247. if (jwtPayload) {
  248. const { context, iss } = jwtPayload;
  249. action.jwt = jwt;
  250. action.issuer = iss;
  251. if (context) {
  252. action.callee = context.callee;
  253. action.caller = _normalizeCallerFields(context.user);
  254. action.group = context.group;
  255. action.server = context.server;
  256. if (action.caller) {
  257. _overwriteLocalParticipant(store, action.caller);
  258. }
  259. }
  260. }
  261. } else if (!jwt && !Object.keys(actionPayload).length) {
  262. const jwtState = store.getState()['features/base/jwt'];
  263. // The logic of restoring JWT overrides make sense only on mobile. On
  264. // web it should eventually be restored from storage, but there's no
  265. // such use case yet.
  266. if (jwtState.caller && typeof APP === 'undefined') {
  267. _resetLocalParticipantOverrides(store, jwtState.caller);
  268. }
  269. }
  270. return _maybeSetCallOverlayVisible(store, next, action);
  271. }