You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actions.any.ts 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import _ from 'lodash';
  2. import { IReduxState, IStore } from '../../app/types';
  3. import { conferenceLeft, conferenceWillLeave } from '../conference/actions';
  4. import { getCurrentConference } from '../conference/functions';
  5. import JitsiMeetJS, { JitsiConnectionEvents } from '../lib-jitsi-meet';
  6. import {
  7. appendURLParam,
  8. getBackendSafeRoomName
  9. } from '../util/uri';
  10. import {
  11. CONNECTION_DISCONNECTED,
  12. CONNECTION_ESTABLISHED,
  13. CONNECTION_FAILED,
  14. CONNECTION_WILL_CONNECT,
  15. SET_LOCATION_URL
  16. } from './actionTypes';
  17. import { JITSI_CONNECTION_URL_KEY } from './constants';
  18. import logger from './logger';
  19. /**
  20. * The error structure passed to the {@link connectionFailed} action.
  21. *
  22. * Note there was an intention to make the error resemble an Error instance (to
  23. * the extent that jitsi-meet needs it).
  24. */
  25. export type ConnectionFailedError = {
  26. /**
  27. * The invalid credentials that were used to authenticate and the
  28. * authentication failed.
  29. */
  30. credentials?: {
  31. /**
  32. * The XMPP user's ID.
  33. */
  34. jid: string;
  35. /**
  36. * The XMPP user's password.
  37. */
  38. password: string;
  39. };
  40. /**
  41. * The details about the connection failed event.
  42. */
  43. details?: Object;
  44. /**
  45. * Error message.
  46. */
  47. message?: string;
  48. /**
  49. * One of {@link JitsiConnectionError} constants (defined in
  50. * lib-jitsi-meet).
  51. */
  52. name: string;
  53. /**
  54. * Indicates whether this event is recoverable or not.
  55. */
  56. recoverable?: boolean;
  57. };
  58. /**
  59. * Create an action for when the signaling connection has been lost.
  60. *
  61. * @param {JitsiConnection} connection - The {@code JitsiConnection} which
  62. * disconnected.
  63. * @private
  64. * @returns {{
  65. * type: CONNECTION_DISCONNECTED,
  66. * connection: JitsiConnection
  67. * }}
  68. */
  69. export function connectionDisconnected(connection?: Object) {
  70. return {
  71. type: CONNECTION_DISCONNECTED,
  72. connection
  73. };
  74. }
  75. /**
  76. * Create an action for when the signaling connection has been established.
  77. *
  78. * @param {JitsiConnection} connection - The {@code JitsiConnection} which was
  79. * established.
  80. * @param {number} timeEstablished - The time at which the
  81. * {@code JitsiConnection} which was established.
  82. * @public
  83. * @returns {{
  84. * type: CONNECTION_ESTABLISHED,
  85. * connection: JitsiConnection,
  86. * timeEstablished: number
  87. * }}
  88. */
  89. export function connectionEstablished(
  90. connection: Object, timeEstablished: number) {
  91. return {
  92. type: CONNECTION_ESTABLISHED,
  93. connection,
  94. timeEstablished
  95. };
  96. }
  97. /**
  98. * Create an action for when the signaling connection could not be created.
  99. *
  100. * @param {JitsiConnection} connection - The {@code JitsiConnection} which
  101. * failed.
  102. * @param {ConnectionFailedError} error - Error.
  103. * @public
  104. * @returns {{
  105. * type: CONNECTION_FAILED,
  106. * connection: JitsiConnection,
  107. * error: ConnectionFailedError
  108. * }}
  109. */
  110. export function connectionFailed(
  111. connection: Object,
  112. error: ConnectionFailedError) {
  113. const { credentials } = error;
  114. if (credentials && !Object.keys(credentials).length) {
  115. error.credentials = undefined;
  116. }
  117. return {
  118. type: CONNECTION_FAILED,
  119. connection,
  120. error
  121. };
  122. }
  123. /**
  124. * Constructs options to be passed to the constructor of {@code JitsiConnection}
  125. * based on the redux state.
  126. *
  127. * @param {Object} state - The redux state.
  128. * @returns {Object} The options to be passed to the constructor of
  129. * {@code JitsiConnection}.
  130. */
  131. export function constructOptions(state: IReduxState) {
  132. // Deep clone the options to make sure we don't modify the object in the
  133. // redux store.
  134. const options = _.cloneDeep(state['features/base/config']);
  135. const { bosh } = options;
  136. let { websocket } = options;
  137. // TESTING: Only enable WebSocket for some percentage of users.
  138. if (websocket && navigator.product === 'ReactNative') {
  139. if ((Math.random() * 100) >= (options?.testing?.mobileXmppWsThreshold ?? 0)) {
  140. websocket = undefined;
  141. }
  142. }
  143. // WebSocket is preferred over BOSH.
  144. const serviceUrl = websocket || bosh;
  145. logger.log(`Using service URL ${serviceUrl}`);
  146. // Append room to the URL's search.
  147. const { room } = state['features/base/conference'];
  148. if (serviceUrl && room) {
  149. const roomName = getBackendSafeRoomName(room);
  150. options.serviceUrl = appendURLParam(serviceUrl, 'room', roomName ?? '');
  151. if (options.websocketKeepAliveUrl) {
  152. options.websocketKeepAliveUrl = appendURLParam(options.websocketKeepAliveUrl, 'room', roomName ?? '');
  153. }
  154. if (options.conferenceRequestUrl) {
  155. options.conferenceRequestUrl = appendURLParam(options.conferenceRequestUrl, 'room', roomName ?? '');
  156. }
  157. }
  158. return options;
  159. }
  160. /**
  161. * Sets the location URL of the application, connection, conference, etc.
  162. *
  163. * @param {URL} [locationURL] - The location URL of the application,
  164. * connection, conference, etc.
  165. * @returns {{
  166. * type: SET_LOCATION_URL,
  167. * locationURL: URL
  168. * }}
  169. */
  170. export function setLocationURL(locationURL?: URL) {
  171. return {
  172. type: SET_LOCATION_URL,
  173. locationURL
  174. };
  175. }
  176. /**
  177. * Opens new connection.
  178. *
  179. * @param {string} [id] - The XMPP user's ID (e.g. {@code user@server.com}).
  180. * @param {string} [password] - The XMPP user's password.
  181. * @returns {Function}
  182. */
  183. export function _connectInternal(id?: string, password?: string) {
  184. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  185. const state = getState();
  186. const options = constructOptions(state);
  187. const { locationURL } = state['features/base/connection'];
  188. const { jwt } = state['features/base/jwt'];
  189. const connection = new JitsiMeetJS.JitsiConnection(options.appId, jwt, options);
  190. connection[JITSI_CONNECTION_URL_KEY] = locationURL;
  191. dispatch(_connectionWillConnect(connection));
  192. return new Promise((resolve, reject) => {
  193. connection.addEventListener(
  194. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  195. _onConnectionDisconnected);
  196. connection.addEventListener(
  197. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  198. _onConnectionEstablished);
  199. connection.addEventListener(
  200. JitsiConnectionEvents.CONNECTION_FAILED,
  201. _onConnectionFailed);
  202. /**
  203. * Unsubscribe the connection instance from
  204. * {@code CONNECTION_DISCONNECTED} and {@code CONNECTION_FAILED} events.
  205. *
  206. * @returns {void}
  207. */
  208. function unsubscribe() {
  209. connection.removeEventListener(
  210. JitsiConnectionEvents.CONNECTION_DISCONNECTED, _onConnectionDisconnected);
  211. connection.removeEventListener(JitsiConnectionEvents.CONNECTION_FAILED, _onConnectionFailed);
  212. }
  213. /**
  214. * Dispatches {@code CONNECTION_DISCONNECTED} action when connection is
  215. * disconnected.
  216. *
  217. * @private
  218. * @returns {void}
  219. */
  220. function _onConnectionDisconnected() {
  221. unsubscribe();
  222. dispatch(connectionDisconnected(connection));
  223. resolve(connection);
  224. }
  225. /**
  226. * Rejects external promise when connection fails.
  227. *
  228. * @param {JitsiConnectionErrors} err - Connection error.
  229. * @param {string} [message] - Error message supplied by lib-jitsi-meet.
  230. * @param {Object} [credentials] - The invalid credentials that were
  231. * used to authenticate and the authentication failed.
  232. * @param {string} [credentials.jid] - The XMPP user's ID.
  233. * @param {string} [credentials.password] - The XMPP user's password.
  234. * @param {Object} details - Additional information about the error.
  235. * @private
  236. * @returns {void}
  237. */
  238. function _onConnectionFailed( // eslint-disable-line max-params
  239. err: string,
  240. message: string,
  241. credentials: any,
  242. details: Object) {
  243. unsubscribe();
  244. dispatch(connectionFailed(connection, {
  245. credentials,
  246. details,
  247. name: err,
  248. message
  249. }));
  250. reject(err);
  251. }
  252. /**
  253. * Resolves external promise when connection is established.
  254. *
  255. * @private
  256. * @returns {void}
  257. */
  258. function _onConnectionEstablished() {
  259. connection.removeEventListener(JitsiConnectionEvents.CONNECTION_ESTABLISHED, _onConnectionEstablished);
  260. dispatch(connectionEstablished(connection, Date.now()));
  261. resolve(connection);
  262. }
  263. connection.connect({
  264. id,
  265. password
  266. });
  267. });
  268. };
  269. }
  270. /**
  271. * Create an action for when a connection will connect.
  272. *
  273. * @param {JitsiConnection} connection - The {@code JitsiConnection} which will
  274. * connect.
  275. * @private
  276. * @returns {{
  277. * type: CONNECTION_WILL_CONNECT,
  278. * connection: JitsiConnection
  279. * }}
  280. */
  281. function _connectionWillConnect(connection: Object) {
  282. return {
  283. type: CONNECTION_WILL_CONNECT,
  284. connection
  285. };
  286. }
  287. /**
  288. * Closes connection.
  289. *
  290. * @returns {Function}
  291. */
  292. export function disconnect() {
  293. return (dispatch: IStore['dispatch'], getState: IStore['getState']): Promise<void> => {
  294. const state = getState();
  295. // The conference we have already joined or are joining.
  296. const conference_ = getCurrentConference(state);
  297. // Promise which completes when the conference has been left and the
  298. // connection has been disconnected.
  299. let promise;
  300. // Leave the conference.
  301. if (conference_) {
  302. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  303. // (and the respective Redux action) which is fired after the
  304. // conference has been left, notify the application about the
  305. // intention to leave the conference.
  306. dispatch(conferenceWillLeave(conference_));
  307. promise
  308. = conference_.leave()
  309. .catch((error: Error) => {
  310. logger.warn(
  311. 'JitsiConference.leave() rejected with:',
  312. error);
  313. // The library lib-jitsi-meet failed to make the
  314. // JitsiConference leave. Which may be because
  315. // JitsiConference thinks it has already left.
  316. // Regardless of the failure reason, continue in
  317. // jitsi-meet as if the leave has succeeded.
  318. dispatch(conferenceLeft(conference_));
  319. });
  320. } else {
  321. promise = Promise.resolve();
  322. }
  323. // Disconnect the connection.
  324. const { connecting, connection } = state['features/base/connection'];
  325. // The connection we have already connected or are connecting.
  326. const connection_ = connection || connecting;
  327. if (connection_) {
  328. promise = promise.then(() => connection_.disconnect());
  329. } else {
  330. logger.info('No connection found while disconnecting.');
  331. }
  332. return promise;
  333. };
  334. }