From 27976a053ca9c078cf60bdbcbba91e47c2c5a9ab Mon Sep 17 00:00:00 2001 From: Beaudan Brown Date: Mon, 2 Sep 2019 12:27:33 +1000 Subject: [PATCH] Save the last retrieved message for public chats on the conversation --- js/background.js | 2 +- js/models/conversations.js | 18 ++++++++++++++++++ js/modules/loki_public_chat_api.js | 12 +++++++++--- js/modules/loki_rss_api.js | 1 - js/modules/loki_snode_api.js | 1 + 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/js/background.js b/js/background.js index 9b27d064c..85f53af2f 100644 --- a/js/background.js +++ b/js/background.js @@ -227,7 +227,7 @@ ); channel.refreshModStatus(); }); - } + }; const initAPIs = async () => { const ourKey = textsecure.storage.user.getNumber(); diff --git a/js/models/conversations.js b/js/models/conversations.js index 5987daed2..0829a5a8e 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -2079,6 +2079,24 @@ ); return channelAPI; }, + getLastRetrievedMessage() { + if (!this.isPublic()) { + return null; + } + const lastMessageId = this.get('lastPublicMessage') || 0; + return lastMessageId; + }, + async setLastRetrievedMessage(newLastMessageId) { + if (!this.isPublic()) { + return; + } + if (this.get('lastPublicMessage') !== newLastMessageId) { + this.set({ lastPublicMessage: newLastMessageId }); + await window.Signal.Data.updateConversation(this.id, this.attributes, { + Conversation: Whisper.Conversation, + }); + } + }, isModerator() { if (!this.isPublic()) { return false; diff --git a/js/modules/loki_public_chat_api.js b/js/modules/loki_public_chat_api.js index eb4fe6c7f..59214f65e 100644 --- a/js/modules/loki_public_chat_api.js +++ b/js/modules/loki_public_chat_api.js @@ -205,7 +205,7 @@ class LokiPublicChannelAPI { this.baseChannelUrl = `channels/${this.channelId}`; this.groupName = 'unknown'; this.conversationId = conversationId; - this.lastGot = 0; + this.lastGot = null; this.stopPolling = false; this.modStatus = false; this.deleteLastId = 1; @@ -416,9 +416,14 @@ class LokiPublicChannelAPI { count: -20, include_deleted: false, }; - if (this.lastGot) { - params.since_id = this.lastGot; + const conversation = ConversationController.get(this.conversationId); + if (!conversation) { + log.warn('Trying to poll for non-existing public conversation'); + this.lastGot = 0; + } else if (!this.lastGot) { + this.lastGot = conversation.getLastRetrievedMessage(); } + params.since_id = this.lastGot; const res = await this.serverRequest(`${this.baseChannelUrl}/messages`, { params, }); @@ -485,6 +490,7 @@ class LokiPublicChannelAPI { this.lastGot = !this.lastGot ? adnMessage.id : Math.max(this.lastGot, adnMessage.id); + conversation.setLastRetrievedMessage(this.lastGot); }); } diff --git a/js/modules/loki_rss_api.js b/js/modules/loki_rss_api.js index bf4dedebb..ee5486d53 100644 --- a/js/modules/loki_rss_api.js +++ b/js/modules/loki_rss_api.js @@ -49,7 +49,6 @@ class LokiRssAPI extends EventEmitter { this.closeable = settings.closeable; // non configureable options this.feedTimer = null; - this.conversationSetup = false; // initial set up this.getFeed(); } diff --git a/js/modules/loki_snode_api.js b/js/modules/loki_snode_api.js index 49e00c773..d46b2755d 100644 --- a/js/modules/loki_snode_api.js +++ b/js/modules/loki_snode_api.js @@ -89,6 +89,7 @@ class LokiSnodeAPI { async initialiseRandomPool(seedNodes = [...window.seedNodeList]) { const params = { limit: 20, + active_only: true, fields: { public_ip: true, storage_port: true,