2017-06-07 15:50:30 +00:00
|
|
|
/* @flow */
|
|
|
|
|
|
|
|
import { NativeModules } from 'react-native';
|
|
|
|
|
|
|
|
import {
|
|
|
|
CONFERENCE_FAILED,
|
|
|
|
CONFERENCE_JOINED,
|
|
|
|
CONFERENCE_LEFT,
|
|
|
|
CONFERENCE_WILL_JOIN,
|
2017-08-01 00:40:55 +00:00
|
|
|
CONFERENCE_WILL_LEAVE,
|
|
|
|
JITSI_CONFERENCE_URL_KEY
|
2017-06-07 15:50:30 +00:00
|
|
|
} from '../../base/conference';
|
2017-08-25 15:21:01 +00:00
|
|
|
import { LOAD_CONFIG_ERROR } from '../../base/config';
|
2017-06-07 15:50:30 +00:00
|
|
|
import { MiddlewareRegistry } from '../../base/redux';
|
2017-08-01 00:40:55 +00:00
|
|
|
import { toURLString } from '../../base/util';
|
2017-06-07 15:50:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Middleware that captures Redux actions and uses the ExternalAPI module to
|
|
|
|
* turn them into native events so the application knows about them.
|
|
|
|
*
|
|
|
|
* @param {Store} store - Redux store.
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
MiddlewareRegistry.register(store => next => action => {
|
2017-06-09 04:59:31 +00:00
|
|
|
const result = next(action);
|
2017-06-07 15:50:30 +00:00
|
|
|
|
|
|
|
switch (action.type) {
|
2017-10-05 15:21:37 +00:00
|
|
|
case CONFERENCE_FAILED: {
|
|
|
|
const { error, ...data } = action;
|
|
|
|
|
|
|
|
// XXX Certain CONFERENCE_FAILED errors are recoverable i.e. they have
|
|
|
|
// prevented the user from joining a specific conference but the app may
|
|
|
|
// be able to eventually join the conference. For example, the app will
|
|
|
|
// ask the user for a password upon
|
|
|
|
// JitsiConferenceErrors.PASSWORD_REQUIRED and will retry joining the
|
|
|
|
// conference afterwards. Such errors are to not reach the native
|
|
|
|
// counterpart of the External API (or at least not in the
|
|
|
|
// fatality/finality semantics attributed to
|
|
|
|
// conferenceFailed:/onConferenceFailed).
|
|
|
|
if (!error.recoverable) {
|
|
|
|
_sendConferenceEvent(store, /* action */ {
|
|
|
|
error: _toErrorString(error),
|
|
|
|
...data
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-06-09 04:59:31 +00:00
|
|
|
case CONFERENCE_JOINED:
|
|
|
|
case CONFERENCE_LEFT:
|
|
|
|
case CONFERENCE_WILL_JOIN:
|
2017-10-05 15:21:37 +00:00
|
|
|
case CONFERENCE_WILL_LEAVE:
|
|
|
|
_sendConferenceEvent(store, action);
|
2017-06-07 15:50:30 +00:00
|
|
|
break;
|
2017-08-25 15:21:01 +00:00
|
|
|
|
|
|
|
case LOAD_CONFIG_ERROR: {
|
2017-09-06 23:26:33 +00:00
|
|
|
const { error, locationURL, type } = action;
|
2017-08-25 15:21:01 +00:00
|
|
|
|
2017-10-05 15:21:37 +00:00
|
|
|
_sendEvent(store, _getSymbolDescription(type), /* data */ {
|
|
|
|
error: _toErrorString(error),
|
2017-09-06 23:26:33 +00:00
|
|
|
url: toURLString(locationURL)
|
|
|
|
});
|
2017-08-25 15:21:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-06-07 15:50:30 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 04:59:31 +00:00
|
|
|
return result;
|
2017-06-07 15:50:30 +00:00
|
|
|
});
|
|
|
|
|
2017-10-05 15:21:37 +00:00
|
|
|
/**
|
|
|
|
* Returns a {@code String} representation of a specific error {@code Object}.
|
|
|
|
*
|
|
|
|
* @param {Error|Object|string} error - The error {@code Object} to return a
|
|
|
|
* {@code String} representation of.
|
|
|
|
* @returns {string} A {@code String} representation of the specified
|
|
|
|
* {@code error}.
|
|
|
|
*/
|
|
|
|
function _toErrorString(
|
|
|
|
error: Error | { message: ?string, name: ?string } | string) {
|
|
|
|
// XXX In lib-jitsi-meet and jitsi-meet we utilize errors in the form of
|
|
|
|
// strings, Error instances, and plain objects which resemble Error.
|
|
|
|
return (
|
|
|
|
error
|
|
|
|
? typeof error === 'string'
|
|
|
|
? error
|
|
|
|
: Error.prototype.toString(error)
|
|
|
|
: '');
|
|
|
|
}
|
|
|
|
|
2017-09-06 03:40:12 +00:00
|
|
|
/**
|
2017-10-01 06:35:19 +00:00
|
|
|
* Gets the description of a specific {@code Symbol}.
|
2017-09-06 03:40:12 +00:00
|
|
|
*
|
2017-10-01 06:35:19 +00:00
|
|
|
* @param {Symbol} symbol - The {@code Symbol} to retrieve the description of.
|
2017-09-06 03:40:12 +00:00
|
|
|
* @private
|
2017-10-01 06:35:19 +00:00
|
|
|
* @returns {string} The description of {@code symbol}.
|
2017-09-06 03:40:12 +00:00
|
|
|
*/
|
|
|
|
function _getSymbolDescription(symbol: Symbol) {
|
|
|
|
let description = symbol.toString();
|
|
|
|
|
|
|
|
if (description.startsWith('Symbol(') && description.endsWith(')')) {
|
|
|
|
description = description.slice(7, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The polyfill es6-symbol that we use does not appear to comply with the
|
|
|
|
// Symbol standard and, merely, adds @@ at the beginning of the description.
|
|
|
|
if (description.startsWith('@@')) {
|
|
|
|
description = description.slice(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return description;
|
2017-10-05 15:21:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends an event to the native counterpart of the External API for a specific
|
|
|
|
* conference-related redux action.
|
|
|
|
*
|
|
|
|
* @param {Store} store - The redux store.
|
|
|
|
* @param {Action} action - The redux action.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function _sendConferenceEvent(
|
|
|
|
store: Object,
|
|
|
|
{ conference, type, ...data }: { conference: Object, type: Symbol }) {
|
|
|
|
// For these (redux) actions, conference identifies a JitsiConference
|
|
|
|
// instance. The external API cannot transport such an object so we have to
|
|
|
|
// transport an "equivalent".
|
|
|
|
if (conference) {
|
|
|
|
data.url = toURLString(conference[JITSI_CONFERENCE_URL_KEY]);
|
|
|
|
}
|
|
|
|
|
|
|
|
_sendEvent(store, _getSymbolDescription(type), data);
|
2017-09-06 03:40:12 +00:00
|
|
|
}
|
|
|
|
|
2017-06-07 15:50:30 +00:00
|
|
|
/**
|
2017-06-09 04:59:31 +00:00
|
|
|
* Sends a specific event to the native counterpart of the External API. Native
|
|
|
|
* apps may listen to such events via the mechanisms provided by the (native)
|
|
|
|
* mobile Jitsi Meet SDK.
|
2017-06-07 15:50:30 +00:00
|
|
|
*
|
2017-10-04 22:36:09 +00:00
|
|
|
* @param {Object} store - The redux store.
|
2017-06-09 04:59:31 +00:00
|
|
|
* @param {string} name - The name of the event to send.
|
|
|
|
* @param {Object} data - The details/specifics of the event to send determined
|
|
|
|
* by/associated with the specified {@code name}.
|
2017-06-07 15:50:30 +00:00
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-10-04 22:36:09 +00:00
|
|
|
function _sendEvent(
|
|
|
|
{ getState }: { getState: Function },
|
|
|
|
name: string,
|
|
|
|
data: Object) {
|
2017-06-10 00:17:01 +00:00
|
|
|
// The JavaScript App needs to provide uniquely identifying information
|
|
|
|
// to the native ExternalAPI module so that the latter may match the former
|
|
|
|
// to the native JitsiMeetView which hosts it.
|
2017-10-04 22:36:09 +00:00
|
|
|
const { app } = getState()['features/app'];
|
2017-06-10 00:17:01 +00:00
|
|
|
|
|
|
|
if (app) {
|
|
|
|
const { externalAPIScope } = app.props;
|
|
|
|
|
|
|
|
if (externalAPIScope) {
|
|
|
|
NativeModules.ExternalAPI.sendEvent(name, data, externalAPIScope);
|
|
|
|
}
|
|
|
|
}
|
2017-06-07 15:50:30 +00:00
|
|
|
}
|