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.
 
 
 
 

96 lines
3.3 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. account = Account.find_remote(username, domain)
  14. return account unless account&.last_webfingered_at.nil? || 1.day.from_now(account.last_webfingered_at) < Time.now.utc
  15. Rails.logger.debug "Looking up webfinger for #{uri}"
  16. data = Goldfinger.finger("acct:#{uri}")
  17. 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?
  18. confirmed_username, confirmed_domain = data.subject.gsub(/\Aacct:/, '').split('@')
  19. return Account.find_local(confirmed_username) if TagManager.instance.local_domain?(confirmed_domain)
  20. confirmed_account = Account.find_remote(confirmed_username, confirmed_domain)
  21. if confirmed_account.nil?
  22. Rails.logger.debug "Creating new remote account for #{uri}"
  23. domain_block = DomainBlock.find_by(domain: domain)
  24. account = Account.new(username: confirmed_username, domain: confirmed_domain)
  25. account.suspended = true if domain_block && domain_block.suspend?
  26. account.silenced = true if domain_block && domain_block.silence?
  27. account.private_key = nil
  28. else
  29. account = confirmed_account
  30. end
  31. account.last_webfingered_at = Time.now.utc
  32. account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href
  33. account.salmon_url = data.link('salmon').href
  34. account.url = data.link('http://webfinger.net/rel/profile-page').href
  35. account.public_key = magic_key_to_pem(data.link('magic-public-key').href)
  36. body, xml = get_feed(account.remote_url)
  37. hubs = get_hubs(xml)
  38. account.uri = get_account_uri(xml)
  39. account.hub_url = hubs.first.attribute('href').value
  40. account.save!
  41. get_profile(body, account)
  42. account
  43. end
  44. private
  45. def get_feed(url)
  46. response = http_client.get(Addressable::URI.parse(url))
  47. [response.to_s, Nokogiri::XML(response)]
  48. end
  49. def get_hubs(xml)
  50. hubs = xml.xpath('//xmlns:link[@rel="hub"]')
  51. raise Goldfinger::Error, 'No PubSubHubbub hubs found' if hubs.empty? || hubs.first.attribute('href').nil?
  52. hubs
  53. end
  54. def get_account_uri(xml)
  55. author_uri = xml.at_xpath('/xmlns:feed/xmlns:author/xmlns:uri')
  56. if author_uri.nil?
  57. owner = xml.at_xpath('/xmlns:feed').at_xpath('./dfrn:owner', dfrn: DFRN_NS)
  58. author_uri = owner.at_xpath('./xmlns:uri') unless owner.nil?
  59. end
  60. raise Goldfinger::Error, 'Author URI could not be found' if author_uri.nil?
  61. author_uri.content
  62. end
  63. def get_profile(body, account)
  64. RemoteProfileUpdateWorker.perform_async(account.id, body.force_encoding('UTF-8'), false)
  65. end
  66. def http_client
  67. HTTP.timeout(:per_operation, write: 20, connect: 20, read: 50)
  68. end
  69. end