From 2311bd132877928911fdf18d85579b764b799de0 Mon Sep 17 00:00:00 2001 From: Beaudan Date: Mon, 11 Feb 2019 16:32:15 +1100 Subject: [PATCH] Fix bug with swarm node reference, now we should only refresh swarm nodes when they fail --- js/conversation_controller.js | 7 ++++--- js/modules/data.js | 9 --------- js/modules/loki_message_api.js | 5 ++++- js/modules/loki_snode_api.js | 10 ++++++---- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/js/conversation_controller.js b/js/conversation_controller.js index 796dd20ed..6d3bd2bf9 100644 --- a/js/conversation_controller.js +++ b/js/conversation_controller.js @@ -199,9 +199,10 @@ return conversation; }; - conversation.initialPromise = create().then(async () => { - window.lokiSnodeAPI.refreshSwarmNodesForPubKey(id); - conversation.updateProfileAvatar(); + conversation.initialPromise = create(); + conversation.initialPromise.then(async () => { + await window.lokiSnodeAPI.refreshSwarmNodesForPubKey(id); + await conversation.updateProfileAvatar(); }); return conversation; diff --git a/js/modules/data.js b/js/modules/data.js index e850c723d..c9c3dddbb 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -111,7 +111,6 @@ module.exports = { removeAllSessions, getSwarmNodesByPubkey, - saveSwarmNodesForPubKey, getConversationCount, saveConversation, @@ -676,14 +675,6 @@ async function getSwarmNodesByPubkey(pubkey) { return channels.getSwarmNodesByPubkey(pubkey); } -async function saveSwarmNodesForPubKey(pubKey, swarmNodes, { Conversation }) { - const conversation = await getConversationById(pubKey, { Conversation }); - conversation.set({ swarmNodes }); - await updateConversation(conversation.id, conversation.attributes, { - Conversation, - }); -} - async function getConversationCount() { return channels.getConversationCount(); } diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index e91812cc3..5dcd82927 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -106,6 +106,7 @@ class LokiMessageAPI { throw err; } const completedNodes = []; + const failedNodes = []; let successfulRequests = 0; let canResolve = true; @@ -150,6 +151,7 @@ class LokiMessageAPI { log.error('Loki SendMessages:', e); if (lokiSnodeAPI.unreachableNode(pubKey, nodeUrl)) { nodeComplete(nodeUrl); + failedNodes.push(nodeUrl); } } } @@ -161,6 +163,8 @@ class LokiMessageAPI { } if (swarmNodes.length === 0) { const freshNodes = await lokiSnodeAPI.getFreshSwarmNodes(pubKey); + const goodNodes = _.difference(freshNodes, failedNodes); + await lokiSnodeAPI.updateSwarmNodes(pubKey, goodNodes); swarmNodes = _.difference(freshNodes, completedNodes); if (swarmNodes.length === 0) { if (successfulRequests !== 0) { @@ -172,7 +176,6 @@ class LokiMessageAPI { new Error('Ran out of swarm nodes to query') ); } - lokiSnodeAPI.updateSwarmNodes(pubKey, swarmNodes); } const remainingRequests = diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index 76e41e893..209c8cd09 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -59,9 +59,10 @@ class LokiSnodeAPI { return false; } const conversation = ConversationController.get(pubKey); - const swarmNodes = conversation.get('swarmNodes'); - if (swarmNodes.delete(nodeUrl)) { - await conversation.updateSwarmNodes(swarmNodes); + const swarmNodes = [...conversation.get('swarmNodes')]; + if (nodeUrl in swarmNodes) { + const filteredNodes = swarmNodes.filter(node => node !== nodeUrl); + await conversation.updateSwarmNodes(filteredNodes); delete this.contactSwarmNodes[nodeUrl]; } return true; @@ -82,7 +83,7 @@ class LokiSnodeAPI { let swarmNodes; try { conversation = ConversationController.get(pubKey); - swarmNodes = conversation.get('swarmNodes'); + swarmNodes = [...conversation.get('swarmNodes')]; } catch (e) { throw new window.textsecure.ReplayableError({ message: 'Could not get conversation', @@ -143,6 +144,7 @@ class LokiSnodeAPI { newSwarmNodes = await this.getSwarmNodes(pubKey); } catch (e) { // TODO: Handle these errors sensibly + log.error('Failed to get new swarm nodes'); newSwarmNodes = []; } resolve(newSwarmNodes);