approve_appeal_service.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # frozen_string_literal: true
  2. class ApproveAppealService < BaseService
  3. def call(appeal, current_account)
  4. @appeal = appeal
  5. @strike = appeal.strike
  6. @current_account = current_account
  7. ApplicationRecord.transaction do
  8. undo_strike_action!
  9. mark_strike_as_appealed!
  10. end
  11. queue_workers!
  12. notify_target_account!
  13. end
  14. private
  15. def target_account
  16. @strike.target_account
  17. end
  18. def undo_strike_action!
  19. case @strike.action
  20. when 'disable'
  21. undo_disable!
  22. when 'delete_statuses'
  23. undo_delete_statuses!
  24. when 'mark_statuses_as_sensitive'
  25. undo_mark_statuses_as_sensitive!
  26. when 'sensitive'
  27. undo_sensitive!
  28. when 'silence'
  29. undo_silence!
  30. when 'suspend'
  31. undo_suspend!
  32. end
  33. end
  34. def mark_strike_as_appealed!
  35. @appeal.approve!(@current_account)
  36. @strike.touch(:overruled_at)
  37. end
  38. def undo_disable!
  39. target_account.user.enable!
  40. end
  41. def undo_delete_statuses!
  42. # Cannot be undone
  43. end
  44. def undo_mark_statuses_as_sensitive!
  45. representative_account = Account.representative
  46. @strike.statuses.includes(:media_attachments).find_each do |status|
  47. UpdateStatusService.new.call(status, representative_account.id, sensitive: false) if status.with_media?
  48. end
  49. end
  50. def undo_sensitive!
  51. target_account.unsensitize!
  52. end
  53. def undo_silence!
  54. target_account.unsilence!
  55. end
  56. def undo_suspend!
  57. target_account.unsuspend!
  58. end
  59. def queue_workers!
  60. case @strike.action
  61. when 'suspend'
  62. Admin::UnsuspensionWorker.perform_async(target_account.id)
  63. end
  64. end
  65. def notify_target_account!
  66. UserMailer.appeal_approved(target_account.user, @appeal).deliver_later
  67. end
  68. end