闭社主体 forked from https://github.com/tootsuite/mastodon
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.

57 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. )
  21. end
  22. def notify_staff!
  23. return if @report.unresolved_siblings?
  24. User.staff.includes(:account).each do |u|
  25. next unless u.allows_report_emails?
  26. AdminMailer.new_report(u.account, @report).deliver_later
  27. end
  28. end
  29. def forward_to_origin!
  30. ActivityPub::DeliveryWorker.perform_async(
  31. payload,
  32. some_local_account.id,
  33. @target_account.inbox_url
  34. )
  35. end
  36. def payload
  37. Oj.dump(ActiveModelSerializers::SerializableResource.new(
  38. @report,
  39. serializer: ActivityPub::FlagSerializer,
  40. adapter: ActivityPub::Adapter,
  41. account: some_local_account
  42. ).as_json)
  43. end
  44. def some_local_account
  45. @some_local_account ||= Account.local.where(suspended: false).first
  46. end
  47. end