闭社主体 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.

69 lines
2.2 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 = status.text.gsub(Account::MENTION_RE) do |match|
  11. username, domain = $1.split('@')
  12. mentioned_account = Account.find_remote(username, domain)
  13. if mention_undeliverable?(status, mentioned_account)
  14. begin
  15. mentioned_account = resolve_account_service.call($1)
  16. rescue Goldfinger::Error, HTTP::Error
  17. mentioned_account = nil
  18. end
  19. end
  20. mentioned_account ||= Account.find_remote(username, domain)
  21. next match if mention_undeliverable?(status, mentioned_account)
  22. mentioned_account.mentions.where(status: status).first_or_create(status: status)
  23. "@#{mentioned_account.acct}"
  24. end
  25. status.save!
  26. status.mentions.includes(:account).each do |mention|
  27. create_notification(status, mention)
  28. end
  29. end
  30. private
  31. def mention_undeliverable?(status, mentioned_account)
  32. mentioned_account.nil? || (!mentioned_account.local? && mentioned_account.ostatus? && status.stream_entry.hidden?)
  33. end
  34. def create_notification(status, mention)
  35. mentioned_account = mention.account
  36. if mentioned_account.local?
  37. NotifyService.new.call(mentioned_account, mention)
  38. elsif mentioned_account.ostatus? && !status.stream_entry.hidden?
  39. NotificationWorker.perform_async(stream_entry_to_xml(status.stream_entry), status.account_id, mentioned_account.id)
  40. elsif mentioned_account.activitypub?
  41. ActivityPub::DeliveryWorker.perform_async(build_json(mention.status), mention.status.account_id, mentioned_account.inbox_url)
  42. end
  43. end
  44. def build_json(status)
  45. Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
  46. status,
  47. serializer: ActivityPub::ActivitySerializer,
  48. adapter: ActivityPub::Adapter
  49. ).as_json).sign!(status.account))
  50. end
  51. def resolve_account_service
  52. ResolveAccountService.new
  53. end
  54. end