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.

60 lines
2.0 KiB

  1. # frozen_string_literal: true
  2. class ProcessMentionsService < BaseService
  3. include StreamEntryRenderer
  4. # Scan status for mentions and fetch remote mentioned users, create
  5. # local mention pointers, send Salmon notifications to mentioned
  6. # remote users
  7. # @param [Status] status
  8. def call(status)
  9. return unless status.local?
  10. status.text.scan(Account::MENTION_RE).each do |match|
  11. username, domain = match.first.split('@')
  12. mentioned_account = Account.find_remote(username, domain)
  13. if mentioned_account.nil? && !domain.nil?
  14. begin
  15. mentioned_account = follow_remote_account_service.call(match.first.to_s)
  16. rescue Goldfinger::Error, HTTP::Error
  17. mentioned_account = nil
  18. end
  19. end
  20. next if mentioned_account.nil?
  21. mentioned_account.mentions.where(status: status).first_or_create(status: status)
  22. end
  23. status.mentions.includes(:account).each do |mention|
  24. create_notification(status, mention)
  25. end
  26. end
  27. private
  28. def create_notification(status, mention)
  29. mentioned_account = mention.account
  30. if mentioned_account.local?
  31. NotifyService.new.call(mentioned_account, mention)
  32. elsif mentioned_account.ostatus? && (Rails.configuration.x.use_ostatus_privacy || !status.stream_entry.hidden?)
  33. NotificationWorker.perform_async(stream_entry_to_xml(status.stream_entry), status.account_id, mentioned_account.id)
  34. elsif mentioned_account.activitypub?
  35. ActivityPub::DeliveryWorker.perform_async(build_json(mention.status), mention.status.account_id, mentioned_account.inbox_url)
  36. end
  37. end
  38. def build_json(status)
  39. Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
  40. status,
  41. serializer: ActivityPub::ActivitySerializer,
  42. adapter: ActivityPub::Adapter
  43. ).as_json).sign!(status.account))
  44. end
  45. def follow_remote_account_service
  46. @follow_remote_account_service ||= ResolveRemoteAccountService.new
  47. end
  48. end