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