api.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import isObject from "lodash-es/isObject";
  2. import log from "@converse/headless/log.js";
  3. import { _converse, api, converse } from "@converse/headless/core.js";
  4. import { getOpenPromise } from '@converse/openpromise';
  5. const { Strophe, $iq } = converse.env;
  6. export default {
  7. /**
  8. * The XEP-0030 service discovery API
  9. *
  10. * This API lets you discover information about entities on the
  11. * XMPP network.
  12. *
  13. * @namespace api.disco
  14. * @memberOf api
  15. */
  16. disco: {
  17. /**
  18. * @namespace api.disco.stream
  19. * @memberOf api.disco
  20. */
  21. stream: {
  22. /**
  23. * @method api.disco.stream.getFeature
  24. * @param {String} name The feature name
  25. * @param {String} xmlns The XML namespace
  26. * @example _converse.api.disco.stream.getFeature('ver', 'urn:xmpp:features:rosterver')
  27. */
  28. async getFeature (name, xmlns) {
  29. await api.waitUntil('streamFeaturesAdded');
  30. if (!name || !xmlns) {
  31. throw new Error("name and xmlns need to be provided when calling disco.stream.getFeature");
  32. }
  33. if (_converse.stream_features === undefined && !api.connection.connected()) {
  34. // Happens during tests when disco lookups happen asynchronously after teardown.
  35. const msg = `Tried to get feature ${name} ${xmlns} but _converse.stream_features has been torn down`;
  36. log.warn(msg);
  37. return;
  38. }
  39. return _converse.stream_features.findWhere({'name': name, 'xmlns': xmlns});
  40. }
  41. },
  42. /**
  43. * @namespace api.disco.own
  44. * @memberOf api.disco
  45. */
  46. own: {
  47. /**
  48. * @namespace api.disco.own.identities
  49. * @memberOf api.disco.own
  50. */
  51. identities: {
  52. /**
  53. * Lets you add new identities for this client (i.e. instance of Converse)
  54. * @method api.disco.own.identities.add
  55. *
  56. * @param {String} category - server, client, gateway, directory, etc.
  57. * @param {String} type - phone, pc, web, etc.
  58. * @param {String} name - "Converse"
  59. * @param {String} lang - en, el, de, etc.
  60. *
  61. * @example _converse.api.disco.own.identities.clear();
  62. */
  63. add (category, type, name, lang) {
  64. for (var i=0; i<_converse.disco._identities.length; i++) {
  65. if (_converse.disco._identities[i].category == category &&
  66. _converse.disco._identities[i].type == type &&
  67. _converse.disco._identities[i].name == name &&
  68. _converse.disco._identities[i].lang == lang) {
  69. return false;
  70. }
  71. }
  72. _converse.disco._identities.push({category: category, type: type, name: name, lang: lang});
  73. },
  74. /**
  75. * Clears all previously registered identities.
  76. * @method api.disco.own.identities.clear
  77. * @example _converse.api.disco.own.identities.clear();
  78. */
  79. clear () {
  80. _converse.disco._identities = []
  81. },
  82. /**
  83. * Returns all of the identities registered for this client
  84. * (i.e. instance of Converse).
  85. * @method api.disco.identities.get
  86. * @example const identities = api.disco.own.identities.get();
  87. */
  88. get () {
  89. return _converse.disco._identities;
  90. }
  91. },
  92. /**
  93. * @namespace api.disco.own.features
  94. * @memberOf api.disco.own
  95. */
  96. features: {
  97. /**
  98. * Lets you register new disco features for this client (i.e. instance of Converse)
  99. * @method api.disco.own.features.add
  100. * @param {String} name - e.g. http://jabber.org/protocol/caps
  101. * @example _converse.api.disco.own.features.add("http://jabber.org/protocol/caps");
  102. */
  103. add (name) {
  104. for (var i=0; i<_converse.disco._features.length; i++) {
  105. if (_converse.disco._features[i] == name) { return false; }
  106. }
  107. _converse.disco._features.push(name);
  108. },
  109. /**
  110. * Clears all previously registered features.
  111. * @method api.disco.own.features.clear
  112. * @example _converse.api.disco.own.features.clear();
  113. */
  114. clear () {
  115. _converse.disco._features = []
  116. },
  117. /**
  118. * Returns all of the features registered for this client (i.e. instance of Converse).
  119. * @method api.disco.own.features.get
  120. * @example const features = api.disco.own.features.get();
  121. */
  122. get () {
  123. return _converse.disco._features;
  124. }
  125. }
  126. },
  127. /**
  128. * Query for information about an XMPP entity
  129. *
  130. * @method api.disco.info
  131. * @param {string} jid The Jabber ID of the entity to query
  132. * @param {string} [node] A specific node identifier associated with the JID
  133. * @returns {promise} Promise which resolves once we have a result from the server.
  134. */
  135. info (jid, node) {
  136. const attrs = {xmlns: Strophe.NS.DISCO_INFO};
  137. if (node) {
  138. attrs.node = node;
  139. }
  140. const info = $iq({
  141. 'from': _converse.connection.jid,
  142. 'to':jid,
  143. 'type':'get'
  144. }).c('query', attrs);
  145. return api.sendIQ(info);
  146. },
  147. /**
  148. * Query for items associated with an XMPP entity
  149. *
  150. * @method api.disco.items
  151. * @param {string} jid The Jabber ID of the entity to query for items
  152. * @param {string} [node] A specific node identifier associated with the JID
  153. * @returns {promise} Promise which resolves once we have a result from the server.
  154. */
  155. items (jid, node) {
  156. const attrs = {'xmlns': Strophe.NS.DISCO_ITEMS};
  157. if (node) {
  158. attrs.node = node;
  159. }
  160. return api.sendIQ(
  161. $iq({
  162. 'from': _converse.connection.jid,
  163. 'to':jid,
  164. 'type':'get'
  165. }).c('query', attrs)
  166. );
  167. },
  168. /**
  169. * Namespace for methods associated with disco entities
  170. *
  171. * @namespace api.disco.entities
  172. * @memberOf api.disco
  173. */
  174. entities: {
  175. /**
  176. * Get the corresponding `DiscoEntity` instance.
  177. *
  178. * @method api.disco.entities.get
  179. * @param {string} jid The Jabber ID of the entity
  180. * @param {boolean} [create] Whether the entity should be created if it doesn't exist.
  181. * @example _converse.api.disco.entities.get(jid);
  182. */
  183. async get (jid, create=false) {
  184. await api.waitUntil('discoInitialized');
  185. if (!jid) {
  186. return _converse.disco_entities;
  187. }
  188. if (_converse.disco_entities === undefined && !api.connection.connected()) {
  189. // Happens during tests when disco lookups happen asynchronously after teardown.
  190. const msg = `Tried to look up entity ${jid} but _converse.disco_entities has been torn down`;
  191. log.warn(msg);
  192. return;
  193. }
  194. const entity = _converse.disco_entities.get(jid);
  195. if (entity || !create) {
  196. return entity;
  197. }
  198. return api.disco.entities.create(jid);
  199. },
  200. /**
  201. * Create a new disco entity. It's identity and features
  202. * will automatically be fetched from cache or from the
  203. * XMPP server.
  204. *
  205. * Fetching from cache can be disabled by passing in
  206. * `ignore_cache: true` in the options parameter.
  207. *
  208. * @method api.disco.entities.create
  209. * @param {string} jid The Jabber ID of the entity
  210. * @param {object} [options] Additional options
  211. * @param {boolean} [options.ignore_cache]
  212. * If true, fetch all features from the XMPP server instead of restoring them from cache
  213. * @example _converse.api.disco.entities.create(jid, {'ignore_cache': true});
  214. */
  215. create (jid, options) {
  216. return _converse.disco_entities.create({'jid': jid}, options);
  217. }
  218. },
  219. /**
  220. * @namespace api.disco.features
  221. * @memberOf api.disco
  222. */
  223. features: {
  224. /**
  225. * Return a given feature of a disco entity
  226. *
  227. * @method api.disco.features.get
  228. * @param {string} feature The feature that might be
  229. * supported. In the XML stanza, this is the `var`
  230. * attribute of the `<feature>` element. For
  231. * example: `http://jabber.org/protocol/muc`
  232. * @param {string} jid The JID of the entity
  233. * (and its associated items) which should be queried
  234. * @returns {promise} A promise which resolves with a list containing
  235. * _converse.Entity instances representing the entity
  236. * itself or those items associated with the entity if
  237. * they support the given feature.
  238. * @example
  239. * api.disco.features.get(Strophe.NS.MAM, _converse.bare_jid);
  240. */
  241. async get (feature, jid) {
  242. if (!jid) {
  243. throw new TypeError('You need to provide an entity JID');
  244. }
  245. await api.waitUntil('discoInitialized');
  246. let entity = await api.disco.entities.get(jid, true);
  247. if (_converse.disco_entities === undefined && !api.connection.connected()) {
  248. // Happens during tests when disco lookups happen asynchronously after teardown.
  249. const msg = `Tried to get feature ${feature} for ${jid} but _converse.disco_entities has been torn down`;
  250. log.warn(msg);
  251. return;
  252. }
  253. entity = await entity.waitUntilFeaturesDiscovered;
  254. const promises = [...entity.items.map(i => i.hasFeature(feature)), entity.hasFeature(feature)];
  255. const result = await Promise.all(promises);
  256. return result.filter(isObject);
  257. }
  258. },
  259. /**
  260. * Used to determine whether an entity supports a given feature.
  261. *
  262. * @method api.disco.supports
  263. * @param {string} feature The feature that might be
  264. * supported. In the XML stanza, this is the `var`
  265. * attribute of the `<feature>` element. For
  266. * example: `http://jabber.org/protocol/muc`
  267. * @param {string} jid The JID of the entity
  268. * (and its associated items) which should be queried
  269. * @returns {promise} A promise which resolves with `true` or `false`.
  270. * @example
  271. * if (await api.disco.supports(Strophe.NS.MAM, _converse.bare_jid)) {
  272. * // The feature is supported
  273. * } else {
  274. * // The feature is not supported
  275. * }
  276. */
  277. async supports (feature, jid) {
  278. const features = await api.disco.features.get(feature, jid) || [];
  279. return features.length > 0;
  280. },
  281. /**
  282. * Refresh the features, fields and identities associated with a
  283. * disco entity by refetching them from the server
  284. * @method api.disco.refresh
  285. * @param {string} jid The JID of the entity whose features are refreshed.
  286. * @returns {promise} A promise which resolves once the features have been refreshed
  287. * @example
  288. * await api.disco.refresh('room@conference.example.org');
  289. */
  290. async refresh (jid) {
  291. if (!jid) {
  292. throw new TypeError('api.disco.refresh: You need to provide an entity JID');
  293. }
  294. await api.waitUntil('discoInitialized');
  295. let entity = await api.disco.entities.get(jid);
  296. if (entity) {
  297. entity.features.reset();
  298. entity.fields.reset();
  299. entity.identities.reset();
  300. if (!entity.waitUntilFeaturesDiscovered.isPending) {
  301. entity.waitUntilFeaturesDiscovered = getOpenPromise()
  302. }
  303. entity.queryInfo();
  304. } else {
  305. // Create it if it doesn't exist
  306. entity = await api.disco.entities.create(jid, {'ignore_cache': true});
  307. }
  308. return entity.waitUntilFeaturesDiscovered;
  309. },
  310. /**
  311. * @deprecated Use {@link api.disco.refresh} instead.
  312. * @method api.disco.refreshFeatures
  313. */
  314. refreshFeatures (jid) {
  315. return api.refresh(jid);
  316. },
  317. /**
  318. * Return all the features associated with a disco entity
  319. *
  320. * @method api.disco.getFeatures
  321. * @param {string} jid The JID of the entity whose features are returned.
  322. * @returns {promise} A promise which resolves with the returned features
  323. * @example
  324. * const features = await api.disco.getFeatures('room@conference.example.org');
  325. */
  326. async getFeatures (jid) {
  327. if (!jid) {
  328. throw new TypeError('api.disco.getFeatures: You need to provide an entity JID');
  329. }
  330. await api.waitUntil('discoInitialized');
  331. let entity = await api.disco.entities.get(jid, true);
  332. entity = await entity.waitUntilFeaturesDiscovered;
  333. return entity.features;
  334. },
  335. /**
  336. * Return all the service discovery extensions fields
  337. * associated with an entity.
  338. *
  339. * See [XEP-0129: Service Discovery Extensions](https://xmpp.org/extensions/xep-0128.html)
  340. *
  341. * @method api.disco.getFields
  342. * @param {string} jid The JID of the entity whose fields are returned.
  343. * @example
  344. * const fields = await api.disco.getFields('room@conference.example.org');
  345. */
  346. async getFields (jid) {
  347. if (!jid) {
  348. throw new TypeError('api.disco.getFields: You need to provide an entity JID');
  349. }
  350. await api.waitUntil('discoInitialized');
  351. let entity = await api.disco.entities.get(jid, true);
  352. entity = await entity.waitUntilFeaturesDiscovered;
  353. return entity.fields;
  354. },
  355. /**
  356. * Get the identity (with the given category and type) for a given disco entity.
  357. *
  358. * For example, when determining support for PEP (personal eventing protocol), you
  359. * want to know whether the user's own JID has an identity with
  360. * `category='pubsub'` and `type='pep'` as explained in this section of
  361. * XEP-0163: https://xmpp.org/extensions/xep-0163.html#support
  362. *
  363. * @method api.disco.getIdentity
  364. * @param {string} The identity category.
  365. * In the XML stanza, this is the `category`
  366. * attribute of the `<identity>` element.
  367. * For example: 'pubsub'
  368. * @param {string} type The identity type.
  369. * In the XML stanza, this is the `type`
  370. * attribute of the `<identity>` element.
  371. * For example: 'pep'
  372. * @param {string} jid The JID of the entity which might have the identity
  373. * @returns {promise} A promise which resolves with a map indicating
  374. * whether an identity with a given type is provided by the entity.
  375. * @example
  376. * api.disco.getIdentity('pubsub', 'pep', _converse.bare_jid).then(
  377. * function (identity) {
  378. * if (identity) {
  379. * // The entity DOES have this identity
  380. * } else {
  381. * // The entity DOES NOT have this identity
  382. * }
  383. * }
  384. * ).catch(e => log.error(e));
  385. */
  386. async getIdentity (category, type, jid) {
  387. const e = await api.disco.entities.get(jid, true);
  388. if (e === undefined && !api.connection.connected()) {
  389. // Happens during tests when disco lookups happen asynchronously after teardown.
  390. const msg = `Tried to look up category ${category} for ${jid} but _converse.disco_entities has been torn down`;
  391. log.warn(msg);
  392. return;
  393. }
  394. return e.getIdentity(category, type);
  395. }
  396. }
  397. }