diff --git a/lib/plugins/omemo.js b/lib/plugins/omemo.js index e594f98a..c2c027e6 100644 --- a/lib/plugins/omemo.js +++ b/lib/plugins/omemo.js @@ -431,31 +431,71 @@ export class OmemoClient { return bundle; } - async announceDevices(devices) { + async announceDevices(devices, newRegistrationId) { // sort by device creation date try { - const maxDevices = 10; + const maxDevices = 5; devices.sort((d1, d2) => { let d1CreationTs = 0; if (d1.label) { const d1LabelSplit = d1.label.split(" "); - d1CreationTs = Date.parse(d1LabelSplit[d1LabelSplit.length - 1]); + const createdAt = d1LabelSplit[d1LabelSplit.length - 1]; + d1CreationTs = Date.parse(createdAt); } let d2CreationTs = 0; if (d2.label) { const d2LabelSplit = d2.label.split(" "); - d2CreationTs = Date.parse(d2LabelSplit[d2LabelSplit.length - 1]); + const createdAt = d2LabelSplit[d2LabelSplit.length - 1]; + d2CreationTs = Date.parse(createdAt); } return d2CreationTs - d1CreationTs; }); - console.log('[OmemoClient][announceDevices] devices: ', devices); + + let devicesMap = {}; + let devicesIdsToRemove = []; + devices.forEach(d => { // devices is already sorted + if (d.label && d.label.includes(",")) { // if device label contains device name + const d1LabelSplit = d.label.split(" "); + const createdAt = d1LabelSplit[d1LabelSplit.length - 1]; + const deviceName = d.label.replace(` ${createdAt}`, ''); + + if (devicesMap[deviceName]) { + const prevDeviceCreatedAt = devicesMap[deviceName].createdAt; + if (Date.parse(createdAt) > Date.parse(prevDeviceCreatedAt)) { + devicesIdsToRemove.push(devicesMap[deviceName].id); + devicesMap[deviceName] = {createdAt, id: d.id}; + } else { + if (newRegistrationId) { + if (newRegistrationId === d.id) { + devicesIdsToRemove.push(devicesMap[deviceName].id); // remove prev + devicesMap[deviceName] = {createdAt, id: d.id}; + } else { + devicesIdsToRemove.push(d.id); + } + } + } + } else { + devicesMap[deviceName] = {createdAt, id: d.id}; + } + } + }); + + console.log('[OmemoClient][announceDevices] devices: ', devices, newRegistrationId); + console.log('[OmemoClient][announceDevices] devicesMap: ', devicesMap); + console.log('[OmemoClient][announceDevices] devicesIdsToRemove: ', devicesIdsToRemove); + + // remove old devices with same name + if (devicesIdsToRemove.length > 0) { + devices = devices.filter(d => !devicesIdsToRemove.includes(d.id)); + console.log('[OmemoClient][announceDevices] removed old devices. devices: ', devices); + } if (devices.length > maxDevices) { devices = devices.slice(0, maxDevices); - console.log('[OmemoClient][announceDevices] removed old devices. devices: ', devices); + console.log('[OmemoClient][announceDevices] removed old devices (by max). devices: ', devices); } } catch(e) { console.log('[OmemoClient][announceDevices] devices: ', devices); @@ -492,7 +532,7 @@ export class OmemoClient { if (!announcedDeviceIds.includes(registrationId)) { announcedDevices.push(this.buildDeviceInfo(registrationId)); - await this.announceDevices(announcedDevices); + await this.announceDevices(announcedDevices, isNew ? registrationId : null); } else { console.log('[OmemoClient][announce] deviceId already found, no need to re-announce'); } @@ -580,7 +620,7 @@ export class OmemoClient { if (recipientBareJid === this.client.jid.bare && !deviceIds.includes(ownDeviceId)){ console.log('[OmemoClient][getRecipientSessions] add current device id', devices); - deviceIds.add(ownDeviceId); + deviceIds.push(ownDeviceId); } for (const deviceId of deviceIds) {