The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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