Put ActivityPub alternate link into Atom, prefer it when processing Atom (#4623)

This commit is contained in:
Eugen Rochko 2017-08-18 01:03:18 +02:00 committed by GitHub
parent efec02f153
commit 40c45f5dd9
6 changed files with 32 additions and 8 deletions

View File

@ -71,7 +71,7 @@ class ActivityPub::TagManager
def local_uri?(uri) def local_uri?(uri)
host = Addressable::URI.parse(uri).normalized_host host = Addressable::URI.parse(uri).normalized_host
::TagManager.instance.local_domain?(host) || ::TagManager.instance.web_domain?(host) !host.nil? && (::TagManager.instance.local_domain?(host) || ::TagManager.instance.web_domain?(host))
end end
def uri_to_local_id(uri, param = :id) def uri_to_local_id(uri, param = :id)

View File

@ -29,16 +29,28 @@ class OStatus::Activity::Base
end end
def url def url
link = @xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS) link = @xml.xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS).find { |link_candidate| link_candidate['type'] == 'text/html' }
link.nil? ? nil : link['href'] link.nil? ? nil : link['href']
end end
def activitypub_uri
link = @xml.xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS).find { |link_candidate| ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(link_candidate['type']) }
link.nil? ? nil : link['href']
end
def activitypub_uri?
activitypub_uri.present?
end
private private
def find_status(uri) def find_status(uri)
if TagManager.instance.local_id?(uri) if TagManager.instance.local_id?(uri)
local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status') local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
return Status.find_by(id: local_id) return Status.find_by(id: local_id)
elsif ActivityPub::TagManager.instance.local_uri?(uri)
local_id = ActivityPub::TagManager.instance.uri_to_local_id(uri)
return Status.find_by(id: local_id)
end end
Status.find_by(uri: uri) Status.find_by(uri: uri)

View File

@ -8,6 +8,7 @@ class OStatus::Activity::Creation < OStatus::Activity::Base
end end
return [nil, false] if @account.suspended? return [nil, false] if @account.suspended?
return perform_via_activitypub if activitypub_uri?
Rails.logger.debug "Creating remote status #{id}" Rails.logger.debug "Creating remote status #{id}"
@ -52,6 +53,10 @@ class OStatus::Activity::Creation < OStatus::Activity::Base
[status, true] [status, true]
end end
def perform_via_activitypub
[find_status(activitypub_uri) || ActivityPub::FetchRemoteStatusService.new.call(activitypub_uri), false]
end
def content def content
@xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content @xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content
end end

View File

@ -2,6 +2,10 @@
class OStatus::Activity::Remote < OStatus::Activity::Base class OStatus::Activity::Remote < OStatus::Activity::Base
def perform def perform
find_status(id) || FetchRemoteStatusService.new.call(url) if activitypub_uri?
find_status(activitypub_uri) || FetchRemoteStatusService.new.call(url)
else
find_status(id) || FetchRemoteStatusService.new.call(url)
end
end end
end end

View File

@ -343,6 +343,8 @@ class OStatus::AtomSerializer
end end
def serialize_status_attributes(entry, status) def serialize_status_attributes(entry, status)
append_element(entry, 'link', nil, rel: :alternate, type: 'application/activity+json', href: ActivityPub::TagManager.instance.uri_for(status)) if status.account.local?
append_element(entry, 'summary', status.spoiler_text, 'xml:lang': status.language) if status.spoiler_text? append_element(entry, 'summary', status.spoiler_text, 'xml:lang': status.language) if status.spoiler_text?
append_element(entry, 'content', Formatter.instance.format(status).to_str, type: 'html', 'xml:lang': status.language) append_element(entry, 'content', Formatter.instance.format(status).to_str, type: 'html', 'xml:lang': status.language)

View File

@ -196,7 +196,7 @@ RSpec.describe OStatus::AtomSerializer do
author = OStatus::AtomSerializer.new.author(account) author = OStatus::AtomSerializer.new.author(account)
link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } link = author.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' && node[:type] == 'text/html' }
expect(link[:type]).to eq 'text/html' expect(link[:type]).to eq 'text/html'
expect(link[:rel]).to eq 'alternate' expect(link[:rel]).to eq 'alternate'
expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/@username' expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/@username'
@ -407,6 +407,7 @@ RSpec.describe OStatus::AtomSerializer do
remote_status.stream_entry.update!(created_at: '2000-01-01T00:00:00Z') remote_status.stream_entry.update!(created_at: '2000-01-01T00:00:00Z')
entry = OStatus::AtomSerializer.new.entry(remote_status.stream_entry, true) entry = OStatus::AtomSerializer.new.entry(remote_status.stream_entry, true)
entry.nodes.delete_if { |node| node[:type] == 'application/activity+json' } # Remove ActivityPub link to simplify test
xml = OStatus::AtomSerializer.render(entry).gsub('cb6e6126.ngrok.io', 'remote') xml = OStatus::AtomSerializer.render(entry).gsub('cb6e6126.ngrok.io', 'remote')
remote_status.destroy! remote_status.destroy!
@ -415,7 +416,7 @@ RSpec.describe OStatus::AtomSerializer do
account = Account.create!( account = Account.create!(
domain: 'remote', domain: 'remote',
username: 'username', username: 'username',
last_webfingered_at: Time.now.utc, last_webfingered_at: Time.now.utc
) )
ProcessFeedService.new.call(xml, account) ProcessFeedService.new.call(xml, account)
@ -529,7 +530,7 @@ RSpec.describe OStatus::AtomSerializer do
entry = OStatus::AtomSerializer.new.entry(status.stream_entry) entry = OStatus::AtomSerializer.new.entry(status.stream_entry)
link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' && node[:type] == 'text/html' }
expect(link[:type]).to eq 'text/html' expect(link[:type]).to eq 'text/html'
expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username/updates/#{status.stream_entry.id}" expect(link[:href]).to eq "https://cb6e6126.ngrok.io/users/username/updates/#{status.stream_entry.id}"
end end
@ -642,7 +643,7 @@ RSpec.describe OStatus::AtomSerializer do
feed = OStatus::AtomSerializer.new.feed(account, []) feed = OStatus::AtomSerializer.new.feed(account, [])
link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } link = feed.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' && node[:type] == 'text/html' }
expect(link[:type]).to eq 'text/html' expect(link[:type]).to eq 'text/html'
expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/@username' expect(link[:href]).to eq 'https://cb6e6126.ngrok.io/@username'
end end
@ -1509,7 +1510,7 @@ RSpec.describe OStatus::AtomSerializer do
entry = OStatus::AtomSerializer.new.object(status) entry = OStatus::AtomSerializer.new.object(status)
link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' } link = entry.nodes.find { |node| node.name == 'link' && node[:rel] == 'alternate' && node[:type] == 'text/html' }
expect(link[:type]).to eq 'text/html' expect(link[:type]).to eq 'text/html'
expect(link[:href]).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}" expect(link[:href]).to eq "https://cb6e6126.ngrok.io/@username/#{status.id}"
end end