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.

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