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.

103 lines
3.0 KiB

8 years ago
8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. require_relative '../models/account'
  3. class ResolveAccountService < BaseService
  4. include JsonLdHelper
  5. # Find or create a local account for a remote user.
  6. # When creating, look up the user's webfinger and fetch all
  7. # important information from their feed
  8. # @param [String, Account] uri User URI in the form of username@domain
  9. # @param [Hash] options
  10. # @return [Account]
  11. def call(uri, options = {})
  12. @options = options
  13. if uri.is_a?(Account)
  14. @account = uri
  15. @username = @account.username
  16. @domain = @account.domain
  17. uri = "#{@username}@#{@domain}"
  18. return @account if @account.local? || !webfinger_update_due?
  19. else
  20. @username, @domain = uri.split('@')
  21. return Account.find_local(@username) if TagManager.instance.local_domain?(@domain)
  22. @account = Account.find_remote(@username, @domain)
  23. return @account unless webfinger_update_due?
  24. end
  25. Rails.logger.debug "Looking up webfinger for #{uri}"
  26. @webfinger = Goldfinger.finger("acct:#{uri}")
  27. confirmed_username, confirmed_domain = @webfinger.subject.gsub(/\Aacct:/, '').split('@')
  28. if confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero?
  29. @username = confirmed_username
  30. @domain = confirmed_domain
  31. elsif options[:redirected].nil?
  32. return call("#{confirmed_username}@#{confirmed_domain}", options.merge(redirected: true))
  33. else
  34. Rails.logger.debug 'Requested and returned acct URIs do not match'
  35. return
  36. end
  37. return Account.find_local(@username) if TagManager.instance.local_domain?(@domain)
  38. return unless activitypub_ready?
  39. RedisLock.acquire(lock_options) do |lock|
  40. if lock.acquired?
  41. @account = Account.find_remote(@username, @domain)
  42. next unless @account.nil? || @account.activitypub?
  43. handle_activitypub
  44. else
  45. raise Mastodon::RaceConditionError
  46. end
  47. end
  48. @account
  49. rescue Goldfinger::Error => e
  50. Rails.logger.debug "Webfinger query for #{uri} unsuccessful: #{e}"
  51. nil
  52. end
  53. private
  54. def webfinger_update_due?
  55. @account.nil? || ((!@options[:skip_webfinger] || @account.ostatus?) && @account.possibly_stale?)
  56. end
  57. def activitypub_ready?
  58. !@webfinger.link('self').nil? && ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(@webfinger.link('self').type)
  59. end
  60. def handle_activitypub
  61. return if actor_json.nil?
  62. @account = ActivityPub::ProcessAccountService.new.call(@username, @domain, actor_json)
  63. rescue Oj::ParseError
  64. nil
  65. end
  66. def actor_url
  67. @actor_url ||= @webfinger.link('self').href
  68. end
  69. def actor_json
  70. return @actor_json if defined?(@actor_json)
  71. json = fetch_resource(actor_url, false)
  72. @actor_json = supported_context?(json) && equals_or_includes_any?(json['type'], ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES) ? json : nil
  73. end
  74. def lock_options
  75. { redis: Redis.current, key: "resolve:#{@username}@#{@domain}" }
  76. end
  77. end