Collections shouldn't have id attributes

Otherwise the collection's items aren't fetched but it's own properties.
The way to persistently remove items from a collection is to destroy
them, NOT to call `remove`.
This commit is contained in:
JC Brand 2018-08-22 19:26:38 +02:00
parent 5ded22207b
commit f01b6c29c8
2 changed files with 20 additions and 42 deletions

26
dist/converse.js vendored
View File

@ -74752,7 +74752,6 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
initialize() { initialize() {
this.devices = new _converse.Devices(); this.devices = new _converse.Devices();
const id = `converse.devicelist-${_converse.bare_jid}-${this.get('jid')}`; const id = `converse.devicelist-${_converse.bare_jid}-${this.get('jid')}`;
this.devices.id = id;
this.devices.browserStorage = new Backbone.BrowserStorage.session(id); this.devices.browserStorage = new Backbone.BrowserStorage.session(id);
this.fetchDevices(); this.fetchDevices();
}, },
@ -74767,9 +74766,6 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
} else { } else {
resolve(); resolve();
} }
},
'error': () => {
this.fetchDevicesFromServer().then(ids => this.publishCurrentDevice(ids)).then(resolve).catch(resolve);
} }
}); });
}); });
@ -74785,22 +74781,17 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
} }
return restoreOMEMOSession().then(() => { return restoreOMEMOSession().then(() => {
const device_id = _converse.omemo_store.get('device_id'); const device_id = _converse.omemo_store.get('device_id'),
own_device = this.devices.findWhere({
if (!_.includes(device_ids, device_id)) {
return this.publishDevices();
} else {
const own_device = this.devices.findWhere({
'id': device_id 'id': device_id
}); });
if (!own_device.get('active')) { if (!_.includes(device_ids, device_id) || !own_device.get('active')) {
own_device.set('active', true, { own_device.save('active', true, {
'silent': true 'silent': true
}); });
return this.publishDevices(); return this.publishDevices();
} }
}
}); });
}, },
@ -74854,7 +74845,8 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
throw new Error("Cannot remove devices from someone else's device list"); throw new Error("Cannot remove devices from someone else's device list");
} }
this.devices.reset(this.devices.filter(d => !_.includes(device_ids, d.get('id').toString()))); _.forEach(device_ids, device_id => this.devices.get(device_id).destroy());
return this.publishDevices(); return this.publishDevices();
} }
@ -74892,8 +74884,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
function fetchDeviceLists() { function fetchDeviceLists() {
return new Promise((resolve, reject) => _converse.devicelists.fetch({ return new Promise((resolve, reject) => _converse.devicelists.fetch({
'success': resolve, 'success': resolve
'error': resolve
})); }));
} }
@ -74950,7 +74941,7 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
devices = devicelist.devices, devices = devicelist.devices,
removed_ids = _.difference(devices.pluck('id'), device_ids); removed_ids = _.difference(devices.pluck('id'), device_ids);
_.forEach(removed_ids, removed_id => devices.get(removed_id).set('active', false)); _.forEach(removed_ids, removed_id => devices.get(removed_id).save('active', false));
_.forEach(device_ids, device_id => { _.forEach(device_ids, device_id => {
const dev = devices.get(device_id); const dev = devices.get(device_id);
@ -75004,7 +74995,6 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
function initOMEMO() { function initOMEMO() {
_converse.devicelists = new _converse.DeviceLists(); _converse.devicelists = new _converse.DeviceLists();
const id = `converse.devicelists-${_converse.bare_jid}`; const id = `converse.devicelists-${_converse.bare_jid}`;
_converse.devicelists.id = id;
_converse.devicelists.browserStorage = new Backbone.BrowserStorage[_converse.storage](id); _converse.devicelists.browserStorage = new Backbone.BrowserStorage[_converse.storage](id);
fetchOwnDevices().then(() => restoreOMEMOSession()).then(() => _converse.omemo.publishBundle()).then(() => _converse.emit('OMEMOInitialized')).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR)); fetchOwnDevices().then(() => restoreOMEMOSession()).then(() => _converse.omemo.publishBundle()).then(() => _converse.emit('OMEMOInitialized')).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR));
} }

