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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // @flow
  2. import _ from 'lodash';
  3. import type { Dispatch } from 'redux';
  4. import {
  5. conferenceLeft,
  6. conferenceWillLeave,
  7. getCurrentConference
  8. } from '../conference';
  9. import JitsiMeetJS, { JitsiConnectionEvents } from '../lib-jitsi-meet';
  10. import { parseURIString } 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. return connection.connect({
  89. id,
  90. password
  91. });
  92. /**
  93. * Dispatches {@code CONNECTION_DISCONNECTED} action when connection is
  94. * disconnected.
  95. *
  96. * @param {string} message - Disconnect reason.
  97. * @private
  98. * @returns {void}
  99. */
  100. function _onConnectionDisconnected(message: string) {
  101. unsubscribe();
  102. dispatch(_connectionDisconnected(connection, message));
  103. }
  104. /**
  105. * Resolves external promise when connection is established.
  106. *
  107. * @private
  108. * @returns {void}
  109. */
  110. function _onConnectionEstablished() {
  111. connection.removeEventListener(
  112. JitsiConnectionEvents.CONNECTION_ESTABLISHED,
  113. _onConnectionEstablished);
  114. dispatch(connectionEstablished(connection, Date.now()));
  115. }
  116. /**
  117. * Rejects external promise when connection fails.
  118. *
  119. * @param {JitsiConnectionErrors} err - Connection error.
  120. * @param {string} [msg] - Error message supplied by lib-jitsi-meet.
  121. * @param {Object} [credentials] - The invalid credentials that were
  122. * used to authenticate and the authentication failed.
  123. * @param {string} [credentials.jid] - The XMPP user's ID.
  124. * @param {string} [credentials.password] - The XMPP user's password.
  125. * @param {Object} details - Additional information about the error.
  126. * @private
  127. * @returns {void}
  128. */
  129. function _onConnectionFailed( // eslint-disable-line max-params
  130. err: string,
  131. msg: string,
  132. credentials: Object,
  133. details: Object) {
  134. unsubscribe();
  135. dispatch(
  136. connectionFailed(
  137. connection, {
  138. credentials,
  139. details,
  140. name: err,
  141. message: msg
  142. }
  143. ));
  144. }
  145. /**
  146. * Unsubscribe the connection instance from
  147. * {@code CONNECTION_DISCONNECTED} and {@code CONNECTION_FAILED} events.
  148. *
  149. * @returns {void}
  150. */
  151. function unsubscribe() {
  152. connection.removeEventListener(
  153. JitsiConnectionEvents.CONNECTION_DISCONNECTED,
  154. _onConnectionDisconnected);
  155. connection.removeEventListener(
  156. JitsiConnectionEvents.CONNECTION_FAILED,
  157. _onConnectionFailed);
  158. }
  159. };
  160. }
  161. /**
  162. * Create an action for when the signaling connection has been lost.
  163. *
  164. * @param {JitsiConnection} connection - The {@code JitsiConnection} which
  165. * disconnected.
  166. * @param {string} message - Error message.
  167. * @private
  168. * @returns {{
  169. * type: CONNECTION_DISCONNECTED,
  170. * connection: JitsiConnection,
  171. * message: string
  172. * }}
  173. */
  174. function _connectionDisconnected(connection: Object, message: string) {
  175. return {
  176. type: CONNECTION_DISCONNECTED,
  177. connection,
  178. message
  179. };
  180. }
  181. /**
  182. * Create an action for when the signaling connection has been established.
  183. *
  184. * @param {JitsiConnection} connection - The {@code JitsiConnection} which was
  185. * established.
  186. * @param {number} timeEstablished - The time at which the
  187. * {@code JitsiConnection} which was established.
  188. * @public
  189. * @returns {{
  190. * type: CONNECTION_ESTABLISHED,
  191. * connection: JitsiConnection,
  192. * timeEstablished: number
  193. * }}
  194. */
  195. export function connectionEstablished(
  196. connection: Object, timeEstablished: number) {
  197. return {
  198. type: CONNECTION_ESTABLISHED,
  199. connection,
  200. timeEstablished
  201. };
  202. }
  203. /**
  204. * Create an action for when the signaling connection could not be created.
  205. *
  206. * @param {JitsiConnection} connection - The {@code JitsiConnection} which
  207. * failed.
  208. * @param {ConnectionFailedError} error - Error.
  209. * @public
  210. * @returns {{
  211. * type: CONNECTION_FAILED,
  212. * connection: JitsiConnection,
  213. * error: ConnectionFailedError
  214. * }}
  215. */
  216. export function connectionFailed(
  217. connection: Object,
  218. error: ConnectionFailedError) {
  219. const { credentials } = error;
  220. if (credentials && !Object.keys(credentials).length) {
  221. error.credentials = undefined;
  222. }
  223. return {
  224. type: CONNECTION_FAILED,
  225. connection,
  226. error
  227. };
  228. }
  229. /**
  230. * Create an action for when a connection will connect.
  231. *
  232. * @param {JitsiConnection} connection - The {@code JitsiConnection} which will
  233. * connect.
  234. * @private
  235. * @returns {{
  236. * type: CONNECTION_WILL_CONNECT,
  237. * connection: JitsiConnection
  238. * }}
  239. */
  240. function _connectionWillConnect(connection) {
  241. return {
  242. type: CONNECTION_WILL_CONNECT,
  243. connection
  244. };
  245. }
  246. /**
  247. * Constructs options to be passed to the constructor of {@code JitsiConnection}
  248. * based on the redux state.
  249. *
  250. * @param {Object} state - The redux state.
  251. * @returns {Object} The options to be passed to the constructor of
  252. * {@code JitsiConnection}.
  253. */
  254. function _constructOptions(state) {
  255. // Deep clone the options to make sure we don't modify the object in the
  256. // redux store.
  257. const options = _.cloneDeep(state['features/base/config']);
  258. // Normalize the BOSH URL.
  259. let { bosh } = options;
  260. if (bosh) {
  261. const { locationURL } = state['features/base/connection'];
  262. if (bosh.startsWith('//')) {
  263. // By default our config.js doesn't include the protocol.
  264. bosh = `${locationURL.protocol}${bosh}`;
  265. } else if (bosh.startsWith('/')) {
  266. // Handle relative URLs, which won't work on mobile.
  267. const {
  268. protocol,
  269. hostname,
  270. contextRoot
  271. } = parseURIString(locationURL.href);
  272. // eslint-disable-next-line max-len
  273. bosh = `${protocol}//${hostname}${contextRoot || '/'}${bosh.substr(1)}`;
  274. }
  275. // Append room to the URL's search.
  276. const { room } = state['features/base/conference'];
  277. // XXX The Jitsi Meet deployments require the room argument to be in
  278. // lower case at the time of this writing but, unfortunately, they do
  279. // not ignore case themselves.
  280. room && (bosh += `?room=${room.toLowerCase()}`);
  281. options.bosh = bosh;
  282. }
  283. return options;
  284. }
  285. /**
  286. * Closes connection.
  287. *
  288. * @returns {Function}
  289. */
  290. export function disconnect() {
  291. return (dispatch: Dispatch<any>, getState: Function): Promise<void> => {
  292. const state = getState();
  293. // The conference we have already joined or are joining.
  294. const conference_ = getCurrentConference(state);
  295. // Promise which completes when the conference has been left and the
  296. // connection has been disconnected.
  297. let promise;
  298. // Leave the conference.
  299. if (conference_) {
  300. // In a fashion similar to JitsiConference's CONFERENCE_LEFT event
  301. // (and the respective Redux action) which is fired after the
  302. // conference has been left, notify the application about the
  303. // intention to leave the conference.
  304. dispatch(conferenceWillLeave(conference_));
  305. promise
  306. = conference_.leave()
  307. .catch(error => {
  308. logger.warn(
  309. 'JitsiConference.leave() rejected with:',
  310. error);
  311. // The library lib-jitsi-meet failed to make the
  312. // JitsiConference leave. Which may be because
  313. // JitsiConference thinks it has already left.
  314. // Regardless of the failure reason, continue in
  315. // jitsi-meet as if the leave has succeeded.
  316. dispatch(conferenceLeft(conference_));
  317. });
  318. } else {
  319. promise = Promise.resolve();
  320. }
  321. // Disconnect the connection.
  322. const { connecting, connection } = state['features/base/connection'];
  323. // The connection we have already connected or are connecting.
  324. const connection_ = connection || connecting;
  325. if (connection_) {
  326. promise = promise.then(() => connection_.disconnect());
  327. } else {
  328. logger.info('No connection found while disconnecting.');
  329. }
  330. return promise;
  331. };
  332. }
  333. /**
  334. * Sets the location URL of the application, connecton, conference, etc.
  335. *
  336. * @param {URL} [locationURL] - The location URL of the application,
  337. * connection, conference, etc.
  338. * @returns {{
  339. * type: SET_LOCATION_URL,
  340. * locationURL: URL
  341. * }}
  342. */
  343. export function setLocationURL(locationURL: ?URL) {
  344. return {
  345. type: SET_LOCATION_URL,
  346. locationURL
  347. };
  348. }