account_batch.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # frozen_string_literal: true
  2. class Form::AccountBatch
  3. include ActiveModel::Model
  4. include Authorization
  5. include Payloadable
  6. attr_accessor :account_ids, :action, :current_account
  7. def save
  8. case action
  9. when 'unfollow'
  10. unfollow!
  11. when 'remove_from_followers'
  12. remove_from_followers!
  13. when 'block_domains'
  14. block_domains!
  15. when 'approve'
  16. approve!
  17. when 'reject'
  18. reject!
  19. end
  20. end
  21. private
  22. def unfollow!
  23. accounts.find_each do |target_account|
  24. UnfollowService.new.call(current_account, target_account)
  25. end
  26. end
  27. def remove_from_followers!
  28. current_account.passive_relationships.where(account_id: account_ids).find_each do |follow|
  29. reject_follow!(follow)
  30. end
  31. end
  32. def block_domains!
  33. AfterAccountDomainBlockWorker.push_bulk(account_domains) do |domain|
  34. [current_account.id, domain]
  35. end
  36. end
  37. def account_domains
  38. accounts.pluck(Arel.sql('distinct domain')).compact
  39. end
  40. def accounts
  41. Account.where(id: account_ids)
  42. end
  43. def reject_follow!(follow)
  44. follow.destroy
  45. return unless follow.account.activitypub?
  46. ActivityPub::DeliveryWorker.perform_async(Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer)), current_account.id, follow.account.inbox_url)
  47. end
  48. def approve!
  49. users = accounts.includes(:user).map(&:user)
  50. users.each { |user| authorize(user, :approve?) }
  51. .each(&:approve!)
  52. end
  53. def reject!
  54. records = accounts.includes(:user)
  55. records.each { |account| authorize(account.user, :reject?) }
  56. .each { |account| SuspendAccountService.new.call(account, including_user: true, destroy: true, skip_distribution: true) }
  57. end
  58. end