account_batch.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # frozen_string_literal: true
  2. class Form::AccountBatch
  3. include ActiveModel::Model
  4. include Authorization
  5. include AccountableConcern
  6. include Payloadable
  7. attr_accessor :account_ids, :action, :current_account,
  8. :select_all_matching, :query
  9. def save
  10. case action
  11. when 'follow'
  12. follow!
  13. when 'unfollow'
  14. unfollow!
  15. when 'remove_from_followers'
  16. remove_from_followers!
  17. when 'block_domains'
  18. block_domains!
  19. when 'approve'
  20. approve!
  21. when 'reject'
  22. reject!
  23. when 'suppress_follow_recommendation'
  24. suppress_follow_recommendation!
  25. when 'unsuppress_follow_recommendation'
  26. unsuppress_follow_recommendation!
  27. when 'suspend'
  28. suspend!
  29. end
  30. end
  31. private
  32. def follow!
  33. accounts.each do |target_account|
  34. FollowService.new.call(current_account, target_account)
  35. end
  36. end
  37. def unfollow!
  38. accounts.each do |target_account|
  39. UnfollowService.new.call(current_account, target_account)
  40. end
  41. end
  42. def remove_from_followers!
  43. RemoveFromFollowersService.new.call(current_account, account_ids)
  44. end
  45. def block_domains!
  46. AfterAccountDomainBlockWorker.push_bulk(account_domains) do |domain|
  47. [current_account.id, domain]
  48. end
  49. end
  50. def account_domains
  51. accounts.group(:domain).pluck(:domain).compact
  52. end
  53. def accounts
  54. if select_all_matching?
  55. query
  56. else
  57. Account.where(id: account_ids)
  58. end
  59. end
  60. def approve!
  61. accounts.includes(:user).find_each do |account|
  62. approve_account(account)
  63. end
  64. end
  65. def reject!
  66. accounts.includes(:user).find_each do |account|
  67. reject_account(account)
  68. end
  69. end
  70. def suspend!
  71. accounts.find_each do |account|
  72. if account.user_pending?
  73. reject_account(account)
  74. else
  75. suspend_account(account)
  76. end
  77. end
  78. end
  79. def suppress_follow_recommendation!
  80. authorize(:follow_recommendation, :suppress?)
  81. accounts.find_each do |account|
  82. FollowRecommendationSuppression.create(account: account)
  83. end
  84. end
  85. def unsuppress_follow_recommendation!
  86. authorize(:follow_recommendation, :unsuppress?)
  87. FollowRecommendationSuppression.where(account_id: account_ids).destroy_all
  88. end
  89. def reject_account(account)
  90. authorize(account.user, :reject?)
  91. log_action(:reject, account.user)
  92. account.suspend!(origin: :local)
  93. AccountDeletionWorker.perform_async(account.id, { 'reserve_username' => false })
  94. end
  95. def suspend_account(account)
  96. authorize(account, :suspend?)
  97. log_action(:suspend, account)
  98. account.suspend!(origin: :local)
  99. account.strikes.create!(
  100. account: current_account,
  101. action: :suspend
  102. )
  103. Admin::SuspensionWorker.perform_async(account.id)
  104. end
  105. def approve_account(account)
  106. authorize(account.user, :approve?)
  107. log_action(:approve, account.user)
  108. account.user.approve!
  109. end
  110. def select_all_matching?
  111. select_all_matching == '1'
  112. end
  113. end