You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.8 KiB

  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 'follow'
  10. follow!
  11. when 'unfollow'
  12. unfollow!
  13. when 'remove_from_followers'
  14. remove_from_followers!
  15. when 'block_domains'
  16. block_domains!
  17. when 'approve'
  18. approve!
  19. when 'reject'
  20. reject!
  21. end
  22. end
  23. private
  24. def follow!
  25. accounts.find_each do |target_account|
  26. FollowService.new.call(current_account, target_account)
  27. end
  28. end
  29. def unfollow!
  30. accounts.find_each do |target_account|
  31. UnfollowService.new.call(current_account, target_account)
  32. end
  33. end
  34. def remove_from_followers!
  35. current_account.passive_relationships.where(account_id: account_ids).find_each do |follow|
  36. reject_follow!(follow)
  37. end
  38. end
  39. def block_domains!
  40. AfterAccountDomainBlockWorker.push_bulk(account_domains) do |domain|
  41. [current_account.id, domain]
  42. end
  43. end
  44. def account_domains
  45. accounts.pluck(Arel.sql('distinct domain')).compact
  46. end
  47. def accounts
  48. Account.where(id: account_ids)
  49. end
  50. def reject_follow!(follow)
  51. follow.destroy
  52. return unless follow.account.activitypub?
  53. ActivityPub::DeliveryWorker.perform_async(Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer)), current_account.id, follow.account.inbox_url)
  54. end
  55. def approve!
  56. users = accounts.includes(:user).map(&:user)
  57. users.each { |user| authorize(user, :approve?) }
  58. .each(&:approve!)
  59. end
  60. def reject!
  61. records = accounts.includes(:user)
  62. records.each { |account| authorize(account.user, :reject?) }
  63. .each { |account| DeleteAccountService.new.call(account, reserve_email: false, reserve_username: false) }
  64. end
  65. end