1
0

account_batch.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 'remove_domains_from_followers'
  18. remove_domains_from_followers!
  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. error = nil
  34. accounts.each do |target_account|
  35. FollowService.new.call(current_account, target_account)
  36. rescue Mastodon::NotPermittedError, ActiveRecord::RecordNotFound => e
  37. error ||= e
  38. end
  39. raise error if error.present?
  40. end
  41. def unfollow!
  42. accounts.each do |target_account|
  43. UnfollowService.new.call(current_account, target_account)
  44. end
  45. end
  46. def remove_from_followers!
  47. RemoveFromFollowersService.new.call(current_account, account_ids)
  48. end
  49. def remove_domains_from_followers!
  50. RemoveDomainsFromFollowersService.new.call(current_account, account_domains)
  51. end
  52. def account_domains
  53. accounts.group(:domain).pluck(:domain).compact
  54. end
  55. def accounts
  56. if select_all_matching?
  57. query
  58. else
  59. Account.where(id: account_ids)
  60. end
  61. end
  62. def approve!
  63. accounts.includes(:user).find_each do |account|
  64. approve_account(account)
  65. end
  66. end
  67. def reject!
  68. accounts.includes(:user).find_each do |account|
  69. reject_account(account)
  70. end
  71. end
  72. def suspend!
  73. accounts.find_each do |account|
  74. if account.user_pending?
  75. reject_account(account)
  76. else
  77. suspend_account(account)
  78. end
  79. end
  80. end
  81. def suppress_follow_recommendation!
  82. authorize(:follow_recommendation, :suppress?)
  83. accounts.find_each do |account|
  84. FollowRecommendationSuppression.create(account: account)
  85. end
  86. end
  87. def unsuppress_follow_recommendation!
  88. authorize(:follow_recommendation, :unsuppress?)
  89. FollowRecommendationSuppression.where(account_id: account_ids).destroy_all
  90. end
  91. def reject_account(account)
  92. authorize(account.user, :reject?)
  93. log_action(:reject, account.user)
  94. account.suspend!(origin: :local)
  95. AccountDeletionWorker.perform_async(account.id, { 'reserve_username' => false })
  96. end
  97. def suspend_account(account)
  98. authorize(account, :suspend?)
  99. log_action(:suspend, account)
  100. account.suspend!(origin: :local)
  101. account.strikes.create!(
  102. account: current_account,
  103. action: :suspend
  104. )
  105. Admin::SuspensionWorker.perform_async(account.id)
  106. # Suspending a single account closes their associated reports, so
  107. # mass-suspending would be consistent.
  108. Report.where(target_account: account).unresolved.find_each do |report|
  109. authorize(report, :update?)
  110. log_action(:resolve, report)
  111. report.resolve!(current_account)
  112. rescue Mastodon::NotPermittedError
  113. # This should not happen, but just in case, do not fail early
  114. end
  115. end
  116. def approve_account(account)
  117. authorize(account.user, :approve?)
  118. log_action(:approve, account.user)
  119. account.user.approve!
  120. end
  121. def select_all_matching?
  122. select_all_matching == '1'
  123. end
  124. end