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.native.js 11KB

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