Commit 197451db authored by JC Brand's avatar JC Brand

Add support XEP-0128 service discovery extensions

Also add new API methods which return features and XEP-128 fields.
parent acef8fea
...@@ -46,6 +46,12 @@ ...@@ -46,6 +46,12 @@
); );
this.features.on('add', this.onFeatureAdded, this); this.features.on('add', this.onFeatureAdded, this);
this.fields = new Backbone.Collection();
this.fields.browserStorage = new Backbone.BrowserStorage.session(
b64_sha1(`converse.fields-${this.get('jid')}`)
);
this.fields.on('add', this.onFieldAdded, this);
this.identities = new Backbone.Collection(); this.identities = new Backbone.Collection();
this.identities.browserStorage = new Backbone.BrowserStorage.session( this.identities.browserStorage = new Backbone.BrowserStorage.session(
b64_sha1(`converse.identities-${this.get('jid')}`) b64_sha1(`converse.identities-${this.get('jid')}`)
...@@ -109,6 +115,11 @@ ...@@ -109,6 +115,11 @@
_converse.emit('serviceDiscovered', feature); _converse.emit('serviceDiscovered', feature);
}, },
onFieldAdded (field) {
field.entity = this;
_converse.emit('discoExtensionFieldDiscovered', field);
},
fetchFeatures () { fetchFeatures () {
if (this.features.browserStorage.records.length === 0) { if (this.features.browserStorage.records.length === 0) {
this.queryInfo(); this.queryInfo();
...@@ -184,12 +195,22 @@ ...@@ -184,12 +195,22 @@
if (stanza.querySelector(`feature[var="${Strophe.NS.DISCO_ITEMS}"]`)) { if (stanza.querySelector(`feature[var="${Strophe.NS.DISCO_ITEMS}"]`)) {
this.queryForItems(); this.queryForItems();
} }
_.forEach(stanza.querySelectorAll('feature'), (feature) => { _.forEach(stanza.querySelectorAll('feature'), feature => {
this.features.create({ this.features.create({
'var': feature.getAttribute('var'), 'var': feature.getAttribute('var'),
'from': stanza.getAttribute('from') 'from': stanza.getAttribute('from')
}); });
}); });
// XEP-0128 Service Discovery Extensions
_.forEach(sizzle('x[type="result"][xmlns="jabber:x:data"] field', stanza), field => {
this.fields.create({
'var': field.getAttribute('var'),
'value': _.get(field.querySelector('value'), 'textContent'),
'from': stanza.getAttribute('from')
});
});
this.waitUntilFeaturesDiscovered.resolve(this); this.waitUntilFeaturesDiscovered.resolve(this);
this.trigger('featuresDiscovered'); this.trigger('featuresDiscovered');
} }
...@@ -541,7 +562,7 @@ ...@@ -541,7 +562,7 @@
*/ */
'supports' (feature, jid) { 'supports' (feature, jid) {
if (_.isNil(jid)) { if (_.isNil(jid)) {
throw new TypeError('disco.supports: You need to provide an entity JID'); throw new TypeError('api.disco.supports: You need to provide an entity JID');
} }
return _converse.api.waitUntil('discoInitialized') return _converse.api.waitUntil('discoInitialized')
.then(() => _converse.api.disco.entities.get(jid, true)) .then(() => _converse.api.disco.entities.get(jid, true))
...@@ -555,6 +576,47 @@ ...@@ -555,6 +576,47 @@
}).then(result => f.filter(f.isObject, result)); }).then(result => f.filter(f.isObject, result));
}, },
/**
* Return all the features associated with a disco entity
*
* @method _converse.api.disco.getFeatures
* @param {string} jid The JID of the entity whose features are returned.
* @example
* const features = await _converse.api.disco.getFeatures('room@conference.example.org');
*/
'getFeatures' (jid) {
if (_.isNil(jid)) {
throw new TypeError('api.disco.getFeatures: You need to provide an entity JID');
}
return _converse.api.waitUntil('discoInitialized')
.then(() => _converse.api.disco.entities.get(jid, true))
.then(entity => entity.waitUntilFeaturesDiscovered)
.then(entity => entity.features)
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
},
/**
* Return all the service discovery extensions fields
* associated with an entity.
*
* See [XEP-0129: Service Discovery Extensions](https://xmpp.org/extensions/xep-0128.html)
*
* @method _converse.api.disco.getFields
* @param {string} jid The JID of the entity whose fields are returned.
* @example
* const fields = await _converse.api.disco.getFields('room@conference.example.org');
*/
'getFields' (jid) {
if (_.isNil(jid)) {
throw new TypeError('api.disco.getFields: You need to provide an entity JID');
}
return _converse.api.waitUntil('discoInitialized')
.then(() => _converse.api.disco.entities.get(jid, true))
.then(entity => entity.waitUntilFeaturesDiscovered)
.then(entity => entity.fields)
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
},
/** /**
* Get the identity (with the given category and type) for a given disco entity. * Get the identity (with the given category and type) for a given disco entity.
* *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment