2017-10-17 22:10:42 +00:00
|
|
|
// @flow
|
2017-02-03 18:50:06 +00:00
|
|
|
|
2017-07-21 21:12:02 +00:00
|
|
|
import _ from 'lodash';
|
2017-02-03 18:50:06 +00:00
|
|
|
import type { Dispatch } from 'redux';
|
|
|
|
|
2018-05-24 20:15:32 +00:00
|
|
|
import {
|
|
|
|
conferenceLeft,
|
|
|
|
conferenceWillLeave,
|
|
|
|
getCurrentConference
|
|
|
|
} from '../conference';
|
2017-02-28 03:21:50 +00:00
|
|
|
import JitsiMeetJS, { JitsiConnectionEvents } from '../lib-jitsi-meet';
|
2018-08-17 04:03:15 +00:00
|
|
|
import { parseURIString } from '../util';
|
2016-12-05 15:14:50 +00:00
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
import {
|
|
|
|
CONNECTION_DISCONNECTED,
|
|
|
|
CONNECTION_ESTABLISHED,
|
|
|
|
CONNECTION_FAILED,
|
2017-07-06 11:51:35 +00:00
|
|
|
CONNECTION_WILL_CONNECT,
|
2017-05-09 05:15:43 +00:00
|
|
|
SET_LOCATION_URL
|
2016-10-05 14:36:59 +00:00
|
|
|
} from './actionTypes';
|
|
|
|
|
2018-05-17 14:53:51 +00:00
|
|
|
const logger = require('jitsi-meet-logger').getLogger(__filename);
|
|
|
|
|
2018-05-02 18:27:50 +00:00
|
|
|
/**
|
|
|
|
* The error structure passed to the {@link connectionFailed} action.
|
|
|
|
*
|
|
|
|
* Note there was an intention to make the error resemble an Error instance (to
|
|
|
|
* the extent that jitsi-meet needs it).
|
|
|
|
*/
|
|
|
|
export type ConnectionFailedError = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The invalid credentials that were used to authenticate and the
|
|
|
|
* authentication failed.
|
|
|
|
*/
|
|
|
|
credentials?: {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The XMPP user's ID.
|
|
|
|
*/
|
|
|
|
jid: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The XMPP user's password.
|
|
|
|
*/
|
|
|
|
password: string
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The details about the connection failed event.
|
|
|
|
*/
|
2018-07-02 21:22:51 +00:00
|
|
|
details?: Object,
|
2018-05-02 18:27:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Error message.
|
|
|
|
*/
|
|
|
|
message?: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* One of {@link JitsiConnectionError} constants (defined in
|
|
|
|
* lib-jitsi-meet).
|
|
|
|
*/
|
|
|
|
name: string,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether this event is recoverable or not.
|
|
|
|
*/
|
|
|
|
recoverable?: boolean
|
2018-05-21 03:58:34 +00:00
|
|
|
};
|
2018-05-02 18:27:50 +00:00
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Opens new connection.
|
|
|
|
*
|
2018-11-08 12:25:02 +00:00
|
|
|
* @param {string} [id] - The XMPP user's ID (e.g. {@code user@server.com}).
|
2017-09-18 07:09:43 +00:00
|
|
|
* @param {string} [password] - The XMPP user's password.
|
2017-02-03 18:50:06 +00:00
|
|
|
* @returns {Function}
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2017-09-18 07:09:43 +00:00
|
|
|
export function connect(id: ?string, password: ?string) {
|
2017-02-03 18:50:06 +00:00
|
|
|
return (dispatch: Dispatch<*>, getState: Function) => {
|
2016-10-05 14:36:59 +00:00
|
|
|
const state = getState();
|
2017-07-21 21:12:02 +00:00
|
|
|
const options = _constructOptions(state);
|
2017-10-05 22:54:13 +00:00
|
|
|
const { issuer, jwt } = state['features/base/jwt'];
|
2016-12-05 15:14:50 +00:00
|
|
|
const connection
|
|
|
|
= new JitsiMeetJS.JitsiConnection(
|
2017-02-23 23:12:50 +00:00
|
|
|
options.appId,
|
2017-05-04 15:20:41 +00:00
|
|
|
jwt && issuer && issuer !== 'anonymous' ? jwt : undefined,
|
2017-07-05 20:23:10 +00:00
|
|
|
options);
|
2016-12-05 15:14:50 +00:00
|
|
|
|
2017-07-06 11:51:35 +00:00
|
|
|
dispatch(_connectionWillConnect(connection));
|
|
|
|
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.addEventListener(
|
2017-06-15 00:40:51 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_DISCONNECTED,
|
|
|
|
_onConnectionDisconnected);
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.addEventListener(
|
2017-06-15 00:40:51 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_ESTABLISHED,
|
|
|
|
_onConnectionEstablished);
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.addEventListener(
|
2017-06-15 00:40:51 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_FAILED,
|
|
|
|
_onConnectionFailed);
|
2016-12-05 15:14:50 +00:00
|
|
|
|
2017-09-22 20:15:06 +00:00
|
|
|
return connection.connect({
|
2017-09-18 07:09:43 +00:00
|
|
|
id,
|
2017-09-08 13:36:42 +00:00
|
|
|
password
|
|
|
|
});
|
2016-12-05 15:14:50 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-21 03:58:34 +00:00
|
|
|
* Dispatches {@code CONNECTION_DISCONNECTED} action when connection is
|
2016-12-05 15:14:50 +00:00
|
|
|
* disconnected.
|
|
|
|
*
|
|
|
|
* @param {string} message - Disconnect reason.
|
2017-01-31 20:58:48 +00:00
|
|
|
* @private
|
2017-09-24 21:51:43 +00:00
|
|
|
* @returns {void}
|
2016-12-05 15:14:50 +00:00
|
|
|
*/
|
2017-01-31 20:58:48 +00:00
|
|
|
function _onConnectionDisconnected(message: string) {
|
2018-05-23 11:43:40 +00:00
|
|
|
unsubscribe();
|
2016-12-05 15:14:50 +00:00
|
|
|
dispatch(_connectionDisconnected(connection, message));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves external promise when connection is established.
|
|
|
|
*
|
2017-01-31 20:58:48 +00:00
|
|
|
* @private
|
2017-09-24 21:51:43 +00:00
|
|
|
* @returns {void}
|
2016-12-05 15:14:50 +00:00
|
|
|
*/
|
2017-01-31 20:58:48 +00:00
|
|
|
function _onConnectionEstablished() {
|
2018-05-23 11:43:40 +00:00
|
|
|
connection.removeEventListener(
|
|
|
|
JitsiConnectionEvents.CONNECTION_ESTABLISHED,
|
|
|
|
_onConnectionEstablished);
|
2018-07-02 21:22:51 +00:00
|
|
|
dispatch(connectionEstablished(connection, Date.now()));
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rejects external promise when connection fails.
|
|
|
|
*
|
|
|
|
* @param {JitsiConnectionErrors} err - Connection error.
|
2017-09-22 18:18:16 +00:00
|
|
|
* @param {string} [msg] - Error message supplied by lib-jitsi-meet.
|
|
|
|
* @param {Object} [credentials] - The invalid credentials that were
|
|
|
|
* used to authenticate and the authentication failed.
|
|
|
|
* @param {string} [credentials.jid] - The XMPP user's ID.
|
|
|
|
* @param {string} [credentials.password] - The XMPP user's password.
|
2018-07-02 21:22:51 +00:00
|
|
|
* @param {Object} details - Additional information about the error.
|
2017-01-31 20:58:48 +00:00
|
|
|
* @private
|
2017-09-24 21:51:43 +00:00
|
|
|
* @returns {void}
|
2016-12-05 15:14:50 +00:00
|
|
|
*/
|
2018-07-02 21:22:51 +00:00
|
|
|
function _onConnectionFailed( // eslint-disable-line max-params
|
|
|
|
err: string,
|
|
|
|
msg: string,
|
|
|
|
credentials: Object,
|
|
|
|
details: Object) {
|
2016-12-05 15:14:50 +00:00
|
|
|
unsubscribe();
|
2018-05-02 18:27:50 +00:00
|
|
|
dispatch(
|
|
|
|
connectionFailed(
|
|
|
|
connection, {
|
|
|
|
credentials,
|
2018-07-02 21:22:51 +00:00
|
|
|
details,
|
2018-05-02 18:27:50 +00:00
|
|
|
name: err,
|
|
|
|
message: msg
|
|
|
|
}
|
|
|
|
));
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-23 11:43:40 +00:00
|
|
|
* Unsubscribe the connection instance from
|
|
|
|
* {@code CONNECTION_DISCONNECTED} and {@code CONNECTION_FAILED} events.
|
2016-12-05 15:14:50 +00:00
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function unsubscribe() {
|
|
|
|
connection.removeEventListener(
|
2018-05-23 11:43:40 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_DISCONNECTED,
|
|
|
|
_onConnectionDisconnected);
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.removeEventListener(
|
2017-06-15 00:40:51 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_FAILED,
|
|
|
|
_onConnectionFailed);
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
2016-10-05 14:36:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an action for when the signaling connection has been lost.
|
|
|
|
*
|
2018-05-21 03:58:34 +00:00
|
|
|
* @param {JitsiConnection} connection - The {@code JitsiConnection} which
|
|
|
|
* disconnected.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @param {string} message - Error message.
|
|
|
|
* @private
|
|
|
|
* @returns {{
|
|
|
|
* type: CONNECTION_DISCONNECTED,
|
|
|
|
* connection: JitsiConnection,
|
|
|
|
* message: string
|
|
|
|
* }}
|
|
|
|
*/
|
2017-05-09 05:15:43 +00:00
|
|
|
function _connectionDisconnected(connection: Object, message: string) {
|
2016-10-05 14:36:59 +00:00
|
|
|
return {
|
|
|
|
type: CONNECTION_DISCONNECTED,
|
|
|
|
connection,
|
|
|
|
message
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an action for when the signaling connection has been established.
|
|
|
|
*
|
2018-05-21 03:58:34 +00:00
|
|
|
* @param {JitsiConnection} connection - The {@code JitsiConnection} which was
|
2016-12-05 15:14:50 +00:00
|
|
|
* established.
|
2018-07-02 21:22:51 +00:00
|
|
|
* @param {number} timeEstablished - The time at which the
|
|
|
|
* {@code JitsiConnection} which was established.
|
2017-05-09 05:15:43 +00:00
|
|
|
* @public
|
2016-12-05 15:14:50 +00:00
|
|
|
* @returns {{
|
|
|
|
* type: CONNECTION_ESTABLISHED,
|
2018-07-02 21:22:51 +00:00
|
|
|
* connection: JitsiConnection,
|
|
|
|
* timeEstablished: number
|
2016-12-05 15:14:50 +00:00
|
|
|
* }}
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2018-07-02 21:22:51 +00:00
|
|
|
export function connectionEstablished(
|
|
|
|
connection: Object, timeEstablished: number) {
|
2016-10-05 14:36:59 +00:00
|
|
|
return {
|
|
|
|
type: CONNECTION_ESTABLISHED,
|
2018-07-02 21:22:51 +00:00
|
|
|
connection,
|
|
|
|
timeEstablished
|
2016-10-05 14:36:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an action for when the signaling connection could not be created.
|
|
|
|
*
|
2018-05-21 03:58:34 +00:00
|
|
|
* @param {JitsiConnection} connection - The {@code JitsiConnection} which
|
|
|
|
* failed.
|
2018-05-02 18:27:50 +00:00
|
|
|
* @param {ConnectionFailedError} error - Error.
|
2017-05-09 05:15:43 +00:00
|
|
|
* @public
|
2016-12-05 15:14:50 +00:00
|
|
|
* @returns {{
|
|
|
|
* type: CONNECTION_FAILED,
|
|
|
|
* connection: JitsiConnection,
|
2018-05-02 18:27:50 +00:00
|
|
|
* error: ConnectionFailedError
|
2016-12-05 15:14:50 +00:00
|
|
|
* }}
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2017-01-31 20:58:48 +00:00
|
|
|
export function connectionFailed(
|
2017-05-09 05:15:43 +00:00
|
|
|
connection: Object,
|
2018-05-02 18:27:50 +00:00
|
|
|
error: ConnectionFailedError) {
|
|
|
|
const { credentials } = error;
|
|
|
|
|
|
|
|
if (credentials && !Object.keys(credentials).length) {
|
|
|
|
error.credentials = undefined;
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
return {
|
|
|
|
type: CONNECTION_FAILED,
|
2016-12-05 15:14:50 +00:00
|
|
|
connection,
|
2018-05-02 18:27:50 +00:00
|
|
|
error
|
2017-05-09 05:15:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-21 03:58:34 +00:00
|
|
|
/**
|
|
|
|
* Create an action for when a connection will connect.
|
|
|
|
*
|
|
|
|
* @param {JitsiConnection} connection - The {@code JitsiConnection} which will
|
|
|
|
* connect.
|
|
|
|
* @private
|
|
|
|
* @returns {{
|
|
|
|
* type: CONNECTION_WILL_CONNECT,
|
|
|
|
* connection: JitsiConnection
|
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
function _connectionWillConnect(connection) {
|
|
|
|
return {
|
|
|
|
type: CONNECTION_WILL_CONNECT,
|
|
|
|
connection
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:12:02 +00:00
|
|
|
/**
|
|
|
|
* Constructs options to be passed to the constructor of {@code JitsiConnection}
|
|
|
|
* based on the redux state.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The redux state.
|
|
|
|
* @returns {Object} The options to be passed to the constructor of
|
|
|
|
* {@code JitsiConnection}.
|
|
|
|
*/
|
|
|
|
function _constructOptions(state) {
|
2018-08-17 04:03:15 +00:00
|
|
|
// Deep clone the options to make sure we don't modify the object in the
|
|
|
|
// redux store.
|
|
|
|
const options = _.cloneDeep(state['features/base/config']);
|
|
|
|
|
|
|
|
// Normalize the BOSH URL.
|
2017-07-21 21:12:02 +00:00
|
|
|
let { bosh } = options;
|
|
|
|
|
|
|
|
if (bosh) {
|
2018-08-17 04:03:15 +00:00
|
|
|
if (bosh.startsWith('//')) {
|
|
|
|
// By default our config.js doesn't include the protocol.
|
|
|
|
const { locationURL } = state['features/base/connection'];
|
|
|
|
|
|
|
|
bosh = `${locationURL.protocol}${bosh}`;
|
|
|
|
} else if (bosh.startsWith('/')) {
|
|
|
|
// Handle relative URLs, which won't work on mobile.
|
|
|
|
const { locationURL } = state['features/base/connection'];
|
|
|
|
const {
|
|
|
|
protocol,
|
|
|
|
hostname,
|
|
|
|
contextRoot
|
|
|
|
} = parseURIString(locationURL.href);
|
|
|
|
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
bosh = `${protocol}//${hostname}${contextRoot || '/'}${bosh.substr(1)}`;
|
|
|
|
}
|
|
|
|
|
2017-07-21 21:12:02 +00:00
|
|
|
// Append room to the URL's search.
|
|
|
|
const { room } = state['features/base/conference'];
|
|
|
|
|
|
|
|
// XXX The Jitsi Meet deployments require the room argument to be in
|
|
|
|
// lower case at the time of this writing but, unfortunately, they do
|
|
|
|
// not ignore case themselves.
|
|
|
|
room && (bosh += `?room=${room.toLowerCase()}`);
|
|
|
|
|
|
|
|
options.bosh = bosh;
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2017-05-09 05:15:43 +00:00
|
|
|
/**
|
|
|
|
* Closes connection.
|
|
|
|
*
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function disconnect() {
|
2017-12-01 21:07:58 +00:00
|
|
|
return (dispatch: Dispatch<*>, getState: Function): Promise<void> => {
|
2017-05-09 05:15:43 +00:00
|
|
|
const state = getState();
|
2017-07-06 11:51:35 +00:00
|
|
|
|
2017-10-17 22:10:42 +00:00
|
|
|
// The conference we have already joined or are joining.
|
2018-05-24 20:15:32 +00:00
|
|
|
const conference_ = getCurrentConference(state);
|
2017-05-09 05:15:43 +00:00
|
|
|
|
2017-07-06 11:51:35 +00:00
|
|
|
// Promise which completes when the conference has been left and the
|
|
|
|
// connection has been disconnected.
|
2017-05-09 05:15:43 +00:00
|
|
|
let promise;
|
|
|
|
|
|
|
|
// Leave the conference.
|
2017-07-07 17:21:08 +00:00
|
|
|
if (conference_) {
|
2017-05-09 05:15:43 +00:00
|
|
|
// In a fashion similar to JitsiConference's CONFERENCE_LEFT event
|
|
|
|
// (and the respective Redux action) which is fired after the
|
|
|
|
// conference has been left, notify the application about the
|
|
|
|
// intention to leave the conference.
|
2017-07-07 17:21:08 +00:00
|
|
|
dispatch(conferenceWillLeave(conference_));
|
2017-05-09 05:15:43 +00:00
|
|
|
|
2018-03-05 01:25:23 +00:00
|
|
|
promise
|
|
|
|
= conference_.leave()
|
2018-05-17 14:53:51 +00:00
|
|
|
.catch(error => {
|
|
|
|
logger.warn(
|
2018-05-21 03:58:34 +00:00
|
|
|
'JitsiConference.leave() rejected with:',
|
2018-05-17 14:53:51 +00:00
|
|
|
error);
|
|
|
|
|
2018-03-05 01:25:23 +00:00
|
|
|
// The library lib-jitsi-meet failed to make the
|
|
|
|
// JitsiConference leave. Which may be because
|
|
|
|
// JitsiConference thinks it has already left.
|
|
|
|
// Regardless of the failure reason, continue in
|
|
|
|
// jitsi-meet as if the leave has succeeded.
|
|
|
|
dispatch(conferenceLeft(conference_));
|
|
|
|
});
|
2017-05-09 05:15:43 +00:00
|
|
|
} else {
|
|
|
|
promise = Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disconnect the connection.
|
2017-07-07 17:21:08 +00:00
|
|
|
const { connecting, connection } = state['features/base/connection'];
|
|
|
|
|
2017-10-17 22:10:42 +00:00
|
|
|
// The connection we have already connected or are connecting.
|
2017-07-07 17:21:08 +00:00
|
|
|
const connection_ = connection || connecting;
|
|
|
|
|
|
|
|
if (connection_) {
|
|
|
|
promise = promise.then(() => connection_.disconnect());
|
2017-05-09 05:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the location URL of the application, connecton, conference, etc.
|
|
|
|
*
|
|
|
|
* @param {URL} [locationURL] - The location URL of the application,
|
|
|
|
* connection, conference, etc.
|
|
|
|
* @returns {{
|
|
|
|
* type: SET_LOCATION_URL,
|
|
|
|
* locationURL: URL
|
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
export function setLocationURL(locationURL: ?URL) {
|
|
|
|
return {
|
|
|
|
type: SET_LOCATION_URL,
|
|
|
|
locationURL
|
2016-10-05 14:36:59 +00:00
|
|
|
};
|
|
|
|
}
|