diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 2906a9d8e..a085a81c4 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -82,6 +82,10 @@ class LokiMessageAPI { } catch (e) { log.warn('Failed to send P2P message, falling back to storage', e); lokiP2pAPI.setContactOffline(pubKey); + if (isPing) { + // If this was just a ping, we don't bother sending to storage server + return; + } } } diff --git a/js/modules/loki_p2p_api.js b/js/modules/loki_p2p_api.js index aa7d8f726..fca8fbcf4 100644 --- a/js/modules/loki_p2p_api.js +++ b/js/modules/loki_p2p_api.js @@ -23,23 +23,47 @@ class LokiP2pAPI extends EventEmitter { ? 60 * 1000 // 1 minute : 2 * 60 * 1000; // 2 minutes - if (this.contactP2pDetails[pubKey]) { - clearTimeout(this.contactP2pDetails[pubKey].pingTimer); + if (!this.contactP2pDetails[pubKey]) { + // We didn't have this contact's details + this.contactP2pDetails[pubKey] = { + address, + port, + timerDuration, + pingTimer: null, + isOnline: false, + }; + // Try ping + this.pingContact(pubKey); + return; } - this.contactP2pDetails[pubKey] = { - address, - port, - timerDuration, - isOnline: false, - pingTimer: null, - }; + // We already had this contact's details + const baseDetails = { ...this.contactP2pDetails[pubKey] }; if (isPing) { + // Received a ping + // Update details in case they are new and mark online + this.contactP2pDetails[pubKey].address = address; + this.contactP2pDetails[pubKey].port = port; this.setContactOnline(pubKey); return; } + // Received a storage broadcast message + if ( + baseDetails.isOnline || + baseDetails.address !== address || + baseDetails.port !== port + ) { + // Had the contact marked as online and details we had were the same + // Do nothing + return; + } + + // Had the contact marked as offline or got new details + this.contactP2pDetails[pubKey].address = address; + this.contactP2pDetails[pubKey].port = port; + this.setContactOffline(pubKey); this.pingContact(pubKey); } @@ -77,7 +101,11 @@ class LokiP2pAPI extends EventEmitter { } pingContact(pubKey) { - if (!this.contactP2pDetails[pubKey]) { + if ( + !this.contactP2pDetails[pubKey] || + this.contactP2pDetails[pubKey].isOnline + ) { + // Don't ping if we don't have their details or they are already online return; } this.emit('pingContact', pubKey); diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 3a0204a81..934603148 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -218,9 +218,6 @@ MessageReceiver.prototype.extend({ const promise = Promise.resolve(request.body.toArrayBuffer()) // textsecure.crypto .then(plaintext => { const envelope = textsecure.protobuf.Envelope.decode(plaintext); - if (isP2p) { - lokiP2pAPI.setContactOnline(envelope.source); - } // After this point, decoding errors are not the server's // fault, and we should handle them gracefully and tell the // user they received an invalid message