From 74051a2df426271b81c765f8b53ea36876488a27 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 8 Jan 2015 16:44:27 -0500 Subject: [PATCH] Allow plugins to build topic lists --- app/controllers/list_controller.rb | 28 ++++------------ lib/topic_list_responder.rb | 24 ++++++++++++++ lib/topic_query.rb | 53 +++++++++++++++--------------- 3 files changed, 57 insertions(+), 48 deletions(-) create mode 100644 lib/topic_list_responder.rb diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index 10e14423c..bea0f7c66 100644 --- a/app/controllers/list_controller.rb +++ b/app/controllers/list_controller.rb @@ -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 diff --git a/lib/topic_list_responder.rb b/lib/topic_list_responder.rb new file mode 100644 index 000000000..f1f1af0db --- /dev/null +++ b/lib/topic_list_responder.rb @@ -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 + diff --git a/lib/topic_query.rb b/lib/topic_query.rb index fa8f89ce0..0a22a00bd 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -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