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