chinwagsocial/app/services/block_domain_service.rb
sdukhovni 06d905f415 Allow domain blocks that only reject media without silencing or suspending (#4325)
* Allow domain blocks to reject media without silencing or suspending

* Fix typo

* Hide 'Reject media' button when superfluous, instead of disabling it

* Properly hide 'reject media' checkbox on page load if needed

This may happen when resubmitting the domain block form after an error.

* Don't ask whether undoing a media-only block should be retroactive

* Rename :media_only block to :noop

* Display :noop block as None in frontend, allow blocks that do nothing

* Remove 'coding' line auto-added by emacs
2017-07-24 14:26:55 +02:00

65 lines
1.4 KiB
Ruby

# frozen_string_literal: true
class BlockDomainService < BaseService
attr_reader :domain_block
def call(domain_block)
@domain_block = domain_block
process_domain_block
end
private
def process_domain_block
clear_media! if domain_block.reject_media?
if domain_block.silence?
silence_accounts!
elsif domain_block.suspend?
suspend_accounts!
end
end
def silence_accounts!
blocked_domain_accounts.in_batches.update_all(silenced: true)
end
def clear_media!
clear_account_images
clear_account_attachments
end
def suspend_accounts!
blocked_domain_accounts.where(suspended: false).find_each do |account|
account.subscription(api_subscription_url(account.id)).unsubscribe if account.subscribed?
SuspendAccountService.new.call(account)
end
end
def clear_account_images
blocked_domain_accounts.find_each do |account|
account.avatar.destroy
account.header.destroy
account.save
end
end
def clear_account_attachments
media_from_blocked_domain.find_each do |attachment|
attachment.file.destroy
attachment.type = :unknown
attachment.save
end
end
def blocked_domain
domain_block.domain
end
def blocked_domain_accounts
Account.where(domain: blocked_domain)
end
def media_from_blocked_domain
MediaAttachment.joins(:account).merge(blocked_domain_accounts).reorder(nil)
end
end