From 0c870d04e3d1e892625d739e282d486e38c78a48 Mon Sep 17 00:00:00 2001 From: Beaudan Brown Date: Wed, 4 Sep 2019 17:45:46 +1000 Subject: [PATCH] Stop polling when leaving public chat, only grab the most recent 100 messages on first poll --- js/models/conversations.js | 8 +------- js/modules/loki_public_chat_api.js | 18 +++++++++--------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index 4c0320458..52cdaf610 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -2327,13 +2327,7 @@ Whisper.events.trigger('showConfirmationDialog', { message, - onOk: () => { - ConversationController.deleteContact(this.id); - if (this.isPublic()) { - const channelAPI = this.getPublicSendData(); - channelAPI.stop(); - } - }, + onOk: () => ConversationController.deleteContact(this.id), }); }, diff --git a/js/modules/loki_public_chat_api.js b/js/modules/loki_public_chat_api.js index f2f312206..c0544b8f6 100644 --- a/js/modules/loki_public_chat_api.js +++ b/js/modules/loki_public_chat_api.js @@ -41,7 +41,7 @@ class LokiPublicChatAPI extends EventEmitter { let thisServer; let i = 0; for (; i < this.servers.length; i += 1) { - if (this.servers[i].server === serverUrl) { + if (this.servers[i].baseServerUrl === serverUrl) { thisServer = this.servers[i]; break; } @@ -95,8 +95,8 @@ class LokiPublicServerAPI { if (!thisChannel) { return; } + thisChannel.stop(); this.channels.splice(i, 1); - thisChannel.stopPolling = true; } // get active token for this server @@ -208,11 +208,10 @@ class LokiPublicChannelAPI { this.conversationId = conversationId; this.conversation = ConversationController.get(conversationId); this.lastGot = null; - this.stopPolling = false; this.modStatus = false; this.deleteLastId = 1; this.timers = {}; - this.stop = false; + this.running = true; // end properties log.info(`registered LokiPublicChannel ${channelId}`); @@ -224,7 +223,7 @@ class LokiPublicChannelAPI { } stop() { - this.stop = true; + this.running = false; if (this.timers.channel) { clearTimeout(this.timers.channel); } @@ -389,7 +388,7 @@ class LokiPublicChannelAPI { } catch (e) { log.warn(`Error while polling for public chat deletions: ${e}`); } - if (!this.stop) { + if (this.running) { this.timers.channel = setTimeout(() => { this.pollForChannelOnce(); }, PUBLICCHAT_CHAN_POLL_EVERY); @@ -431,7 +430,7 @@ class LokiPublicChannelAPI { } catch (e) { log.warn(`Error while polling for public chat deletions: ${e}`); } - if (!this.stop) { + if (this.running) { this.timers.delete = setTimeout(() => { this.pollForDeletions(); }, PUBLICCHAT_DELETION_POLL_EVERY); @@ -485,7 +484,7 @@ class LokiPublicChannelAPI { } catch (e) { log.warn(`Error while polling for public chat messages: ${e}`); } - if (!this.stop) { + if (this.running) { setTimeout(() => { this.timers.message = this.pollForMessages(); }, PUBLICCHAT_MSG_POLL_EVERY); @@ -495,7 +494,6 @@ class LokiPublicChannelAPI { async pollOnceForMessages() { const params = { include_annotations: 1, - count: -20, include_deleted: false, }; if (!this.conversation) { @@ -505,6 +503,8 @@ class LokiPublicChannelAPI { this.lastGot = this.conversation.getLastRetrievedMessage(); } params.since_id = this.lastGot; + // Just grab the most recent 100 messages if you don't have a valid lastGot + params.count = this.lastGot === 0 ? -100 : 20; const res = await this.serverRequest(`${this.baseChannelUrl}/messages`, { params, });