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

46 lines
1.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.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. mentioned_account = mention.account
  25. if mentioned_account.local?
  26. NotifyService.new.call(mentioned_account, mention)
  27. else
  28. NotificationWorker.perform_async(stream_entry_to_xml(status.stream_entry), status.account_id, mentioned_account.id)
  29. end
  30. end
  31. end
  32. private
  33. def follow_remote_account_service
  34. @follow_remote_account_service ||= FollowRemoteAccountService.new
  35. end
  36. end