From 6d5647ec112d3fd0c93d7f217dc2f6beed9fde04 Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Thu, 12 Oct 2017 03:37:35 +0200 Subject: [PATCH] Handle duplicate account deletions --- lib/diaspora/federation/receive.rb | 6 +++++- spec/lib/diaspora/federation/receive_spec.rb | 21 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/diaspora/federation/receive.rb b/lib/diaspora/federation/receive.rb index adddf4000..13e235de1 100644 --- a/lib/diaspora/federation/receive.rb +++ b/lib/diaspora/federation/receive.rb @@ -10,7 +10,11 @@ module Diaspora end def self.account_deletion(entity) - AccountDeletion.create!(person: author_of(entity)) + person = author_of(entity) + AccountDeletion.create!(person: person) unless AccountDeletion.where(person: person).exists? + rescue => e # rubocop:disable Lint/RescueWithoutErrorClass + raise e unless AccountDeletion.where(person: person).exists? + logger.warn "ignoring error on receive AccountDeletion:#{entity.author}: #{e.class}: #{e.message}" end def self.account_migration(entity) diff --git a/spec/lib/diaspora/federation/receive_spec.rb b/spec/lib/diaspora/federation/receive_spec.rb index 466dd4bfe..1fd76b08d 100644 --- a/spec/lib/diaspora/federation/receive_spec.rb +++ b/spec/lib/diaspora/federation/receive_spec.rb @@ -12,6 +12,27 @@ describe Diaspora::Federation::Receive do expect(AccountDeletion.exists?(person: sender)).to be_truthy end + + it "ignores duplicate the account deletion" do + AccountDeletion.create(person: sender) + + expect(AccountDeletion).not_to receive(:create!) + + Diaspora::Federation::Receive.account_deletion(account_deletion_entity) + + expect(AccountDeletion.exists?(person: sender)).to be_truthy + end + + it "handles race conditions on parallel receive" do + expect(AccountDeletion).to receive(:create!) do + AccountDeletion.create(person: sender) + raise "Some database error" + end + + Diaspora::Federation::Receive.account_deletion(account_deletion_entity) + + expect(AccountDeletion.exists?(person: sender)).to be_truthy + end end describe ".account_migration" do