diff --git a/js/models/messages.js b/js/models/messages.js index d2e1cd87e..49084a411 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -12,7 +12,6 @@ Whisper, clipboard, libloki, - lokiFileServerAPI, */ /* eslint-disable more/no-then */ diff --git a/libloki/storage.js b/libloki/storage.js index aea53b4b7..733d3df92 100644 --- a/libloki/storage.js +++ b/libloki/storage.js @@ -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) {