The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

95 lines
3.1 KiB

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