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.

97 lines
3.5 KiB

  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 = begin
  24. if TagManager.instance.local_domain?(domain)
  25. nil
  26. else
  27. TagManager.instance.normalize_domain(domain)
  28. end
  29. end
  30. mentioned_account = Account.find_remote(username, domain)
  31. # Unapproved and unconfirmed accounts should not be mentionable
  32. next match if mentioned_account&.local? && !(mentioned_account.user_confirmed? && mentioned_account.user_approved?)
  33. # If the account cannot be found or isn't the right protocol,
  34. # first try to resolve it
  35. if mention_undeliverable?(mentioned_account)
  36. begin
  37. mentioned_account = ResolveAccountService.new.call(Regexp.last_match(1))
  38. rescue Webfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::UnexpectedResponseError
  39. mentioned_account = nil
  40. end
  41. end
  42. # If after resolving it still isn't found or isn't the right
  43. # protocol, then give up
  44. next match if mention_undeliverable?(mentioned_account) || mentioned_account&.suspended?
  45. mention = @previous_mentions.find { |x| x.account_id == mentioned_account.id }
  46. mention ||= @current_mentions.find { |x| x.account_id == mentioned_account.id }
  47. mention ||= @status.mentions.new(account: mentioned_account)
  48. @current_mentions << mention
  49. "@#{mentioned_account.acct}"
  50. end
  51. @status.save! if @save_records
  52. end
  53. def assign_mentions!
  54. # Make sure we never mention blocked accounts
  55. unless @current_mentions.empty?
  56. mentioned_domains = @current_mentions.map { |m| m.account.domain }.compact.uniq
  57. blocked_domains = Set.new(mentioned_domains.empty? ? [] : AccountDomainBlock.where(account_id: @status.account_id, domain: mentioned_domains))
  58. mentioned_account_ids = @current_mentions.map(&:account_id)
  59. blocked_account_ids = Set.new(@status.account.block_relationships.where(target_account_id: mentioned_account_ids).pluck(:target_account_id))
  60. dropped_mentions, @current_mentions = @current_mentions.partition { |mention| blocked_account_ids.include?(mention.account_id) || blocked_domains.include?(mention.account.domain) }
  61. dropped_mentions.each(&:destroy)
  62. end
  63. @current_mentions.each do |mention|
  64. mention.save if mention.new_record? && @save_records
  65. end
  66. # If previous mentions are no longer contained in the text, convert them
  67. # to silent mentions, since withdrawing access from someone who already
  68. # received a notification might be more confusing
  69. removed_mentions = @previous_mentions - @current_mentions
  70. Mention.where(id: removed_mentions.map(&:id)).update_all(silent: true) unless removed_mentions.empty?
  71. end
  72. def mention_undeliverable?(mentioned_account)
  73. mentioned_account.nil? || (!mentioned_account.local? && !mentioned_account.activitypub?)
  74. end
  75. end