diff --git a/app/models/user_history.rb b/app/models/user_history.rb index 0c18ccdcd8..d7a99bdbc9 100644 --- a/app/models/user_history.rb +++ b/app/models/user_history.rb @@ -53,7 +53,8 @@ class UserHistory < ActiveRecord::Base revoke_moderation: 35, backup_operation: 36, rate_limited_like: 37, # not used anymore - revoke_email: 38 + revoke_email: 38, + deactivate_user: 39, ) end @@ -88,7 +89,8 @@ class UserHistory < ActiveRecord::Base :grant_moderation, :revoke_moderation, :backup_operation, - :revoke_email] + :revoke_email, + :deactivate_user] end def self.staff_action_ids diff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rb index 8623602b3a..3beaf9bcae 100644 --- a/app/services/staff_action_logger.rb +++ b/app/services/staff_action_logger.rb @@ -336,11 +336,20 @@ class StaffActionLogger })) end - def log_revoke_email(user, opts={}) + def log_revoke_email(user, reason, opts={}) UserHistory.create(params(opts).merge({ action: UserHistory.actions[:revoke_email], target_user_id: user.id, - details: "Won't be sending emails to '#{user.email}' until #{user.user_stat.reset_bounce_score_after}" + details: reason + })) + end + + def log_user_deactivate(user, reason, opts={}) + raise Discourse::InvalidParameters.new(:user) unless user + UserHistory.create(params(opts).merge({ + action: UserHistory.actions[:deactivate_user], + target_user_id: user.id, + details: reason })) end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index de168d25db..daac10ed10 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1206,12 +1206,13 @@ en: maximum_staged_users_per_email: "Maximum number of staged users created when processing an incoming email." auto_generated_whitelist: "List of email addresses that won't be checked for auto-generated content." block_auto_generated_emails: "Block incoming emails identified as being auto generated." - bounce_score_threshold: "Max score before we will stop emailing a user. Soft bounce adds 1, hard bounce adds 2, score reset 30 days after last bounce." ignore_by_title: "Ignore incoming emails based on their title." mailgun_api_key: "Mailgun Secret API key used to verify webhook messages." soft_bounce_score: "Score added to the user when a temporary bounce happens." hard_bounce_score: "Score added to the user when a permanent bounce happens." + bounce_score_threshold: "Max score before we will stop emailing a user." + bounce_score_threshold_deactivate: "Max score before we will deactive a user." reset_bounce_score_after_days: "Automatically reset bounce score after X days." manual_polling_enabled: "Push emails using the API for email replies." @@ -1496,6 +1497,7 @@ en: user: no_accounts_associated: "No accounts associated" + deactivated: "Was deactivated due to too many bounced emails to '%{email}'." username: short: "must be at least %{min} characters" long: "must be no more than %{max} characters" @@ -1509,6 +1511,7 @@ en: email: not_allowed: "is not allowed from that email provider. Please use another email address." blocked: "is not allowed." + revoked: "Won't be sending emails to '%{email}' until %{date}." ip_address: blocked: "New registrations are not allowed from your IP address." max_new_accounts_per_registration_ip: "New registrations are not allowed from your IP address (maximum limit reached). Contact a staff member." diff --git a/config/site_settings.yml b/config/site_settings.yml index 26cd4c5af8..6f41f3c425 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -612,18 +612,23 @@ email: default: '' type: list block_auto_generated_emails: true - bounce_score_threshold: - client: true - default: 4 - min: 1 ignore_by_title: type: list default: '' mailgun_api_key: default: '' regex: '^key-\h{32}$' - soft_bounce_score: 1 - hard_bounce_score: 2 + bounce_score_threshold: + client: true + default: 4 + min: 1 + bounce_score_threshold_deactivate: 30 + soft_bounce_score: + default: 1 + min: 1 + hard_bounce_score: + default: 2 + min: 2 reset_bounce_score_after_days: 30 diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 55fffe1b2f..df1aacf213 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -173,8 +173,16 @@ module Email user.user_stat.reset_bounce_score_after = SiteSetting.reset_bounce_score_after_days.days.from_now user.user_stat.save - if user.user_stat.bounce_score >= SiteSetting.bounce_score_threshold - StaffActionLogger.new(Discourse.system_user).log_revoke_email(user) + bounce_score = user.user_stat.bounce_score + if user.active && bounce_score >= SiteSetting.bounce_score_threshold_deactivate + user.update_columns(active: false) + reason = I18n.t("user.deactivated", email: user.email) + StaffActionLogger.new(Discourse.system_user).log_user_deactivate(user, reason) + elsif bounce_score >= SiteSetting.bounce_score_threshold + # NOTE: we check bounce_score before sending emails, nothing to do + # here other than log it happened. + reason = I18n.t("user.email.revoked", email: user.email, date: user.user_stat.reset_bounce_score_after) + StaffActionLogger.new(Discourse.system_user).log_revoke_email(user, reason) end end