From 042a26d05dd63a895b3078254591049a2f99cb72 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 2 Nov 2017 23:23:01 +0100 Subject: [PATCH] New API method `_converse.disco.supports` to check whether a certain service discovery feature is supported by an entity. --- CHANGES.md | 4 +++ docs/source/developer_api.rst | 37 ++++++++++++++++++++++++ src/converse-disco.js | 45 ++++++++++++++++++++++++++--- src/converse-mam.js | 54 +++++++++-------------------------- 4 files changed, 96 insertions(+), 44 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 1aa04e505..44fe84192 100755 --- a/CHANGES.md +++ b/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. diff --git a/docs/source/developer_api.rst b/docs/source/developer_api.rst index ca1c69480..f15bc4784 100644 --- a/docs/source/developer_api.rst +++ b/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 +`_. + +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 --------------------- diff --git a/src/converse-disco.js b/src/converse-disco.js index d19a33e61..73295ecc9 100644 --- a/src/converse-disco.js +++ b/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 `` 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); + } + }) + ); + } + } + }); } }); })); diff --git a/src/converse-mam.js b/src/converse-mam.js index b6a7daff4..41bb9d913 100644 --- a/src/converse-mam.js +++ b/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); + } + } + }); } }); }));