Merge pull request #1397 from Retrospring/fix-user-content-deletion

Fix user content deletion
This commit is contained in:
Georg Gadinger 2023-10-18 22:18:40 +02:00 committed by GitHub
commit 182654dc8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 7 deletions

View File

@ -14,15 +14,16 @@ class Inbox < ApplicationRecord
end
after_create do
user.touch(:inbox_updated_at)
user.touch(:inbox_updated_at) # rubocop:disable Rails/SkipsModelValidations
end
after_update do
user.touch(:inbox_updated_at)
user.touch(:inbox_updated_at) # rubocop:disable Rails/SkipsModelValidations
end
after_destroy do
user.touch(:inbox_updated_at)
# user might not exist at this point (account deleted, records are cleaned up async)
user&.touch(:inbox_updated_at) # rubocop:disable Rails/SkipsModelValidations
end
def answer(answer_content, user)
@ -49,7 +50,7 @@ class Inbox < ApplicationRecord
user.profile.anon_display_name || APP_CONFIG["anonymous_name"]
else
question.user.profile.safe_name
end
end,
),
icon: notification_icon,
body: question.content.truncate(Question::SHORT_QUESTION_MAX_LENGTH),

View File

@ -5,15 +5,16 @@ class Notification < ApplicationRecord
belongs_to :target, polymorphic: true
after_create do
recipient.touch(:notifications_updated_at)
recipient.touch(:notifications_updated_at) # rubocop:disable Rails/SkipsModelValidations
end
after_update do
recipient.touch(:notifications_updated_at)
recipient.touch(:notifications_updated_at) # rubocop:disable Rails/SkipsModelValidations
end
after_destroy do
recipient.touch(:notifications_updated_at)
# recipient might not exist at this point (account deleted, records are cleaned up async)
recipient&.touch(:notifications_updated_at) # rubocop:disable Rails/SkipsModelValidations
end
class << self

26
spec/models/inbox_spec.rb Normal file
View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
require "rails_helper"
describe Inbox, type: :model do
describe "associations" do
it { should belong_to(:user) }
it { should belong_to(:question) }
end
describe "before_destroy" do
let(:user) { FactoryBot.create(:user) }
let(:question) { FactoryBot.create(:question, author_is_anonymous: true) }
it "does not fail if the user wants to delete their account" do
Inbox.create(user:, question:)
# this deletes the User record and enqueues the deletion of all
# associated records in sidekiq
user.destroy!
# so let's drain the queues
expect { Sidekiq::Worker.drain_all }.not_to raise_error
end
end
end

View File

@ -0,0 +1,26 @@
# frozen_string_literal: true
require "rails_helper"
describe Notification, type: :model do
describe "associations" do
it { should belong_to(:recipient) }
it { should belong_to(:target) }
end
describe "before_destroy" do
let(:user) { FactoryBot.create(:user) }
let(:answer) { FactoryBot.create(:answer, user: FactoryBot.create(:user)) }
it "does not fail if the user wants to delete their account" do
Notification::QuestionAnswered.create(recipient: user, target: answer)
# this deletes the User record and enqueues the deletion of all
# associated records in sidekiq
user.destroy!
# so let's drain the queues
expect { Sidekiq::Worker.drain_all }.not_to raise_error
end
end
end