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.

78 lines
2.3 KiB

  1. # frozen_string_literal: true
  2. class ProcessMentionsService < BaseService
  3. include Payloadable
  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 = status
  11. mentions = []
  12. status.text = status.text.gsub(Account::MENTION_RE) do |match|
  13. username, domain = Regexp.last_match(1).split('@')
  14. domain = begin
  15. if TagManager.instance.local_domain?(domain)
  16. nil
  17. else
  18. TagManager.instance.normalize_domain(domain)
  19. end
  20. end
  21. mentioned_account = Account.find_remote(username, domain)
  22. if mention_undeliverable?(mentioned_account)
  23. begin
  24. mentioned_account = resolve_account_service.call(Regexp.last_match(1))
  25. rescue Goldfinger::Error, HTTP::Error, OpenSSL::SSL::SSLError, Mastodon::UnexpectedResponseError
  26. mentioned_account = nil
  27. end
  28. end
  29. next match if mention_undeliverable?(mentioned_account) || mentioned_account&.suspended?
  30. mentions << mentioned_account.mentions.where(status: status).first_or_create(status: status)
  31. "@#{mentioned_account.acct}"
  32. end
  33. status.save!
  34. check_for_spam(status)
  35. mentions.each { |mention| create_notification(mention) }
  36. end
  37. private
  38. def mention_undeliverable?(mentioned_account)
  39. mentioned_account.nil? || (!mentioned_account.local? && mentioned_account.ostatus?)
  40. end
  41. def create_notification(mention)
  42. mentioned_account = mention.account
  43. if mentioned_account.local?
  44. LocalNotificationWorker.perform_async(mentioned_account.id, mention.id, mention.class.name)
  45. elsif mentioned_account.activitypub?
  46. ActivityPub::DeliveryWorker.perform_async(activitypub_json, mention.status.account_id, mentioned_account.inbox_url)
  47. end
  48. end
  49. def activitypub_json
  50. return @activitypub_json if defined?(@activitypub_json)
  51. @activitypub_json = Oj.dump(serialize_payload(ActivityPub::ActivityPresenter.from_status(@status), ActivityPub::ActivitySerializer, signer: @status.account))
  52. end
  53. def resolve_account_service
  54. ResolveAccountService.new
  55. end
  56. def check_for_spam(status)
  57. SpamCheck.perform(status)
  58. end
  59. end