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.

83 lines
2.7 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. class FollowRemoteAccountService < BaseService
  2. include OStatus2::MagicKey
  3. DFRN_NS = 'http://purl.org/macgirvin/dfrn/1.0'.freeze
  4. # Find or create a local account for a remote user.
  5. # When creating, look up the user's webfinger and fetch all
  6. # important information from their feed
  7. # @param [String] uri User URI in the form of username@domain
  8. # @return [Account]
  9. def call(uri)
  10. username, domain = uri.split('@')
  11. return Account.find_local(username) if TagManager.instance.local_domain?(domain)
  12. return nil if DomainBlock.blocked?(domain)
  13. account = Account.find_remote(username, domain)
  14. return account unless account.nil?
  15. Rails.logger.debug "Creating new remote account for #{uri}"
  16. account = Account.new(username: username, domain: domain)
  17. data = Goldfinger.finger("acct:#{uri}")
  18. raise Goldfinger::Error, 'Missing resource links' if data.link('http://schemas.google.com/g/2010#updates-from').nil? || data.link('salmon').nil? || data.link('http://webfinger.net/rel/profile-page').nil? || data.link('magic-public-key').nil?
  19. account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href
  20. account.salmon_url = data.link('salmon').href
  21. account.url = data.link('http://webfinger.net/rel/profile-page').href
  22. account.public_key = magic_key_to_pem(data.link('magic-public-key').href)
  23. account.private_key = nil
  24. xml = get_feed(account.remote_url)
  25. hubs = get_hubs(xml)
  26. account.uri = get_account_uri(xml)
  27. account.hub_url = hubs.first.attribute('href').value
  28. get_profile(xml, account)
  29. account.save!
  30. return account
  31. end
  32. private
  33. def get_feed(url)
  34. response = http_client.get(Addressable::URI.parse(url))
  35. Nokogiri::XML(response)
  36. end
  37. def get_hubs(xml)
  38. hubs = xml.xpath('//xmlns:link[@rel="hub"]')
  39. raise Goldfinger::Error, 'No PubSubHubbub hubs found' if hubs.empty? || hubs.first.attribute('href').nil?
  40. hubs
  41. end
  42. def get_account_uri(xml)
  43. author_uri = xml.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri')
  44. if author_uri.nil?
  45. owner = xml.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS)
  46. author_uri = owner.at_xpath('./xmlns:uri') unless owner.nil?
  47. end
  48. raise Goldfinger::Error, 'Author URI could not be found' if author_uri.nil?
  49. author_uri.content
  50. end
  51. def get_profile(xml, account)
  52. author = xml.at_xpath('/xmlns:feed/xmlns:author') || xml.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS)
  53. update_remote_profile_service.call(author, account)
  54. end
  55. def update_remote_profile_service
  56. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  57. end
  58. def http_client
  59. HTTP.timeout(:per_operation, write: 20, connect: 20, read: 50)
  60. end
  61. end