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.

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