View File

@ -723,7 +723,6 @@
initialize () { initialize () {
this.devices = new _converse.Devices(); this.devices = new _converse.Devices();
const id = `converse.devicelist-${_converse.bare_jid}-${this.get('jid')}`; const id = `converse.devicelist-${_converse.bare_jid}-${this.get('jid')}`;
this.devices.id = id;
this.devices.browserStorage = new Backbone.BrowserStorage.session(id); this.devices.browserStorage = new Backbone.BrowserStorage.session(id);
this.fetchDevices(); this.fetchDevices();
}, },
@ -741,12 +740,6 @@
} else { } else {
resolve(); resolve();
} }
},
'error': () => {
this.fetchDevicesFromServer()
.then(ids => this.publishCurrentDevice(ids))
.then(resolve)
.catch(resolve)
} }
}); });
}); });
@ -761,15 +754,12 @@
} }
return restoreOMEMOSession() return restoreOMEMOSession()
.then(() => { .then(() => {
const device_id = _converse.omemo_store.get('device_id'); const device_id = _converse.omemo_store.get('device_id'),
if (!_.includes(device_ids, device_id)) { own_device = this.devices.findWhere({'id': device_id});
if (!_.includes(device_ids, device_id) || !own_device.get('active')) {
own_device.save('active', true, {'silent': true});
return this.publishDevices(); return this.publishDevices();
} else {
const own_device = this.devices.findWhere({'id': device_id})
if (!own_device.get('active')) {
own_device.set('active', true, {'silent': true});
return this.publishDevices();
}
} }
}); });
}, },
@ -807,7 +797,7 @@
if (this.get('jid') !== _converse.bare_jid) { if (this.get('jid') !== _converse.bare_jid) {
throw new Error("Cannot remove devices from someone else's device list"); throw new Error("Cannot remove devices from someone else's device list");
} }
this.devices.reset(this.devices.filter(d => (!_.includes(device_ids, d.get('id').toString())))); _.forEach(device_ids, (device_id) => this.devices.get(device_id).destroy());
return this.publishDevices(); return this.publishDevices();
} }
}); });
@ -849,8 +839,7 @@
function fetchDeviceLists () { function fetchDeviceLists () {
return new Promise((resolve, reject) => _converse.devicelists.fetch({ return new Promise((resolve, reject) => _converse.devicelists.fetch({
'success': resolve, 'success': resolve
'error': resolve
})); }));
} }
@ -891,7 +880,7 @@
devices = devicelist.devices, devices = devicelist.devices,
removed_ids = _.difference(devices.pluck('id'), device_ids); removed_ids = _.difference(devices.pluck('id'), device_ids);
_.forEach(removed_ids, (removed_id) => devices.get(removed_id).set('active', false)); _.forEach(removed_ids, (removed_id) => devices.get(removed_id).save('active', false));
_.forEach(device_ids, (device_id) => { _.forEach(device_ids, (device_id) => {
const dev = devices.get(device_id); const dev = devices.get(device_id);
if (dev) { if (dev) {
@ -935,7 +924,6 @@
function initOMEMO() { function initOMEMO() {
_converse.devicelists = new _converse.DeviceLists(); _converse.devicelists = new _converse.DeviceLists();
const id = `converse.devicelists-${_converse.bare_jid}`; const id = `converse.devicelists-${_converse.bare_jid}`;
_converse.devicelists.id = id;
_converse.devicelists.browserStorage = new Backbone.BrowserStorage[_converse.storage](id); _converse.devicelists.browserStorage = new Backbone.BrowserStorage[_converse.storage](id);
fetchOwnDevices() fetchOwnDevices()