From 29bca71d5a179b6df02cacb0b21b99f69f6df40d Mon Sep 17 00:00:00 2001 From: Beaudan Date: Mon, 18 Feb 2019 12:46:19 +1100 Subject: [PATCH] Try ping contacts that send us storage server messages in the last 2 mins --- js/modules/loki_p2p_api.js | 17 +++++++++++------ libtextsecure/message_receiver.js | 10 ++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/js/modules/loki_p2p_api.js b/js/modules/loki_p2p_api.js index fca8fbcf4..941495b10 100644 --- a/js/modules/loki_p2p_api.js +++ b/js/modules/loki_p2p_api.js @@ -56,7 +56,7 @@ class LokiP2pAPI extends EventEmitter { baseDetails.port !== port ) { // Had the contact marked as online and details we had were the same - // Do nothing + this.pingContact(pubKey); return; } @@ -71,6 +71,14 @@ class LokiP2pAPI extends EventEmitter { return this.contactP2pDetails[pubKey] || null; } + isContactOnline(pubKey) { + const contactDetails = this.contactP2pDetails[pubKey]; + if (!contactDetails || !contactDetails.isOnline) { + return false; + } + return contactDetails.isOnline; + } + setContactOffline(pubKey) { this.emit('offline', pubKey); if (!this.contactP2pDetails[pubKey]) { @@ -101,11 +109,8 @@ class LokiP2pAPI extends EventEmitter { } pingContact(pubKey) { - if ( - !this.contactP2pDetails[pubKey] || - this.contactP2pDetails[pubKey].isOnline - ) { - // Don't ping if we don't have their details or they are already online + if (!this.contactP2pDetails[pubKey]) { + // Don't ping if we don't have their details return; } this.emit('pingContact', pubKey); diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 934603148..985e42af3 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -945,6 +945,16 @@ MessageReceiver.prototype.extend({ return this.removeFromCache(envelope); }, handleDataMessage(envelope, msg) { + if (envelope.isP2p) { + lokiP2pAPI.setContactOnline(envelope.source); + } else { + const timestamp = envelope.timestamp.toNumber(); + const now = Date.now(); + const ageInSeconds = (now - timestamp) / 1000; + if (ageInSeconds <= 120) { + lokiP2pAPI.pingContact(envelope.source); + } + } window.log.info('data message from', this.getEnvelopeId(envelope)); let p = Promise.resolve(); // eslint-disable-next-line no-bitwise