Skip to content

Commit

Permalink
Merge pull request #508 from Mikunj/duplicate-message-fix
Browse files Browse the repository at this point in the history
Public chat duplicate message fix
  • Loading branch information
sachaaaaa authored Sep 24, 2019
2 parents ff7b396 + 75a527e commit a3d6778
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions js/modules/loki_public_chat_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -213,6 +214,10 @@ class LokiPublicChannelAPI {
this.deleteLastId = 1;
this.timers = {};
this.running = true;

// Cache for duplicate checking
this.lastMessagesCache = [];

// end properties

log.info(`registered LokiPublicChannel ${channelId}`);
Expand Down Expand Up @@ -574,6 +579,34 @@ class LokiPublicChannelAPI {
return; // Invalid message
}

// 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
const timestampsSimilar =
Math.abs(message.timestamp - timestamp) <=
PUBLICCHAT_MIN_TIME_BETWEEN_DUPLICATE_MESSAGES;

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,
Expand Down

0 comments on commit a3d6778

Please sign in to comment.