From efe95a70bc8afd58df062e8cc715f37d1402e241 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 31 Jan 2019 11:58:43 +1100 Subject: [PATCH] Fix review issues. --- js/modules/loki_message_api.js | 30 +++++++++++++++++++++--------- libtextsecure/message_receiver.js | 7 ++----- libtextsecure/outgoing_message.js | 5 +++-- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 90a1920ca..ce4bc1b83 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -22,7 +22,7 @@ class NotFoundError extends Error { // A small wrapper around node-fetch which deserializes response const fetch = async (url, options = {}) => { - const timeout = 10000; + const timeout = options.timeout || 10000; const method = options.method || 'GET'; try { @@ -92,6 +92,11 @@ class LokiMessageAPI { let swarmNodes = await window.Signal.Data.getSwarmNodesByPubkey(pubKey); + const nodeComplete = nodeUrl => { + completedNodes.push(nodeUrl); + swarmNodes = swarmNodes.filter(node => node !== nodeUrl); + }; + const doRequest = async nodeUrl => { const url = `${nodeUrl}${this.messageServerPort}/store`; const fetchOptions = { @@ -108,8 +113,7 @@ class LokiMessageAPI { try { await fetch(url, fetchOptions); - completedNodes.push(nodeUrl); - swarmNodes = swarmNodes.filter(node => node !== nodeUrl); + nodeComplete(nodeUrl); successfulRequests += 1; } catch (e) { if (e instanceof NotFoundError) { @@ -120,11 +124,13 @@ class LokiMessageAPI { e.response.status, 'Error sending message' ); + + // We mark the node as complete as we could still reach it + nodeComplete(nodeUrl); } else { log.error('Loki SendMessages:', e); if (window.LokiSnodeAPI.unreachableNode(pubKey, nodeUrl)) { - completedNodes.push(nodeUrl); - swarmNodes = swarmNodes.filter(node => node !== nodeUrl); + nodeComplete(nodeUrl); } } } @@ -176,6 +182,11 @@ class LokiMessageAPI { throw new window.textsecure.EmptySwarmError(ourKey, e); } + const nodeComplete = nodeUrl => { + completedNodes.push(nodeUrl); + delete ourSwarmNodes[nodeUrl]; + }; + const doRequest = async (nodeUrl, nodeData) => { const url = `${nodeUrl}${this.messageServerPort}/retrieve`; const headers = { @@ -191,8 +202,7 @@ class LokiMessageAPI { headers, }); - completedNodes.push(nodeUrl); - delete ourSwarmNodes[nodeUrl]; + nodeComplete(nodeUrl); if (result.lastHash) { window.LokiSnodeAPI.updateLastHash(nodeUrl, result.lastHash); @@ -208,11 +218,13 @@ class LokiMessageAPI { e.response.status, `Error retrieving messages from ${nodeUrl}` ); + + // We mark the node as complete as we could still reach it + nodeComplete(nodeUrl); } else { log.error('Loki RetrieveMessages:', e); if (window.LokiSnodeAPI.unreachableNode(ourKey, nodeUrl)) { - completedNodes.push(nodeUrl); - delete ourSwarmNodes[nodeUrl]; + nodeComplete(nodeUrl); } } } diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 66298084b..d0e42d7d2 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -15,6 +15,7 @@ /* global localLokiServer: false */ /* global localServerPort: false */ /* global lokiMessageAPI: false */ +/* global lokiP2pAPI: false */ /* eslint-disable more/no-then */ /* eslint-disable no-unreachable */ @@ -900,11 +901,7 @@ MessageReceiver.prototype.extend({ }, async handleLokiAddressMessage(envelope, lokiAddressMessage) { const { p2pAddress, p2pPort } = lokiAddressMessage; - window.lokiP2pAPI.addContactP2pDetails( - envelope.source, - p2pAddress, - p2pPort - ); + lokiP2pAPI.addContactP2pDetails(envelope.source, p2pAddress, p2pPort); return this.removeFromCache(envelope); }, handleDataMessage(envelope, msg) { diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 1e3b0f080..3bee70dbd 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -7,6 +7,7 @@ StringView, dcodeIO, log, + lokiMessageAPI, */ /* eslint-disable more/no-then */ @@ -121,7 +122,7 @@ OutgoingMessage.prototype = { .processPreKey(device) .then(async () => { // TODO: only remove the keys that were used above! - await window.libloki.storage.removeContactPreKeyBundle(number); + await libloki.storage.removeContactPreKeyBundle(number); return true; }) .catch(error => { @@ -184,7 +185,7 @@ OutgoingMessage.prototype = { async transmitMessage(number, data, timestamp, ttl = 24 * 60 * 60) { const pubKey = number; try { - await window.lokiMessageAPI.sendMessage(pubKey, data, timestamp, ttl); + await lokiMessageAPI.sendMessage(pubKey, data, timestamp, ttl); } catch (e) { if (e.name === 'HTTPError' && (e.code !== 409 && e.code !== 410)) { // 409 and 410 should bubble and be handled by doSendMessage