Allow plugins to build topic lists

This commit is contained in:
Robin Ward 2015-01-08 16:44:27 -05:00
parent 6b602d48b9
commit 74051a2df4
3 changed files with 57 additions and 48 deletions

View file

@ -1,4 +1,7 @@
require_dependency 'topic_list_responder'
class ListController < ApplicationController
include TopicListResponder
skip_before_filter :check_xhr
@ -75,7 +78,7 @@ class ListController < ApplicationController
end
end
respond(list)
respond_with_list(list)
end
define_method("category_#{filter}") do
@ -118,7 +121,7 @@ class ListController < ApplicationController
url_prefix = "topics" unless action == :topics_by
list.more_topics_url = url_for(construct_next_url_with(list_opts, url_prefix))
list.prev_topics_url = url_for(construct_prev_url_with(list_opts, url_prefix))
respond(list)
respond_with_list(list)
end
end
@ -168,7 +171,7 @@ class ListController < ApplicationController
@title = I18n.t("js.filters.top.#{period}.title")
end
respond(list)
respond_with_list(list)
end
define_method("category_top_#{period}") do
@ -186,25 +189,6 @@ class ListController < ApplicationController
protected
def respond(list)
discourse_expires_in 1.minute
list.draft_key = Draft::NEW_TOPIC
list.draft_sequence = DraftSequence.current(current_user, Draft::NEW_TOPIC)
list.draft = Draft.get(current_user, list.draft_key, list.draft_sequence) if current_user
respond_to do |format|
format.html do
@list = list
store_preloaded(list.preload_key, MultiJson.dump(TopicListSerializer.new(list, scope: guardian)))
render 'list'
end
format.json do
render_serialized(list, TopicListSerializer)
end
end
end
def next_page_params(opts = nil)
page_params(opts).merge(page: params[:page].to_i + 1)
end

View file

@ -0,0 +1,24 @@
# Helps us respond with a topic list from a controller
module TopicListResponder
def respond_with_list(list)
discourse_expires_in 1.minute
list.draft_key = Draft::NEW_TOPIC
list.draft_sequence = DraftSequence.current(current_user, Draft::NEW_TOPIC)
list.draft = Draft.get(current_user, list.draft_key, list.draft_sequence) if current_user
respond_to do |format|
format.html do
@list = list
store_preloaded(list.preload_key, MultiJson.dump(TopicListSerializer.new(list, scope: guardian)))
render 'list'
end
format.json do
render_serialized(list, TopicListSerializer)
end
end
end
end

View file

@ -150,19 +150,39 @@ class TopicQuery
.where("COALESCE(tu.notification_level, :regular) >= :tracking", regular: TopicUser.notification_levels[:regular], tracking: TopicUser.notification_levels[:tracking])
end
def create_list(filter, options={}, topics = nil)
topics ||= default_results(options)
topics = yield(topics) if block_given?
list = TopicList.new(filter, @user, topics, options.merge(@options))
list.per_page = per_page_setting
list
end
def latest_results(options={})
result = default_results(options)
result = remove_muted_categories(result, @user, exclude: options[:category])
result
end
def unread_results(options={})
result = TopicQuery.unread_filter(default_results(options.reverse_merge(:unordered => true)))
.order('CASE WHEN topics.user_id = tu.user_id THEN 1 ELSE 2 END')
suggested_ordering(result, options)
end
def new_results(options={})
result = TopicQuery.new_filter(default_results(options.reverse_merge(:unordered => true)), @user.treat_as_new_topic_start_date)
result = remove_muted_categories(result, @user, exclude: options[:category])
suggested_ordering(result, options)
end
protected
def per_page_setting
@options[:slow_platform] ? 15 : 30
end
def create_list(filter, options={}, topics = nil)
topics ||= default_results(options)
topics = yield(topics) if block_given?
list = TopicList.new(filter, @user, topics, options.merge(@options))
list.per_page = per_page_setting
list
end
def private_messages_for(user)
options = @options
@ -349,12 +369,6 @@ class TopicQuery
result
end
def latest_results(options={})
result = default_results(options)
result = remove_muted_categories(result, @user, exclude: options[:category])
result
end
def remove_muted_categories(list, user, opts=nil)
category_id = get_category_id(opts[:exclude]) if opts
if user
@ -376,19 +390,6 @@ class TopicQuery
end
def unread_results(options={})
result = TopicQuery.unread_filter(default_results(options.reverse_merge(:unordered => true)))
.order('CASE WHEN topics.user_id = tu.user_id THEN 1 ELSE 2 END')
suggested_ordering(result, options)
end
def new_results(options={})
result = TopicQuery.new_filter(default_results(options.reverse_merge(:unordered => true)), @user.treat_as_new_topic_start_date)
result = remove_muted_categories(result, @user, exclude: options[:category])
suggested_ordering(result, options)
end
def random_suggested(topic, count, excluded_topic_ids=[])
result = default_results(unordered: true, per_page: count).where(closed: false, archived: false)
excluded_topic_ids += Category.pluck(:topic_id).compact