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.

58 lines
1.4 KiB

  1. # frozen_string_literal: true
  2. class ReportService < BaseService
  3. def call(source_account, target_account, options = {})
  4. @source_account = source_account
  5. @target_account = target_account
  6. @status_ids = options.delete(:status_ids) || []
  7. @comment = options.delete(:comment) || ''
  8. @options = options
  9. create_report!
  10. notify_staff!
  11. forward_to_origin! if !@target_account.local? && ActiveModel::Type::Boolean.new.cast(@options[:forward])
  12. @report
  13. end
  14. private
  15. def create_report!
  16. @report = @source_account.reports.create!(
  17. target_account: @target_account,
  18. status_ids: @status_ids,
  19. comment: @comment,
  20. uri: @options[:uri]
  21. )
  22. end
  23. def notify_staff!
  24. return if @report.unresolved_siblings?
  25. User.staff.includes(:account).each do |u|
  26. next unless u.allows_report_emails?
  27. AdminMailer.new_report(u.account, @report).deliver_later
  28. end
  29. end
  30. def forward_to_origin!
  31. ActivityPub::DeliveryWorker.perform_async(
  32. payload,
  33. some_local_account.id,
  34. @target_account.inbox_url
  35. )
  36. end
  37. def payload
  38. Oj.dump(ActiveModelSerializers::SerializableResource.new(
  39. @report,
  40. serializer: ActivityPub::FlagSerializer,
  41. adapter: ActivityPub::Adapter,
  42. account: some_local_account
  43. ).as_json)
  44. end
  45. def some_local_account
  46. @some_local_account ||= Account.representative
  47. end
  48. end