2017-01-02 05:52:25 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RemoteFollow
|
|
|
|
include ActiveModel::Validations
|
2019-08-08 05:14:08 +10:00
|
|
|
include RoutingHelper
|
2017-01-02 05:52:25 +11:00
|
|
|
|
2017-05-02 08:44:23 +10:00
|
|
|
attr_accessor :acct, :addressable_template
|
2017-01-02 05:52:25 +11:00
|
|
|
|
2019-08-30 10:19:17 +10:00
|
|
|
validates :acct, presence: true, domain: { acct: true }
|
2017-06-21 04:40:56 +10:00
|
|
|
|
2019-08-08 05:14:08 +10:00
|
|
|
def initialize(attrs = {})
|
|
|
|
@acct = normalize_acct(attrs[:acct])
|
2017-01-02 05:52:25 +11:00
|
|
|
end
|
2017-05-02 08:44:23 +10:00
|
|
|
|
|
|
|
def valid?
|
2017-06-21 04:40:56 +10:00
|
|
|
return false unless super
|
|
|
|
|
2019-08-08 05:14:08 +10:00
|
|
|
fetch_template!
|
|
|
|
|
2017-05-02 08:44:23 +10:00
|
|
|
errors.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def subscribe_address_for(account)
|
2019-08-30 10:19:17 +10:00
|
|
|
addressable_template.expand(uri: ActivityPub::TagManager.instance.uri_for(account)).to_s
|
2017-05-02 08:44:23 +10:00
|
|
|
end
|
|
|
|
|
2018-08-18 11:03:12 +10:00
|
|
|
def interact_address_for(status)
|
|
|
|
addressable_template.expand(uri: ActivityPub::TagManager.instance.uri_for(status)).to_s
|
|
|
|
end
|
|
|
|
|
2017-05-02 08:44:23 +10:00
|
|
|
private
|
|
|
|
|
2019-08-08 05:14:08 +10:00
|
|
|
def normalize_acct(value)
|
|
|
|
return if value.blank?
|
|
|
|
|
|
|
|
username, domain = value.strip.gsub(/\A@/, '').split('@')
|
|
|
|
|
2023-02-19 09:09:40 +11:00
|
|
|
domain = if TagManager.instance.local_domain?(domain)
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
TagManager.instance.normalize_domain(domain)
|
|
|
|
end
|
2019-08-08 05:14:08 +10:00
|
|
|
|
|
|
|
[username, domain].compact.join('@')
|
2019-08-30 10:19:17 +10:00
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
value
|
2019-08-08 05:14:08 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_template!
|
2019-09-20 04:58:19 +10:00
|
|
|
return missing_resource_error if acct.blank?
|
2019-08-08 05:14:08 +10:00
|
|
|
|
|
|
|
_, domain = acct.split('@')
|
|
|
|
|
|
|
|
if domain.nil?
|
|
|
|
@addressable_template = Addressable::Template.new("#{authorize_interaction_url}?uri={uri}")
|
2020-10-08 09:34:57 +11:00
|
|
|
elsif redirect_uri_template.nil?
|
2017-05-02 08:44:23 +10:00
|
|
|
missing_resource_error
|
|
|
|
else
|
|
|
|
@addressable_template = Addressable::Template.new(redirect_uri_template)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_uri_template
|
2020-10-08 09:34:57 +11:00
|
|
|
acct_resource&.link('http://ostatus.org/schema/1.0/subscribe', 'template')
|
2017-05-02 08:44:23 +10:00
|
|
|
end
|
|
|
|
|
|
|
|
def acct_resource
|
2024-10-01 19:54:42 +10:00
|
|
|
@acct_resource ||= Webfinger.new("acct:#{acct}").perform
|
2020-10-08 09:34:57 +11:00
|
|
|
rescue Webfinger::Error, HTTP::ConnectionError
|
2017-05-02 08:44:23 +10:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def missing_resource_error
|
|
|
|
errors.add(:acct, I18n.t('remote_follow.missing_resource'))
|
|
|
|
end
|
2017-01-02 05:52:25 +11:00
|
|
|
end
|