[Dmails] Tweak rate limit

* Lower hourly limit
* Add daily limit
This commit is contained in:
Earlopain 2023-09-17 17:57:17 +02:00
parent 632b611c4c
commit 26ed6928ba
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
3 changed files with 29 additions and 14 deletions

View File

@ -141,13 +141,22 @@ class Dmail < ApplicationRecord
return true if bypass_limits == true
return true if from_id == User.system.id
return true if from.is_moderator?
allowed = CurrentUser.can_dmail_with_reason
minute_allowed = CurrentUser.can_dmail_minute_with_reason
if allowed != true || minute_allowed != true
errors.add(:base, "Sender #{User.throttle_reason(allowed != true ? allowed : minute_allowed)}")
false
if allowed != true
errors.add(:base, "Sender #{User.throttle_reason(allowed)}")
return
end
minute_allowed = CurrentUser.can_dmail_minute_with_reason
if minute_allowed != true
errors.add(:base, "Please wait a bit before trying to send again")
return
end
day_allowed = CurrentUser.can_dmail_day_with_reason
if day_allowed != true
errors.add(:base, "Sender #{User.throttle_reason(day_allowed, 'daily')}")
return
end
true
end
def recipient_accepts_dmails

View File

@ -410,12 +410,12 @@ class User < ApplicationRecord
end
module ThrottleMethods
def throttle_reason(reason)
def throttle_reason(reason, timeframe = "hourly")
reasons = {
REJ_NEWBIE: 'can not yet perform this action. Account is too new',
REJ_LIMITED: 'have reached the hourly limit for this action'
REJ_NEWBIE: "can not yet perform this action. Account is too new",
REJ_LIMITED: "have reached the #{timeframe} limit for this action",
}
reasons.fetch(reason, 'unknown throttle reason, please report this as a bug')
reasons.fetch(reason, "unknown throttle reason, please report this as a bug")
end
def upload_reason_string(reason)
@ -482,9 +482,11 @@ class User < ApplicationRecord
nil, 3.days)
create_user_throttle(:blip, ->{ Danbooru.config.blip_limit - Blip.for_creator(id).where('created_at > ?', 1.hour.ago).count },
:general_bypass_throttle?, 3.days)
create_user_throttle(:dmail_minute, ->{ Danbooru.config.dmail_minute_limit - Dmail.sent_by_id(id).where('created_at > ?', 1.minute.ago).count },
nil, 7.days)
create_user_throttle(:dmail, ->{ Danbooru.config.dmail_limit - Dmail.sent_by_id(id).where('created_at > ?', 1.hour.ago).count },
nil, 7.days)
create_user_throttle(:dmail_minute, ->{ Danbooru.config.dmail_minute_limit - Dmail.sent_by_id(id).where('created_at > ?', 1.minute.ago).count },
create_user_throttle(:dmail_day, ->{ Danbooru.config.dmail_day_limit - Dmail.sent_by_id(id).where('created_at > ?', 1.day.ago).count },
nil, 7.days)
create_user_throttle(:comment_vote, ->{ Danbooru.config.comment_vote_limit - CommentVote.for_user(id).where("created_at > ?", 1.hour.ago).count },
:general_bypass_throttle?, 3.days)

View File

@ -184,14 +184,18 @@ module Danbooru
3_000
end
def dmail_limit
20
end
def dmail_minute_limit
1
end
def dmail_limit
10
end
def dmail_day_limit
50
end
def tag_suggestion_limit
15
end