FIX: `Post#summary` returning posts from other topics.

This commit is contained in:
Guo Xiang Tan 2018-06-21 12:00:54 +08:00
parent c5cc582549
commit 6ddd214476
2 changed files with 17 additions and 17 deletions

View File

@ -344,23 +344,22 @@ class Post < ActiveRecord::Base
order('sort_order desc, post_number desc')
end
def self.summary(topic_id = nil)
# PERF: if you pass in nil it is WAY slower
# pg chokes getting a reasonable plan
topic_id = topic_id ? topic_id.to_i : "posts.topic_id"
def self.summary(topic_id)
topic_id = topic_id.to_i
# percent rank has tons of ties
where(["post_number = 1 or id in (
SELECT p1.id
FROM posts p1
WHERE p1.percent_rank <= ? AND
p1.topic_id = #{topic_id}
ORDER BY p1.percent_rank
LIMIT ?
)",
SiteSetting.summary_percent_filter.to_f / 100.0,
SiteSetting.summary_max_results
])
where(topic_id: topic_id)
.where(["(post_number = 1) or id in (
SELECT p1.id
FROM posts p1
WHERE p1.percent_rank <= ? AND
p1.topic_id = #{topic_id}
ORDER BY p1.percent_rank
LIMIT ?
)",
SiteSetting.summary_percent_filter.to_f / 100.0,
SiteSetting.summary_max_results
])
end
def update_flagged_posts_count

View File

@ -805,12 +805,13 @@ describe Post do
let!(:p1) { Fabricate(:post, post_args.merge(score: 4, percent_rank: 0.33)) }
let!(:p2) { Fabricate(:post, post_args.merge(score: 10, percent_rank: 0.66)) }
let!(:p3) { Fabricate(:post, post_args.merge(score: 5, percent_rank: 0.99)) }
let!(:p4) { Fabricate(:post, percent_rank: 0.99) }
it "returns the OP and posts above the threshold in summary mode" do
SiteSetting.summary_percent_filter = 66
expect(Post.summary.order(:post_number)).to eq([p1, p2])
expect(Post.summary(topic.id).order(:post_number)).to eq([p1, p2])
expect(Post.summary(p4.topic.id)).to eq([p4])
end
end
context 'sort_order' do