mirror of
https://github.com/discourse/discourse.git
synced 2025-05-21 18:12:32 +08:00
DEV: Chat thread reply counter cache (#21050)
Similar to 22a55ef0ce42823b860f2d3432e4d7f14a00752f, this commit adds a replies_count to the Chat::Thread table, which is updated every 15 minutes via PeriodicalUpdates. This is done so the new thread indicator for the UI can show the count without intense serializer queries, but in future we likely want this to update more frequently.
This commit is contained in:
45
plugins/chat/spec/models/chat/thread_spec.rb
Normal file
45
plugins/chat/spec/models/chat/thread_spec.rb
Normal file
@ -0,0 +1,45 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Chat::Thread do
|
||||
describe ".ensure_consistency!" do
|
||||
fab!(:channel) { Fabricate(:category_channel) }
|
||||
fab!(:thread_1) { Fabricate(:chat_thread, channel: channel) }
|
||||
fab!(:thread_2) { Fabricate(:chat_thread, channel: channel) }
|
||||
fab!(:thread_3) { Fabricate(:chat_thread, channel: channel) }
|
||||
|
||||
before do
|
||||
Fabricate(:chat_message, chat_channel: channel, thread: thread_1)
|
||||
Fabricate(:chat_message, chat_channel: channel, thread: thread_1)
|
||||
Fabricate(:chat_message, chat_channel: channel, thread: thread_1)
|
||||
|
||||
Fabricate(:chat_message, chat_channel: channel, thread: thread_2)
|
||||
Fabricate(:chat_message, chat_channel: channel, thread: thread_2)
|
||||
Fabricate(:chat_message, chat_channel: channel, thread: thread_2)
|
||||
Fabricate(:chat_message, chat_channel: channel, thread: thread_2)
|
||||
|
||||
Fabricate(:chat_message, chat_channel: channel, thread: thread_3)
|
||||
end
|
||||
|
||||
describe "updating replies_count for all threads" do
|
||||
it "counts correctly and does not include the original message" do
|
||||
described_class.ensure_consistency!
|
||||
expect(thread_1.reload.replies_count).to eq(3)
|
||||
expect(thread_2.reload.replies_count).to eq(4)
|
||||
expect(thread_3.reload.replies_count).to eq(1)
|
||||
end
|
||||
|
||||
it "does not count deleted messages" do
|
||||
thread_1.chat_messages.last.trash!
|
||||
described_class.ensure_consistency!
|
||||
expect(thread_1.reload.replies_count).to eq(2)
|
||||
end
|
||||
|
||||
it "sets the replies count to 0 if all the messages but the original message are deleted" do
|
||||
thread_1.replies.delete_all
|
||||
|
||||
described_class.ensure_consistency!
|
||||
expect(thread_1.reload.replies_count).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user