2022-10-11 10:47:54 +00:00
|
|
|
import { IStore } from '../../app/types';
|
|
|
|
// eslint-disable-next-line lines-around-comment
|
2022-09-14 07:54:56 +00:00
|
|
|
// @ts-ignore
|
2020-06-04 14:09:13 +00:00
|
|
|
import { conferenceLeft, conferenceWillLeave } from '../conference/actions';
|
|
|
|
import { getCurrentConference } from '../conference/functions';
|
2017-02-28 03:21:50 +00:00
|
|
|
import JitsiMeetJS, { JitsiConnectionEvents } from '../lib-jitsi-meet';
|
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';
|
2022-06-16 12:27:41 +00:00
|
|
|
import { constructOptions } from './actions.any';
|
2019-04-25 08:14:23 +00:00
|
|
|
import { JITSI_CONNECTION_URL_KEY } from './constants';
|
2019-08-21 14:50:00 +00:00
|
|
|
import logger from './logger';
|
2018-05-17 14:53:51 +00:00
|
|
|
|
2022-06-16 12:27:41 +00:00
|
|
|
export * from './actions.any';
|
|
|
|
|
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.
|
|
|
|
*/
|
2022-09-14 07:54:56 +00:00
|
|
|
jid: string;
|
2018-05-02 18:27:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The XMPP user's password.
|
|
|
|
*/
|
2022-09-14 07:54:56 +00:00
|
|
|
password: string;
|
|
|
|
};
|
2018-05-02 18:27:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The details about the connection failed event.
|
|
|
|
*/
|
2022-09-14 07:54:56 +00:00
|
|
|
details?: Object;
|
2018-05-02 18:27:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Error message.
|
|
|
|
*/
|
2022-09-14 07:54:56 +00:00
|
|
|
message?: string;
|
2018-05-02 18:27:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* One of {@link JitsiConnectionError} constants (defined in
|
|
|
|
* lib-jitsi-meet).
|
|
|
|
*/
|
2022-09-14 07:54:56 +00:00
|
|
|
name: string;
|
2018-05-02 18:27:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Indicates whether this event is recoverable or not.
|
|
|
|
*/
|
2022-09-14 07:54:56 +00:00
|
|
|
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
|
|
|
*/
|
2022-09-14 07:54:56 +00:00
|
|
|
export function connect(id?: string, password?: string) {
|
2022-10-11 10:47:54 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
|
2016-10-05 14:36:59 +00:00
|
|
|
const state = getState();
|
2022-06-16 12:27:41 +00:00
|
|
|
const options = constructOptions(state);
|
2019-04-25 08:14:23 +00:00
|
|
|
const { locationURL } = state['features/base/connection'];
|
2020-08-12 20:32:09 +00:00
|
|
|
const { jwt } = state['features/base/jwt'];
|
|
|
|
const connection = new JitsiMeetJS.JitsiConnection(options.appId, jwt, options);
|
2016-12-05 15:14:50 +00:00
|
|
|
|
2019-04-25 08:14:23 +00:00
|
|
|
connection[JITSI_CONNECTION_URL_KEY] = locationURL;
|
|
|
|
|
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
|
|
|
|
2019-05-03 18:11:33 +00:00
|
|
|
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.
|
|
|
|
*
|
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
|
|
|
*/
|
2020-05-20 08:25:31 +00:00
|
|
|
function _onConnectionDisconnected() {
|
2018-05-23 11:43:40 +00:00
|
|
|
unsubscribe();
|
2020-05-20 08:25:31 +00:00
|
|
|
dispatch(connectionDisconnected(connection));
|
2016-12-05 15:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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,
|
2022-09-14 07:54:56 +00:00
|
|
|
credentials: any,
|
2018-07-02 21:22:51 +00:00
|
|
|
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
|
|
|
* @private
|
|
|
|
* @returns {{
|
|
|
|
* type: CONNECTION_DISCONNECTED,
|
2020-05-20 08:25:31 +00:00
|
|
|
* connection: JitsiConnection
|
2016-10-05 14:36:59 +00:00
|
|
|
* }}
|
|
|
|
*/
|
2020-05-20 08:25:31 +00:00
|
|
|
export function connectionDisconnected(connection: Object) {
|
2016-10-05 14:36:59 +00:00
|
|
|
return {
|
|
|
|
type: CONNECTION_DISCONNECTED,
|
2020-05-20 08:25:31 +00:00
|
|
|
connection
|
2016-10-05 14:36:59 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* }}
|
|
|
|
*/
|
2022-09-14 07:54:56 +00:00
|
|
|
function _connectionWillConnect(connection: Object) {
|
2018-05-21 03:58:34 +00:00
|
|
|
return {
|
|
|
|
type: CONNECTION_WILL_CONNECT,
|
|
|
|
connection
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-09 05:15:43 +00:00
|
|
|
/**
|
|
|
|
* Closes connection.
|
|
|
|
*
|
|
|
|
* @returns {Function}
|
|
|
|
*/
|
|
|
|
export function disconnect() {
|
2022-10-11 10:47:54 +00:00
|
|
|
return (dispatch: IStore['dispatch'], getState: IStore['getState']): 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()
|
2022-09-14 07:54:56 +00:00
|
|
|
.catch((error: Error) => {
|
2018-05-17 14:53:51 +00:00
|
|
|
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());
|
2019-04-25 08:14:23 +00:00
|
|
|
} else {
|
2019-05-22 10:01:58 +00:00
|
|
|
logger.info('No connection found while disconnecting.');
|
2017-05-09 05:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-16 15:59:33 +00:00
|
|
|
* Sets the location URL of the application, connection, conference, etc.
|
2017-05-09 05:15:43 +00:00
|
|
|
*
|
|
|
|
* @param {URL} [locationURL] - The location URL of the application,
|
|
|
|
* connection, conference, etc.
|
|
|
|
* @returns {{
|
|
|
|
* type: SET_LOCATION_URL,
|
|
|
|
* locationURL: URL
|
|
|
|
* }}
|
|
|
|
*/
|
2022-09-14 07:54:56 +00:00
|
|
|
export function setLocationURL(locationURL?: URL) {
|
2017-05-09 05:15:43 +00:00
|
|
|
return {
|
|
|
|
type: SET_LOCATION_URL,
|
|
|
|
locationURL
|
2016-10-05 14:36:59 +00:00
|
|
|
};
|
|
|
|
}
|