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.

138 lines
3.1 KiB

  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. end
  107. def approve_account(account)
  108. authorize(account.user, :approve?)
  109. log_action(:approve, account.user)
  110. account.user.approve!
  111. end
  112. def select_all_matching?
  113. select_all_matching == '1'
  114. end
  115. end