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

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