0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/spec/models/topic_posters_summary_spec.rb
Elliot Temple a9f4ec5367
FIX: Select better featured posters (#39691)
There was a bug where the frequent posters were reordered by the date of
their most recent post in the topic, with older posts preferred. This
hid posters who should have been displayed (e.g. someone with the most
posts and the second most recent post). This PR fixes the bug and
improves the selection with better visibility for recent discussion and
new participants.

Bug report showing the issue with screenshots:
https://meta.discourse.org/t/frequent-posters-shown-on-latest-page/235281

New display logic: The topic list shows up to five posters in this
order: the OP, two frequent posters, and two recent posters. Recent
posters are excluded from frequent posters, and the OP is excluded from
both. The latest poster is handled separately and replaces a recent
poster unless the OP is the latest poster.

---------

Co-authored-by: awesomerobot <kris.aubuchon@discourse.org>
2026-07-24 16:10:46 -04:00

109 lines
3.4 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe TopicPostersSummary do
describe "#summary" do
let!(:topic_creator) { Fabricate(:user) }
context "when there are no other posters" do
let!(:topic) { Fabricate(:topic, user: topic_creator) }
let!(:summary) { described_class.new(topic).summary }
it "contains only the topic creator" do
expect(summary.count).to eq 1
summary.first.tap do |topic_poster|
expect(topic_poster.user).to eq topic_creator
expect(topic_poster.description).to eq(
[I18n.t(:original_poster), I18n.t(:most_recent_poster)].join(
I18n.t(:poster_description_joiner),
),
)
end
end
end
context "when the last poster is also the topic creator" do
fab!(:featured_user1, :user)
let!(:topic) do
Fabricate(
:topic,
user: topic_creator,
last_poster: topic_creator,
featured_user1: featured_user1,
)
end
let!(:summary) { described_class.new(topic).summary }
it "keeps the topic creator at the front of the summary" do
expect(summary.map(&:user)).to eq([topic_creator, featured_user1])
end
end
context "when the last poster is also the topic creator and the topic has many posters" do
fab!(:featured_user1, :user)
fab!(:featured_user2, :user)
fab!(:featured_user3, :user)
fab!(:featured_user4, :user)
let!(:topic) do
Fabricate(
:topic,
user: topic_creator,
last_poster: topic_creator,
featured_user1: featured_user1,
featured_user2: featured_user2,
featured_user3: featured_user3,
featured_user4: featured_user4,
)
end
let!(:summary) { described_class.new(topic).summary }
it "puts the recent poster after frequent posters" do
expect(summary.map(&:user)).to eq(
[topic_creator, featured_user1, featured_user2, featured_user3, featured_user4],
)
expect(summary[1].description).to eq(I18n.t(:frequent_poster))
expect(summary[2].description).to eq(I18n.t(:frequent_poster))
expect(summary[3].description).to eq(I18n.t(:recent_poster))
expect(summary[4].description).to eq(I18n.t(:recent_poster))
end
end
context "when the topic has many posters" do
let!(:last_poster) { Fabricate(:user) }
fab!(:featured_user1, :user)
fab!(:featured_user2, :user)
fab!(:featured_user3, :user)
fab!(:featured_user4, :user)
let!(:topic) do
Fabricate(
:topic,
user: topic_creator,
last_poster: last_poster,
featured_user1: featured_user1,
featured_user2: featured_user2,
featured_user3: featured_user3,
featured_user4: featured_user4,
)
end
let!(:summary) { described_class.new(topic).summary }
it "contains only five posters with latest poster at the end" do
expect(summary.map(&:user)).to eq(
[topic_creator, featured_user1, featured_user2, featured_user3, last_poster],
)
expect(summary[1].description).to eq(I18n.t(:frequent_poster))
expect(summary[2].description).to eq(I18n.t(:frequent_poster))
expect(summary[3].description).to eq(I18n.t(:recent_poster))
# If more than one user, attach the latest class
expect(summary.last.extras).to eq "latest"
end
end
end
end