mirror of
https://github.com/discourse/discourse.git
synced 2026-09-05 04:40:41 -05:00
Refactor TopicView - extract filter method to FilterBestPosts
Still much to do but this should reduce the complexity scores for TopicView
This commit is contained in:
+30
-58
@@ -1,5 +1,6 @@
|
||||
require_dependency 'guardian'
|
||||
require_dependency 'topic_query'
|
||||
require_dependency 'filter_best_posts'
|
||||
require_dependency 'summarize'
|
||||
|
||||
class TopicView
|
||||
@@ -10,32 +11,17 @@ class TopicView
|
||||
def initialize(topic_id, user=nil, options={})
|
||||
@user = user
|
||||
@topic = find_topic(topic_id)
|
||||
|
||||
raise Discourse::NotFound if @topic.blank?
|
||||
|
||||
@guardian = Guardian.new(@user)
|
||||
|
||||
# Special case: If the topic is private and the user isn't logged in, ask them
|
||||
# to log in!
|
||||
if @topic.present? && @topic.private_message? && @user.blank?
|
||||
raise Discourse::NotLoggedIn.new
|
||||
end
|
||||
guardian.ensure_can_see!(@topic)
|
||||
check_and_raise_exceptions
|
||||
|
||||
@post_number, @page = options[:post_number], options[:page].to_i
|
||||
@page = 1 if @page == 0
|
||||
|
||||
@limit = options[:limit] || SiteSetting.posts_per_page;
|
||||
|
||||
@filtered_posts = @topic.posts
|
||||
@filtered_posts = @filtered_posts.with_deleted.without_nuked_users if user.try(:staff?)
|
||||
@filtered_posts = @filtered_posts.best_of if options[:filter] == 'best_of'
|
||||
@filtered_posts = @filtered_posts.where('posts.post_type <> ?', Post.types[:moderator_action]) if options[:best].present?
|
||||
|
||||
if options[:username_filters].present?
|
||||
usernames = options[:username_filters].map{|u| u.downcase}
|
||||
@filtered_posts = @filtered_posts.where('post_number = 1 or user_id in (select u.id from users u where username_lower in (?))', usernames)
|
||||
end
|
||||
@username_filters = options[:username_filters]
|
||||
@best = options[:best]
|
||||
@filter = options[:filter]
|
||||
setup_filtered_posts
|
||||
|
||||
@initial_load = true
|
||||
@index_reverse = false
|
||||
@@ -165,44 +151,9 @@ class TopicView
|
||||
|
||||
|
||||
def filter_best(max, opts={})
|
||||
if opts[:min_replies] && @topic.posts_count < opts[:min_replies] + 1
|
||||
@posts = []
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
if opts[:only_moderator_liked]
|
||||
liked_by_moderators = PostAction.where(post_id: @filtered_posts.pluck(:id), post_action_type_id: PostActionType.types[:like])
|
||||
liked_by_moderators = liked_by_moderators.joins(:user).where('users.moderator').pluck(:post_id)
|
||||
@filtered_posts = @filtered_posts.where(id: liked_by_moderators)
|
||||
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)
|
||||
|
||||
min_trust_level = opts[:min_trust_level]
|
||||
if min_trust_level && min_trust_level > 0
|
||||
|
||||
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]
|
||||
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
|
||||
filter = FilterBestPosts.new(@topic, @filtered_posts, max, opts)
|
||||
@posts = filter.posts
|
||||
@filtered_posts = filter.filtered_posts
|
||||
end
|
||||
|
||||
def read?(post_number)
|
||||
@@ -321,4 +272,25 @@ class TopicView
|
||||
finder = finder.with_deleted if @user.try(:staff?)
|
||||
finder.first
|
||||
end
|
||||
|
||||
def setup_filtered_posts
|
||||
@filtered_posts = @topic.posts
|
||||
@filtered_posts = @filtered_posts.with_deleted.without_nuked_users if @user.try(:staff?)
|
||||
@filtered_posts = @filtered_posts.best_of if @filter == 'best_of'
|
||||
@filtered_posts = @filtered_posts.where('posts.post_type <> ?', Post.types[:moderator_action]) if @best.present?
|
||||
return unless @username_filters.present?
|
||||
usernames = @username_filters.map{|u| u.downcase}
|
||||
@filtered_posts = @filtered_posts.where('post_number = 1 or user_id in (select u.id from users u where username_lower in (?))', usernames)
|
||||
end
|
||||
|
||||
def check_and_raise_exceptions
|
||||
raise Discourse::NotFound if @topic.blank?
|
||||
# Special case: If the topic is private and the user isn't logged in, ask them
|
||||
# to log in!
|
||||
if @topic.present? && @topic.private_message? && @user.blank?
|
||||
raise Discourse::NotLoggedIn.new
|
||||
end
|
||||
guardian.ensure_can_see!(@topic)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user