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

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