diff --git a/lib/archive_importer/entity_importer.rb b/lib/archive_importer/entity_importer.rb index 6314c9e21..9ed4d04f9 100644 --- a/lib/archive_importer/entity_importer.rb +++ b/lib/archive_importer/entity_importer.rb @@ -11,7 +11,7 @@ class ArchiveImporter end def import - self.persisted_object = Diaspora::Federation::Receive.perform(entity) + self.persisted_object = Diaspora::Federation::Receive.perform(entity, skip_relaying: true) rescue DiasporaFederation::Entities::Signable::SignatureVerificationFailed, DiasporaFederation::Discovery::InvalidDocument, DiasporaFederation::Discovery::DiscoveryError, diff --git a/lib/diaspora/federation/receive.rb b/lib/diaspora/federation/receive.rb index 7148c2521..7ab52c2b0 100644 --- a/lib/diaspora/federation/receive.rb +++ b/lib/diaspora/federation/receive.rb @@ -5,8 +5,8 @@ module Diaspora module Receive extend Diaspora::Logging - def self.perform(entity) - public_send(Mappings.receiver_for(entity), entity) + def self.perform(entity, opts={}) + public_send(Mappings.receiver_for(entity), entity, opts) end def self.account_deletion(entity) @@ -17,9 +17,9 @@ module Diaspora logger.warn "ignoring error on receive AccountDeletion:#{entity.author}: #{e.class}: #{e.message}" end - def self.account_migration(entity) + def self.account_migration(entity, opts) old_person = author_of(entity) - profile = profile(entity.profile) + profile = profile(entity.profile, opts) return if AccountMigration.where(old_person: old_person, new_person: profile.person).exists? AccountMigration.create!(old_person: old_person, new_person: profile.person) rescue => e # rubocop:disable Lint/RescueWithoutErrorClass @@ -28,8 +28,8 @@ module Diaspora nil end - def self.comment(entity) - receive_relayable(Comment, entity) do + def self.comment(entity, opts) + receive_relayable(Comment, entity, opts) do Comment.new( author: author_of(entity), guid: entity.guid, @@ -40,7 +40,7 @@ module Diaspora end end - def self.contact(entity) + def self.contact(entity, _opts) recipient = Person.find_by(diaspora_handle: entity.recipient).owner if entity.sharing Contact.create_or_update_sharing_contact(recipient, author_of(entity)) @@ -50,7 +50,7 @@ module Diaspora end end - def self.conversation(entity) + def self.conversation(entity, _opts) author = author_of(entity) ignore_existing_guid(Conversation, entity.guid, author) do Conversation.create!( @@ -64,8 +64,8 @@ module Diaspora end end - def self.like(entity) - receive_relayable(Like, entity) do + def self.like(entity, opts) + receive_relayable(Like, entity, opts) do Like.new( author: author_of(entity), guid: entity.guid, @@ -75,13 +75,13 @@ module Diaspora end end - def self.message(entity) + def self.message(entity, _opts) ignore_existing_guid(Message, entity.guid, author_of(entity)) do build_message(entity).tap(&:save!) end end - def self.participation(entity) + def self.participation(entity, _opts) author = author_of(entity) ignore_existing_guid(Participation, entity.guid, author) do Participation.create!( @@ -92,7 +92,7 @@ module Diaspora end end - def self.photo(entity) + def self.photo(entity, _opts) author = author_of(entity) persisted_photo = load_from_database(Photo, entity.guid, author) @@ -114,8 +114,8 @@ module Diaspora end end - def self.poll_participation(entity) - receive_relayable(PollParticipation, entity) do + def self.poll_participation(entity, opts) + receive_relayable(PollParticipation, entity, opts) do PollParticipation.new( author: author_of(entity), guid: entity.guid, @@ -125,7 +125,7 @@ module Diaspora end end - def self.profile(entity) + def self.profile(entity, _opts) author_of(entity).profile.tap do |profile| profile.update_attributes( first_name: entity.first_name, @@ -145,7 +145,7 @@ module Diaspora end end - def self.reshare(entity) + def self.reshare(entity, _opts) author = author_of(entity) ignore_existing_guid(Reshare, entity.guid, author) do Reshare.create!( @@ -178,7 +178,7 @@ module Diaspora end end - def self.status_message(entity) + def self.status_message(entity, _opts) # rubocop:disable Metrics/AbcSize try_load_existing_guid(StatusMessage, entity.guid, author_of(entity)) do StatusMessage.new( author: author_of(entity), @@ -257,8 +257,9 @@ module Diaspora end end - private_class_method def self.receive_relayable(klass, entity) - save_relayable(klass, entity) { yield }.tap {|relayable| relay_relayable(relayable) if relayable } + private_class_method def self.receive_relayable(klass, entity, opts) + save_relayable(klass, entity) { yield } + .tap {|relayable| relay_relayable(relayable) if relayable && !opts[:skip_relaying] } end private_class_method def self.save_relayable(klass, entity) diff --git a/spec/lib/archive_importer/entity_importer_spec.rb b/spec/lib/archive_importer/entity_importer_spec.rb index 969368ce2..ecc11b49a 100644 --- a/spec/lib/archive_importer/entity_importer_spec.rb +++ b/spec/lib/archive_importer/entity_importer_spec.rb @@ -23,7 +23,7 @@ describe ArchiveImporter::EntityImporter do it "runs entity receive routine" do expect(Diaspora::Federation::Receive).to receive(:perform) - .with(kind_of(DiasporaFederation::Entities::StatusMessage)) + .with(kind_of(DiasporaFederation::Entities::StatusMessage), skip_relaying: true) .and_call_original instance.import @@ -59,7 +59,7 @@ describe ArchiveImporter::EntityImporter do it "runs entity receive routine" do expect(Diaspora::Federation::Receive).to receive(:perform) - .with(kind_of(DiasporaFederation::Entities::Comment)) + .with(kind_of(DiasporaFederation::Entities::Comment), skip_relaying: true) .and_call_original instance.import comment = Comment.find_by(guid: guid) @@ -67,6 +67,24 @@ describe ArchiveImporter::EntityImporter do expect(comment.author).to eq(author.person) end + it "does not relay a remote comment during import" do + comment_author = FactoryGirl.build(:user) + comment_author.person.owner = nil + comment_author.person.pod = Pod.find_or_create_by(url: "http://example.net") + comment_author.person.save! + + status_message = FactoryGirl.create(:status_message, author: alice.person, public: true) + comment_data = Fabricate.attributes_for(:comment_entity, + author: comment_author.diaspora_handle, + parent_guid: status_message.guid).tap do |data| + data[:author_signature] = Fabricate(:comment_entity, data).sign_with_key(comment_author.encryption_key) + end + + expect(Diaspora::Federation::Dispatcher).not_to receive(:defer_dispatch) + + ArchiveImporter::EntityImporter.new(Fabricate(:comment_entity, comment_data).to_json.as_json, nil).import + end + it "rescues DiasporaFederation::Entities::Signable::SignatureVerificationFailed" do expect(Person).to receive(:find_or_fetch_by_identifier) .with(author.diaspora_handle) diff --git a/spec/lib/diaspora/federation/receive_spec.rb b/spec/lib/diaspora/federation/receive_spec.rb index 1b3e087e7..9e345dc25 100644 --- a/spec/lib/diaspora/federation/receive_spec.rb +++ b/spec/lib/diaspora/federation/receive_spec.rb @@ -43,7 +43,7 @@ describe Diaspora::Federation::Receive do } it "saves the account deletion" do - Diaspora::Federation::Receive.account_migration(account_migration_entity) + Diaspora::Federation::Receive.perform(account_migration_entity) expect(AccountMigration.exists?(old_person: sender, new_person: new_person)).to be_truthy end @@ -53,7 +53,7 @@ describe Diaspora::Federation::Receive do expect(AccountMigration).not_to receive(:create!) - expect(Diaspora::Federation::Receive.account_migration(account_migration_entity)).to be_nil + expect(Diaspora::Federation::Receive.perform(account_migration_entity)).to be_nil expect(AccountMigration.exists?(old_person: sender, new_person: new_person)).to be_truthy end @@ -64,7 +64,7 @@ describe Diaspora::Federation::Receive do raise "Some database error" end - expect(Diaspora::Federation::Receive.account_migration(account_migration_entity)).to be_nil + expect(Diaspora::Federation::Receive.perform(account_migration_entity)).to be_nil expect(AccountMigration.exists?(old_person: sender, new_person: new_person)).to be_truthy end @@ -757,7 +757,7 @@ describe Diaspora::Federation::Receive do end it "does not overwrite the photos if they already exist" do - received_photo = Diaspora::Federation::Receive.photo(photo1) + received_photo = Diaspora::Federation::Receive.perform(photo1) received_photo.text = "foobar" received_photo.save! diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index 225d609a8..0b4996506 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -197,7 +197,7 @@ describe Photo, :type => :model do @saved_photo.destroy - Diaspora::Federation::Receive.photo(federation_photo) + Diaspora::Federation::Receive.perform(federation_photo) new_photo = Photo.find_by(guid: @saved_photo.guid) expect(new_photo.url).to eq(url)