add min replies, min score and min trust level params for wordpress

This commit is contained in:
Sam 2013-07-01 21:29:45 +10:00
parent 5d4760f91d
commit b92e912ac9
3 changed files with 46 additions and 7 deletions

View File

@ -52,10 +52,16 @@ class TopicsController < ApplicationController
def wordpress
params.require(:best)
params.require(:topic_id)
params.permit(:min_trust_level, :min_score, :min_replies)
@topic_view = TopicView.new(params[:topic_id], current_user, best: params[:best].to_i)
raise Discourse::NotFound if @topic_view.posts.blank?
@topic_view = TopicView.new(
params[:topic_id],
current_user,
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
)
anonymous_etag(@topic_view.topic) do
wordpress_serializer = TopicViewWordpressSerializer.new(@topic_view, scope: guardian, root: false)

View File

@ -96,7 +96,10 @@ class TopicView
return filter_posts_near(opts[:post_number].to_i) if opts[:post_number].present?
return filter_posts_before(opts[:posts_before].to_i) if opts[:posts_before].present?
return filter_posts_after(opts[:posts_after].to_i) if opts[:posts_after].present?
return filter_best(opts[:best]) if opts[:best].present?
if opts[:best].present?
return filter_best(opts[:best], opts)
end
filter_posts_paged(opts[:page].to_i)
end
@ -176,11 +179,28 @@ class TopicView
@posts = @posts.includes(:reply_to_user).includes(:topic).joins(:user).limit(@limit)
end
def filter_best(max)
def filter_best(max, opts={})
@index_offset = 0
@posts = @filtered_posts.order('percent_rank asc, sort_order asc').where("post_number > 1")
if opts[:min_replies] && @topic.posts_count < opts[:min_replies] + 1
@posts = []
return
end
@posts = @filtered_posts.order('percent_rank asc, sort_order asc')
.where("post_number > 1")
@posts = @posts.includes(:reply_to_user).includes(:topic).joins(:user).limit(max)
@posts = @posts.where('COALESCE(users.trust_level,0) > 0')
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)
end
min_score = opts[:min_score]
if min_score && min_score > 0
@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

@ -37,6 +37,19 @@ describe TopicView do
best.posts.count.should == 2
best.filtered_posts_count.should == 3
best.current_post_ids.should =~ [p2.id, p3.id]
# should get no results for trust level too low
best = TopicView.new(topic.id, nil, best: 99, min_trust_level: coding_horror.trust_level + 1)
best.posts.count.should == 0
# should filter out the posts with a score that is too low
best = TopicView.new(topic.id, nil, best: 99, min_score: 99)
best.posts.count.should == 0
# 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
end