discourse/plugins/chat/spec/services/chat_message_destroyer_spec.rb
Roman Rizzi 07a9163ea8
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.
2022-11-28 13:32:57 -03:00

51 lines
1.6 KiB
Ruby

# frozen_string_literal: true
RSpec.describe ChatMessageDestroyer do
describe "#destroy_in_batches" do
fab!(:message_1) { Fabricate(:chat_message) }
fab!(:user_1) { Fabricate(:user) }
it "resets last_read_message_id from memberships" do
membership =
UserChatChannelMembership.create!(
user: user_1,
chat_channel: message_1.chat_channel,
last_read_message: message_1,
following: true,
desktop_notification_level: 2,
mobile_notification_level: 2,
)
described_class.new.destroy_in_batches(ChatMessage.where(id: message_1.id))
expect(membership.reload.last_read_message_id).to be_nil
end
it "deletes flags associated to deleted chat messages" do
guardian = Guardian.new(Discourse.system_user)
Chat::ChatReviewQueue.new.flag_message(
message_1,
guardian,
ReviewableScore.types[:off_topic],
)
reviewable = ReviewableChatMessage.last
expect(reviewable).to be_present
described_class.new.destroy_in_batches(ChatMessage.where(id: message_1.id))
expect { message_1.reload }.to raise_exception(ActiveRecord::RecordNotFound)
expect { reviewable.reload }.to raise_exception(ActiveRecord::RecordNotFound)
end
it "doesn't delete other messages" do
message_2 = Fabricate(:chat_message, chat_channel: message_1.chat_channel)
described_class.new.destroy_in_batches(ChatMessage.where(id: message_1.id))
expect { message_1.reload }.to raise_exception(ActiveRecord::RecordNotFound)
expect(message_2.reload).to be_present
end
end
end