Refactor fetchDevices to make it smaller and simpler to read

This commit is contained in:
JC Brand 2019-03-30 17:01:11 +01:00
parent 1b08df89bb
commit c32ecb7e82

View File

@ -950,11 +950,7 @@ converse.plugins.add('converse-omemo', {
this.fetchDevices(); this.fetchDevices();
}, },
fetchDevices () { async onDevicesFound (collection) {
if (_.isUndefined(this._devices_promise)) {
this._devices_promise = new Promise(resolve => {
this.devices.fetch({
'success': async collection => {
if (collection.length === 0) { if (collection.length === 0) {
let ids; let ids;
try { try {
@ -963,16 +959,19 @@ converse.plugins.add('converse-omemo', {
_converse.log(`Could not fetch devices for ${this.get('jid')}`); _converse.log(`Could not fetch devices for ${this.get('jid')}`);
_converse.log(e, Strophe.LogLevel.ERROR); _converse.log(e, Strophe.LogLevel.ERROR);
this.destroy(); this.destroy();
return resolve(e);
} }
if (this.get('jid') === _converse.bare_jid) {
await this.publishCurrentDevice(ids); await this.publishCurrentDevice(ids);
} }
resolve();
},
'error': e => {
_converse.log(e, Strophe.LogLevel.ERROR);
resolve(e);
} }
},
fetchDevices () {
if (_.isUndefined(this._devices_promise)) {
this._devices_promise = new Promise(resolve => {
this.devices.fetch({
'success': _.flow(c => this.onDevicesFound(c), resolve),
'error': _.flow(_.partial(_converse.log, _, Strophe.LogLevel.ERROR), resolve)
}); });
}); });
} }
@ -981,8 +980,7 @@ converse.plugins.add('converse-omemo', {
async publishCurrentDevice (device_ids) { async publishCurrentDevice (device_ids) {
if (this.get('jid') !== _converse.bare_jid) { if (this.get('jid') !== _converse.bare_jid) {
// We only publish for ourselves. return // We only publish for ourselves.
return
} }
await restoreOMEMOSession(); await restoreOMEMOSession();
let device_id = _converse.omemo_store.get('device_id'); let device_id = _converse.omemo_store.get('device_id');