From 4934714eae65e1d6e4aa82894f1903bb0df3ee1d Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 22 Jun 2022 13:51:36 +1000 Subject: [PATCH] feat: rate limit --- .../Conversations/ConversationVC+Interaction.swift | 11 +++++++++++ SessionMessagingKit/Utilities/General.swift | 1 + 2 files changed, 12 insertions(+) diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index e97fbb2dd..a4869382f 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -862,6 +862,17 @@ extension ConversationVC : InputViewDelegate, MessageCellDelegate, ContextMenuAc reactMessage.sender = getUserHexEncodedPublicKey() let thread = self.thread let sentTimestamp: UInt64 = NSDate.millisecondTimestamp() + General.Cache.recentReactionTimestamps.mutate{ $0.append(sentTimestamp) } + // Rate Limit + if General.Cache.recentReactionTimestamps.wrappedValue.count > 20 { + let firstTimestamp = General.Cache.recentReactionTimestamps.wrappedValue.first! + if sentTimestamp - firstTimestamp < 60 * 1000 { + General.Cache.recentReactionTimestamps.mutate{ $0.removeLast() } + return + } else { + General.Cache.recentReactionTimestamps.mutate{ $0.removeFirst() } + } + } let visibleMessage = VisibleMessage() visibleMessage.sentTimestamp = sentTimestamp visibleMessage.reaction = .from(reactMessage) diff --git a/SessionMessagingKit/Utilities/General.swift b/SessionMessagingKit/Utilities/General.swift index d2e4e0c96..a9d4873ff 100644 --- a/SessionMessagingKit/Utilities/General.swift +++ b/SessionMessagingKit/Utilities/General.swift @@ -3,6 +3,7 @@ import Foundation public enum General { public enum Cache { public static var cachedEncodedPublicKey: Atomic = Atomic(nil) + public static var recentReactionTimestamps: Atomic<[UInt64]> = Atomic([]) } }