2017-04-21 10:00:50 +00:00
|
|
|
import jwtDecode from 'jwt-decode';
|
|
|
|
|
2017-06-05 18:19:25 +00:00
|
|
|
import {
|
|
|
|
CONFERENCE_FAILED,
|
|
|
|
CONFERENCE_LEFT,
|
|
|
|
CONFERENCE_WILL_LEAVE,
|
|
|
|
SET_ROOM
|
|
|
|
} from '../base/conference';
|
2017-05-26 22:11:33 +00:00
|
|
|
import { SET_CONFIG } from '../base/config';
|
2017-05-09 05:15:43 +00:00
|
|
|
import { SET_LOCATION_URL } from '../base/connection';
|
2017-06-05 18:19:25 +00:00
|
|
|
import { LIB_INIT_ERROR } from '../base/lib-jitsi-meet';
|
2017-06-27 22:56:55 +00:00
|
|
|
import {
|
|
|
|
getLocalParticipant,
|
|
|
|
getParticipantCount,
|
|
|
|
PARTICIPANT_JOINED
|
|
|
|
} from '../base/participants';
|
2017-04-21 10:00:50 +00:00
|
|
|
import { MiddlewareRegistry } from '../base/redux';
|
|
|
|
|
2017-06-05 18:19:25 +00:00
|
|
|
import { setCallOverlayVisible, setJWT } from './actions';
|
2017-04-21 10:00:50 +00:00
|
|
|
import { SET_JWT } from './actionTypes';
|
2017-05-26 22:11:33 +00:00
|
|
|
import { parseJWTFromURLParams } from './functions';
|
2017-04-21 10:00:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware to parse token data upon setting a new room URL.
|
|
|
|
*
|
2017-06-05 18:19:25 +00:00
|
|
|
* @param {Store} store - The redux store.
|
2017-04-21 10:00:50 +00:00
|
|
|
* @private
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
|
|
|
switch (action.type) {
|
2017-06-05 18:19:25 +00:00
|
|
|
case CONFERENCE_FAILED:
|
|
|
|
case CONFERENCE_LEFT:
|
|
|
|
case CONFERENCE_WILL_LEAVE:
|
|
|
|
case LIB_INIT_ERROR:
|
|
|
|
case PARTICIPANT_JOINED:
|
|
|
|
case SET_ROOM:
|
|
|
|
return _maybeSetCallOverlayVisible(store, next, action);
|
|
|
|
|
2017-04-21 10:00:50 +00:00
|
|
|
case SET_CONFIG:
|
2017-05-09 05:15:43 +00:00
|
|
|
case SET_LOCATION_URL:
|
2017-04-21 10:00:50 +00:00
|
|
|
// XXX The JSON Web Token (JWT) is not the only piece of state that we
|
|
|
|
// have decided to store in the feature jwt, there is isGuest as well
|
|
|
|
// which depends on the states of the features base/config and jwt. So
|
2017-05-09 05:15:43 +00:00
|
|
|
// the JSON Web Token comes from the conference/room's URL and isGuest
|
|
|
|
// needs a recalculation upon SET_CONFIG as well.
|
|
|
|
return _setConfigOrLocationURL(store, next, action);
|
2017-04-21 10:00:50 +00:00
|
|
|
|
|
|
|
case SET_JWT:
|
|
|
|
return _setJWT(store, next, action);
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(action);
|
|
|
|
});
|
|
|
|
|
2017-06-05 18:19:25 +00:00
|
|
|
/**
|
|
|
|
* Notifies the feature jwt that a specific {@code action} is being dispatched
|
|
|
|
* within a specific redux {@code store} which may have an effect on the
|
|
|
|
* visiblity of (the) {@code CallOverlay}.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
|
|
|
* is being dispatched.
|
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
|
|
|
* specified {@code action} to the specified {@code store}.
|
|
|
|
* @param {Action} action - The redux action which is being dispatched in the
|
|
|
|
* specified {@code store}.
|
|
|
|
* @private
|
|
|
|
* @returns {Object} The new state that is the result of the reduction of the
|
|
|
|
* specified {@code action}.
|
|
|
|
*/
|
|
|
|
function _maybeSetCallOverlayVisible({ dispatch, getState }, next, action) {
|
|
|
|
const result = next(action);
|
|
|
|
|
|
|
|
const state = getState();
|
|
|
|
const stateFeaturesJWT = state['features/jwt'];
|
|
|
|
let callOverlayVisible;
|
|
|
|
|
|
|
|
if (stateFeaturesJWT.callee) {
|
|
|
|
const { conference, leaving, room } = state['features/base/conference'];
|
|
|
|
|
|
|
|
// XXX The CallOverlay is to be displayed/visible as soon as
|
|
|
|
// possible including even before the conference is joined.
|
|
|
|
if (room && (!conference || conference !== leaving)) {
|
|
|
|
switch (action.type) {
|
|
|
|
case CONFERENCE_FAILED:
|
|
|
|
case CONFERENCE_LEFT:
|
|
|
|
case CONFERENCE_WILL_LEAVE:
|
|
|
|
case LIB_INIT_ERROR:
|
|
|
|
// Because the CallOverlay is to be displayed/visible as soon as
|
|
|
|
// possible even before the connection is established and/or the
|
|
|
|
// conference is joined, it is very complicated to figure out
|
|
|
|
// based on the current state alone. In order to reduce the
|
|
|
|
// risks of displaying the CallOverly at inappropirate times, do
|
|
|
|
// not even attempt to figure out based on the current state.
|
|
|
|
// The (redux) actions listed above are also the equivalents of
|
|
|
|
// the execution ponints at which APP.UI.hideRingOverlay() used
|
|
|
|
// to be invoked.
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: {
|
|
|
|
// The CallOverlay it to no longer be displayed/visible as soon
|
|
|
|
// as another participant joins.
|
2017-06-27 22:56:55 +00:00
|
|
|
callOverlayVisible = getParticipantCount(state) === 1
|
|
|
|
&& Boolean(getLocalParticipant(state));
|
2017-06-05 18:19:25 +00:00
|
|
|
|
|
|
|
// However, the CallDialog is not to be displayed/visible again
|
|
|
|
// after all remote participants leave.
|
|
|
|
if (callOverlayVisible
|
|
|
|
&& stateFeaturesJWT.callOverlayVisible === false) {
|
|
|
|
callOverlayVisible = false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dispatch(setCallOverlayVisible(callOverlayVisible));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-21 10:00:50 +00:00
|
|
|
/**
|
|
|
|
* Notifies the feature jwt that the action {@link SET_CONFIG} or
|
2017-06-05 18:19:25 +00:00
|
|
|
* {@link SET_LOCATION_URL} is being dispatched within a specific redux
|
2017-04-21 10:00:50 +00:00
|
|
|
* {@code store}.
|
|
|
|
*
|
2017-06-05 18:19:25 +00:00
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
2017-04-21 10:00:50 +00:00
|
|
|
* is being dispatched.
|
2017-06-05 18:19:25 +00:00
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
2017-04-21 10:00:50 +00:00
|
|
|
* specified {@code action} to the specified {@code store}.
|
2017-06-05 18:19:25 +00:00
|
|
|
* @param {Action} action - The redux action {@code SET_CONFIG} or
|
2017-05-09 05:15:43 +00:00
|
|
|
* {@code SET_LOCATION_URL} which is being dispatched in the specified
|
2017-04-21 10:00:50 +00:00
|
|
|
* {@code store}.
|
|
|
|
* @private
|
|
|
|
* @returns {Object} The new state that is the result of the reduction of the
|
|
|
|
* specified {@code action}.
|
|
|
|
*/
|
2017-05-09 05:15:43 +00:00
|
|
|
function _setConfigOrLocationURL({ dispatch, getState }, next, action) {
|
2017-04-21 10:00:50 +00:00
|
|
|
const result = next(action);
|
|
|
|
|
2017-05-09 05:15:43 +00:00
|
|
|
const { locationURL } = getState()['features/base/connection'];
|
2017-04-21 10:00:50 +00:00
|
|
|
let jwt;
|
|
|
|
|
2017-05-09 05:15:43 +00:00
|
|
|
if (locationURL) {
|
2017-05-26 22:11:33 +00:00
|
|
|
jwt = parseJWTFromURLParams(locationURL);
|
2017-04-21 10:00:50 +00:00
|
|
|
}
|
|
|
|
dispatch(setJWT(jwt));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies the feature jwt that the action {@link SET_JWT} is being dispatched
|
2017-06-05 18:19:25 +00:00
|
|
|
* within a specific redux {@code store}.
|
2017-04-21 10:00:50 +00:00
|
|
|
*
|
2017-06-05 18:19:25 +00:00
|
|
|
* @param {Store} store - The redux store in which the specified {@code action}
|
2017-04-21 10:00:50 +00:00
|
|
|
* is being dispatched.
|
2017-06-05 18:19:25 +00:00
|
|
|
* @param {Dispatch} next - The redux dispatch function to dispatch the
|
2017-04-21 10:00:50 +00:00
|
|
|
* specified {@code action} to the specified {@code store}.
|
2017-06-05 18:19:25 +00:00
|
|
|
* @param {Action} action - The redux action {@code SET_JWT} which is being
|
2017-04-21 10:00:50 +00:00
|
|
|
* dispatched in the specified {@code store}.
|
|
|
|
* @private
|
|
|
|
* @returns {Object} The new state that is the result of the reduction of the
|
|
|
|
* specified {@code action}.
|
|
|
|
*/
|
2017-06-05 18:19:25 +00:00
|
|
|
function _setJWT(store, next, action) {
|
2017-04-21 10:00:50 +00:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
const { jwt, type, ...actionPayload } = action;
|
|
|
|
|
|
|
|
if (jwt && !Object.keys(actionPayload).length) {
|
|
|
|
const {
|
|
|
|
enableUserRolesBasedOnToken
|
2017-06-05 18:19:25 +00:00
|
|
|
} = store.getState()['features/base/config'];
|
2017-04-21 10:00:50 +00:00
|
|
|
|
|
|
|
action.isGuest = !enableUserRolesBasedOnToken;
|
|
|
|
|
|
|
|
const jwtPayload = jwtDecode(jwt);
|
|
|
|
|
|
|
|
if (jwtPayload) {
|
|
|
|
const { context, iss } = jwtPayload;
|
|
|
|
|
2017-06-14 18:41:22 +00:00
|
|
|
action.jwt = jwt;
|
2017-04-21 10:00:50 +00:00
|
|
|
action.issuer = iss;
|
|
|
|
if (context) {
|
|
|
|
action.callee = context.callee;
|
|
|
|
action.caller = context.user;
|
|
|
|
action.group = context.group;
|
|
|
|
action.server = context.server;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 18:19:25 +00:00
|
|
|
return _maybeSetCallOverlayVisible(store, next, action);
|
2017-04-21 10:00:50 +00:00
|
|
|
}
|