process_mentions_service.rb 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # frozen_string_literal: true
  2. class ProcessMentionsService < BaseService
  3. include Payloadable
  4. # Scan status for mentions and fetch remote mentioned users,
  5. # and create local mention pointers
  6. # @param [Status] status
  7. # @param [Boolean] save_records Whether to save records in database
  8. def call(status, save_records: true)
  9. @status = status
  10. @save_records = save_records
  11. return unless @status.local?
  12. @previous_mentions = @status.active_mentions.includes(:account).to_a
  13. @current_mentions = []
  14. Status.transaction do
  15. scan_text!
  16. assign_mentions!
  17. end
  18. end
  19. private
  20. def scan_text!
  21. @status.text = @status.text.gsub(Account::MENTION_RE) do |match|
  22. username, domain = Regexp.last_match(1).split('@')
  23. domain = if TagManager.instance.local_domain?(domain)
  24. nil
  25. else
  26. TagManager.instance.normalize_domain(domain)
  27. end
  28. mentioned_account = Account.find_remote(username, domain)
  29. # Unapproved and unconfirmed accounts should not be mentionable
  30. next match if mentioned_account&.local? && !(mentioned_account.user_confirmed? && mentioned_account.user_approved?)
  31. # If the account cannot be found or isn't the right protocol,
  32. # first try to resolve it
  33. if mention_undeliverable?(mentioned_account)
  34. begin
  35. mentioned_account = ResolveAccountService.new.call(Regexp.last_match(1))
  36. rescue Webfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::UnexpectedResponseError
  37. mentioned_account = nil
  38. end
  39. end
  40. # If after resolving it still isn't found or isn't the right
  41. # protocol, then give up
  42. next match if mention_undeliverable?(mentioned_account) || mentioned_account&.unavailable?
  43. mention = @previous_mentions.find { |x| x.account_id == mentioned_account.id }
  44. mention ||= @current_mentions.find { |x| x.account_id == mentioned_account.id }
  45. mention ||= @status.mentions.new(account: mentioned_account)
  46. @current_mentions << mention
  47. "@#{mentioned_account.acct}"
  48. end
  49. @status.save! if @save_records
  50. end
  51. def assign_mentions!
  52. # Make sure we never mention blocked accounts
  53. unless @current_mentions.empty?
  54. mentioned_domains = @current_mentions.filter_map { |m| m.account.domain }.uniq
  55. blocked_domains = Set.new(mentioned_domains.empty? ? [] : AccountDomainBlock.where(account_id: @status.account_id, domain: mentioned_domains))
  56. mentioned_account_ids = @current_mentions.map(&:account_id)
  57. blocked_account_ids = Set.new(@status.account.block_relationships.where(target_account_id: mentioned_account_ids).pluck(:target_account_id))
  58. dropped_mentions, @current_mentions = @current_mentions.partition { |mention| blocked_account_ids.include?(mention.account_id) || blocked_domains.include?(mention.account.domain) }
  59. dropped_mentions.each(&:destroy)
  60. end
  61. @current_mentions.each do |mention|
  62. mention.save if mention.new_record? && @save_records
  63. end
  64. # If previous mentions are no longer contained in the text, convert them
  65. # to silent mentions, since withdrawing access from someone who already
  66. # received a notification might be more confusing
  67. removed_mentions = @previous_mentions - @current_mentions
  68. Mention.where(id: removed_mentions.map(&:id)).update_all(silent: true) unless removed_mentions.empty?
  69. end
  70. def mention_undeliverable?(mentioned_account)
  71. mentioned_account.nil? || (!mentioned_account.local? && !mentioned_account.activitypub?)
  72. end
  73. end