logic to bypass trust level filter for high scoring posts

This commit is contained in:
Sam 2013-07-03 10:21:10 +10:00
parent ce0a7739cf
commit 4d4a5735d2
3 changed files with 26 additions and 3 deletions

View File

@ -51,7 +51,7 @@ class TopicsController < ApplicationController
def wordpress
params.require(:best)
params.require(:topic_id)
params.permit(:min_trust_level, :min_score, :min_replies)
params.permit(:min_trust_level, :min_score, :min_replies, :bypass_trust_level_score)
@topic_view = TopicView.new(
params[:topic_id],
@ -59,7 +59,8 @@ class TopicsController < ApplicationController
best: params[:best].to_i,
min_trust_level: params[:min_trust_level].nil? ? 1 : params[:min_trust_level].to_i,
min_score: params[:min_score].to_i,
min_replies: params[:min_replies].to_i
min_replies: params[:min_replies].to_i,
bypass_trust_level_score: params[:bypass_trust_level_score].to_i # safe cause 0 means ignore
)
anonymous_etag(@topic_view.topic) do

View File

@ -161,7 +161,17 @@ class TopicView
min_trust_level = opts[:min_trust_level]
if min_trust_level && min_trust_level > 0
@posts = @posts.where('COALESCE(users.trust_level,0) >= ?', min_trust_level)
bypass_trust_level_score = opts[:bypass_trust_level_score]
if bypass_trust_level_score && bypass_trust_level_score > 0
@posts = @posts.where('COALESCE(users.trust_level,0) >= ? OR posts.score >= ?',
min_trust_level,
bypass_trust_level_score
)
else
@posts = @posts.where('COALESCE(users.trust_level,0) >= ?', min_trust_level)
end
end
min_score = opts[:min_score]
@ -169,6 +179,7 @@ class TopicView
@posts = @posts.where('posts.score >= ?', min_score)
end
@posts = @posts.to_a
@posts.sort!{|a,b| a.post_number <=> b.post_number}
@posts

View File

@ -24,6 +24,7 @@ describe TopicView do
let!(:p3) { Fabricate(:post, topic: topic, user: first_poster, percent_rank: 0 )}
it "it can find the best responses" do
best2 = TopicView.new(topic.id, coding_horror, best: 2)
best2.posts.count.should == 2
best2.posts[0].id.should == p2.id
@ -50,6 +51,16 @@ describe TopicView do
# should filter out everything if min replies not met
best = TopicView.new(topic.id, nil, best: 99, min_replies: 99)
best.posts.count.should == 0
# should punch through posts if the score is high enough
p2.update_column(:score, 100)
best = TopicView.new(topic.id, nil, best: 99, bypass_trust_level_score: 100, min_trust_level: coding_horror.trust_level + 1)
best.posts.count.should == 1
# 0 means ignore
best = TopicView.new(topic.id, nil, best: 99, bypass_trust_level_score: 0, min_trust_level: coding_horror.trust_level + 1)
best.posts.count.should == 0
end