report_service.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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) || []
  8. @comment = options.delete(:comment) || ''
  9. @options = options
  10. create_report!
  11. notify_staff!
  12. forward_to_origin! if !@target_account.local? && ActiveModel::Type::Boolean.new.cast(@options[:forward])
  13. @report
  14. end
  15. private
  16. def create_report!
  17. @report = @source_account.reports.create!(
  18. target_account: @target_account,
  19. status_ids: @status_ids,
  20. comment: @comment,
  21. uri: @options[:uri]
  22. )
  23. end
  24. def notify_staff!
  25. return if @report.unresolved_siblings?
  26. User.staff.includes(:account).each do |u|
  27. next unless u.allows_report_emails?
  28. AdminMailer.new_report(u.account, @report).deliver_later
  29. end
  30. end
  31. def forward_to_origin!
  32. ActivityPub::DeliveryWorker.perform_async(
  33. payload,
  34. some_local_account.id,
  35. @target_account.inbox_url
  36. )
  37. end
  38. def payload
  39. Oj.dump(serialize_payload(@report, ActivityPub::FlagSerializer, account: some_local_account))
  40. end
  41. def some_local_account
  42. @some_local_account ||= Account.representative
  43. end
  44. end