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.

48 lines
1.4 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. text = [status.text, status.spoiler_text].reject(&:blank?).join(' ')
  11. text.scan(Account::MENTION_RE).each do |match|
  12. username, domain = match.first.split('@')
  13. mentioned_account = Account.find_remote(username, domain)
  14. if mentioned_account.nil? && !domain.nil?
  15. begin
  16. mentioned_account = follow_remote_account_service.call(match.first.to_s)
  17. rescue Goldfinger::Error, HTTP::Error
  18. mentioned_account = nil
  19. end
  20. end
  21. next if mentioned_account.nil?
  22. mentioned_account.mentions.where(status: status).first_or_create(status: status)
  23. end
  24. status.mentions.includes(:account).each do |mention|
  25. mentioned_account = mention.account
  26. if mentioned_account.local?
  27. NotifyService.new.call(mentioned_account, mention)
  28. else
  29. NotificationWorker.perform_async(stream_entry_to_xml(status.stream_entry), status.account_id, mentioned_account.id)
  30. end
  31. end
  32. end
  33. private
  34. def follow_remote_account_service
  35. @follow_remote_account_service ||= FollowRemoteAccountService.new
  36. end
  37. end