1
0

report_service.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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[:rule_ids].present? ? 'violation' : (options.delete(:category).presence || 'other')
  10. @rule_ids = options.delete(:rule_ids).presence
  11. @application = options.delete(:application).presence
  12. @options = options
  13. raise ActiveRecord::RecordNotFound if @target_account.unavailable?
  14. create_report!
  15. notify_staff!
  16. if forward?
  17. forward_to_origin!
  18. forward_to_replied_to!
  19. end
  20. @report
  21. end
  22. private
  23. def create_report!
  24. @report = @source_account.reports.create!(
  25. target_account: @target_account,
  26. status_ids: reported_status_ids,
  27. comment: @comment,
  28. uri: @options[:uri],
  29. forwarded: forward_to_origin?,
  30. category: @category,
  31. rule_ids: @rule_ids,
  32. application: @application
  33. )
  34. end
  35. def notify_staff!
  36. return if @report.unresolved_siblings?
  37. User.those_who_can(:manage_reports).includes(:account).find_each do |u|
  38. LocalNotificationWorker.perform_async(u.account_id, @report.id, 'Report', 'admin.report')
  39. AdminMailer.with(recipient: u.account).new_report(@report).deliver_later if u.allows_report_emails?
  40. end
  41. end
  42. def forward_to_origin!
  43. return unless forward_to_origin?
  44. # Send report to the server where the account originates from
  45. ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, @target_account.inbox_url)
  46. end
  47. def forward_to_replied_to!
  48. # Send report to servers to which the account was replying to, so they also have a chance to act
  49. inbox_urls = Account.remote.where(domain: forward_to_domains).where(id: Status.where(id: reported_status_ids).where.not(in_reply_to_account_id: nil).select(:in_reply_to_account_id)).inboxes - [@target_account.inbox_url, @target_account.shared_inbox_url]
  50. inbox_urls.each do |inbox_url|
  51. ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
  52. end
  53. end
  54. def forward?
  55. !@target_account.local? && ActiveModel::Type::Boolean.new.cast(@options[:forward])
  56. end
  57. def forward_to_origin?
  58. forward? && forward_to_domains.include?(@target_account.domain)
  59. end
  60. def forward_to_domains
  61. @forward_to_domains ||= (@options[:forward_to_domains] || [@target_account.domain]).filter_map { |domain| TagManager.instance.normalize_domain(domain&.strip) }.uniq
  62. end
  63. def reported_status_ids
  64. return AccountStatusesFilter.new(@target_account, @source_account).results.with_discarded.find(Array(@status_ids)).pluck(:id) if @source_account.local?
  65. # If the account making reports is remote, it is likely anonymized so we have to relax the requirements for attaching statuses.
  66. domain = @source_account.domain.to_s.downcase
  67. has_followers = @target_account.followers.with_domain(domain).exists?
  68. visibility = has_followers ? %i(public unlisted private) : %i(public unlisted)
  69. scope = @target_account.statuses.with_discarded
  70. scope.merge!(scope.where(visibility: visibility).or(scope.where('EXISTS (SELECT 1 FROM mentions m JOIN accounts a ON m.account_id = a.id WHERE lower(a.domain) = ?)', domain)))
  71. # Allow missing posts to not drop reports that include e.g. a deleted post
  72. scope.where(id: Array(@status_ids)).pluck(:id)
  73. end
  74. def payload
  75. Oj.dump(serialize_payload(@report, ActivityPub::FlagSerializer, account: some_local_account))
  76. end
  77. def some_local_account
  78. @some_local_account ||= Account.representative
  79. end
  80. end