From eb263a9ee7fec279bacadc5138d6c4968f572d23 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Mon, 10 Feb 2020 16:16:31 -0800 Subject: [PATCH 1/4] open() --- js/modules/loki_public_chat_api.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/js/modules/loki_public_chat_api.js b/js/modules/loki_public_chat_api.js index 8e4cd0f3d..0dff04c9b 100644 --- a/js/modules/loki_public_chat_api.js +++ b/js/modules/loki_public_chat_api.js @@ -13,6 +13,13 @@ class LokiPublicChatFactoryAPI extends EventEmitter { this.primaryUserProfileName = {}; } + // MessageReceiver.connect calls this + // start polling in all existing registered channels + async open() { + await Promise.all(this.servers.map(server => server.open())); + } + + // MessageReceiver.close async close() { await Promise.all(this.servers.map(server => server.close())); } From dc14eb9ca7f8b544d6e75b457221ab3e975dc294 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Mon, 10 Feb 2020 16:43:30 -0800 Subject: [PATCH 2/4] open(), logging, stop() clears timers --- js/modules/loki_app_dot_net_api.js | 65 ++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 7 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index fab433da6..a3aca1d10 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -34,8 +34,17 @@ class LokiAppDotNetServerAPI { log.info(`LokiAppDotNetAPI registered server ${url}`); } + async open() { + // check token, we're not sure how long we were asleep, token may have expired + await this.getOrRefreshServerToken(); + // now that we have a working token, start up pollers + this.channels.forEach(channel => channel.open()); + } + async close() { this.channels.forEach(channel => channel.stop()); + // match sure our pending requests are finished + // in case it's still starting up if (this.tokenPromise) { await this.tokenPromise; } @@ -70,6 +79,7 @@ class LokiAppDotNetServerAPI { } async partChannel(channelId) { + log.info('partChannel', channelId, 'from', this.baseServerUrl); await this.serverRequest(`channels/${channelId}/subscribe`, { method: 'DELETE', }); @@ -78,6 +88,7 @@ class LokiAppDotNetServerAPI { // deallocate resources channel uses unregisterChannel(channelId) { + log.info('unregisterChannel', channelId, 'from', this.baseServerUrl); let thisChannel; let i = 0; for (; i < this.channels.length; i += 1) { @@ -189,6 +200,7 @@ class LokiAppDotNetServerAPI { if (!token) { // if we haven't forced it if (!forceRefresh) { + // try one more time with requesting a fresh token token = await this.getOrRefreshServerToken(true); } return token; @@ -898,7 +910,6 @@ class LokiPublicChannelAPI { this.modStatus = false; this.deleteLastId = 1; this.timers = {}; - this.running = true; this.myPrivateKey = false; // can escalated to SQL if it start uses too much memory this.logMop = {}; @@ -914,12 +925,7 @@ class LokiPublicChannelAPI { }` ); // start polling - this.pollForMessages(); - this.pollForDeletions(); - this.pollForChannel(); - this.pollForModerators(); - - // TODO: poll for group members here? + this.open(); } async getPrivateKey() { @@ -948,19 +954,64 @@ class LokiPublicChannelAPI { return true; } + open() { + log.info( + `LokiPublicChannel open ${this.channelId} on ${ + this.serverAPI.baseServerUrl + }` + ) + if (this.running) { + log.warn( + `LokiPublicChannel already open ${this.channelId} on ${ + this.serverAPI.baseServerUrl + }` + ) + } + this.running = true; + if (!this.timers.channel) { + this.pollForChannel(); + } + if (!this.timers.moderator) { + this.pollForModerators(); + } + if (!this.timers.delete) { + this.pollForDeletions(); + } + if (!this.timers.message) { + this.pollForMessages(); + } + // TODO: poll for group members here? + } + stop() { + log.info( + `LokiPublicChannel close ${this.channelId} on ${ + this.serverAPI.baseServerUrl + }` + ) + if (!this.running) { + log.warn( + `LokiPublicChannel already open ${this.channelId} on ${ + this.serverAPI.baseServerUrl + }` + ) + } this.running = false; if (this.timers.channel) { clearTimeout(this.timers.channel); + this.timers.channel = false; } if (this.timers.moderator) { clearTimeout(this.timers.moderator); + this.timers.moderator = false; } if (this.timers.delete) { clearTimeout(this.timers.delete); + this.timers.delete = false; } if (this.timers.message) { clearTimeout(this.timers.message); + this.timers.message = false; } } From 031330154b85d2d02bead01700bed02f7f49f05b Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Mon, 10 Feb 2020 16:45:34 -0800 Subject: [PATCH 3/4] only bind open group events once, start open group polling on connect --- libtextsecure/message_receiver.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 6146f86dc..3a4e67de0 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -48,6 +48,17 @@ function MessageReceiver(username, password, signalingKey, options = {}) { if (options.retryCached) { this.pending = this.queueAllCached(); } + + // only do this once to prevent duplicates + if (lokiPublicChatAPI) { + // bind events + lokiPublicChatAPI.on( + 'publicMessage', + this.handleUnencryptedMessage.bind(this) + ); + } else { + window.log.error('Can not handle open group data, API is not available'); + } } MessageReceiver.stringToArrayBuffer = string => @@ -79,11 +90,11 @@ MessageReceiver.prototype.extend({ handleRequest: this.handleRequest.bind(this), }); this.httpPollingResource.pollServer(); + + // start polling all open group rooms you have registered + // if not registered yet, they'll get started when they're created if (lokiPublicChatAPI) { - lokiPublicChatAPI.on( - 'publicMessage', - this.handleUnencryptedMessage.bind(this) - ); + lokiPublicChatAPI.open(); } // set up pollers for any RSS feeds feeds.forEach(feed => { @@ -166,6 +177,7 @@ MessageReceiver.prototype.extend({ this.wsr.close(3000, 'called close'); } + // stop polling all open group rooms if (lokiPublicChatAPI) { await lokiPublicChatAPI.close(); } From d0f0cac31ea503ad72798c8d65de7c7ce46f65a2 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Mon, 10 Feb 2020 16:53:53 -0800 Subject: [PATCH 4/4] lint --- js/modules/loki_app_dot_net_api.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index a3aca1d10..da8acc12b 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -499,7 +499,10 @@ class LokiAppDotNetServerAPI { try { response = options.textResponse ? respStr : JSON.parse(respStr); } catch (e) { - log.warn(`_sendToProxy Could not parse inner JSON [${respStr}]`, endpoint); + log.warn( + `_sendToProxy Could not parse inner JSON [${respStr}]`, + endpoint + ); } } else { log.warn( @@ -959,13 +962,13 @@ class LokiPublicChannelAPI { `LokiPublicChannel open ${this.channelId} on ${ this.serverAPI.baseServerUrl }` - ) + ); if (this.running) { log.warn( `LokiPublicChannel already open ${this.channelId} on ${ this.serverAPI.baseServerUrl }` - ) + ); } this.running = true; if (!this.timers.channel) { @@ -988,13 +991,13 @@ class LokiPublicChannelAPI { `LokiPublicChannel close ${this.channelId} on ${ this.serverAPI.baseServerUrl }` - ) + ); if (!this.running) { log.warn( `LokiPublicChannel already open ${this.channelId} on ${ this.serverAPI.baseServerUrl }` - ) + ); } this.running = false; if (this.timers.channel) {