From d4ceb6d340e4f09f88871d68f0d0cb67b2a4eb6b Mon Sep 17 00:00:00 2001 From: JC Brand Date: Wed, 7 Feb 2018 17:23:04 +0100 Subject: [PATCH] Add the ability to query for a disco identity --- CHANGES.md | 6 +++++ docs/source/developer_api.rst | 35 +++++++++++++++++++++++- src/converse-disco.js | 50 ++++++++++++++++++++++++++++++++--- 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index dd17ddfd5..3cc4937f7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,9 +2,15 @@ ## 3.3.3 (Unreleased) +### Bugfixes - Attribute error when empty IQ stanza is returned for vCard query + +### New features - 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) ### Bugfixes diff --git a/docs/source/developer_api.rst b/docs/source/developer_api.rst index 072dcd792..3e510fd54 100644 --- a/docs/source/developer_api.rst +++ b/docs/source/developer_api.rst @@ -437,6 +437,39 @@ The **disco** grouping This grouping collects API functions related to `service discovery `_. +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 ~~~~~~~~ @@ -450,7 +483,7 @@ Returns a `Promise` which, when resolved, returns a map/object with keys converse.plugins.add('myplugin', { 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) { // `value` is a map with two keys, `supported` and `feature`. diff --git a/src/converse-disco.js b/src/converse-disco.js index 435322b02..7ffb91439 100644 --- a/src/converse-disco.js +++ b/src/converse-disco.js @@ -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) { /* Returns a Promise which resolves with a map indicating * whether a given feature is supported. @@ -130,8 +153,8 @@ _.forEach(stanza.querySelectorAll('identity'), (identity) => { this.identities.create({ 'category': identity.getAttribute('category'), - 'type': stanza.getAttribute('type'), - 'name': stanza.getAttribute('name') + 'type': identity.getAttribute('type'), + 'name': identity.getAttribute('name') }); }); if (stanza.querySelector('feature[var="'+Strophe.NS.DISCO_ITEMS+'"]')) { @@ -245,7 +268,28 @@ return _converse.api.waitUntil('discoInitialized').then(() => { const entity = _converse.api.disco.entities.get(entity_jid, true); 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 `` element. + * For example: 'pubsub' + * (String) type - The identity type. + * In the XML stanza, this is the `type` + * attribute of the `` 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)); } } });