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.

70 lines
1.7 KiB

  1. # frozen_string_literal: true
  2. class ReportService < BaseService
  3. include Payloadable
  4. def call(source_account, target_account, options = {})
  5. @source_account = source_account
  6. @target_account = target_account
  7. @status_ids = options.delete(:status_ids).presence || []
  8. @comment = options.delete(:comment).presence || ''
  9. @category = options.delete(:category).presence || 'other'
  10. @rule_ids = options.delete(:rule_ids).presence
  11. @options = options
  12. raise ActiveRecord::RecordNotFound if @target_account.suspended?
  13. create_report!
  14. notify_staff!
  15. forward_to_origin! if forward?
  16. @report
  17. end
  18. private
  19. def create_report!
  20. @report = @source_account.reports.create!(
  21. target_account: @target_account,
  22. status_ids: reported_status_ids,
  23. comment: @comment,
  24. uri: @options[:uri],
  25. forwarded: forward?,
  26. category: @category,
  27. rule_ids: @rule_ids
  28. )
  29. end
  30. def notify_staff!
  31. return if @report.unresolved_siblings?
  32. User.staff.includes(:account).each do |u|
  33. next unless u.allows_report_emails?
  34. AdminMailer.new_report(u.account, @report).deliver_later
  35. end
  36. end
  37. def forward_to_origin!
  38. ActivityPub::DeliveryWorker.perform_async(
  39. payload,
  40. some_local_account.id,
  41. @target_account.inbox_url
  42. )
  43. end
  44. def forward?
  45. !@target_account.local? && ActiveModel::Type::Boolean.new.cast(@options[:forward])
  46. end
  47. def reported_status_ids
  48. @target_account.statuses.with_discarded.find(Array(@status_ids)).pluck(:id)
  49. end
  50. def payload
  51. Oj.dump(serialize_payload(@report, ActivityPub::FlagSerializer, account: some_local_account))
  52. end
  53. def some_local_account
  54. @some_local_account ||= Account.representative
  55. end
  56. end