From 24a687c1066820ea012f26b77eaa2de7f6ddff81 Mon Sep 17 00:00:00 2001 From: Beaudan Brown Date: Wed, 13 Nov 2019 13:34:27 +1100 Subject: [PATCH] Throttle the refreshing of device mappings to once every minute --- js/models/messages.js | 2 -- libloki/storage.js | 10 ++++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/js/models/messages.js b/js/models/messages.js index 243481218..f85630a9b 100644 --- a/js/models/messages.js +++ b/js/models/messages.js @@ -367,8 +367,6 @@ await window.Signal.Data.saveMessage(this.attributes, { Message: Whisper.Message, }); - const pubKey = this.get('conversationId'); - await libloki.storage.saveAllPairingAuthorisationsFor(pubKey); conversation.onAcceptFriendRequest(); }, async declineFriendRequest() { diff --git a/libloki/storage.js b/libloki/storage.js index 6d8daf1d5..407d6a0fb 100644 --- a/libloki/storage.js +++ b/libloki/storage.js @@ -5,6 +5,9 @@ (function() { window.libloki = window.libloki || {}; + const timers = {}; + const REFRESH_DELAY = 60 * 1000; + async function getPreKeyBundleForContact(pubKey) { const myKeyPair = await textsecure.storage.protocol.getIdentityKeyPair(); const identityKey = myKeyPair.pubKey; @@ -149,6 +152,12 @@ // if the device is a secondary device, // fetch the device mappings for its primary device async function saveAllPairingAuthorisationsFor(pubKey) { + // Will be false if there is no timer + const cacheValid = timers[pubKey] > Date.now(); + if (cacheValid) { + return; + } + timers[pubKey] = Date.now() + REFRESH_DELAY; const authorisations = await getPrimaryDeviceMapping(pubKey); await Promise.all( authorisations.map(authorisation => @@ -199,6 +208,7 @@ // Transforms signatures from base64 to ArrayBuffer! async function getAuthorisationForSecondaryPubKey(secondaryPubKey) { + await saveAllPairingAuthorisationsFor(secondaryPubKey); const authorisation = await window.Signal.Data.getAuthorisationForSecondaryPubKey( secondaryPubKey );