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.

52 lines
1.1 KiB

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