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