The code powering m.abunchtell.com https://m.abunchtell.com
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

56 lines
1.7 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. begin
  12. mentioned_account = resolve_remote_account_service.call(match.first.to_s)
  13. rescue Goldfinger::Error, HTTP::Error
  14. mentioned_account = nil
  15. end
  16. next if mentioned_account.nil?
  17. mentioned_account.mentions.where(status: status).first_or_create(status: status)
  18. end
  19. status.mentions.includes(:account).each do |mention|
  20. create_notification(status, mention)
  21. end
  22. end
  23. private
  24. def create_notification(status, mention)
  25. mentioned_account = mention.account
  26. if mentioned_account.local?
  27. NotifyService.new.call(mentioned_account, mention)
  28. elsif mentioned_account.ostatus? && !status.stream_entry.hidden?
  29. NotificationWorker.perform_async(stream_entry_to_xml(status.stream_entry), status.account_id, mentioned_account.id)
  30. elsif mentioned_account.activitypub?
  31. ActivityPub::DeliveryWorker.perform_async(build_json(mention.status), mention.status.account_id, mentioned_account.inbox_url)
  32. end
  33. end
  34. def build_json(status)
  35. Oj.dump(ActivityPub::LinkedDataSignature.new(ActiveModelSerializers::SerializableResource.new(
  36. status,
  37. serializer: ActivityPub::ActivitySerializer,
  38. adapter: ActivityPub::Adapter
  39. ).as_json).sign!(status.account))
  40. end
  41. def resolve_remote_account_service
  42. ResolveRemoteAccountService.new
  43. end
  44. end