Added small helper functions

This commit is contained in:
Mikunj 2020-06-17 10:51:07 +10:00
parent a83ce4ee16
commit f681fd619d
2 changed files with 29 additions and 4 deletions

View file

@ -211,11 +211,9 @@
return true;
}
const ourDevices = await window.libsession.Protocols.MultiDeviceProtocol.getAllDevices(
this.ourNumber
return window.libsession.Protocols.MultiDeviceProtocol.isOurDevice(
this.id
);
return ourDevices.some(device => device.key === this.id);
},
isOurLocalDevice() {
return this.id === this.ourNumber;

View file

@ -192,4 +192,31 @@ export class MultiDeviceProtocol {
.map(a => a.secondaryDevicePubKey)
.map(pubKey => new SecondaryPubKey(pubKey));
}
/**
* Get all devices linked to the current user.
*/
public static async getOurDevices(): Promise<Array<PubKey>> {
const ourPubKey = await UserUtil.getCurrentDevicePubKey();
if (!ourPubKey) {
throw new Error('Public key not set.');
}
return this.getAllDevices(ourPubKey);
}
/**
* Check if the given device is one of our own.
* @param device The device to check.
*/
public static async isOurDevice(device: PubKey | string): Promise<boolean> {
const pubKey = typeof device === 'string' ? new PubKey(device) : device;
try {
const ourDevices = await this.getOurDevices();
return ourDevices.some(d => PubKey.isEqual(d, pubKey));
} catch (e) {
return false;
}
}
}