闭社主体 forked from https://github.com/tootsuite/mastodon
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

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. # Find or create a local account for a remote user.
  3. # When creating, look up the user's webfinger and fetch all
  4. # important information from their feed
  5. # @param [String] uri User URI in the form of username@domain
  6. # @return [Account]
  7. def call(uri)
  8. username, domain = uri.split('@')
  9. return Account.find_local(username) if domain == Rails.configuration.x.local_domain || domain.nil?
  10. account = Account.find_remote(username, domain)
  11. return account unless account.nil?
  12. Rails.logger.debug "Creating new remote account for #{uri}"
  13. account = Account.new(username: username, domain: domain)
  14. data = Goldfinger.finger("acct:#{uri}")
  15. account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href
  16. account.salmon_url = data.link('salmon').href
  17. account.url = data.link('http://webfinger.net/rel/profile-page').href
  18. account.public_key = magic_key_to_pem(data.link('magic-public-key').href)
  19. account.private_key = nil
  20. feed = get_feed(account.remote_url)
  21. hubs = feed.xpath('//xmlns:link[@rel="hub"]')
  22. if hubs.empty? || hubs.first.attribute('href').nil?
  23. raise Goldfinger::Error, "No PubSubHubbub hubs found"
  24. end
  25. if feed.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri').nil?
  26. raise Goldfinger::Error, "No author URI found"
  27. end
  28. account.uri = feed.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri').content
  29. account.hub_url = hubs.first.attribute('href').value
  30. get_profile(feed, account)
  31. account.save!
  32. return account
  33. end
  34. private
  35. def get_feed(url)
  36. response = http_client.get(Addressable::URI.parse(url))
  37. Nokogiri::XML(response)
  38. end
  39. def get_profile(xml, account)
  40. author = xml.at_xpath('/xmlns:feed/xmlns:author')
  41. update_remote_profile_service.(author, account)
  42. end
  43. def magic_key_to_pem(magic_key)
  44. _, modulus, exponent = magic_key.split('.')
  45. modulus, exponent = [modulus, exponent].map { |n| Base64.urlsafe_decode64(n).bytes.inject(0) { |num, byte| (num << 8) | byte } }
  46. key = OpenSSL::PKey::RSA.new
  47. key.n = modulus
  48. key.e = exponent
  49. key.to_pem
  50. end
  51. def update_remote_profile_service
  52. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  53. end
  54. def http_client
  55. HTTP
  56. end
  57. end