Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaaaaa committed Oct 2, 2019
1 parent 1f3311b commit 715691f
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions libloki/storage.js
Expand Up @@ -114,30 +114,39 @@
}

// fetches device mappings from server.
// if the device is a secondary device,
// fetch the device mappings for its primary device
async function saveAllPairingAuthorisationsFor(pubKey) {
async function getPrimaryDeviceMapping(pubKey) {
const deviceMapping = await lokiFileServerAPI.getUserDeviceMapping(pubKey);
let { authorisations } = deviceMapping || {};
if (deviceMapping) {
if (deviceMapping.isPrimary !== '1') {
const { primaryDevicePubKey } =
authorisations.find(
authorisation => authorisation.secondaryDevicePubKey === pubKey
) || {};
if (primaryDevicePubKey) {
({ authorisations } =
(await lokiFileServerAPI.getUserDeviceMapping(
primaryDevicePubKey
)) || {});
}
await Promise.all(
authorisations.map(authorisation =>
savePairingAuthorisation(authorisation)
)
);
if (!deviceMapping) {
return [];
}
let { authorisations } = deviceMapping;
if (!authorisations) {
return [];
}
if (deviceMapping.isPrimary !== '1') {
const { primaryDevicePubKey } = authorisations.find(
authorisation => authorisation.secondaryDevicePubKey === pubKey
);
if (primaryDevicePubKey) {
// do NOT call getprimaryDeviceMapping recursively
// in case both devices are out of sync and think they are
// each others' secondary pubkey.
({ authorisations } = await lokiFileServerAPI.getUserDeviceMapping(
primaryDevicePubKey
));
}
}
return authorisations || [];
}
// if the device is a secondary device,
// fetch the device mappings for its primary device
async function saveAllPairingAuthorisationsFor(pubKey) {
const authorisations = await getPrimaryDeviceMapping(pubKey);
await Promise.all(
authorisations.map(authorisation =>
savePairingAuthorisation(authorisation)
)
);
}

function savePairingAuthorisation(authorisation) {
Expand Down

0 comments on commit 715691f

Please sign in to comment.