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 10KB

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