2017-01-01 19:52:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RemoteFollow
|
|
|
|
include ActiveModel::Validations
|
2019-08-07 21:14:08 +02:00
|
|
|
include RoutingHelper
|
2017-01-01 19:52:25 +01:00
|
|
|
|
2017-05-02 00:44:23 +02:00
|
|
|
attr_accessor :acct, :addressable_template
|
2017-01-01 19:52:25 +01:00
|
|
|
|
2019-08-30 02:19:17 +02:00
|
|
|
validates :acct, presence: true, domain: { acct: true }
|
2017-06-20 20:40:56 +02:00
|
|
|
|
2019-08-07 21:14:08 +02:00
|
|
|
def initialize(attrs = {})
|
|
|
|
@acct = normalize_acct(attrs[:acct])
|
2017-01-01 19:52:25 +01:00
|
|
|
end
|
2017-05-02 00:44:23 +02:00
|
|
|
|
|
|
|
def valid?
|
2017-06-20 20:40:56 +02:00
|
|
|
return false unless super
|
|
|
|
|
2019-08-07 21:14:08 +02:00
|
|
|
fetch_template!
|
|
|
|
|
2017-05-02 00:44:23 +02:00
|
|
|
errors.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def subscribe_address_for(account)
|
2019-08-30 02:19:17 +02:00
|
|
|
addressable_template.expand(uri: ActivityPub::TagManager.instance.uri_for(account)).to_s
|
2017-05-02 00:44:23 +02:00
|
|
|
end
|
|
|
|
|
2018-08-18 03:03:12 +02:00
|
|
|
def interact_address_for(status)
|
|
|
|
addressable_template.expand(uri: ActivityPub::TagManager.instance.uri_for(status)).to_s
|
|
|
|
end
|
|
|
|
|
2017-05-02 00:44:23 +02:00
|
|
|
private
|
|
|
|
|
2019-08-07 21:14:08 +02: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 02:19:17 +02:00
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
value
|
2019-08-07 21:14:08 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_template!
|
|
|
|
return missing_resource if acct.blank?
|
|
|
|
|
|
|
|
_, domain = acct.split('@')
|
|
|
|
|
|
|
|
if domain.nil?
|
|
|
|
@addressable_template = Addressable::Template.new("#{authorize_interaction_url}?uri={uri}")
|
|
|
|
elsif redirect_url_link.nil? || redirect_url_link.template.nil?
|
2017-05-02 00:44:23 +02:00
|
|
|
missing_resource_error
|
|
|
|
else
|
|
|
|
@addressable_template = Addressable::Template.new(redirect_uri_template)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_uri_template
|
|
|
|
redirect_url_link.template
|
|
|
|
end
|
|
|
|
|
|
|
|
def redirect_url_link
|
|
|
|
acct_resource&.link('http://ostatus.org/schema/1.0/subscribe')
|
|
|
|
end
|
|
|
|
|
|
|
|
def acct_resource
|
2019-08-07 21:14:08 +02:00
|
|
|
@acct_resource ||= Goldfinger.finger("acct:#{acct}")
|
2017-08-28 19:12:09 +02:00
|
|
|
rescue Goldfinger::Error, HTTP::ConnectionError
|
2017-05-02 00:44:23 +02:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def missing_resource_error
|
|
|
|
errors.add(:acct, I18n.t('remote_follow.missing_resource'))
|
|
|
|
end
|
2017-01-01 19:52:25 +01:00
|
|
|
end
|