2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-disco
|
2020-01-26 16:21:20 +01:00
|
|
|
* @copyright The Converse.js contributors
|
2019-09-19 16:54:55 +02:00
|
|
|
* @license Mozilla Public License (MPLv2)
|
|
|
|
* @description Converse plugin which add support for XEP-0030: Service Discovery
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2019-11-06 11:01:34 +01:00
|
|
|
import log from "./log";
|
2018-10-23 03:41:38 +02:00
|
|
|
import sizzle from "sizzle";
|
2020-06-08 16:08:50 +02:00
|
|
|
import { Collection } from "@converse/skeletor/src/collection";
|
|
|
|
import { Model } from '@converse/skeletor/src/model.js';
|
2020-05-18 10:54:37 +02:00
|
|
|
import { _converse, api, converse } from "./converse-core";
|
2020-06-08 16:08:50 +02:00
|
|
|
import { isEmpty, isObject } from "lodash-es";
|
2017-07-21 12:38:16 +02:00
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
const { Strophe, $iq, utils } = converse.env;
|
2017-07-21 12:38:16 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
converse.plugins.add('converse-disco', {
|
2017-07-21 12:38:16 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
initialize () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by converse.js's plugin machinery.
|
|
|
|
*/
|
2017-07-21 17:38:08 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
// Promises exposed by this plugin
|
2020-03-31 13:15:57 +02:00
|
|
|
api.promises.add('discoInitialized');
|
|
|
|
api.promises.add('streamFeaturesAdded');
|
2018-04-17 16:42:20 +02:00
|
|
|
|
2017-07-21 17:38:08 +02:00
|
|
|
|
2019-03-29 23:47:56 +01:00
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
* @namespace _converse.DiscoEntity
|
|
|
|
* @memberOf _converse
|
|
|
|
*/
|
2019-09-19 16:54:55 +02:00
|
|
|
_converse.DiscoEntity = Model.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
/* A Disco Entity is a JID addressable entity that can be queried
|
|
|
|
* for features.
|
|
|
|
*
|
|
|
|
* See XEP-0030: https://xmpp.org/extensions/xep-0030.html
|
|
|
|
*/
|
|
|
|
idAttribute: 'jid',
|
2018-09-12 12:29:47 +02:00
|
|
|
|
2019-06-25 11:18:06 +02:00
|
|
|
initialize (attrs, options) {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.waitUntilFeaturesDiscovered = utils.getResolveablePromise();
|
2017-11-10 15:53:59 +01:00
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
this.dataforms = new Collection();
|
2019-10-11 20:29:12 +02:00
|
|
|
let id = `converse.dataforms-${this.get('jid')}`;
|
|
|
|
this.dataforms.browserStorage = _converse.createStore(id, 'session');
|
2017-11-10 15:53:59 +01:00
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
this.features = new Collection();
|
2019-10-11 20:29:12 +02:00
|
|
|
id = `converse.features-${this.get('jid')}`;
|
|
|
|
this.features.browserStorage = _converse.createStore(id, 'session');
|
2019-09-06 14:34:59 +02:00
|
|
|
this.listenTo(this.features, 'add', this.onFeatureAdded)
|
2018-02-07 17:23:04 +01:00
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
this.fields = new Collection();
|
2019-10-11 20:29:12 +02:00
|
|
|
id = `converse.fields-${this.get('jid')}`;
|
|
|
|
this.fields.browserStorage = _converse.createStore(id, 'session');
|
2019-09-06 14:34:59 +02:00
|
|
|
this.listenTo(this.fields, 'add', this.onFieldAdded)
|
2017-07-21 17:38:08 +02:00
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
this.identities = new Collection();
|
2019-10-11 20:29:12 +02:00
|
|
|
id = `converse.identities-${this.get('jid')}`;
|
|
|
|
this.identities.browserStorage = _converse.createStore(id, 'session');
|
2019-06-25 11:18:06 +02:00
|
|
|
this.fetchFeatures(options);
|
2017-07-21 17:38:08 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
this.items = new _converse.DiscoEntities();
|
2019-10-11 20:29:12 +02:00
|
|
|
id = `converse.disco-items-${this.get('jid')}`;
|
|
|
|
this.items.browserStorage = _converse.createStore(id, 'session');
|
2018-10-23 03:41:38 +02:00
|
|
|
this.items.fetch();
|
|
|
|
},
|
2018-09-12 12:29:47 +02:00
|
|
|
|
2019-03-29 23:47:56 +01:00
|
|
|
/**
|
|
|
|
* Returns a Promise which resolves with a map indicating
|
|
|
|
* whether a given identity is provided by this entity.
|
|
|
|
* @private
|
|
|
|
* @method _converse.DiscoEntity#getIdentity
|
|
|
|
* @param { String } category - The identity category
|
|
|
|
* @param { String } type - The identity type
|
|
|
|
*/
|
2019-01-10 13:09:14 +01:00
|
|
|
async getIdentity (category, type) {
|
|
|
|
await this.waitUntilFeaturesDiscovered;
|
|
|
|
return this.identities.findWhere({
|
|
|
|
'category': category,
|
|
|
|
'type': type
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
},
|
2017-07-21 17:38:08 +02:00
|
|
|
|
2019-03-29 23:47:56 +01:00
|
|
|
/**
|
|
|
|
* Returns a Promise which resolves with a map indicating
|
|
|
|
* whether a given feature is supported.
|
|
|
|
* @private
|
|
|
|
* @method _converse.DiscoEntity#hasFeature
|
|
|
|
* @param { String } feature - The feature that might be supported.
|
|
|
|
*/
|
2019-02-13 15:49:51 +01:00
|
|
|
async hasFeature (feature) {
|
|
|
|
await this.waitUntilFeaturesDiscovered
|
|
|
|
if (this.features.findWhere({'var': feature})) {
|
|
|
|
return this;
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onFeatureAdded (feature) {
|
|
|
|
feature.entity = this;
|
2019-03-29 15:47:23 +01:00
|
|
|
/**
|
|
|
|
* Triggered when Converse has learned of a service provided by the XMPP server.
|
|
|
|
* See XEP-0030.
|
|
|
|
* @event _converse#serviceDiscovered
|
2019-09-19 16:54:55 +02:00
|
|
|
* @type { Model }
|
2019-03-29 15:47:23 +01:00
|
|
|
* @example _converse.api.listen.on('featuresDiscovered', feature => { ... });
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('serviceDiscovered', feature);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onFieldAdded (field) {
|
|
|
|
field.entity = this;
|
2019-03-29 15:47:23 +01:00
|
|
|
/**
|
|
|
|
* Triggered when Converse has learned of a disco extension field.
|
|
|
|
* See XEP-0030.
|
|
|
|
* @event _converse#discoExtensionFieldDiscovered
|
|
|
|
* @example _converse.api.listen.on('discoExtensionFieldDiscovered', () => { ... });
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('discoExtensionFieldDiscovered', field);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
2019-10-24 14:29:15 +02:00
|
|
|
async fetchFeatures (options) {
|
|
|
|
if (options.ignore_cache) {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.queryInfo();
|
|
|
|
} else {
|
2019-10-24 14:29:15 +02:00
|
|
|
const store_id = this.features.browserStorage.name;
|
|
|
|
const result = await this.features.browserStorage.store.getItem(store_id);
|
|
|
|
if (result && result.length === 0 || result === null) {
|
|
|
|
this.queryInfo();
|
|
|
|
} else {
|
|
|
|
this.features.fetch({
|
|
|
|
add: true,
|
|
|
|
success: () => {
|
|
|
|
this.waitUntilFeaturesDiscovered.resolve(this);
|
|
|
|
this.trigger('featuresDiscovered');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.identities.fetch({add: true});
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-12-17 14:36:55 +01:00
|
|
|
async queryInfo () {
|
2019-05-22 18:03:04 +02:00
|
|
|
let stanza;
|
2018-12-17 14:36:55 +01:00
|
|
|
try {
|
2020-03-31 13:15:57 +02:00
|
|
|
stanza = await api.disco.info(this.get('jid'), null);
|
2019-03-31 19:36:04 +02:00
|
|
|
} catch (iq) {
|
2020-04-10 14:41:23 +02:00
|
|
|
iq === null ? log.error(`Timeout for disco#info query for ${this.get('jid')}`) : log.error(iq);
|
2019-03-31 19:36:04 +02:00
|
|
|
this.waitUntilFeaturesDiscovered.resolve(this);
|
2019-06-12 06:09:08 +02:00
|
|
|
return;
|
2018-12-17 14:36:55 +01:00
|
|
|
}
|
2019-05-22 18:03:04 +02:00
|
|
|
this.onInfo(stanza);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2018-03-29 16:12:19 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
onDiscoItems (stanza) {
|
2019-09-07 22:00:28 +02:00
|
|
|
sizzle(`query[xmlns="${Strophe.NS.DISCO_ITEMS}"] item`, stanza).forEach(item => {
|
2018-10-23 03:41:38 +02:00
|
|
|
if (item.getAttribute("node")) {
|
2019-09-07 22:00:28 +02:00
|
|
|
// XXX: Ignore nodes for now.
|
2018-10-23 03:41:38 +02:00
|
|
|
// See: https://xmpp.org/extensions/xep-0030.html#items-nodes
|
2017-07-21 17:38:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
const jid = item.getAttribute('jid');
|
2019-07-29 10:19:05 +02:00
|
|
|
if (this.items.get(jid) === undefined) {
|
2018-10-23 03:41:38 +02:00
|
|
|
const entity = _converse.disco_entities.get(jid);
|
|
|
|
if (entity) {
|
|
|
|
this.items.add(entity);
|
|
|
|
} else {
|
|
|
|
this.items.create({'jid': jid});
|
|
|
|
}
|
2017-07-21 17:38:08 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-10-24 18:28:28 +02:00
|
|
|
async queryForItems () {
|
2019-11-26 21:56:37 +01:00
|
|
|
if (isEmpty(this.identities.where({'category': 'server'}))) {
|
2018-10-23 03:41:38 +02:00
|
|
|
// Don't fetch features and items if this is not a
|
|
|
|
// server or a conference component.
|
|
|
|
return;
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
const stanza = await api.disco.items(this.get('jid'));
|
2018-10-24 18:28:28 +02:00
|
|
|
this.onDiscoItems(stanza);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
onInfo (stanza) {
|
2019-05-23 14:26:20 +02:00
|
|
|
Array.from(stanza.querySelectorAll('identity')).forEach(identity => {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.identities.create({
|
|
|
|
'category': identity.getAttribute('category'),
|
|
|
|
'type': identity.getAttribute('type'),
|
|
|
|
'name': identity.getAttribute('name')
|
2017-07-21 17:38:08 +02:00
|
|
|
});
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
2018-09-12 12:29:47 +02:00
|
|
|
|
2019-09-07 22:00:28 +02:00
|
|
|
sizzle(`x[type="result"][xmlns="${Strophe.NS.XFORM}"]`, stanza).forEach(form => {
|
2018-10-23 03:41:38 +02:00
|
|
|
const data = {};
|
2019-09-07 22:00:28 +02:00
|
|
|
sizzle('field', form).forEach(field => {
|
2018-10-23 03:41:38 +02:00
|
|
|
data[field.getAttribute('var')] = {
|
2020-03-24 12:26:34 +01:00
|
|
|
'value': field.querySelector('value')?.textContent,
|
2018-10-23 03:41:38 +02:00
|
|
|
'type': field.getAttribute('type')
|
|
|
|
};
|
2018-09-12 12:29:47 +02:00
|
|
|
});
|
2018-10-23 03:41:38 +02:00
|
|
|
this.dataforms.create(data);
|
|
|
|
});
|
2018-09-12 12:29:47 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
if (stanza.querySelector(`feature[var="${Strophe.NS.DISCO_ITEMS}"]`)) {
|
|
|
|
this.queryForItems();
|
2017-07-21 12:38:16 +02:00
|
|
|
}
|
2019-11-26 21:16:36 +01:00
|
|
|
Array.from(stanza.querySelectorAll('feature')).forEach(feature => {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.features.create({
|
|
|
|
'var': feature.getAttribute('var'),
|
|
|
|
'from': stanza.getAttribute('from')
|
|
|
|
});
|
|
|
|
});
|
2017-07-21 12:38:16 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
// XEP-0128 Service Discovery Extensions
|
2019-06-20 12:04:05 +02:00
|
|
|
sizzle('x[type="result"][xmlns="jabber:x:data"] field', stanza).forEach(field => {
|
2018-10-23 03:41:38 +02:00
|
|
|
this.fields.create({
|
|
|
|
'var': field.getAttribute('var'),
|
2020-03-24 12:26:34 +01:00
|
|
|
'value': field.querySelector('value')?.textContent,
|
2018-10-23 03:41:38 +02:00
|
|
|
'from': stanza.getAttribute('from')
|
|
|
|
});
|
|
|
|
});
|
2017-07-21 12:38:16 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
this.waitUntilFeaturesDiscovered.resolve(this);
|
|
|
|
this.trigger('featuresDiscovered');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-09-19 16:54:55 +02:00
|
|
|
_converse.DiscoEntities = Collection.extend({
|
2018-10-23 03:41:38 +02:00
|
|
|
model: _converse.DiscoEntity,
|
|
|
|
|
|
|
|
fetchEntities () {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.fetch({
|
|
|
|
add: true,
|
|
|
|
success: resolve,
|
2019-12-17 13:39:32 +01:00
|
|
|
error (m, e) {
|
|
|
|
log.error(e);
|
2018-10-23 03:41:38 +02:00
|
|
|
reject (new Error("Could not fetch disco entities"));
|
|
|
|
}
|
2017-07-21 12:38:16 +02:00
|
|
|
});
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-11-26 21:16:36 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
function addClientFeatures () {
|
2019-03-04 17:49:44 +01:00
|
|
|
// See https://xmpp.org/registrar/disco-categories.html
|
2020-03-31 13:15:57 +02:00
|
|
|
api.disco.own.identities.add('client', 'web', 'Converse');
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.disco.own.features.add(Strophe.NS.CHATSTATES);
|
|
|
|
api.disco.own.features.add(Strophe.NS.DISCO_INFO);
|
|
|
|
api.disco.own.features.add(Strophe.NS.ROSTERX); // Limited support
|
|
|
|
if (api.settings.get("message_carbons")) {
|
|
|
|
api.disco.own.features.add(Strophe.NS.CARBONS);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2019-03-29 15:47:23 +01:00
|
|
|
/**
|
|
|
|
* Triggered in converse-disco once the core disco features of
|
|
|
|
* Converse have been added.
|
|
|
|
* @event _converse#addClientFeatures
|
|
|
|
* @example _converse.api.listen.on('addClientFeatures', () => { ... });
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('addClientFeatures');
|
2018-10-23 03:41:38 +02:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2019-11-26 21:16:36 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
function initStreamFeatures () {
|
2019-11-26 21:16:36 +01:00
|
|
|
// Initialize the stream_features collection, and if we're
|
|
|
|
// re-attaching to a pre-existing BOSH session, we restore the
|
|
|
|
// features from cache.
|
|
|
|
// Otherwise the features will be created once we've received them
|
|
|
|
// from the server (see populateStreamFeatures).
|
|
|
|
if (!_converse.stream_features) {
|
|
|
|
const bare_jid = Strophe.getBareJidFromJid(_converse.jid);
|
|
|
|
const id = `converse.stream-features-${bare_jid}`;
|
2020-03-31 13:15:57 +02:00
|
|
|
api.promises.add('streamFeaturesAdded');
|
2019-09-19 16:54:55 +02:00
|
|
|
_converse.stream_features = new Collection();
|
2019-10-11 20:29:12 +02:00
|
|
|
_converse.stream_features.browserStorage = _converse.createStore(id, "session");
|
2018-05-29 12:00:23 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2017-07-21 12:38:16 +02:00
|
|
|
|
2019-11-26 21:16:36 +01:00
|
|
|
|
|
|
|
function populateStreamFeatures () {
|
|
|
|
// Strophe.js sets the <stream:features> element on the
|
|
|
|
// Strophe.Connection instance (_converse.connection).
|
|
|
|
//
|
|
|
|
// Once this is done, we populate the _converse.stream_features collection
|
|
|
|
// and trigger streamFeaturesAdded.
|
|
|
|
initStreamFeatures();
|
|
|
|
Array.from(_converse.connection.features.childNodes).forEach(feature => {
|
|
|
|
_converse.stream_features.create({
|
|
|
|
'name': feature.nodeName,
|
|
|
|
'xmlns': feature.getAttribute('xmlns')
|
|
|
|
});
|
|
|
|
});
|
|
|
|
notifyStreamFeaturesAdded();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function notifyStreamFeaturesAdded () {
|
|
|
|
/**
|
|
|
|
* Triggered as soon as the stream features are known.
|
|
|
|
* If you want to check whether a stream feature is supported before proceeding,
|
|
|
|
* then you'll first want to wait for this event.
|
|
|
|
* @event _converse#streamFeaturesAdded
|
|
|
|
* @example _converse.api.listen.on('streamFeaturesAdded', () => { ... });
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('streamFeaturesAdded');
|
2019-11-26 21:16:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-01 08:55:44 +02:00
|
|
|
const plugin = this;
|
|
|
|
plugin._identities = [];
|
|
|
|
plugin._features = [];
|
|
|
|
|
|
|
|
function onDiscoInfoRequest (stanza) {
|
|
|
|
const node = stanza.getElementsByTagName('query')[0].getAttribute('node');
|
|
|
|
const attrs = {xmlns: Strophe.NS.DISCO_INFO};
|
|
|
|
if (node) { attrs.node = node; }
|
|
|
|
|
|
|
|
const iqresult = $iq({'type': 'result', 'id': stanza.getAttribute('id')});
|
|
|
|
const from = stanza.getAttribute('from');
|
|
|
|
if (from !== null) {
|
|
|
|
iqresult.attrs({'to': from});
|
|
|
|
}
|
|
|
|
iqresult.c('query', attrs);
|
2019-09-07 22:00:28 +02:00
|
|
|
plugin._identities.forEach(identity => {
|
2019-08-01 08:55:44 +02:00
|
|
|
const attrs = {
|
|
|
|
'category': identity.category,
|
|
|
|
'type': identity.type
|
|
|
|
};
|
|
|
|
if (identity.name) {
|
|
|
|
attrs.name = identity.name;
|
|
|
|
}
|
|
|
|
if (identity.lang) {
|
|
|
|
attrs['xml:lang'] = identity.lang;
|
|
|
|
}
|
|
|
|
iqresult.c('identity', attrs).up();
|
|
|
|
});
|
2019-09-16 14:02:14 +02:00
|
|
|
plugin._features.forEach(feature => iqresult.c('feature', {'var': feature}).up());
|
2020-03-31 13:15:57 +02:00
|
|
|
api.send(iqresult.tree());
|
2019-08-01 08:55:44 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-24 18:28:28 +02:00
|
|
|
async function initializeDisco () {
|
2018-10-23 03:41:38 +02:00
|
|
|
addClientFeatures();
|
|
|
|
_converse.connection.addHandler(onDiscoInfoRequest, Strophe.NS.DISCO_INFO, 'iq', 'get', null, null);
|
2017-07-21 17:38:08 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
_converse.disco_entities = new _converse.DiscoEntities();
|
2019-10-11 20:29:12 +02:00
|
|
|
const id = `converse.disco-entities-${_converse.bare_jid}`;
|
|
|
|
_converse.disco_entities.browserStorage = _converse.createStore(id, 'session');
|
2018-10-24 18:28:28 +02:00
|
|
|
const collection = await _converse.disco_entities.fetchEntities();
|
|
|
|
if (collection.length === 0 || !collection.get(_converse.domain)) {
|
|
|
|
// If we don't have an entity for our own XMPP server,
|
|
|
|
// create one.
|
|
|
|
_converse.disco_entities.create({'jid': _converse.domain});
|
|
|
|
}
|
2019-03-29 15:47:23 +01:00
|
|
|
/**
|
|
|
|
* Triggered once the `converse-disco` plugin has been initialized and the
|
|
|
|
* `_converse.disco_entities` collection will be available and populated with at
|
|
|
|
* least the service discovery features of the user's own server.
|
|
|
|
* @event _converse#discoInitialized
|
|
|
|
* @example _converse.api.listen.on('discoInitialized', () => { ... });
|
|
|
|
*/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.trigger('discoInitialized');
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2017-07-21 12:38:16 +02:00
|
|
|
|
2019-08-01 08:55:44 +02:00
|
|
|
/******************** Event Handlers ********************/
|
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('userSessionInitialized', async () => {
|
2019-11-26 21:16:36 +01:00
|
|
|
initStreamFeatures();
|
|
|
|
if (_converse.connfeedback.get('connection_status') === Strophe.Status.ATTACHED) {
|
|
|
|
// When re-attaching to a BOSH session, we fetch the stream features from the cache.
|
|
|
|
await new Promise((success, error) => _converse.stream_features.fetch({ success, error }));
|
|
|
|
notifyStreamFeaturesAdded();
|
|
|
|
}
|
|
|
|
});
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('beforeResourceBinding', populateStreamFeatures);
|
2018-05-29 12:00:23 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('reconnected', initializeDisco);
|
|
|
|
api.listen.on('connected', initializeDisco);
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('beforeTearDown', async () => {
|
|
|
|
api.promises.add('streamFeaturesAdded')
|
2018-05-29 12:00:23 +02:00
|
|
|
if (_converse.stream_features) {
|
2019-09-19 16:54:55 +02:00
|
|
|
await _converse.stream_features.clearStore();
|
2019-08-01 10:26:35 +02:00
|
|
|
delete _converse.stream_features;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('clearSession', () => {
|
2019-08-01 10:26:35 +02:00
|
|
|
if (_converse.shouldClearCache() && _converse.disco_entities) {
|
2019-09-19 16:54:55 +02:00
|
|
|
Array.from(_converse.disco_entities.models).forEach(e => e.features.clearStore());
|
|
|
|
Array.from(_converse.disco_entities.models).forEach(e => e.identities.clearStore());
|
|
|
|
Array.from(_converse.disco_entities.models).forEach(e => e.dataforms.clearStore());
|
|
|
|
Array.from(_converse.disco_entities.models).forEach(e => e.fields.clearStore());
|
|
|
|
_converse.disco_entities.clearStore();
|
2019-08-01 10:26:35 +02:00
|
|
|
delete _converse.disco_entities;
|
2018-05-29 12:00:23 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
2018-05-28 11:40:33 +02:00
|
|
|
|
2017-11-02 23:23:01 +01:00
|
|
|
|
2019-08-01 08:55:44 +02:00
|
|
|
/************************ API ************************/
|
2018-05-07 17:08:31 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
Object.assign(api, {
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
|
|
|
* The XEP-0030 service discovery API
|
|
|
|
*
|
|
|
|
* This API lets you discover information about entities on the
|
|
|
|
* XMPP network.
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @namespace api.disco
|
|
|
|
* @memberOf api
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
disco: {
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
2020-03-31 13:15:57 +02:00
|
|
|
* @namespace api.disco.stream
|
|
|
|
* @memberOf api.disco
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
stream: {
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.stream.getFeature
|
2018-10-23 03:41:38 +02:00
|
|
|
* @param {String} name The feature name
|
|
|
|
* @param {String} xmlns The XML namespace
|
|
|
|
* @example _converse.api.disco.stream.getFeature('ver', 'urn:xmpp:features:rosterver')
|
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
async getFeature (name, xmlns) {
|
2020-03-31 13:15:57 +02:00
|
|
|
await api.waitUntil('streamFeaturesAdded');
|
2019-08-05 01:39:57 +02:00
|
|
|
if (!name || !xmlns) {
|
2018-10-23 03:41:38 +02:00
|
|
|
throw new Error("name and xmlns need to be provided when calling disco.stream.getFeature");
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
if (_converse.stream_features === undefined && !api.connection.connected()) {
|
2019-08-05 10:27:38 +02:00
|
|
|
// Happens during tests when disco lookups happen asynchronously after teardown.
|
|
|
|
const msg = `Tried to get feature ${name} ${xmlns} but _converse.stream_features has been torn down`;
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn(msg);
|
2019-08-05 10:27:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
return _converse.stream_features.findWhere({'name': name, 'xmlns': xmlns});
|
2018-05-07 17:08:31 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2018-09-02 15:07:14 +02:00
|
|
|
|
2018-05-11 13:31:48 +02:00
|
|
|
/**
|
2020-03-31 13:15:57 +02:00
|
|
|
* @namespace api.disco.own
|
|
|
|
* @memberOf api.disco
|
2018-05-11 13:31:48 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
own: {
|
2018-09-02 15:07:14 +02:00
|
|
|
/**
|
2020-03-31 13:15:57 +02:00
|
|
|
* @namespace api.disco.own.identities
|
|
|
|
* @memberOf api.disco.own
|
2018-09-02 15:07:14 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
identities: {
|
2018-09-02 15:07:14 +02:00
|
|
|
/**
|
2018-10-23 03:41:38 +02:00
|
|
|
* Lets you add new identities for this client (i.e. instance of Converse)
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.own.identities.add
|
2018-10-23 03:41:38 +02:00
|
|
|
*
|
|
|
|
* @param {String} category - server, client, gateway, directory, etc.
|
|
|
|
* @param {String} type - phone, pc, web, etc.
|
|
|
|
* @param {String} name - "Converse"
|
|
|
|
* @param {String} lang - en, el, de, etc.
|
|
|
|
*
|
|
|
|
* @example _converse.api.disco.own.identities.clear();
|
2018-09-02 15:07:14 +02:00
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
add (category, type, name, lang) {
|
|
|
|
for (var i=0; i<plugin._identities.length; i++) {
|
|
|
|
if (plugin._identities[i].category == category &&
|
|
|
|
plugin._identities[i].type == type &&
|
|
|
|
plugin._identities[i].name == name &&
|
|
|
|
plugin._identities[i].lang == lang) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-28 11:40:33 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
plugin._identities.push({category: category, type: type, name: name, lang: lang});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Clears all previously registered identities.
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.own.identities.clear
|
2018-10-23 03:41:38 +02:00
|
|
|
* @example _converse.api.disco.own.identities.clear();
|
|
|
|
*/
|
|
|
|
clear () {
|
|
|
|
plugin._identities = []
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Returns all of the identities registered for this client
|
|
|
|
* (i.e. instance of Converse).
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.identities.get
|
|
|
|
* @example const identities = api.disco.own.identities.get();
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
|
|
|
get () {
|
|
|
|
return plugin._identities;
|
2018-05-28 11:40:33 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-05-11 13:31:48 +02:00
|
|
|
/**
|
2020-03-31 13:15:57 +02:00
|
|
|
* @namespace api.disco.own.features
|
|
|
|
* @memberOf api.disco.own
|
2018-05-11 13:31:48 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
features: {
|
2018-05-11 13:31:48 +02:00
|
|
|
/**
|
2018-10-23 03:41:38 +02:00
|
|
|
* Lets you register new disco features for this client (i.e. instance of Converse)
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.own.features.add
|
2018-10-23 03:41:38 +02:00
|
|
|
* @param {String} name - e.g. http://jabber.org/protocol/caps
|
|
|
|
* @example _converse.api.disco.own.features.add("http://jabber.org/protocol/caps");
|
2018-05-11 13:31:48 +02:00
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
add (name) {
|
|
|
|
for (var i=0; i<plugin._features.length; i++) {
|
|
|
|
if (plugin._features[i] == name) { return false; }
|
2018-05-11 13:31:48 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
plugin._features.push(name);
|
2018-05-11 13:31:48 +02:00
|
|
|
},
|
2018-05-11 13:42:35 +02:00
|
|
|
/**
|
2018-10-23 03:41:38 +02:00
|
|
|
* Clears all previously registered features.
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.own.features.clear
|
2018-10-23 03:41:38 +02:00
|
|
|
* @example _converse.api.disco.own.features.clear();
|
2018-05-11 13:42:35 +02:00
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
clear () {
|
|
|
|
plugin._features = []
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Returns all of the features registered for this client (i.e. instance of Converse).
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.own.features.get
|
|
|
|
* @example const features = api.disco.own.features.get();
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
|
|
|
get () {
|
|
|
|
return plugin._features;
|
2018-05-11 13:31:48 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
},
|
2018-05-11 13:31:48 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
|
|
|
* Query for information about an XMPP entity
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.info
|
2018-10-23 03:41:38 +02:00
|
|
|
* @param {string} jid The Jabber ID of the entity to query
|
|
|
|
* @param {string} [node] A specific node identifier associated with the JID
|
|
|
|
* @returns {promise} Promise which resolves once we have a result from the server.
|
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
info (jid, node) {
|
2018-10-23 03:41:38 +02:00
|
|
|
const attrs = {xmlns: Strophe.NS.DISCO_INFO};
|
|
|
|
if (node) {
|
|
|
|
attrs.node = node;
|
|
|
|
}
|
|
|
|
const info = $iq({
|
|
|
|
'from': _converse.connection.jid,
|
|
|
|
'to':jid,
|
|
|
|
'type':'get'
|
|
|
|
}).c('query', attrs);
|
2020-03-31 13:15:57 +02:00
|
|
|
return api.sendIQ(info);
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query for items associated with an XMPP entity
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.items
|
2018-10-23 03:41:38 +02:00
|
|
|
* @param {string} jid The Jabber ID of the entity to query for items
|
|
|
|
* @param {string} [node] A specific node identifier associated with the JID
|
|
|
|
* @returns {promise} Promise which resolves once we have a result from the server.
|
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
items (jid, node) {
|
2018-10-23 03:41:38 +02:00
|
|
|
const attrs = {'xmlns': Strophe.NS.DISCO_ITEMS};
|
|
|
|
if (node) {
|
|
|
|
attrs.node = node;
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
return api.sendIQ(
|
2018-10-23 03:41:38 +02:00
|
|
|
$iq({
|
2018-05-07 17:08:31 +02:00
|
|
|
'from': _converse.connection.jid,
|
|
|
|
'to':jid,
|
|
|
|
'type':'get'
|
2018-10-23 03:41:38 +02:00
|
|
|
}).c('query', attrs)
|
|
|
|
);
|
|
|
|
},
|
2017-11-10 15:53:59 +01:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
|
|
|
* Namespace for methods associated with disco entities
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @namespace api.disco.entities
|
|
|
|
* @memberOf api.disco
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
entities: {
|
2018-09-12 12:29:47 +02:00
|
|
|
/**
|
2019-06-25 11:18:06 +02:00
|
|
|
* Get the corresponding `DiscoEntity` instance.
|
2018-09-12 12:29:47 +02:00
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.entities.get
|
2018-10-23 03:41:38 +02:00
|
|
|
* @param {string} jid The Jabber ID of the entity
|
|
|
|
* @param {boolean} [create] Whether the entity should be created if it doesn't exist.
|
|
|
|
* @example _converse.api.disco.entities.get(jid);
|
2018-09-12 12:29:47 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
async get (jid, create=false) {
|
2020-03-31 13:15:57 +02:00
|
|
|
await api.waitUntil('discoInitialized');
|
2019-08-05 01:39:57 +02:00
|
|
|
if (!jid) {
|
2018-10-24 18:28:28 +02:00
|
|
|
return _converse.disco_entities;
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
if (_converse.disco_entities === undefined && !api.connection.connected()) {
|
2019-08-05 10:27:38 +02:00
|
|
|
// Happens during tests when disco lookups happen asynchronously after teardown.
|
|
|
|
const msg = `Tried to look up entity ${jid} but _converse.disco_entities has been torn down`;
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn(msg);
|
2019-08-05 10:27:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-10-24 18:28:28 +02:00
|
|
|
const entity = _converse.disco_entities.get(jid);
|
|
|
|
if (entity || !create) {
|
|
|
|
return entity;
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
return api.disco.entities.create(jid);
|
2019-06-25 11:18:06 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new disco entity. It's identity and features
|
|
|
|
* will automatically be fetched from cache or from the
|
|
|
|
* XMPP server.
|
|
|
|
*
|
|
|
|
* Fetching from cache can be disabled by passing in
|
|
|
|
* `ignore_cache: true` in the options parameter.
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.entities.create
|
2019-06-25 11:18:06 +02:00
|
|
|
* @param {string} jid The Jabber ID of the entity
|
|
|
|
* @param {object} [options] Additional options
|
|
|
|
* @param {boolean} [options.ignore_cache]
|
|
|
|
* If true, fetch all features from the XMPP server instead of restoring them from cache
|
|
|
|
* @example _converse.api.disco.entities.create(jid, {'ignore_cache': true});
|
|
|
|
*/
|
|
|
|
create (jid, options) {
|
|
|
|
return _converse.disco_entities.create({'jid': jid}, options);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
},
|
2018-02-07 17:23:04 +01:00
|
|
|
|
2019-05-07 09:53:36 +02:00
|
|
|
/**
|
2020-03-31 13:15:57 +02:00
|
|
|
* @namespace api.disco.features
|
|
|
|
* @memberOf api.disco
|
2019-05-07 09:53:36 +02:00
|
|
|
*/
|
2019-06-20 13:35:33 +02:00
|
|
|
features: {
|
2019-05-07 09:53:36 +02:00
|
|
|
/**
|
|
|
|
* Return a given feature of a disco entity
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.features.get
|
2019-05-07 09:53:36 +02:00
|
|
|
* @param {string} feature The feature that might be
|
|
|
|
* supported. In the XML stanza, this is the `var`
|
|
|
|
* attribute of the `<feature>` element. For
|
|
|
|
* example: `http://jabber.org/protocol/muc`
|
|
|
|
* @param {string} jid The JID of the entity
|
|
|
|
* (and its associated items) which should be queried
|
|
|
|
* @returns {promise} A promise which resolves with a list containing
|
|
|
|
* _converse.Entity instances representing the entity
|
|
|
|
* itself or those items associated with the entity if
|
|
|
|
* they support the given feature.
|
|
|
|
* @example
|
2020-03-31 13:15:57 +02:00
|
|
|
* api.disco.features.get(Strophe.NS.MAM, _converse.bare_jid);
|
2019-05-07 09:53:36 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
async get (feature, jid) {
|
2019-08-05 01:39:57 +02:00
|
|
|
if (!jid) {
|
2019-05-07 09:53:36 +02:00
|
|
|
throw new TypeError('You need to provide an entity JID');
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
await api.waitUntil('discoInitialized');
|
|
|
|
let entity = await api.disco.entities.get(jid, true);
|
2019-08-05 10:27:38 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
if (_converse.disco_entities === undefined && !api.connection.connected()) {
|
2019-08-05 10:27:38 +02:00
|
|
|
// Happens during tests when disco lookups happen asynchronously after teardown.
|
|
|
|
const msg = `Tried to get feature ${feature} for ${jid} but _converse.disco_entities has been torn down`;
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn(msg);
|
2019-08-05 10:27:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-05-07 09:53:36 +02:00
|
|
|
entity = await entity.waitUntilFeaturesDiscovered;
|
2019-11-26 21:56:37 +01:00
|
|
|
const promises = [...entity.items.map(i => i.hasFeature(feature)), entity.hasFeature(feature)];
|
2019-05-07 09:53:36 +02:00
|
|
|
const result = await Promise.all(promises);
|
2019-11-26 21:56:37 +01:00
|
|
|
return result.filter(isObject);
|
2019-05-14 11:38:41 +02:00
|
|
|
}
|
2019-05-07 09:53:36 +02:00
|
|
|
},
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
|
|
|
* Used to determine whether an entity supports a given feature.
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.supports
|
2018-10-23 03:41:38 +02:00
|
|
|
* @param {string} feature The feature that might be
|
|
|
|
* supported. In the XML stanza, this is the `var`
|
|
|
|
* attribute of the `<feature>` element. For
|
|
|
|
* example: `http://jabber.org/protocol/muc`
|
|
|
|
* @param {string} jid The JID of the entity
|
|
|
|
* (and its associated items) which should be queried
|
2019-05-07 09:53:36 +02:00
|
|
|
* @returns {promise} A promise which resolves with `true` or `false`.
|
2018-10-23 03:41:38 +02:00
|
|
|
* @example
|
2020-03-31 13:15:57 +02:00
|
|
|
* if (await api.disco.supports(Strophe.NS.MAM, _converse.bare_jid)) {
|
2019-05-07 09:53:36 +02:00
|
|
|
* // The feature is supported
|
|
|
|
* } else {
|
|
|
|
* // The feature is not supported
|
|
|
|
* }
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
async supports (feature, jid) {
|
2020-03-31 13:15:57 +02:00
|
|
|
const features = await api.disco.features.get(feature, jid);
|
2019-05-07 09:53:36 +02:00
|
|
|
return features.length > 0;
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2018-10-05 04:39:19 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
2020-01-16 13:01:57 +01:00
|
|
|
* Refresh the features, fields and identities associated with a
|
2018-10-23 03:41:38 +02:00
|
|
|
* disco entity by refetching them from the server
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.refresh
|
2018-10-23 03:41:38 +02:00
|
|
|
* @param {string} jid The JID of the entity whose features are refreshed.
|
|
|
|
* @returns {promise} A promise which resolves once the features have been refreshed
|
|
|
|
* @example
|
2020-03-31 13:15:57 +02:00
|
|
|
* await api.disco.refresh('room@conference.example.org');
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
2020-01-16 13:01:57 +01:00
|
|
|
async refresh (jid) {
|
2019-08-05 01:39:57 +02:00
|
|
|
if (!jid) {
|
2020-01-16 13:01:57 +01:00
|
|
|
throw new TypeError('api.disco.refresh: You need to provide an entity JID');
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
await api.waitUntil('discoInitialized');
|
|
|
|
let entity = await api.disco.entities.get(jid);
|
2019-06-25 11:18:06 +02:00
|
|
|
if (entity) {
|
|
|
|
entity.features.reset();
|
|
|
|
entity.fields.reset();
|
|
|
|
entity.identities.reset();
|
|
|
|
if (!entity.waitUntilFeaturesDiscovered.isPending) {
|
|
|
|
entity.waitUntilFeaturesDiscovered = utils.getResolveablePromise()
|
|
|
|
}
|
|
|
|
entity.queryInfo();
|
|
|
|
} else {
|
|
|
|
// Create it if it doesn't exist
|
2020-03-31 13:15:57 +02:00
|
|
|
entity = await api.disco.entities.create(jid, {'ignore_cache': true});
|
2019-06-20 13:35:33 +02:00
|
|
|
}
|
2018-10-24 18:28:28 +02:00
|
|
|
return entity.waitUntilFeaturesDiscovered;
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2018-09-12 12:29:47 +02:00
|
|
|
|
2020-01-16 13:01:57 +01:00
|
|
|
/**
|
2020-03-31 13:15:57 +02:00
|
|
|
* @deprecated Use {@link api.disco.refresh} instead.
|
|
|
|
* @method api.disco.refreshFeatures
|
2020-01-16 13:01:57 +01:00
|
|
|
*/
|
|
|
|
refreshFeatures (jid) {
|
2020-03-31 13:15:57 +02:00
|
|
|
return api.refresh(jid);
|
2020-01-16 13:01:57 +01:00
|
|
|
},
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
|
|
|
* Return all the features associated with a disco entity
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.getFeatures
|
2018-10-23 03:41:38 +02:00
|
|
|
* @param {string} jid The JID of the entity whose features are returned.
|
|
|
|
* @returns {promise} A promise which resolves with the returned features
|
|
|
|
* @example
|
2020-03-31 13:15:57 +02:00
|
|
|
* const features = await api.disco.getFeatures('room@conference.example.org');
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
async getFeatures (jid) {
|
2019-08-05 01:39:57 +02:00
|
|
|
if (!jid) {
|
2018-10-23 03:41:38 +02:00
|
|
|
throw new TypeError('api.disco.getFeatures: You need to provide an entity JID');
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
await api.waitUntil('discoInitialized');
|
|
|
|
let entity = await api.disco.entities.get(jid, true);
|
2018-10-24 18:28:28 +02:00
|
|
|
entity = await entity.waitUntilFeaturesDiscovered;
|
|
|
|
return entity.features;
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
2018-09-12 12:29:47 +02:00
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
|
|
|
* Return all the service discovery extensions fields
|
|
|
|
* associated with an entity.
|
|
|
|
*
|
|
|
|
* See [XEP-0129: Service Discovery Extensions](https://xmpp.org/extensions/xep-0128.html)
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.getFields
|
2018-10-23 03:41:38 +02:00
|
|
|
* @param {string} jid The JID of the entity whose fields are returned.
|
|
|
|
* @example
|
2020-03-31 13:15:57 +02:00
|
|
|
* const fields = await api.disco.getFields('room@conference.example.org');
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
async getFields (jid) {
|
2019-08-05 01:39:57 +02:00
|
|
|
if (!jid) {
|
2018-10-23 03:41:38 +02:00
|
|
|
throw new TypeError('api.disco.getFields: You need to provide an entity JID');
|
2017-11-02 23:23:01 +01:00
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
await api.waitUntil('discoInitialized');
|
|
|
|
let entity = await api.disco.entities.get(jid, true);
|
2018-10-24 18:28:28 +02:00
|
|
|
entity = await entity.waitUntilFeaturesDiscovered;
|
|
|
|
return entity.fields;
|
2018-10-23 03:41:38 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the identity (with the given category and type) for a given disco entity.
|
|
|
|
*
|
|
|
|
* For example, when determining support for PEP (personal eventing protocol), you
|
|
|
|
* want to know whether the user's own JID has an identity with
|
|
|
|
* `category='pubsub'` and `type='pep'` as explained in this section of
|
|
|
|
* XEP-0163: https://xmpp.org/extensions/xep-0163.html#support
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.disco.getIdentity
|
2018-10-23 03:41:38 +02:00
|
|
|
* @param {string} The identity category.
|
|
|
|
* In the XML stanza, this is the `category`
|
|
|
|
* attribute of the `<identity>` element.
|
|
|
|
* For example: 'pubsub'
|
|
|
|
* @param {string} type The identity type.
|
|
|
|
* In the XML stanza, this is the `type`
|
|
|
|
* attribute of the `<identity>` element.
|
|
|
|
* For example: 'pep'
|
|
|
|
* @param {string} jid The JID of the entity which might have the identity
|
|
|
|
* @returns {promise} A promise which resolves with a map indicating
|
|
|
|
* whether an identity with a given type is provided by the entity.
|
|
|
|
* @example
|
2020-03-31 13:15:57 +02:00
|
|
|
* api.disco.getIdentity('pubsub', 'pep', _converse.bare_jid).then(
|
2018-10-23 03:41:38 +02:00
|
|
|
* function (identity) {
|
2019-08-05 01:39:57 +02:00
|
|
|
* if (identity) {
|
2018-10-23 03:41:38 +02:00
|
|
|
* // The entity DOES have this identity
|
2019-08-05 01:39:57 +02:00
|
|
|
* } else {
|
|
|
|
* // The entity DOES NOT have this identity
|
2018-10-23 03:41:38 +02:00
|
|
|
* }
|
|
|
|
* }
|
2019-11-06 11:01:34 +01:00
|
|
|
* ).catch(e => log.error(e));
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
2019-06-12 06:09:08 +02:00
|
|
|
async getIdentity (category, type, jid) {
|
2020-03-31 13:15:57 +02:00
|
|
|
const e = await api.disco.entities.get(jid, true);
|
|
|
|
if (e === undefined && !api.connection.connected()) {
|
2019-08-05 10:27:38 +02:00
|
|
|
// Happens during tests when disco lookups happen asynchronously after teardown.
|
|
|
|
const msg = `Tried to look up category ${category} for ${jid} but _converse.disco_entities has been torn down`;
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn(msg);
|
2019-08-05 10:27:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-10-24 18:28:28 +02:00
|
|
|
return e.getIdentity(category, type);
|
2017-11-02 23:23:01 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|