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

middleware.js 11KB

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