status_batch.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # frozen_string_literal: true
  2. class Trends::StatusBatch
  3. include ActiveModel::Model
  4. include Authorization
  5. attr_accessor :status_ids, :action, :current_account
  6. def save
  7. case action
  8. when 'approve'
  9. approve!
  10. when 'approve_accounts'
  11. approve_accounts!
  12. when 'reject'
  13. reject!
  14. when 'reject_accounts'
  15. reject_accounts!
  16. end
  17. end
  18. private
  19. def statuses
  20. @statuses ||= Status.where(id: status_ids)
  21. end
  22. def status_accounts
  23. @status_accounts ||= Account.where(id: statuses.map(&:account_id).uniq)
  24. end
  25. def approve!
  26. statuses.each { |status| authorize([:admin, status], :review?) }
  27. statuses.update_all(trendable: true)
  28. end
  29. def approve_accounts!
  30. status_accounts.each do |account|
  31. authorize(account, :review?)
  32. account.update(trendable: true, reviewed_at: action_time)
  33. end
  34. # Reset any individual overrides
  35. statuses.update_all(trendable: nil)
  36. end
  37. def reject!
  38. statuses.each { |status| authorize([:admin, status], :review?) }
  39. statuses.update_all(trendable: false)
  40. end
  41. def reject_accounts!
  42. status_accounts.each do |account|
  43. authorize(account, :review?)
  44. account.update(trendable: false, reviewed_at: action_time)
  45. end
  46. # Reset any individual overrides
  47. statuses.update_all(trendable: nil)
  48. end
  49. def action_time
  50. @action_time ||= Time.now.utc
  51. end
  52. end