2013-02-06 03:16:51 +08:00
|
|
|
class ListController < ApplicationController
|
|
|
|
|
2014-01-14 08:02:14 +08:00
|
|
|
before_filter :ensure_logged_in, except: [
|
|
|
|
:topics_by,
|
|
|
|
# anonymous filters
|
|
|
|
Discourse.anonymous_filters, Discourse.anonymous_filters.map { |f| "#{f}_feed".to_sym },
|
|
|
|
# category
|
|
|
|
:category, :category_feed,
|
|
|
|
# top
|
|
|
|
:top_lists, TopTopic.periods.map { |p| "top_#{p}".to_sym }
|
|
|
|
].flatten
|
|
|
|
|
2013-06-20 10:11:14 +08:00
|
|
|
before_filter :set_category, only: [:category, :category_feed]
|
2013-02-06 03:16:51 +08:00
|
|
|
skip_before_filter :check_xhr
|
|
|
|
|
|
|
|
# Create our filters
|
2013-12-24 07:50:36 +08:00
|
|
|
Discourse.filters.each do |filter|
|
2013-02-07 23:45:24 +08:00
|
|
|
define_method(filter) do
|
2013-06-20 10:11:14 +08:00
|
|
|
list_opts = build_topic_list_options
|
2013-08-25 01:38:02 +08:00
|
|
|
user = list_target_user
|
2013-08-22 07:18:54 +08:00
|
|
|
list = TopicQuery.new(user, list_opts).public_send("list_#{filter}")
|
2013-10-19 21:21:17 +08:00
|
|
|
list.more_topics_url = construct_url_with(filter, list_opts)
|
2013-12-24 07:50:36 +08:00
|
|
|
if Discourse.anonymous_filters.include?(filter)
|
2013-10-12 00:35:12 +08:00
|
|
|
@description = SiteSetting.site_description
|
|
|
|
@rss = filter
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
respond(list)
|
2013-02-07 23:45:24 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-12-24 07:50:36 +08:00
|
|
|
Discourse.anonymous_filters.each do |filter|
|
2013-07-06 04:49:06 +08:00
|
|
|
define_method("#{filter}_feed") do
|
2013-10-16 13:39:18 +08:00
|
|
|
discourse_expires_in 1.minute
|
|
|
|
|
|
|
|
@title = "#{filter.capitalize} Topics"
|
|
|
|
@link = "#{Discourse.base_url}/#{filter}"
|
|
|
|
@description = I18n.t("rss_description.#{filter}")
|
|
|
|
@atom_link = "#{Discourse.base_url}/#{filter}.rss"
|
2013-10-22 22:30:25 +08:00
|
|
|
@topic_list = TopicQuery.new.public_send("list_#{filter}")
|
2013-12-24 07:50:36 +08:00
|
|
|
|
2013-10-16 13:39:18 +08:00
|
|
|
render 'list', formats: [:rss]
|
2013-07-06 04:49:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-19 21:21:17 +08:00
|
|
|
[:topics_by, :private_messages, :private_messages_sent, :private_messages_unread].each do |action|
|
|
|
|
define_method("#{action}") do
|
|
|
|
list_opts = build_topic_list_options
|
|
|
|
target_user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_see_private_messages!(target_user.id) unless action == :topics_by
|
|
|
|
list = generate_list_for(action.to_s, target_user, list_opts)
|
|
|
|
url_prefix = "topics" unless action == :topics_by
|
|
|
|
url = construct_url_with(action, list_opts, url_prefix)
|
|
|
|
list.more_topics_url = url_for(url)
|
|
|
|
respond(list)
|
|
|
|
end
|
2013-08-31 00:32:05 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def category
|
2013-12-14 06:18:28 +08:00
|
|
|
category_response
|
|
|
|
end
|
|
|
|
|
|
|
|
def category_none
|
|
|
|
category_response(no_subcategories: true)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-28 11:36:12 +08:00
|
|
|
def category_feed
|
|
|
|
guardian.ensure_can_see!(@category)
|
2013-10-16 13:39:18 +08:00
|
|
|
discourse_expires_in 1.minute
|
|
|
|
|
|
|
|
@title = @category.name
|
|
|
|
@link = "#{Discourse.base_url}/category/#{@category.slug}"
|
|
|
|
@description = "#{I18n.t('topics_in_category', category: @category.name)} #{@category.description}"
|
|
|
|
@atom_link = "#{Discourse.base_url}/category/#{@category.slug}.rss"
|
|
|
|
@topic_list = TopicQuery.new.list_new_in_category(@category)
|
|
|
|
render 'list', formats: [:rss]
|
2013-02-28 11:36:12 +08:00
|
|
|
end
|
|
|
|
|
2013-03-28 04:17:49 +08:00
|
|
|
def popular_redirect
|
|
|
|
# We've renamed popular to latest. Use a redirect until we're sure we can
|
|
|
|
# safely remove this.
|
|
|
|
redirect_to latest_path, :status => 301
|
|
|
|
end
|
|
|
|
|
2014-01-14 08:02:14 +08:00
|
|
|
def top_lists
|
|
|
|
discourse_expires_in 1.minute
|
|
|
|
|
2013-12-28 01:10:35 +08:00
|
|
|
top = generate_top_lists
|
2013-12-24 07:50:36 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
@top = top
|
2014-01-14 08:02:14 +08:00
|
|
|
store_preloaded('top_lists', MultiJson.dump(TopListSerializer.new(top, scope: guardian, root: false)))
|
2013-12-24 07:50:36 +08:00
|
|
|
render 'top'
|
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
render json: MultiJson.dump(TopListSerializer.new(top, scope: guardian, root: false))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-14 08:02:14 +08:00
|
|
|
TopTopic.periods.each do |period|
|
|
|
|
define_method("top_#{period}") do
|
|
|
|
options = build_topic_list_options
|
|
|
|
user = list_target_user
|
|
|
|
list = TopicQuery.new(user, options).public_send("list_top_#{period}")
|
|
|
|
list.more_topics_url = construct_url_with(period, options, "top")
|
|
|
|
respond(list)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
protected
|
|
|
|
|
2013-12-14 06:18:28 +08:00
|
|
|
def category_response(extra_opts=nil)
|
|
|
|
list_opts = build_topic_list_options
|
|
|
|
list_opts.merge!(extra_opts) if extra_opts
|
|
|
|
query = TopicQuery.new(current_user, list_opts)
|
|
|
|
list = query.list_latest
|
|
|
|
list.more_topics_url = construct_url_with(:latest, list_opts)
|
|
|
|
respond(list)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def respond(list)
|
2013-12-24 07:50:36 +08:00
|
|
|
discourse_expires_in 1.minute
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
list.draft_key = Draft::NEW_TOPIC
|
|
|
|
list.draft_sequence = DraftSequence.current(current_user, Draft::NEW_TOPIC)
|
2013-12-27 07:35:53 +08:00
|
|
|
list.draft = Draft.get(current_user, list.draft_key, list.draft_sequence) if current_user
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2013-02-07 23:45:24 +08:00
|
|
|
@list = list
|
2013-02-06 03:16:51 +08:00
|
|
|
store_preloaded('topic_list', MultiJson.dump(TopicListSerializer.new(list, scope: guardian)))
|
|
|
|
render 'list'
|
|
|
|
end
|
|
|
|
format.json do
|
|
|
|
render_serialized(list, TopicListSerializer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-11-15 01:35:18 +08:00
|
|
|
def next_page_params(opts=nil)
|
|
|
|
opts = opts || {}
|
|
|
|
route_params = { format: 'json', page: params[:page].to_i + 1 }
|
|
|
|
route_params[:sort_order] = opts[:sort_order] if opts[:sort_order].present?
|
|
|
|
route_params[:sort_descending] = opts[:sort_descending] if opts[:sort_descending].present?
|
|
|
|
route_params
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-06-20 10:11:14 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_category
|
2013-08-23 03:46:17 +08:00
|
|
|
slug = params.fetch(:category)
|
2013-10-24 02:40:39 +08:00
|
|
|
parent_slug = params[:parent_category]
|
|
|
|
|
|
|
|
parent_category_id = nil
|
|
|
|
if parent_slug.present?
|
|
|
|
parent_category_id = Category.where(slug: parent_slug).pluck(:id).first ||
|
|
|
|
Category.where(id: parent_slug.to_i).pluck(:id).first
|
|
|
|
|
|
|
|
raise Discourse::NotFound.new if parent_category_id.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
@category = Category.where(slug: slug, parent_category_id: parent_category_id).includes(:featured_users).first ||
|
|
|
|
Category.where(id: slug.to_i, parent_category_id: parent_category_id).includes(:featured_users).first
|
2013-06-20 10:11:14 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_topic_list_options
|
|
|
|
# html format means we need to parse exclude category (aka filter) from the site options top menu
|
|
|
|
menu_items = SiteSetting.top_menu_items
|
|
|
|
menu_item = menu_items.select { |item| item.query_should_exclude_category?(action_name, params[:format]) }.first
|
|
|
|
|
|
|
|
# exclude_category = 1. from params / 2. parsed from top menu / 3. nil
|
2013-12-14 06:18:28 +08:00
|
|
|
result = {
|
2013-06-20 10:11:14 +08:00
|
|
|
page: params[:page],
|
|
|
|
topic_ids: param_to_integer_list(:topic_ids),
|
2013-10-24 18:05:06 +08:00
|
|
|
exclude_category: (params[:exclude_category] || menu_item.try(:filter)),
|
2013-11-12 08:35:57 +08:00
|
|
|
category: params[:category],
|
|
|
|
sort_order: params[:sort_order],
|
2013-12-14 06:18:28 +08:00
|
|
|
sort_descending: params[:sort_descending],
|
2014-01-13 11:40:21 +08:00
|
|
|
status: params[:status]
|
2013-06-20 10:11:14 +08:00
|
|
|
}
|
2013-12-14 06:18:28 +08:00
|
|
|
result[:no_subcategories] = true if params[:no_subcategories] == 'true'
|
|
|
|
result
|
2013-06-20 10:11:14 +08:00
|
|
|
end
|
2013-08-25 01:38:02 +08:00
|
|
|
|
|
|
|
def list_target_user
|
|
|
|
if params[:user_id] && guardian.is_staff?
|
|
|
|
User.find(params[:user_id].to_i)
|
|
|
|
else
|
|
|
|
current_user
|
|
|
|
end
|
|
|
|
end
|
2013-10-19 21:21:17 +08:00
|
|
|
|
|
|
|
def generate_list_for(action, target_user, opts)
|
2013-11-15 01:35:18 +08:00
|
|
|
TopicQuery.new(current_user, opts).send("list_#{action}", target_user)
|
2013-10-19 21:21:17 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def construct_url_with(action, opts, url_prefix=nil)
|
|
|
|
method = url_prefix.blank? ? "#{action}_path" : "#{url_prefix}_#{action}_path"
|
2013-11-15 01:35:18 +08:00
|
|
|
public_send(method, opts.merge(next_page_params(opts)))
|
2013-10-19 21:21:17 +08:00
|
|
|
end
|
2013-12-24 07:50:36 +08:00
|
|
|
|
2013-12-28 01:10:35 +08:00
|
|
|
def generate_top_lists
|
2013-12-24 07:50:36 +08:00
|
|
|
top = {}
|
2014-01-14 08:02:14 +08:00
|
|
|
options = {
|
|
|
|
per_page: SiteSetting.topics_per_period_in_summary,
|
|
|
|
category: params[:category]
|
|
|
|
}
|
|
|
|
topic_query = TopicQuery.new(current_user, options)
|
2014-01-16 04:45:28 +08:00
|
|
|
|
|
|
|
if current_user.present?
|
|
|
|
periods = [best_period_for(current_user.last_seen_at)]
|
|
|
|
else
|
|
|
|
periods = TopTopic.periods
|
|
|
|
end
|
2014-01-14 08:02:14 +08:00
|
|
|
|
|
|
|
periods.each { |period| top[period] = topic_query.list_top_for(period) }
|
2013-12-24 07:50:36 +08:00
|
|
|
|
|
|
|
top
|
|
|
|
end
|
|
|
|
|
2014-01-16 04:45:28 +08:00
|
|
|
def best_period_for(date)
|
|
|
|
return :yearly if date < 180.days.ago
|
|
|
|
return :monthly if date < 35.days.ago
|
|
|
|
return :weekly if date < 8.days.ago
|
|
|
|
:daily
|
2014-01-14 08:02:14 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|