From 19b0dabe11cfbe87934e0f790607e282a09a695b Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 13 Sep 2019 15:48:57 +1000 Subject: [PATCH 1/3] Keep a cache of the last 5 fetched messages for public chat so we can use it to detect duplicate messages. --- js/modules/loki_public_chat_api.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/js/modules/loki_public_chat_api.js b/js/modules/loki_public_chat_api.js index 4823b0c1d..30a9758dc 100644 --- a/js/modules/loki_public_chat_api.js +++ b/js/modules/loki_public_chat_api.js @@ -213,6 +213,10 @@ class LokiPublicChannelAPI { this.deleteLastId = 1; this.timers = {}; this.running = true; + + // Cache for duplicate checking + this.lastMessagesCache = []; + // end properties log.info(`registered LokiPublicChannel ${channelId}`); @@ -574,6 +578,32 @@ class LokiPublicChannelAPI { return; // Invalid message } + // Duplicate check + const isDuplicate = message => { + const sameUsername = message.username === adnMessage.user.username; + const sameText = message.text === adnMessage.text; + // Don't filter out messages that are too far apart from each other + const timestampsSimilar = + Math.abs(message.timestamp - timestamp) <= 10000; + + return sameUsername && sameText && timestampsSimilar; + }; + + // Filter out any messages that we got previously + if (this.lastMessagesCache.some(isDuplicate)) { + return; // Duplicate message + } + + // Add the message to the lastMessage cache and keep the last 5 recent messages + this.lastMessagesCache = [ + ...this.lastMessagesCache, + { + username: adnMessage.user.username, + text: adnMessage.text, + timestamp, + }, + ].splice(-5); + const messageData = { serverId: adnMessage.id, friendRequest: false, From 001d8822738ae5e3586b8a84dd6e9256baf6f47d Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 16 Sep 2019 10:10:05 +1000 Subject: [PATCH 2/3] Add comment to fix up confusion. --- js/modules/loki_public_chat_api.js | 1 + 1 file changed, 1 insertion(+) diff --git a/js/modules/loki_public_chat_api.js b/js/modules/loki_public_chat_api.js index 30a9758dc..5bd563537 100644 --- a/js/modules/loki_public_chat_api.js +++ b/js/modules/loki_public_chat_api.js @@ -580,6 +580,7 @@ class LokiPublicChannelAPI { // Duplicate check const isDuplicate = message => { + // The username in this case is the users pubKey const sameUsername = message.username === adnMessage.user.username; const sameText = message.text === adnMessage.text; // Don't filter out messages that are too far apart from each other From 75a527e82801513e6c09f44c61b9c5654801de10 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Mon, 16 Sep 2019 10:18:46 +1000 Subject: [PATCH 3/3] Replaced value with a descriptive constant. --- js/modules/loki_public_chat_api.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/modules/loki_public_chat_api.js b/js/modules/loki_public_chat_api.js index 5bd563537..250baf1a3 100644 --- a/js/modules/loki_public_chat_api.js +++ b/js/modules/loki_public_chat_api.js @@ -9,6 +9,7 @@ const PUBLICCHAT_MSG_POLL_EVERY = 1.5 * 1000; // 1.5s const PUBLICCHAT_CHAN_POLL_EVERY = 20 * 1000; // 20s const PUBLICCHAT_DELETION_POLL_EVERY = 5 * 1000; // 5s const PUBLICCHAT_MOD_POLL_EVERY = 30 * 1000; // 30s +const PUBLICCHAT_MIN_TIME_BETWEEN_DUPLICATE_MESSAGES = 10 * 1000; // 10s // singleton to relay events to libtextsecure/message_receiver class LokiPublicChatAPI extends EventEmitter { @@ -585,7 +586,8 @@ class LokiPublicChannelAPI { const sameText = message.text === adnMessage.text; // Don't filter out messages that are too far apart from each other const timestampsSimilar = - Math.abs(message.timestamp - timestamp) <= 10000; + Math.abs(message.timestamp - timestamp) <= + PUBLICCHAT_MIN_TIME_BETWEEN_DUPLICATE_MESSAGES; return sameUsername && sameText && timestampsSimilar; };