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.

56 lines
1.4 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) || []
  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. forwarded: ActiveModel::Type::Boolean.new.cast(@options[:forward])
  23. )
  24. end
  25. def notify_staff!
  26. return if @report.unresolved_siblings?
  27. User.staff.includes(:account).each do |u|
  28. next unless u.allows_report_emails?
  29. AdminMailer.new_report(u.account, @report).deliver_later
  30. end
  31. end
  32. def forward_to_origin!
  33. ActivityPub::DeliveryWorker.perform_async(
  34. payload,
  35. some_local_account.id,
  36. @target_account.inbox_url
  37. )
  38. end
  39. def payload
  40. Oj.dump(serialize_payload(@report, ActivityPub::FlagSerializer, account: some_local_account))
  41. end
  42. def some_local_account
  43. @some_local_account ||= Account.representative
  44. end
  45. end