Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemeted advanced device management flow #7

Open
wants to merge 7 commits into
base: omemo
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions lib/plugins/omemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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) {
Expand Down