Commit a9d28818 authored by JC Brand's avatar JC Brand

Fix disco hierarchy

Previously we kept all entities and their items (which are also
instances of _converse.DiscoEntity) in a flat array.

Instead, we should have a tree-like structure where items are stored
on the relevant entity (and recursively on other items).
parent 77a51cc2
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
"accessor-pairs": "error", "accessor-pairs": "error",
"array-bracket-spacing": "off", "array-bracket-spacing": "off",
"array-callback-return": "error", "array-callback-return": "error",
"arrow-body-style": "error", "arrow-body-style": "off",
"arrow-parens": "error", "arrow-parens": "error",
"arrow-spacing": "error", "arrow-spacing": "error",
"block-scoped-var": "off", "block-scoped-var": "off",
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
var IQ_ids = _converse.connection.IQ_ids; var IQ_ids = _converse.connection.IQ_ids;
test_utils.waitUntil(function () { test_utils.waitUntil(function () {
return _.filter(IQ_stanzas, function (iq) { return _.filter(IQ_stanzas, function (iq) {
return iq.nodeTree.querySelector('query[xmlns="http://jabber.org/protocol/disco#info"]'); return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
}).length > 0; }).length > 0;
}, 300).then(function () { }, 300).then(function () {
/* <iq type='result' /* <iq type='result'
...@@ -49,8 +49,11 @@ ...@@ -49,8 +49,11 @@
* </query> * </query>
* </iq> * </iq>
*/ */
var info_IQ_id = IQ_ids[0]; var stanza = _.filter(IQ_stanzas, function (iq) {
var stanza = $iq({ return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
})[0];
var info_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
stanza = $iq({
'type': 'result', 'type': 'result',
'from': 'localhost', 'from': 'localhost',
'to': 'dummy@localhost/resource', 'to': 'dummy@localhost/resource',
...@@ -79,7 +82,7 @@ ...@@ -79,7 +82,7 @@
'var': 'jabber:iq:version'}); 'var': 'jabber:iq:version'});
_converse.connection._dataRecv(test_utils.createRequest(stanza)); _converse.connection._dataRecv(test_utils.createRequest(stanza));
var entities = _converse.disco_entities; _converse.api.disco.entities.get().then(function (entities) {
expect(entities.length).toBe(2); // We have an extra entity, which is the user's JID expect(entities.length).toBe(2); // We have an extra entity, which is the user's JID
expect(entities.get(_converse.domain).features.length).toBe(5); expect(entities.get(_converse.domain).features.length).toBe(5);
expect(entities.get(_converse.domain).identities.length).toBe(3); expect(entities.get(_converse.domain).identities.length).toBe(3);
...@@ -125,7 +128,10 @@ ...@@ -125,7 +128,10 @@
* </query> * </query>
* </iq> * </iq>
*/ */
var items_IQ_id = IQ_ids.pop(); var stanza = _.filter(IQ_stanzas, function (iq) {
return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
})[0];
var items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
stanza = $iq({ stanza = $iq({
'type': 'result', 'type': 'result',
'from': 'localhost', 'from': 'localhost',
...@@ -141,9 +147,6 @@ ...@@ -141,9 +147,6 @@
.c('item', { .c('item', {
'jid': 'words.shakespeare.lit', 'jid': 'words.shakespeare.lit',
'name': 'Gateway to Marlowe IM'}).up() 'name': 'Gateway to Marlowe IM'}).up()
.c('item', {
'jid': 'localhost',
'name': 'Shakespearean Lexicon'}).up()
.c('item', { .c('item', {
'jid': 'localhost', 'jid': 'localhost',
...@@ -160,12 +163,17 @@ ...@@ -160,12 +163,17 @@
_converse.connection._dataRecv(test_utils.createRequest(stanza)); _converse.connection._dataRecv(test_utils.createRequest(stanza));
entities = _converse.disco_entities; entities = _converse.disco_entities;
expect(entities.length).toBe(5); // We have an extra entity, which is the user's JID expect(entities.length).toBe(2); // We have an extra entity, which is the user's JID
expect(entities.get(_converse.domain).items.length).toBe(3);
expect(_.includes(entities.get(_converse.domain).items.pluck('jid'), 'people.shakespeare.lit')).toBeTruthy();
expect(_.includes(entities.get(_converse.domain).items.pluck('jid'), 'plays.shakespeare.lit')).toBeTruthy();
expect(_.includes(entities.get(_converse.domain).items.pluck('jid'), 'words.shakespeare.lit')).toBeTruthy();
expect(entities.get(_converse.domain).identities.where({'category': 'conference'}).length).toBe(1); expect(entities.get(_converse.domain).identities.where({'category': 'conference'}).length).toBe(1);
expect(entities.get(_converse.domain).identities.where({'category': 'directory'}).length).toBe(1); expect(entities.get(_converse.domain).identities.where({'category': 'directory'}).length).toBe(1);
done(); done();
}); });
}); });
});
})); }));
}); });
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
var Strophe = converse.env.Strophe; var Strophe = converse.env.Strophe;
var $iq = converse.env.$iq; var $iq = converse.env.$iq;
var _ = converse.env._; var _ = converse.env._;
var f = converse.env.f;
describe("XEP-0363: HTTP File Upload", function () { describe("XEP-0363: HTTP File Upload", function () {
...@@ -18,9 +19,11 @@ ...@@ -18,9 +19,11 @@
it("is done automatically", mock.initConverseWithAsync(function (done, _converse) { it("is done automatically", mock.initConverseWithAsync(function (done, _converse) {
var IQ_stanzas = _converse.connection.IQ_stanzas; var IQ_stanzas = _converse.connection.IQ_stanzas;
var IQ_ids = _converse.connection.IQ_ids; var IQ_ids = _converse.connection.IQ_ids;
test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], []).then(function () {
test_utils.waitUntil(function () { test_utils.waitUntil(function () {
return _.filter(IQ_stanzas, function (iq) { return _.filter(IQ_stanzas, function (iq) {
return iq.nodeTree.querySelector('query[xmlns="http://jabber.org/protocol/disco#info"]'); return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
}).length > 0; }).length > 0;
}, 300).then(function () { }, 300).then(function () {
/* <iq type='result' /* <iq type='result'
...@@ -36,8 +39,12 @@ ...@@ -36,8 +39,12 @@
* </query> * </query>
* </iq> * </iq>
*/ */
var info_IQ_id = IQ_ids[0]; var stanza = _.filter(IQ_stanzas, function (iq) {
var stanza = $iq({ return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
})[0];
var info_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
stanza = $iq({
'type': 'result', 'type': 'result',
'from': 'localhost', 'from': 'localhost',
'to': 'dummy@localhost/resource', 'to': 'dummy@localhost/resource',
...@@ -52,8 +59,11 @@ ...@@ -52,8 +59,11 @@
'var': 'http://jabber.org/protocol/disco#items'}); 'var': 'http://jabber.org/protocol/disco#items'});
_converse.connection._dataRecv(test_utils.createRequest(stanza)); _converse.connection._dataRecv(test_utils.createRequest(stanza));
var entities = _converse.disco_entities; _converse.api.disco.entities.get().then(function(entities) {
expect(entities.length).toBe(2); // We have an extra entity, which is the user's JID expect(entities.length).toBe(2);
expect(_.includes(entities.pluck('jid'), 'localhost')).toBe(true);
expect(_.includes(entities.pluck('jid'), 'dummy@localhost')).toBe(true);
expect(entities.get(_converse.domain).features.length).toBe(2); expect(entities.get(_converse.domain).features.length).toBe(2);
expect(entities.get(_converse.domain).identities.length).toBe(1); expect(entities.get(_converse.domain).identities.length).toBe(1);
...@@ -61,9 +71,10 @@ ...@@ -61,9 +71,10 @@
// Converse.js sees that the entity has a disco#items feature, // Converse.js sees that the entity has a disco#items feature,
// so it will make a query for it. // so it will make a query for it.
return _.filter(IQ_stanzas, function (iq) { return _.filter(IQ_stanzas, function (iq) {
return iq.nodeTree.querySelector('query[xmlns="http://jabber.org/protocol/disco#items"]'); return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
}).length > 0; }).length > 0;
}, 300); }, 300);
});
}).then(function () { }).then(function () {
/* <iq from='montague.tld' /* <iq from='montague.tld'
* id='step_01' * id='step_01'
...@@ -75,8 +86,11 @@ ...@@ -75,8 +86,11 @@
* </query> * </query>
* </iq> * </iq>
*/ */
var items_IQ_id = IQ_ids[IQ_ids.length-1]; var stanza = _.filter(IQ_stanzas, function (iq) {
var stanza = $iq({ return iq.nodeTree.querySelector('iq[to="localhost"] query[xmlns="http://jabber.org/protocol/disco#items"]');
})[0];
var items_IQ_id = IQ_ids[IQ_stanzas.indexOf(stanza)];
stanza = $iq({
'type': 'result', 'type': 'result',
'from': 'localhost', 'from': 'localhost',
'to': 'dummy@localhost/resource', 'to': 'dummy@localhost/resource',
...@@ -84,22 +98,20 @@ ...@@ -84,22 +98,20 @@
}).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'}) }).c('query', {'xmlns': 'http://jabber.org/protocol/disco#items'})
.c('item', { .c('item', {
'jid': 'upload.localhost', 'jid': 'upload.localhost',
'name': 'HTTP File Upload'}).up() 'name': 'HTTP File Upload'});
.c('item', {
'jid': 'conference.localhost',
'name': 'Chatrooms Service'});
_converse.connection._dataRecv(test_utils.createRequest(stanza)); _converse.connection._dataRecv(test_utils.createRequest(stanza));
var entities = _converse.disco_entities; _converse.api.disco.entities.get().then(function (entities) {
expect(entities.length).toBe(4); // We have an extra entity, which is the user's JID expect(entities.length).toBe(2);
expect(entities.get('localhost').items.length).toBe(1);
return test_utils.waitUntil(function () { return test_utils.waitUntil(function () {
// Converse.js sees that the entity has a disco#items feature, // Converse.js sees that the entity has a disco#info feature,
// so it will make a query for it. // so it will make a query for it.
return _.filter(IQ_stanzas, function (iq) { return _.filter(IQ_stanzas, function (iq) {
return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]'); return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
}).length > 0; }).length > 0;
}, 300); }, 300);
});
}).then(function () { }).then(function () {
var stanza = _.filter(IQ_stanzas, function (iq) { var stanza = _.filter(IQ_stanzas, function (iq) {
return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]'); return iq.nodeTree.querySelector('iq[to="upload.localhost"] query[xmlns="http://jabber.org/protocol/disco#info"]');
...@@ -142,11 +154,36 @@ ...@@ -142,11 +154,36 @@
.c('value').t('5242880'); .c('value').t('5242880');
_converse.connection._dataRecv(test_utils.createRequest(stanza)); _converse.connection._dataRecv(test_utils.createRequest(stanza));
var entities = _converse.disco_entities; _converse.api.disco.entities.get().then(function (entities) {
expect(entities.get('upload.localhost').identities.where({'category': 'store'}).length).toBe(1); expect(entities.get('localhost').items.get('upload.localhost').identities.where({'category': 'store'}).length).toBe(1);
_converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain).then(
function (result) {
expect(result.length).toBe(1);
expect(result[0].get('jid')).toBe('upload.localhost');
done(); done();
}
);
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
})
}) })
})); }));
}); });
describe("When supported", function () {
describe("A file upload toolbar button", function () {
it("appears in private chats", mock.initConverseWithAsync(function (done, _converse) {
test_utils.createContacts(_converse, 'current');
var contact_jid = mock.cur_names[2].replace(/ /g,'.').toLowerCase() + '@localhost';
test_utils.openChatBoxFor(_converse, contact_jid);
done();
}));
it("appears in MUC chats", mock.initConverseWithAsync(function (done, _converse) {
done();
}));
});
});
}); });
})); }));
This diff is collapsed.
...@@ -550,7 +550,7 @@ ...@@ -550,7 +550,7 @@
_converse.api.disco.getIdentity('pubsub', 'pep', _converse.bare_jid), _converse.api.disco.getIdentity('pubsub', 'pep', _converse.bare_jid),
_converse.api.disco.supports(Strophe.NS.PUBSUB+'#publish-options', _converse.bare_jid) _converse.api.disco.supports(Strophe.NS.PUBSUB+'#publish-options', _converse.bare_jid)
]).then((args) => { ]).then((args) => {
resolve(args[0] && (args[1].supported || _converse.allow_public_bookmarks)); resolve(args[0] && (args[1].length || _converse.allow_public_bookmarks));
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL)); }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL)); }).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
} }
......
...@@ -341,8 +341,7 @@ ...@@ -341,8 +341,7 @@
Promise.all(_.map(_.keys(resources), (resource) => Promise.all(_.map(_.keys(resources), (resource) =>
_converse.api.disco.supports(Strophe.NS.SPOILER, `${contact_jid}/${resource}`) _converse.api.disco.supports(Strophe.NS.SPOILER, `${contact_jid}/${resource}`)
)).then((results) => { )).then((results) => {
const supported = _.every(f.map(f.get('supported'))(results)); if (results.length) {
if (supported) {
const html = tpl_spoiler_button(this.model.toJSON()); const html = tpl_spoiler_button(this.model.toJSON());
if (_converse.visible_toolbar_buttons.emoji) { if (_converse.visible_toolbar_buttons.emoji) {
this.el.querySelector('.toggle-smiley').insertAdjacentHTML('afterEnd', html); this.el.querySelector('.toggle-smiley').insertAdjacentHTML('afterEnd', html);
......
This diff is collapsed.
(function (root, factory) { (function (root, factory) {
define(["converse-http-file-upload"], factory); define(["converse-core"], factory);
}(this, function (converse) { }(this, function (converse) {
"use strict"; "use strict";
const { Promise, Strophe, _ } = converse.env;
const u = converse.env.utils;
Strophe.addNamespace('HTTPUPLOAD', 'urn:xmpp:http:upload:0');
converse.plugins.add('converse-http-file-upload', { converse.plugins.add('converse-http-file-upload', {
/* Plugin dependencies are other plugins which might be
* overridden or relied upon, and therefore need to be loaded before
* this plugin.
*
* If the setting "strict_plugin_dependencies" is set to true,
* an error will be raised if the plugin is not found. By default it's
* false, which means these plugins are only loaded opportunistically.
*
* NB: These plugins need to have already been loaded via require.js.
*/
dependencies: ["converse-chatview"],
overrides: {
ChatBoxView: {
addFileUploadButton (options) {
},
renderToolbar (toolbar, options) {
const { _converse } = this.__super__;
const result = this.__super__.renderToolbar.apply(this, arguments);
// TODO: check results.length
_converse.api.disco.supports(Strophe.NS.HTTPUPLOAD, _converse.domain)
.then(this.addFileUploadButton.bind(this));
return result;
}
}
},
initialize () { initialize () {
/* The initialize function gets called as soon as the plugin is /* The initialize function gets called as soon as the plugin is
* loaded by converse.js's plugin machinery. * loaded by converse.js's plugin machinery.
*/ */
const { _converse } = this; const { _converse } = this;
} }
}); });
})); }));
...@@ -154,8 +154,8 @@ ...@@ -154,8 +154,8 @@
if (this.disable_mam) { return; } if (this.disable_mam) { return; }
const { _converse } = this.__super__; const { _converse } = this.__super__;
_converse.api.disco.supports(Strophe.NS.MAM, _converse.bare_jid).then( _converse.api.disco.supports(Strophe.NS.MAM, _converse.bare_jid).then(
(result) => { // Success (results) => { // Success
if (result.supported) { if (result.length) {
const most_recent_msg = utils.getMostRecentMessage(this.model); const most_recent_msg = utils.getMostRecentMessage(this.model);
if (_.isNil(most_recent_msg)) { if (_.isNil(most_recent_msg)) {
this.fetchArchivedMessages(); this.fetchArchivedMessages();
...@@ -193,7 +193,7 @@ ...@@ -193,7 +193,7 @@
const { _converse } = this.__super__; const { _converse } = this.__super__;
_converse.api.disco.supports(Strophe.NS.MAM, _converse.bare_jid).then( _converse.api.disco.supports(Strophe.NS.MAM, _converse.bare_jid).then(
(result) => { // Success (result) => { // Success
if (result.supported) { if (result.length) {
this.fetchArchivedMessages(); this.fetchArchivedMessages();
} }
this.model.save({'mam_initialized': true}); this.model.save({'mam_initialized': true});
......
...@@ -186,7 +186,7 @@ ...@@ -186,7 +186,7 @@
if (_.isNil(_converse.xmppstatus.get('vcard_updated'))) { if (_.isNil(_converse.xmppstatus.get('vcard_updated'))) {
_converse.api.disco.supports(Strophe.NS.VCARD, _converse.domain) _converse.api.disco.supports(Strophe.NS.VCARD, _converse.domain)
.then((result) => { .then((result) => {
if (result.supported) { if (result.length) {
_converse.api.vcard.get(_converse.bare_jid) _converse.api.vcard.get(_converse.bare_jid)
.then((vcard) => _converse.xmppstatus.save(vcard)); .then((vcard) => _converse.xmppstatus.save(vcard));
}}) }})
......
...@@ -7,24 +7,25 @@ if (typeof define !== 'undefined') { ...@@ -7,24 +7,25 @@ if (typeof define !== 'undefined') {
* -------------------- * --------------------
* Any of the following components may be removed if they're not needed. * Any of the following components may be removed if they're not needed.
*/ */
"converse-bookmarks", // XEP-0048 Bookmarks
"converse-chatview", // Renders standalone chat boxes for single user chat "converse-chatview", // Renders standalone chat boxes for single user chat
"converse-controlbox", // The control box "converse-controlbox", // The control box
"converse-bookmarks", // XEP-0048 Bookmarks "converse-dragresize", // Allows chat boxes to be resized by dragging them
"converse-roomslist", // Show currently open chat rooms "converse-fullscreen",
"converse-headline", // Support for headline messages
"converse-http-file-upload",
"converse-mam", // XEP-0313 Message Archive Management "converse-mam", // XEP-0313 Message Archive Management
"converse-minimize", // Allows chat boxes to be minimized
"converse-muc", // XEP-0045 Multi-user chat "converse-muc", // XEP-0045 Multi-user chat
"converse-muc-views", // Views related to MUC
"converse-muc-embedded", "converse-muc-embedded",
"converse-muc-views", "converse-muc-views",
"converse-vcard", // XEP-0054 VCard-temp "converse-muc-views", // Views related to MUC
"converse-notification", // HTML5 Notifications
"converse-otr", // Off-the-record encryption for one-on-one messages "converse-otr", // Off-the-record encryption for one-on-one messages
"converse-register", // XEP-0077 In-band registration
"converse-ping", // XEP-0199 XMPP Ping "converse-ping", // XEP-0199 XMPP Ping
"converse-notification",// HTML5 Notifications "converse-register", // XEP-0077 In-band registration
"converse-minimize", // Allows chat boxes to be minimized "converse-roomslist", // Show currently open chat rooms
"converse-dragresize", // Allows chat boxes to be resized by dragging them "converse-vcard", // XEP-0054 VCard-temp
"converse-headline", // Support for headline messages
"converse-fullscreen"
/* END: Removable components */ /* END: Removable components */
], function (converse) { ], function (converse) {
return converse; return converse;
......
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