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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 { setCalleeInfoVisible, 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 _maybeSetCalleeInfoVisible(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 CalleeInfo}.
  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 _maybeSetCalleeInfoVisible({ dispatch, getState }, next, action) {
  68. const result = next(action);
  69. const state = getState();
  70. const stateFeaturesBaseJWT = state['features/base/jwt'];
  71. let calleeInfoVisible;
  72. if (stateFeaturesBaseJWT.callee) {
  73. const { conference, leaving, room } = state['features/base/conference'];
  74. // XXX The CalleeInfo 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 CalleeInfo 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 CalleeInfo is to no longer be displayed/visible as soon
  94. // as another participant joins.
  95. calleeInfoVisible
  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 (calleeInfoVisible
  101. && stateFeaturesBaseJWT.calleeInfoVisible === false) {
  102. calleeInfoVisible = false;
  103. }
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. dispatch(setCalleeInfoVisible(calleeInfoVisible));
  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 = {
  130. id: localParticipant.id,
  131. local: true
  132. };
  133. if (avatarURL) {
  134. newProperties.avatarURL = avatarURL;
  135. }
  136. if (email) {
  137. newProperties.email = email;
  138. }
  139. if (name) {
  140. newProperties.name = name;
  141. }
  142. dispatch(participantUpdated(newProperties));
  143. }
  144. }
  145. /**
  146. * Notifies the feature jwt that the action {@link SET_CONFIG} or
  147. * {@link SET_LOCATION_URL} is being dispatched within a specific redux
  148. * {@code store}.
  149. *
  150. * @param {Store} store - The redux store in which the specified {@code action}
  151. * is being dispatched.
  152. * @param {Dispatch} next - The redux dispatch function to dispatch the
  153. * specified {@code action} to the specified {@code store}.
  154. * @param {Action} action - The redux action {@code SET_CONFIG} or
  155. * {@code SET_LOCATION_URL} which is being dispatched in the specified
  156. * {@code store}.
  157. * @private
  158. * @returns {Object} The new state that is the result of the reduction of the
  159. * specified {@code action}.
  160. */
  161. function _setConfigOrLocationURL({ dispatch, getState }, next, action) {
  162. const result = next(action);
  163. const { locationURL } = getState()['features/base/connection'];
  164. dispatch(
  165. setJWT(locationURL ? parseJWTFromURLParams(locationURL) : undefined));
  166. return result;
  167. }
  168. /**
  169. * Notifies the feature jwt that the action {@link SET_JWT} is being dispatched
  170. * within a specific redux {@code store}.
  171. *
  172. * @param {Store} store - The redux store in which the specified {@code action}
  173. * is being dispatched.
  174. * @param {Dispatch} next - The redux dispatch function to dispatch the
  175. * specified {@code action} to the specified {@code store}.
  176. * @param {Action} action - The redux action {@code SET_JWT} which is being
  177. * dispatched in the specified {@code store}.
  178. * @private
  179. * @returns {Object} The new state that is the result of the reduction of the
  180. * specified {@code action}.
  181. */
  182. function _setJWT(store, next, action) {
  183. // eslint-disable-next-line no-unused-vars
  184. const { jwt, type, ...actionPayload } = action;
  185. if (!Object.keys(actionPayload).length) {
  186. if (jwt) {
  187. const {
  188. enableUserRolesBasedOnToken
  189. } = store.getState()['features/base/config'];
  190. action.isGuest = !enableUserRolesBasedOnToken;
  191. const jwtPayload = jwtDecode(jwt);
  192. if (jwtPayload) {
  193. const { context, iss } = jwtPayload;
  194. action.jwt = jwt;
  195. action.issuer = iss;
  196. if (context) {
  197. const user = _user2participant(context.user);
  198. action.callee = context.callee;
  199. action.group = context.group;
  200. action.server = context.server;
  201. action.user = user;
  202. user && _overwriteLocalParticipant(store, user);
  203. }
  204. }
  205. } else if (typeof APP === 'undefined') {
  206. // The logic of restoring JWT overrides make sense only on mobile.
  207. // On Web it should eventually be restored from storage, but there's
  208. // no such use case yet.
  209. const { user } = store.getState()['features/base/jwt'];
  210. user && _undoOverwriteLocalParticipant(store, user);
  211. }
  212. }
  213. return _maybeSetCalleeInfoVisible(store, next, action);
  214. }
  215. /**
  216. * Undoes/resets the values overwritten by {@link _overwriteLocalParticipant}
  217. * by either clearing them or setting to default values. Only the values that
  218. * have not changed since the overwrite happened will be restored.
  219. *
  220. * NOTE Once it is possible to edit and save participant properties, this
  221. * function should restore values from the storage instead.
  222. *
  223. * @param {Store} store - The redux store.
  224. * @param {Object} localParticipant - The {@code Participant} structure used
  225. * previously to {@link _overwriteLocalParticipant}.
  226. * @private
  227. * @returns {void}
  228. */
  229. function _undoOverwriteLocalParticipant(
  230. { dispatch, getState },
  231. { avatarURL, name, email }) {
  232. let localParticipant;
  233. if ((avatarURL || name || email)
  234. && (localParticipant = getLocalParticipant(getState))) {
  235. const newProperties: Object = {
  236. id: localParticipant.id,
  237. local: true
  238. };
  239. if (avatarURL === localParticipant.avatarURL) {
  240. newProperties.avatarURL = undefined;
  241. }
  242. if (email === localParticipant.email) {
  243. newProperties.email = undefined;
  244. }
  245. if (name === localParticipant.name) {
  246. newProperties.name = undefined;
  247. }
  248. dispatch(participantUpdated(newProperties));
  249. }
  250. }
  251. /**
  252. * Converts the JWT {@code context.user} structure to the {@code Participant}
  253. * structure stored in the redux state base/participants.
  254. *
  255. * @param {Object} user - The JWT {@code context.user} structure to convert.
  256. * @private
  257. * @returns {{
  258. * avatarURL: ?string,
  259. * email: ?string,
  260. * id: ?string,
  261. * name: ?string
  262. * }}
  263. */
  264. function _user2participant({ avatar, avatarUrl, email, id, name }) {
  265. const participant = {};
  266. if (typeof avatarUrl === 'string') {
  267. participant.avatarURL = avatarUrl.trim();
  268. } else if (typeof avatar === 'string') {
  269. participant.avatarURL = avatar.trim();
  270. }
  271. if (typeof email === 'string') {
  272. participant.email = email.trim();
  273. }
  274. if (typeof id === 'string') {
  275. participant.id = id.trim();
  276. }
  277. if (typeof name === 'string') {
  278. participant.name = name.trim();
  279. }
  280. return Object.keys(participant).length ? participant : undefined;
  281. }