dmails: update unread dmail count when dmail is hard deleted.

When a dmail is hard deleted, update the user's unread dmail count and
delete any associated mod reports.

Dmails aren't normally hard deleted, except when there's a large spam
wave that necessitates manual intervention.
This commit is contained in:
evazion
2020-08-28 11:28:28 -05:00
parent 319a2c011f
commit 5baeb3eecc
2 changed files with 28 additions and 2 deletions

View File

@@ -6,10 +6,11 @@ class Dmail < ApplicationRecord
belongs_to :owner, :class_name => "User"
belongs_to :to, :class_name => "User"
belongs_to :from, :class_name => "User"
has_many :moderation_reports, as: :model
has_many :moderation_reports, as: :model, dependent: :destroy
before_create :autoreport_spam
after_save :update_unread_dmail_count
after_destroy :update_unread_dmail_count
after_commit :send_email, on: :create
deletable
@@ -160,7 +161,7 @@ class Dmail < ApplicationRecord
end
def update_unread_dmail_count
return unless saved_change_to_id? || saved_change_to_is_read? || saved_change_to_is_deleted?
return unless saved_change_to_id? || saved_change_to_is_read? || saved_change_to_is_deleted? || destroyed?
owner.with_lock do
unread_count = owner.dmails.active.unread.count