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.

49 lines
1.0 KiB

  1. # frozen_string_literal: true
  2. class RemoteFollow
  3. include ActiveModel::Validations
  4. attr_accessor :acct, :addressable_template
  5. def initialize(attrs = {})
  6. @acct = attrs[:acct].strip unless attrs[:acct].nil?
  7. end
  8. def valid?
  9. populate_template
  10. errors.empty?
  11. end
  12. def subscribe_address_for(account)
  13. addressable_template.expand(uri: account.to_webfinger_s).to_s
  14. end
  15. private
  16. def populate_template
  17. if acct.blank? || redirect_url_link.nil? || redirect_url_link.template.nil?
  18. missing_resource_error
  19. else
  20. @addressable_template = Addressable::Template.new(redirect_uri_template)
  21. end
  22. end
  23. def redirect_uri_template
  24. redirect_url_link.template
  25. end
  26. def redirect_url_link
  27. acct_resource&.link('http://ostatus.org/schema/1.0/subscribe')
  28. end
  29. def acct_resource
  30. @_acct_resource ||= Goldfinger.finger("acct:#{acct}")
  31. rescue Goldfinger::Error
  32. missing_resource_error
  33. nil
  34. end
  35. def missing_resource_error
  36. errors.add(:acct, I18n.t('remote_follow.missing_resource'))
  37. end
  38. end