Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Replace and delete users in notifications once deleted

Oliver Günther5d52ac0a262c
diff --git a/app/services/principals/replace_references_service.rb b/app/services/principals/replace_references_service.rb
index f824b0901bb..b769cdd1cff 100644
--- a/app/services/principals/replace_references_service.rb
+++ b/app/services/principals/replace_references_service.rb
@@ -49,6 +49,7 @@ module Principals
rewrite_user(from, to)
rewrite_assigned_to(from, to)
rewrite_responsible(from, to)
+ rewrite_actor(from, to)
end
def rewrite_custom_value(from, to)
@@ -102,6 +103,12 @@ module Principals
end
end
+ def rewrite_actor(from, to)
+ [::Notification].each do |klass|
+ klass.where(actor_id: from.id).update_all(actor_id: to.id)
+ end
+ end
+
def rewrite_assigned_to(from, to)
[WorkPackage].each do |klass|
klass.where(assigned_to_id: from.id).update_all(assigned_to_id: to.id)
diff --git a/app/workers/principals/delete_job.rb b/app/workers/principals/delete_job.rb
index 9e67fc1a510..90b4d5f7368 100644
--- a/app/workers/principals/delete_job.rb
+++ b/app/workers/principals/delete_job.rb
@@ -54,9 +54,14 @@ class Principals::DeleteJob < ApplicationJob
end
def delete_associated(principal)
+ delete_notifications(principal)
delete_private_queries(principal)
end
+ def delete_notifications(principal)
+ ::Notification.where(recipient: principal).delete_all
+ end
+
def delete_private_queries(principal)
::Query.where(user_id: principal.id, is_public: false).delete_all
CostQuery.where(user_id: principal.id, is_public: false).delete_all
diff --git a/spec/services/principals/replace_references_service_call_integration_spec.rb b/spec/services/principals/replace_references_service_call_integration_spec.rb
index 4fe0bc86ee6..2a54a9368a0 100644
--- a/spec/services/principals/replace_references_service_call_integration_spec.rb
+++ b/spec/services/principals/replace_references_service_call_integration_spec.rb
@@ -395,5 +395,23 @@ describe Principals::ReplaceReferencesService, '#call', type: :model do
end
end
end
+
+ context 'with Notification actor' do
+ let(:recipient) { FactoryBot.create :user }
+
+ it_behaves_like 'rewritten record',
+ :notification,
+ :actor_id do
+ let(:attributes) do
+ {
+ recipient_id: user.id,
+ resource_id: 1234,
+ resource_type: "'WorkPackage'",
+ created_at: 'NOW()',
+ updated_at: 'NOW()'
+ }
+ end
+ end
+ end
end
end
diff --git a/spec/workers/principals/delete_job_integration_spec.rb b/spec/workers/principals/delete_job_integration_spec.rb
index bb6f7919153..6e6fe66380e 100644
--- a/spec/workers/principals/delete_job_integration_spec.rb
+++ b/spec/workers/principals/delete_job_integration_spec.rb
@@ -193,6 +193,20 @@ describe Principals::DeleteJob, type: :model do
it { expect(Token::RSS.find_by(id: token.id)).to be_nil }
end
+ shared_examples_for 'notification handling' do
+ let(:notification) do
+ FactoryBot.create(:notification, recipient: principal)
+ end
+
+ before do
+ notification
+
+ job
+ end
+
+ it { expect(Notification.find_by(id: notification.id)).to be_nil }
+ end
+
shared_examples_for 'private query handling' do
let!(:query) do
FactoryBot.create(:private_query, user: principal)
@@ -360,6 +374,7 @@ describe Principals::DeleteJob, type: :model do
it_behaves_like 'member handling'
it_behaves_like 'watcher handling'
it_behaves_like 'token handling'
+ it_behaves_like 'notification handling'
it_behaves_like 'private query handling'
it_behaves_like 'issue category handling'
it_behaves_like 'private cost_query handling'