about summary refs log tree commit diff
diff options
context:
space:
mode:
authorunarist <m.unarist@gmail.com>2017-09-09 01:20:03 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-09-08 18:20:03 +0200
commita12572e074581832744b243f2fadd1742e8418b6 (patch)
tree9139c1f0fcfb3e48b426a479dab5c565ce356639
parentdabc309ca3329c455e4e26a8bf9a691fcafe20a7 (diff)
Handle stream_entry URL correctly in ActivityPub (#4854)
In before, the method uses stream_entry id as status id, so replied status was wrongly selected.

This PR uses StatusFinder which was introduced with `Api::Web::EmbedsController`.
-rw-r--r--app/lib/activitypub/tag_manager.rb4
-rw-r--r--spec/lib/activitypub/tag_manager_spec.rb28
2 files changed, 30 insertions, 2 deletions
diff --git a/app/lib/activitypub/tag_manager.rb b/app/lib/activitypub/tag_manager.rb
index de575d9e6..929e87852 100644
--- a/app/lib/activitypub/tag_manager.rb
+++ b/app/lib/activitypub/tag_manager.rb
@@ -96,12 +96,14 @@ class ActivityPub::TagManager
       when 'Account'
         klass.find_local(uri_to_local_id(uri, :username))
       else
-        klass.find_by(id: uri_to_local_id(uri))
+        StatusFinder.new(uri).status
       end
     elsif ::TagManager.instance.local_id?(uri)
       klass.find_by(id: ::TagManager.instance.unique_tag_to_local_id(uri, klass.to_s))
     else
       klass.find_by(uri: uri.split('#').first)
     end
+  rescue ActiveRecord::RecordNotFound
+    nil
   end
 end
diff --git a/spec/lib/activitypub/tag_manager_spec.rb b/spec/lib/activitypub/tag_manager_spec.rb
index 8f7662e24..dea8abc65 100644
--- a/spec/lib/activitypub/tag_manager_spec.rb
+++ b/spec/lib/activitypub/tag_manager_spec.rb
@@ -91,9 +91,35 @@ RSpec.describe ActivityPub::TagManager do
   end
 
   describe '#uri_to_resource' do
-    it 'returns the local resource' do
+    it 'returns the local account' do
       account = Fabricate(:account)
       expect(subject.uri_to_resource(subject.uri_for(account), Account)).to eq account
     end
+
+    it 'returns the remote account by matching URI without fragment part' do
+      account = Fabricate(:account, uri: 'https://example.com/123')
+      expect(subject.uri_to_resource('https://example.com/123#456', Account)).to eq account
+    end
+
+    it 'returns the local status for ActivityPub URI' do
+      status = Fabricate(:status)
+      expect(subject.uri_to_resource(subject.uri_for(status), Status)).to eq status
+    end
+
+    it 'returns the local status for OStatus tag: URI' do
+      status = Fabricate(:status)
+      expect(subject.uri_to_resource(::TagManager.instance.uri_for(status), Status)).to eq status
+    end
+
+    it 'returns the local status for OStatus StreamEntry URL' do
+      status = Fabricate(:status)
+      stream_entry_url = account_stream_entry_url(status.account, status.stream_entry)
+      expect(subject.uri_to_resource(stream_entry_url, Status)).to eq status
+    end
+
+    it 'returns the remote status by matching URI without fragment part' do
+      status = Fabricate(:status, uri: 'https://example.com/123')
+      expect(subject.uri_to_resource('https://example.com/123#456', Status)).to eq status
+    end
   end
 end