2017-02-03 18:50:06 +00:00
|
|
|
/* @flow */
|
|
|
|
|
2017-07-21 21:12:02 +00:00
|
|
|
import _ from 'lodash';
|
2017-02-03 18:50:06 +00:00
|
|
|
import type { Dispatch } from 'redux';
|
|
|
|
|
2016-12-05 15:14:50 +00:00
|
|
|
import { conferenceWillLeave } from '../conference';
|
2017-02-28 03:21:50 +00:00
|
|
|
import JitsiMeetJS, { JitsiConnectionEvents } from '../lib-jitsi-meet';
|
2017-07-21 21:12:02 +00:00
|
|
|
import { parseStandardURIString } 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';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens new connection.
|
|
|
|
*
|
2017-09-18 07:09:43 +00:00
|
|
|
* @param {string} [id] - The XMPP user's ID (e.g. user@server.com).
|
|
|
|
* @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-05-04 15:20:41 +00:00
|
|
|
const { issuer, jwt } = state['features/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
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches CONNECTION_DISCONNECTED action when connection is
|
|
|
|
* 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) {
|
2016-12-05 15:14:50 +00:00
|
|
|
connection.removeEventListener(
|
2017-06-15 00:40:51 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_DISCONNECTED,
|
|
|
|
_onConnectionDisconnected);
|
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() {
|
2016-12-05 15:14:50 +00:00
|
|
|
unsubscribe();
|
2017-01-31 20:58:48 +00:00
|
|
|
dispatch(connectionEstablished(connection));
|
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.
|
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-09-22 18:18:16 +00:00
|
|
|
function _onConnectionFailed(err, msg, credentials) {
|
2016-12-05 15:14:50 +00:00
|
|
|
unsubscribe();
|
2017-09-19 19:14:17 +00:00
|
|
|
console.error('CONNECTION FAILED:', err, msg);
|
2017-09-22 18:18:16 +00:00
|
|
|
dispatch(connectionFailed(connection, err, msg, credentials));
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsubscribes connection instance from CONNECTION_ESTABLISHED
|
|
|
|
* and CONNECTION_FAILED events.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function unsubscribe() {
|
|
|
|
connection.removeEventListener(
|
2017-06-15 00:40:51 +00:00
|
|
|
JitsiConnectionEvents.CONNECTION_ESTABLISHED,
|
|
|
|
_onConnectionEstablished);
|
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.
|
|
|
|
*
|
2016-12-05 15:14:50 +00:00
|
|
|
* @param {JitsiConnection} connection - The 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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-06 11:51:35 +00:00
|
|
|
/**
|
|
|
|
* Create an action for when a connection will connect.
|
|
|
|
*
|
|
|
|
* @param {JitsiConnection} connection - The JitsiConnection which will connect.
|
|
|
|
* @private
|
|
|
|
* @returns {{
|
|
|
|
* type: CONNECTION_WILL_CONNECT,
|
|
|
|
* connection: JitsiConnection
|
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
function _connectionWillConnect(connection) {
|
|
|
|
return {
|
|
|
|
type: CONNECTION_WILL_CONNECT,
|
|
|
|
connection
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Create an action for when the signaling connection has been established.
|
|
|
|
*
|
2016-12-05 15:14:50 +00:00
|
|
|
* @param {JitsiConnection} connection - The 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,
|
|
|
|
* connection: JitsiConnection
|
|
|
|
* }}
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2017-01-31 20:58:48 +00:00
|
|
|
export function connectionEstablished(connection: Object) {
|
2016-10-05 14:36:59 +00:00
|
|
|
return {
|
|
|
|
type: CONNECTION_ESTABLISHED,
|
|
|
|
connection
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-22 18:18:16 +00:00
|
|
|
/* eslint-disable max-params */
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
|
|
|
* Create an action for when the signaling connection could not be created.
|
|
|
|
*
|
2016-12-05 15:14:50 +00:00
|
|
|
* @param {JitsiConnection} connection - The JitsiConnection which failed.
|
2017-01-31 20:58:48 +00:00
|
|
|
* @param {string} error - Error.
|
2017-09-22 18:18:16 +00:00
|
|
|
* @param {string} [message] - Error message.
|
|
|
|
* @param {Object} [credentials] - The invalid credentials that failed
|
|
|
|
* the authentication.
|
2017-05-09 05:15:43 +00:00
|
|
|
* @public
|
2016-12-05 15:14:50 +00:00
|
|
|
* @returns {{
|
|
|
|
* type: CONNECTION_FAILED,
|
|
|
|
* connection: JitsiConnection,
|
2017-09-24 21:51:43 +00:00
|
|
|
* error: Object
|
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,
|
|
|
|
error: string,
|
2017-09-22 18:18:16 +00:00
|
|
|
message: ?string,
|
|
|
|
credentials: ?Object) {
|
2016-10-05 14:36:59 +00:00
|
|
|
return {
|
|
|
|
type: CONNECTION_FAILED,
|
2016-12-05 15:14:50 +00:00
|
|
|
connection,
|
2017-09-24 21:51:43 +00:00
|
|
|
|
|
|
|
// Make the error resemble an Error instance (to the extent that
|
|
|
|
// jitsi-meet needs it).
|
|
|
|
error: {
|
|
|
|
credentials:
|
|
|
|
credentials && Object.keys(credentials).length
|
|
|
|
? credentials
|
|
|
|
: undefined,
|
|
|
|
message,
|
|
|
|
name: error
|
|
|
|
}
|
2017-05-09 05:15:43 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-09-22 18:18:16 +00:00
|
|
|
/* eslint-enable max-params */
|
|
|
|
|
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) {
|
|
|
|
const defaultOptions = state['features/base/connection'].options;
|
|
|
|
const options = _.merge(
|
|
|
|
{},
|
|
|
|
defaultOptions,
|
|
|
|
|
|
|
|
// Lib-jitsi-meet wants the config passed in multiple places and here is
|
|
|
|
// the latest one I have discovered.
|
|
|
|
state['features/base/config'],
|
|
|
|
);
|
|
|
|
let { bosh } = options;
|
|
|
|
|
|
|
|
if (bosh) {
|
|
|
|
// 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()}`);
|
|
|
|
|
|
|
|
// XXX By default, config.js does not add a protocol to the BOSH URL.
|
|
|
|
// Which trips React Native. Make sure there is a protocol in order to
|
|
|
|
// satisfy React Native.
|
|
|
|
if (bosh !== defaultOptions.bosh
|
|
|
|
&& !parseStandardURIString(bosh).protocol) {
|
|
|
|
const { protocol } = parseStandardURIString(defaultOptions.bosh);
|
|
|
|
|
|
|
|
protocol && (bosh = protocol + bosh);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.bosh = bosh;
|
|
|
|
}
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2017-05-09 05:15:43 +00:00
|
|
|
/**
|
|
|
|
* Closes connection.
|
|
|
|
*
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function disconnect() {
|
|
|
|
return (dispatch: Dispatch<*>, getState: Function) => {
|
|
|
|
const state = getState();
|
2017-07-06 11:51:35 +00:00
|
|
|
const { conference, joining } = state['features/base/conference'];
|
|
|
|
|
|
|
|
// The conference we are joining or have already joined.
|
2017-07-07 17:21:08 +00:00
|
|
|
const conference_ = conference || joining;
|
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
|
|
|
|
2017-07-07 17:21:08 +00:00
|
|
|
promise = conference_.leave();
|
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'];
|
|
|
|
|
|
|
|
// The connection we are connecting or have already connected.
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|