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

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