Various fixes to code checking for server and contact client support
This commit is contained in:
JC Brand 2018-05-12 11:42:58 +02:00
parent bfc246d9c7
commit e833af14bf
2 changed files with 34 additions and 16 deletions

View File

@ -33,8 +33,8 @@
notify_all_room_messages: [ notify_all_room_messages: [
'discuss@conference.conversejs.org' 'discuss@conference.conversejs.org'
], ],
bosh_service_url: 'http://chat.example.org:5280/http-bind/', // bosh_service_url: 'http://chat.example.org:5280/http-bind/',
// bosh_service_url: 'https://conversejs.org/http-bind/', // Please use this connection manager only for testing purposes bosh_service_url: 'https://conversejs.org/http-bind/', // Please use this connection manager only for testing purposes
message_archiving: 'always', message_archiving: 'always',
debug: true debug: true
}); });

View File

@ -23,17 +23,27 @@
const TRUSTED = 1; const TRUSTED = 1;
const UNTRUSTED = -1; const UNTRUSTED = -1;
function contactHasOMEMOSupport (_converse, contact_jid) { function getDevicesForContact (_converse, jid) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
_converse.api.waitUntil('OMEMOInitialized', () => { _converse.api.waitUntil('OMEMOInitialized').then(() => {
resolve(_converse.devicelists.get(contact_jid).devices.length > 0); const devicelist = _converse.devicelists.get(jid);
}); resolve(devicelist ? devicelist.devices : []);
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
});
}
function contactHasOMEMOSupport (_converse, jid) {
return new Promise((resolve, reject) => {
getDevicesForContact(_converse, jid).then((devices) => {
resolve(devices.length > 0)
}).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
}); });
} }
function serverHasOMEMOSupport (_converse) { function serverHasOMEMOSupport (_converse) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
_converse.api.disco.getIdentity('pubsub', 'pep').then((identity) => resolve(!_.isNil(identity))) _converse.api.disco.getIdentity('pubsub', 'pep', _converse.bare_jid)
.then((identity) => resolve(!_.isNil(identity)));
}); });
} }
@ -48,14 +58,14 @@
addOMEMOToolbarButton (options) { addOMEMOToolbarButton (options) {
const { _converse } = this.__super__; const { _converse } = this.__super__;
Promise.all([ Promise.all([
contactHasOMEMOSupport(_converse, this.model.get('jid')), contactHasOMEMOSupport(_converse, this.model.get('jid')),
serverHasOMEMOSupport(_converse) serverHasOMEMOSupport(_converse)
]).then((client_support, server_support) => { ]).then((support) => {
debugger; const client_supports = support[0],
server_supports = support[1];
if (client_support && server_support) { if (client_supports && server_supports) {
this.el.querySelector('.chat-toolbar').insertAdjacentHTML( this.el.querySelector('.chat-toolbar').insertAdjacentHTML(
'beforeend', 'beforeend',
tpl_toolbar_omemo({'__': __})); tpl_toolbar_omemo({'__': __}));
@ -124,12 +134,15 @@
initialize () { initialize () {
this.devices = new _converse.Devices(); this.devices = new _converse.Devices();
this.devices.browserStorage = new Backbone.BrowserStorage.session(
b64_sha1(`converse.devicelist-${_converse.bare_jid}-${this.get('jid')}`)
);
}, },
fetchDevices () { fetchDevices () {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.devices.fetch({ this.devices.fetch({
success (collection) { 'success': (collection) => {
if (collection.length === 0) { if (collection.length === 0) {
this.fetchDevicesFromServer().then(resolve).catch(reject); this.fetchDevicesFromServer().then(resolve).catch(reject);
} else { } else {
@ -168,9 +181,15 @@
/* If our own device is not on the list, add it. /* If our own device is not on the list, add it.
* Also, deduplicate devices if necessary. * Also, deduplicate devices if necessary.
*/ */
// TODO: return new Promise((resolve, reject) => {
const devicelist = _converse.devicelists.get(_converse.bare_jid); let own_devicelist = _converse.devicelists.get(_converse.bare_jid);
return Promise.resolve(); if (_.isNil(own_devicelist)) {
own_devicelist = _converse.devicelists.create({'jid': _converse.bare_jid});
}
own_devicelist.fetchDevices().then(resolve).catch(reject);
// TODO: if our own device is not onthe list, add it.
// TODO: deduplicate
});
} }
function updateDevicesFromStanza (stanza) { function updateDevicesFromStanza (stanza) {
@ -207,7 +226,6 @@
*/ */
publishBundle() publishBundle()
.then(() => fetchDeviceLists()) .then(() => fetchDeviceLists())
.then(() => _converse.devicelists.get(_converse.bare_jid).fetchDevices())
.then(() => updateOwnDeviceList()) .then(() => updateOwnDeviceList())
.then(() => _converse.emit('OMEMOInitialized')) .then(() => _converse.emit('OMEMOInitialized'))
.catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR)); .catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));