From 1ec031475260d513661620ac52b799cffc3f544e Mon Sep 17 00:00:00 2001 From: cmrd Senya Date: Sun, 5 May 2019 10:54:42 +0300 Subject: [PATCH 1/3] Fix reshare import when root is not fetchable --- lib/archive_importer/entity_importer.rb | 2 ++ lib/archive_importer/own_entity_importer.rb | 4 ++- .../archive_importer/post_importer_spec.rb | 34 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/archive_importer/entity_importer.rb b/lib/archive_importer/entity_importer.rb index 9ed4d04f9..a005f22eb 100644 --- a/lib/archive_importer/entity_importer.rb +++ b/lib/archive_importer/entity_importer.rb @@ -15,8 +15,10 @@ class ArchiveImporter rescue DiasporaFederation::Entities::Signable::SignatureVerificationFailed, DiasporaFederation::Discovery::InvalidDocument, DiasporaFederation::Discovery::DiscoveryError, + DiasporaFederation::Federation::Fetcher::NotFetchable, ActiveRecord::RecordInvalid => e logger.warn "#{self}: #{e}" + self.persisted_object = nil end attr_reader :json diff --git a/lib/archive_importer/own_entity_importer.rb b/lib/archive_importer/own_entity_importer.rb index 2d585c0cd..736115f63 100644 --- a/lib/archive_importer/own_entity_importer.rb +++ b/lib/archive_importer/own_entity_importer.rb @@ -21,7 +21,9 @@ class ArchiveImporter attr_reader :old_author_id def persisted_object - @persisted_object ||= (instance if real_author == old_author_id) + return @persisted_object if defined?(@persisted_object) + + @persisted_object = (instance if real_author == old_author_id) end def real_author diff --git a/spec/lib/archive_importer/post_importer_spec.rb b/spec/lib/archive_importer/post_importer_spec.rb index 8361164a9..3a11f2eab 100644 --- a/spec/lib/archive_importer/post_importer_spec.rb +++ b/spec/lib/archive_importer/post_importer_spec.rb @@ -113,5 +113,39 @@ describe ArchiveImporter::PostImporter do end end end + + context "with reshare" do + let!(:author) { FactoryGirl.create(:person, diaspora_handle: "author@example.com") } + let(:entity_json) { JSON.parse(<<~JSON) } + { + "entity_data" : { + "created_at" : "2015-10-19T13:58:16Z", + "guid" : "#{UUID.generate(:compact)}", + "text" : "test post", + "author" : "author@example.com", + "root_author": "root_author@remote-pod.com", + "root_guid": "#{UUID.generate(:compact)}" + }, + "entity_type": "reshare" + } + JSON + + context "when a remote pod responds 403 to discovery requests" do + before do + stub_request(:get, "https://remote-pod.com/.well-known/webfinger?resource=acct:root_author@remote-pod.com") + .to_return(status: 403, body: "", headers: {}) + stub_request(:get, "https://remote-pod.com/.well-known/host-meta") + .to_return(status: 403, body: "", headers: {}) + stub_request(:get, "http://remote-pod.com/.well-known/host-meta") + .to_return(status: 403, body: "", headers: {}) + end + + it "doesn't raise error" do + expect { + instance.import + }.not_to raise_error + end + end + end end end From 9723bd37a78ed65c365835204ee699f981461106 Mon Sep 17 00:00:00 2001 From: cmrd Senya Date: Sun, 5 May 2019 19:38:34 +0300 Subject: [PATCH 2/3] Fix relayable import when parent is not fetchable --- lib/archive_importer/entity_importer.rb | 1 + .../own_relayable_importer.rb | 4 ++++ .../own_relayable_importer_spec.rb | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/lib/archive_importer/entity_importer.rb b/lib/archive_importer/entity_importer.rb index a005f22eb..22c6f49e4 100644 --- a/lib/archive_importer/entity_importer.rb +++ b/lib/archive_importer/entity_importer.rb @@ -16,6 +16,7 @@ class ArchiveImporter DiasporaFederation::Discovery::InvalidDocument, DiasporaFederation::Discovery::DiscoveryError, DiasporaFederation::Federation::Fetcher::NotFetchable, + OwnRelayableImporter::NoParentError, ActiveRecord::RecordInvalid => e logger.warn "#{self}: #{e}" self.persisted_object = nil diff --git a/lib/archive_importer/own_relayable_importer.rb b/lib/archive_importer/own_relayable_importer.rb index a3325735a..c9ff6f82f 100644 --- a/lib/archive_importer/own_relayable_importer.rb +++ b/lib/archive_importer/own_relayable_importer.rb @@ -2,6 +2,8 @@ class ArchiveImporter class OwnRelayableImporter < OwnEntityImporter + class NoParentError < RuntimeError; end + def entity fetch_parent(symbolized_entity_data) entity_class.new(symbolized_entity_data) @@ -19,6 +21,8 @@ class ArchiveImporter break entity_class::PARENT_TYPE if entity_class.const_defined?(:PARENT_TYPE) } entity = Diaspora::Federation::Mappings.model_class_for(type).find_by(guid: data.fetch(:parent_guid)) + raise NoParentError if entity.nil? + data[:parent] = Diaspora::Federation::Entities.related_entity(entity) end end diff --git a/spec/lib/archive_importer/own_relayable_importer_spec.rb b/spec/lib/archive_importer/own_relayable_importer_spec.rb index 2de01b0e1..d8c591924 100644 --- a/spec/lib/archive_importer/own_relayable_importer_spec.rb +++ b/spec/lib/archive_importer/own_relayable_importer_spec.rb @@ -28,4 +28,27 @@ describe ArchiveImporter::OwnRelayableImporter do ).to_json } end + + context "with comment with unknown parent" do + let(:new_user) { FactoryBot.create(:user) } + let(:instance) { described_class.new(entity_json, new_user) } + let(:entity_json) { JSON.parse(<<~JSON) } + { + "entity_data" : { + "created_at" : "2015-10-19T13:58:16Z", + "guid" : "#{UUID.generate(:compact)}", + "text" : "test post", + "author" : "author@example.com", + "parent_guid": "#{UUID.generate(:compact)}" + }, + "entity_type": "comment" + } + JSON + + it "doesn't raise error" do + expect { + instance.import + }.not_to raise_error + end + end end From 4630b49ec49b627cdff955567614f127a1841f91 Mon Sep 17 00:00:00 2001 From: Thorsten Claus Date: Sun, 29 Aug 2021 18:20:54 +0200 Subject: [PATCH 3/3] Update reshare import test with more generic test from #8257 and #8017 Just test for generic NotFetchable error, which also includes the root author failing to be fetched. --- .../archive_importer/post_importer_spec.rb | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/spec/lib/archive_importer/post_importer_spec.rb b/spec/lib/archive_importer/post_importer_spec.rb index 3a11f2eab..9d7852349 100644 --- a/spec/lib/archive_importer/post_importer_spec.rb +++ b/spec/lib/archive_importer/post_importer_spec.rb @@ -115,14 +115,13 @@ describe ArchiveImporter::PostImporter do end context "with reshare" do - let!(:author) { FactoryGirl.create(:person, diaspora_handle: "author@example.com") } + let(:guid) { UUID.generate(:compact) } let(:entity_json) { JSON.parse(<<~JSON) } { "entity_data" : { "created_at" : "2015-10-19T13:58:16Z", - "guid" : "#{UUID.generate(:compact)}", - "text" : "test post", - "author" : "author@example.com", + "guid" : "#{guid}", + "author" : "#{new_user.diaspora_handle}", "root_author": "root_author@remote-pod.com", "root_guid": "#{UUID.generate(:compact)}" }, @@ -130,20 +129,16 @@ describe ArchiveImporter::PostImporter do } JSON - context "when a remote pod responds 403 to discovery requests" do - before do - stub_request(:get, "https://remote-pod.com/.well-known/webfinger?resource=acct:root_author@remote-pod.com") - .to_return(status: 403, body: "", headers: {}) - stub_request(:get, "https://remote-pod.com/.well-known/host-meta") - .to_return(status: 403, body: "", headers: {}) - stub_request(:get, "http://remote-pod.com/.well-known/host-meta") - .to_return(status: 403, body: "", headers: {}) - end + context "with fetch problems" do + it "handles unfetchable root post" do + allow(DiasporaFederation::Federation::Fetcher).to receive(:fetch_public) + .and_raise(DiasporaFederation::Federation::Fetcher::NotFetchable) - it "doesn't raise error" do expect { instance.import }.not_to raise_error + + expect(Reshare.find_by(guid: guid)).to be_nil end end end