From bfabd6a2b882c4eca186e8fe47ce98a47f74662f Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 7 Oct 2024 08:02:04 -0400 Subject: [PATCH] Move account suspension-related methods to concern (#28351) --- app/models/account.rb | 38 +------------------ app/models/concerns/account/suspensions.rb | 44 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 37 deletions(-) create mode 100644 app/models/concerns/account/suspensions.rb diff --git a/app/models/account.rb b/app/models/account.rb index 3b841e7c77..708415b6ee 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -89,6 +89,7 @@ class Account < ApplicationRecord include Account::Merging include Account::Search include Account::StatusesSearch + include Account::Suspensions include Account::AttributionDomains include DomainMaterializable include DomainNormalizable @@ -127,9 +128,7 @@ class Account < ApplicationRecord scope :local, -> { where(domain: nil) } scope :partitioned, -> { order(Arel.sql('row_number() over (partition by domain)')) } scope :silenced, -> { where.not(silenced_at: nil) } - scope :suspended, -> { where.not(suspended_at: nil) } scope :sensitized, -> { where.not(sensitized_at: nil) } - scope :without_suspended, -> { where(suspended_at: nil) } scope :without_silenced, -> { where(silenced_at: nil) } scope :without_instance_actor, -> { where.not(id: INSTANCE_ACTOR_ID) } scope :recent, -> { reorder(id: :desc) } @@ -255,41 +254,6 @@ class Account < ApplicationRecord update!(silenced_at: nil) end - def suspended? - suspended_at.present? && !instance_actor? - end - - def suspended_locally? - suspended? && suspension_origin_local? - end - - def suspended_permanently? - suspended? && deletion_request.nil? - end - - def suspended_temporarily? - suspended? && deletion_request.present? - end - - alias unavailable? suspended? - alias permanently_unavailable? suspended_permanently? - - def suspend!(date: Time.now.utc, origin: :local, block_email: true) - transaction do - create_deletion_request! - update!(suspended_at: date, suspension_origin: origin) - create_canonical_email_block! if block_email - end - end - - def unsuspend! - transaction do - deletion_request&.destroy! - update!(suspended_at: nil, suspension_origin: nil) - destroy_canonical_email_block! - end - end - def sensitized? sensitized_at.present? end diff --git a/app/models/concerns/account/suspensions.rb b/app/models/concerns/account/suspensions.rb new file mode 100644 index 0000000000..c981fb5a29 --- /dev/null +++ b/app/models/concerns/account/suspensions.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +module Account::Suspensions + extend ActiveSupport::Concern + + included do + scope :suspended, -> { where.not(suspended_at: nil) } + scope :without_suspended, -> { where(suspended_at: nil) } + end + + def suspended? + suspended_at.present? && !instance_actor? + end + alias unavailable? suspended? + + def suspended_locally? + suspended? && suspension_origin_local? + end + + def suspended_permanently? + suspended? && deletion_request.nil? + end + alias permanently_unavailable? suspended_permanently? + + def suspended_temporarily? + suspended? && deletion_request.present? + end + + def suspend!(date: Time.now.utc, origin: :local, block_email: true) + transaction do + create_deletion_request! + update!(suspended_at: date, suspension_origin: origin) + create_canonical_email_block! if block_email + end + end + + def unsuspend! + transaction do + deletion_request&.destroy! + update!(suspended_at: nil, suspension_origin: nil) + destroy_canonical_email_block! + end + end +end