浏览代码

New API method `_converse.disco.supports`

to check whether a certain service discovery feature is supported by an entity.
JC Brand 7 年之前
父节点
当前提交
042a26d05d
共有 4 个文件被更改,包括 96 次插入44 次删除
  1. 4 0
      CHANGES.md
  2. 37 0
      docs/source/developer_api.rst
  3. 41 4
      src/converse-disco.js
  4. 14 40
      src/converse-mam.js

+ 4 - 0
CHANGES.md

@@ -22,6 +22,10 @@
   builds. Instead the `converse.js` build is now used with `view_mode` set to
   `fullscreen` and `mobile` respectively.
 
+### API changes
+- New API method `_converse.disco.supports` to check whether a certain
+  service discovery feature is supported by an entity.
+
 ### UX/UI changes
 - Use CSS3 fade transitions to render various elements.
 - Remove `Login` and `Registration` tabs and consolidate into one panel.

+ 37 - 0
docs/source/developer_api.rst

@@ -431,6 +431,43 @@ disconnect
 Terminates the connection.
 
 
+The **disco** grouping
+----------------------
+
+This grouping collects API functions related to `service discovery
+<https://xmpp.org/extensions/xep-0030.html>`_.
+
+supports
+~~~~~~~~
+
+Used to determine whether an entity supports a given feature.
+
+.. code-block:: javascript
+
+    converse.plugins.add('myplugin', {
+        initialize: function () {
+
+            _converse.api.disco.supports(_converse.bare_jid, Strophe.NS.MAM).then(
+                function (supported) {
+                    if (supported) {
+                        // The feature is supported
+                    } else {
+                        // The feature is not supported
+                    }
+                },
+                function () { // Error
+                    _converse.log(
+                        "Error or timeout while checking for feature support",
+                        Strophe.LogLevel.ERROR
+                    );
+                }
+            ).catch((msg) => {
+                _converse.log(msg, Strophe.LogLevel.FATAL);
+            });
+        }
+    });
+
+
 The **user** grouping
 ---------------------
 

+ 41 - 4
src/converse-disco.js

@@ -42,10 +42,10 @@
 
             _converse.DiscoEntity = Backbone.Model.extend({
                 /* A Disco Entity is a JID addressable entity that can be queried
-                * for features.
-                *
-                * See XEP-0030: https://xmpp.org/extensions/xep-0030.html
-                */
+                 * for features.
+                 *
+                 * See XEP-0030: https://xmpp.org/extensions/xep-0030.html
+                 */
                 idAttribute: 'jid',
 
                 initialize () {
@@ -178,6 +178,43 @@
                     _converse.disco_entities.browserStorage._clear();
                 }
             });
+
+            /* We extend the default converse.js API to add methods specific to service discovery */
+            _.extend(_converse.api, {
+                'disco': {
+                    'supports' (entity_jid, feature) {
+                        /* Returns a Promise which returns a boolean indicating
+                         * whether the feature is supported or by the given
+                         * entity or not.
+                         *
+                         * Parameters:
+                         *    (String) entity_jid - The JID of the entity which might support the feature.
+                         *    (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'
+                         */
+                        return _converse.api.waitUntil('discoInitialized').then(() =>
+                            new Promise((resolve, reject) => {
+                                function fulfillPromise (entity) {
+                                    if (entity.features.findWhere({'var': feature})) {
+                                        resolve(true);
+                                    } else {
+                                        resolve(false);
+                                    }
+                                }
+                                let entity = _converse.disco_entities.get(entity_jid);
+                                if (_.isUndefined(entity)) {
+                                    entity = _converse.disco_entities.create({'jid': entity_jid});
+                                    entity.on('featuresDiscovered', _.partial(fulfillPromise, entity));
+                                } else {
+                                    fulfillPromise(entity);
+                                }
+                            })
+                        );
+                    }
+                }
+            });
         }
     });
 }));

+ 14 - 40
src/converse-mam.js

@@ -24,32 +24,6 @@
     // XEP-0313 Message Archive Management
     const MAM_ATTRIBUTES = ['with', 'start', 'end'];
 
-    function checkMAMSupport (_converse) {
-        /* Returns a promise which resolves when MAM is supported
-         * for this user, or which rejects if not.
-         */
-        return _converse.api.waitUntil('discoInitialized').then(() =>
-            new Promise((resolve, reject) => {
-
-                function fulfillPromise (entity) {
-                    if (entity.features.findWhere({'var': Strophe.NS.MAM})) {
-                        resolve(true);
-                    } else {
-                        resolve(false);
-                    }
-                }
-                let entity = _converse.disco_entities.get(_converse.bare_jid);
-                if (_.isUndefined(entity)) {
-                    entity = _converse.disco_entities.create({'jid': _converse.bare_jid});
-                    entity.on('featuresDiscovered', _.partial(fulfillPromise, entity));
-                } else {
-                    fulfillPromise(entity);
-                }
-            })
-        );
-    }
-
-
     converse.plugins.add('converse-mam', {
 
         overrides: {
@@ -83,7 +57,7 @@
                     const { _converse } = this.__super__;
                     this.addSpinner();
 
-                    checkMAMSupport(_converse).then(
+                    _converse.api.disco.supports(_converse.bare_jid, Strophe.NS.MAM).then(
                         (supported) => { // Success
                             if (supported) {
                                 this.fetchArchivedMessages();
@@ -329,19 +303,6 @@
                 );
             };
 
-            _.extend(_converse.api, {
-                /* Extend default converse.js API to add methods specific to MAM
-                 */
-                'archive': {
-                    'query': function () {
-                        if (!_converse.api.connection.connected()) {
-                            throw new Error('Can\'t call `api.archive.query` before having established an XMPP session');
-                        }
-                        return _converse.queryForArchivedMessages.apply(this, arguments);
-                    }
-                }
-            });
-
             _converse.onMAMError = function (iq) {
                 if ($(iq).find('feature-not-implemented').length) {
                     _converse.log(
@@ -409,6 +370,19 @@
             _converse.on('afterMessagesFetched', (chatboxview) => {
                 chatboxview.fetchArchivedMessagesIfNecessary();
             });
+
+            _.extend(_converse.api, {
+                /* Extend default converse.js API to add methods specific to MAM
+                 */
+                'archive': {
+                    'query': function () {
+                        if (!_converse.api.connection.connected()) {
+                            throw new Error('Can\'t call `api.archive.query` before having established an XMPP session');
+                        }
+                        return _converse.queryForArchivedMessages.apply(this, arguments);
+                    }
+                }
+            });
         }
     });
 }));