UX: Update topics stats automatically (#17135)

Updates automatically data on the stats section of the topic.

It will update automatically the following information: likes, replies and last reply (timestamp and user)
This commit is contained in:
Sérgio Saquetim
2022-06-27 18:21:05 -03:00
committed by GitHub
parent 31a0bf11f2
commit 5840fb5c62
13 changed files with 438 additions and 17 deletions

View File

@@ -1826,10 +1826,41 @@ describe Post do
version: post.version
}
MessageBus.expects(:publish).once.with("/topic/#{topic.id}", message, is_a(Hash)) do |_, _, options|
options[:user_ids].sort == [user1.id, user2.id, user3.id].sort
messages = MessageBus.track_publish("/topic/#{topic.id}") do
post.publish_change_to_clients!(:created)
end
post.publish_change_to_clients!(:created)
created_message = messages.select { |msg| msg.data[:type] == :created }.first
expect(created_message).to be_present
expect(created_message.data).to eq(message)
expect(created_message.user_ids.sort).to eq([user1.id, user2.id, user3.id].sort)
stats_message = messages.select { |msg| msg.data[:type] == :created }.first
expect(stats_message).to be_present
expect(stats_message.user_ids.sort).to eq([user1.id, user2.id, user3.id].sort)
end
it 'also publishes topic stats' do
messages = MessageBus.track_publish("/topic/#{topic.id}") do
post.publish_change_to_clients!(:created)
end
stats_message = messages.select { |msg| msg.data[:type] == :stats }.first
expect(stats_message).to be_present
end
it 'skips publishing topic stats when requested' do
messages = MessageBus.track_publish("/topic/#{topic.id}") do
post.publish_change_to_clients!(:anything, { skip_topic_stats: true })
end
stats_message = messages.select { |msg| msg.data[:type] == :stats }.first
expect(stats_message).to be_blank
# ensure that :skip_topic_stats did not get merged with the message
other_message = messages.select { |msg| msg.data[:type] == :anything }.first
expect(other_message).to be_present
expect(other_message.data.key?(:skip_topic_stats)).to be_falsey
end
end

View File

@@ -3051,4 +3051,53 @@ describe Topic do
expect(topic.reload.cannot_permanently_delete_reason(Fabricate(:admin))).to eq(nil)
end
end
describe "#publish_stats_to_clients!" do
fab!(:user1) { Fabricate(:user) }
fab!(:user2) { Fabricate(:user) }
fab!(:topic) { Fabricate(:topic, user: user1) }
fab!(:post1) { Fabricate(:post, topic: topic, user: user1) }
fab!(:post2) { Fabricate(:post, topic: topic, user: user2) }
fab!(:like1) { Fabricate(:like, post: post1, user: user2) }
it "it is triggered when a post publishes a message of type :liked or :unliked" do
[:liked, :unliked].each do |action|
messages = MessageBus.track_publish("/topic/#{topic.id}") do
post1.publish_change_to_clients!(action)
end
stats_message = messages.select { |msg| msg.data[:type] == :stats }.first
expect(stats_message).to be_present
expect(stats_message.data[:like_count]).to eq(topic.like_count)
end
end
it "it is triggered when a post publishes a message of type :created, :destroyed, :deleted, :recovered" do
freeze_time Date.today
[:created, :destroyed, :deleted, :recovered].each do |action|
messages = MessageBus.track_publish("/topic/#{topic.id}") do
post1.publish_change_to_clients!(action)
end
stats_message = messages.select { |msg| msg.data[:type] == :stats }.first
expect(stats_message).to be_present
expect(stats_message.data[:posts_count]).to eq(topic.posts_count)
expect(stats_message.data[:last_posted_at]).to eq(topic.last_posted_at.as_json)
expect(stats_message.data[:last_poster]).to eq(BasicUserSerializer.new(topic.last_poster, root: false).as_json)
end
end
it "it is not triggered when a post publishes an unhandled kind of message" do
[:unhandled, :unknown, :dont_care].each do |action|
messages = MessageBus.track_publish("/topic/#{topic.id}") do
post1.publish_change_to_clients!(action)
end
stats_message = messages.select { |msg| msg.data[:type] == :stats }.first
expect(stats_message).to be_blank
end
end
end
end