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.

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