Skip to content

Commit

Permalink
Add additional limits on sending messages
Browse files Browse the repository at this point in the history
Additional limits apply to new accounts and accounts with
unresolved issues reported against them.

Fixes #3135
  • Loading branch information
tomhughes committed Mar 31, 2021
1 parent 41f9546 commit 25510b6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/controllers/messages_controller.rb
Expand Up @@ -26,7 +26,7 @@ def create
@message.sender = current_user
@message.sent_on = Time.now.getutc

if current_user.sent_messages.where("sent_on >= ?", Time.now.getutc - 1.hour).count >= Settings.max_messages_per_hour
if current_user.sent_messages.where("sent_on >= ?", Time.now.getutc - 1.hour).count >= current_user.max_messages_per_hour
flash.now[:error] = t ".limit_exceeded"
render :action => "new"
elsif @message.save
Expand Down
2 changes: 1 addition & 1 deletion app/models/issue.rb
Expand Up @@ -46,7 +46,7 @@ class Issue < ApplicationRecord

before_validation :set_reported_user

scope :with_status, ->(issue_status) { where(:status => statuses[issue_status]) }
scope :with_status, ->(issue_status) { where(:status => issue_status) }
scope :visible_to, ->(user) { where(:assigned_role => user.roles.map(&:role)) }

def read_reports
Expand Down
9 changes: 9 additions & 0 deletions app/models/user.rb
Expand Up @@ -296,6 +296,15 @@ def fingerprint
digest.hexdigest
end

def max_messages_per_hour
account_age_in_seconds = Time.now.utc - creation_time
account_age_in_hours = account_age_in_seconds / 3600
recent_messages = messages.where("sent_on >= ?", Time.now.utc - 3600).count
active_reports = issues.with_status(:open).sum(:reports_count)
max_messages = account_age_in_hours.ceil + recent_messages - active_reports * 10
max_messages.clamp(0, Settings.max_messages_per_hour)
end

private

def set_defaults
Expand Down

0 comments on commit 25510b6

Please sign in to comment.