1
0

report_service.rb 3.6 KB

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