浏览代码

Add the ability to query for a disco identity

JC Brand 7 年之前
父节点
当前提交
d4ceb6d340
共有 3 个文件被更改,包括 87 次插入4 次删除
  1. 6 0
      CHANGES.md
  2. 34 1
      docs/source/developer_api.rst
  3. 47 3
      src/converse-disco.js

+ 6 - 0
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

+ 34 - 1
docs/source/developer_api.rst

@@ -437,6 +437,39 @@ The **disco** grouping
 This grouping collects API functions related to `service discovery
 <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
 ~~~~~~~~
 
@@ -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`.
 

+ 47 - 3
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 `<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));
                     }
                 }
             });