Commit d4ceb6d3 authored by JC Brand's avatar JC Brand

Add the ability to query for a disco identity

parent 2f19fd98
...@@ -2,9 +2,15 @@ ...@@ -2,9 +2,15 @@
## 3.3.3 (Unreleased) ## 3.3.3 (Unreleased)
### Bugfixes
- Attribute error when empty IQ stanza is returned for vCard query - Attribute error when empty IQ stanza is returned for vCard query
### New features
- XEP-0382 Spoiler Messages (currently only for private chats) - XEP-0382 Spoiler Messages (currently only for private chats)
### API changes
- New API method `_converse.disco.getIdentity` to check whether a JID has a given identity.
## 3.3.2 (2018-01-29) ## 3.3.2 (2018-01-29)
### Bugfixes ### Bugfixes
......
...@@ -437,6 +437,39 @@ The **disco** grouping ...@@ -437,6 +437,39 @@ The **disco** grouping
This grouping collects API functions related to `service discovery This grouping collects API functions related to `service discovery
<https://xmpp.org/extensions/xep-0030.html>`_. <https://xmpp.org/extensions/xep-0030.html>`_.
getIdentity
~~~~~~~~~~~
Paramters:
* (String) category
* (String) type
* (String) entity JID
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
.. code-block:: javascript
converse.plugins.add('myplugin', {
initialize: function () {
_converse.api.disco.getIdentity('pubsub', 'pep', _converse.bare_jid).then(
function (identity) {
if (_.isNil(identity)) {
// The entity DOES NOT have this identity
} else {
// The entity DOES have this identity
}
}
).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
}
});
supports supports
~~~~~~~~ ~~~~~~~~
...@@ -450,7 +483,7 @@ Returns a `Promise` which, when resolved, returns a map/object with keys ...@@ -450,7 +483,7 @@ Returns a `Promise` which, when resolved, returns a map/object with keys
converse.plugins.add('myplugin', { converse.plugins.add('myplugin', {
initialize: function () { initialize: function () {
_converse.api.disco.supports(_converse.bare_jid, Strophe.NS.MAM).then( _converse.api.disco.supports(Strophe.NS.MAM, _converse.bare_jid).then(
function (value) { function (value) {
// `value` is a map with two keys, `supported` and `feature`. // `value` is a map with two keys, `supported` and `feature`.
......
...@@ -65,6 +65,29 @@ ...@@ -65,6 +65,29 @@
}, },
getIdentity (category, type) {
/* Returns a Promise which resolves with a map indicating
* whether a given identity is provided.
*
* Parameters:
* (String) category - The identity category
* (String) type - The identity type
*/
const entity = this;
return new Promise((resolve, reject) => {
function fulfillPromise () {
const model = entity.identities.findWhere({
'category': category,
'type': type
});
resolve(model);
}
entity.waitUntilFeaturesDiscovered
.then(fulfillPromise)
.catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
});
},
hasFeature (feature) { hasFeature (feature) {
/* Returns a Promise which resolves with a map indicating /* Returns a Promise which resolves with a map indicating
* whether a given feature is supported. * whether a given feature is supported.
...@@ -130,8 +153,8 @@ ...@@ -130,8 +153,8 @@
_.forEach(stanza.querySelectorAll('identity'), (identity) => { _.forEach(stanza.querySelectorAll('identity'), (identity) => {
this.identities.create({ this.identities.create({
'category': identity.getAttribute('category'), 'category': identity.getAttribute('category'),
'type': stanza.getAttribute('type'), 'type': identity.getAttribute('type'),
'name': stanza.getAttribute('name') 'name': identity.getAttribute('name')
}); });
}); });
if (stanza.querySelector('feature[var="'+Strophe.NS.DISCO_ITEMS+'"]')) { if (stanza.querySelector('feature[var="'+Strophe.NS.DISCO_ITEMS+'"]')) {
...@@ -245,7 +268,28 @@ ...@@ -245,7 +268,28 @@
return _converse.api.waitUntil('discoInitialized').then(() => { return _converse.api.waitUntil('discoInitialized').then(() => {
const entity = _converse.api.disco.entities.get(entity_jid, true); const entity = _converse.api.disco.entities.get(entity_jid, true);
return entity.hasFeature(feature); return entity.hasFeature(feature);
}); }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
},
'getIdentity' (category, type, entity_jid) {
/* Returns a Promise which resolves with a map indicating
* whether an identity with a given type is provided by
* the entity.
*
* Parameters:
* (String) category - The identity category.
* In the XML stanza, this is the `category`
* attribute of the `<identity>` element.
* For example: 'pubsub'
* (String) type - The identity type.
* In the XML stanza, this is the `type`
* attribute of the `<identity>` element.
* For example: 'pep'
*/
return _converse.api.waitUntil('discoInitialized').then(() => {
const entity = _converse.api.disco.entities.get(entity_jid, true);
return entity.getIdentity(category, type);
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
} }
} }
}); });
......
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