1
0

remote_follow.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 = nil)
  7. @acct = attrs[:acct].gsub(/\A@/, '').strip if !attrs.nil? && !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. def interact_address_for(status)
  18. addressable_template.expand(uri: ActivityPub::TagManager.instance.uri_for(status)).to_s
  19. end
  20. private
  21. def populate_template
  22. if acct.blank? || redirect_url_link.nil? || redirect_url_link.template.nil?
  23. missing_resource_error
  24. else
  25. @addressable_template = Addressable::Template.new(redirect_uri_template)
  26. end
  27. end
  28. def redirect_uri_template
  29. redirect_url_link.template
  30. end
  31. def redirect_url_link
  32. acct_resource&.link('http://ostatus.org/schema/1.0/subscribe')
  33. end
  34. def acct_resource
  35. @_acct_resource ||= Goldfinger.finger("acct:#{acct}")
  36. rescue Goldfinger::Error, HTTP::ConnectionError
  37. nil
  38. end
  39. def missing_resource_error
  40. errors.add(:acct, I18n.t('remote_follow.missing_resource'))
  41. end
  42. end