discourse/plugins/chat/lib/chat_message_rate_limiter.rb
Roman Rizzi 0a5f548635
DEV: Move discourse-chat to the core repo. (#18776)
As part of this move, we are also renaming `discourse-chat` to `chat`.
2022-11-02 10:41:30 -03:00

50 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class Chat::ChatMessageRateLimiter
def self.run!(user)
instance = self.new(user)
instance.run!
end
def initialize(user)
@user = user
end
def run!
return if @user.staff?
allowed_message_count =
(
if @user.trust_level == TrustLevel[0]
SiteSetting.chat_allowed_messages_for_trust_level_0
else
SiteSetting.chat_allowed_messages_for_other_trust_levels
end
)
return if allowed_message_count.zero?
@rate_limiter = RateLimiter.new(@user, "create_chat_message", allowed_message_count, 30.seconds)
silence_user if @rate_limiter.remaining.zero?
@rate_limiter.performed!
end
def clear!
# Used only for testing. Need to clear the rate limiter between tests.
@rate_limiter.clear! if defined?(@rate_limiter)
end
private
def silence_user
silenced_for_minutes = SiteSetting.chat_auto_silence_duration
return unless silenced_for_minutes > 0
UserSilencer.silence(
@user,
Discourse.system_user,
silenced_till: silenced_for_minutes.minutes.from_now,
reason: I18n.t("chat.errors.rate_limit_exceeded"),
)
end
end