mirror of
https://github.com/discourse/discourse.git
synced 2026-08-19 01:14:56 -05:00
FEATURE: Deleting a user with their posts also deletes chat messages. (#19194)
This commit introduce a new API for registering callbacks, which we'll execute when a user gets destroyed, and the `delete_posts` opt is true. The chat plugin registers one callback and queues a job to destroy every message from that user in batches.
This commit is contained in:
@@ -109,40 +109,6 @@ describe Jobs::DeleteOldChatMessages do
|
||||
SiteSetting.chat_channel_retention_days = 800
|
||||
expect { described_class.new.execute }.not_to change { ChatMessage.in_public_channel.count }
|
||||
end
|
||||
|
||||
it "resets last_read_message_id from memberships" do
|
||||
SiteSetting.chat_channel_retention_days = 20
|
||||
membership =
|
||||
UserChatChannelMembership.create!(
|
||||
user: Fabricate(:user),
|
||||
chat_channel: public_channel,
|
||||
last_read_message_id: public_days_old_30.id,
|
||||
following: true,
|
||||
desktop_notification_level: 2,
|
||||
mobile_notification_level: 2,
|
||||
)
|
||||
described_class.new.execute
|
||||
|
||||
expect(membership.reload.last_read_message_id).to be_nil
|
||||
end
|
||||
|
||||
it "deletes flags associated to deleted chat messages" do
|
||||
SiteSetting.chat_channel_retention_days = 10
|
||||
guardian = Guardian.new(Discourse.system_user)
|
||||
Chat::ChatReviewQueue.new.flag_message(
|
||||
public_days_old_20,
|
||||
guardian,
|
||||
ReviewableScore.types[:off_topic],
|
||||
)
|
||||
|
||||
reviewable = ReviewableChatMessage.last
|
||||
expect(reviewable).to be_present
|
||||
|
||||
described_class.new.execute
|
||||
|
||||
expect { public_days_old_20.reload }.to raise_exception(ActiveRecord::RecordNotFound)
|
||||
expect { reviewable.reload }.to raise_exception(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
end
|
||||
|
||||
describe "dm channels" do
|
||||
@@ -166,21 +132,5 @@ describe Jobs::DeleteOldChatMessages do
|
||||
SiteSetting.chat_dm_retention_days = 800
|
||||
expect { described_class.new.execute }.not_to change { ChatMessage.in_dm_channel.count }
|
||||
end
|
||||
|
||||
it "resets last_read_message_id from memberships" do
|
||||
SiteSetting.chat_dm_retention_days = 20
|
||||
membership =
|
||||
UserChatChannelMembership.create!(
|
||||
user: Fabricate(:user),
|
||||
chat_channel: dm_channel,
|
||||
last_read_message_id: dm_days_old_30.id,
|
||||
following: true,
|
||||
desktop_notification_level: 2,
|
||||
mobile_notification_level: 2,
|
||||
)
|
||||
described_class.new.execute
|
||||
|
||||
expect(membership.reload.last_read_message_id).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Jobs::DeleteUserMessages do
|
||||
describe "#execute" do
|
||||
fab!(:user_1) { Fabricate(:user) }
|
||||
fab!(:channel) { Fabricate(:chat_channel) }
|
||||
fab!(:chat_message) { Fabricate(:chat_message, chat_channel: channel, user: user_1) }
|
||||
|
||||
it "deletes messages from the user" do
|
||||
subject.execute(user_id: user_1)
|
||||
|
||||
expect { chat_message.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
||||
end
|
||||
|
||||
it "doesn't delete messages from other users" do
|
||||
user_2 = Fabricate(:user)
|
||||
user_2_message = Fabricate(:chat_message, chat_channel: channel, user: user_2)
|
||||
|
||||
subject.execute(user_id: user_1)
|
||||
|
||||
expect(user_2_message.reload).to be_present
|
||||
end
|
||||
|
||||
it "deletes trashed messages" do
|
||||
chat_message.trash!
|
||||
|
||||
subject.execute(user_id: user_1)
|
||||
|
||||
expect(ChatMessage.with_deleted.where(id: chat_message.id)).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user