2013-02-05 14:16:51 -05:00
|
|
|
class ListController < ApplicationController
|
|
|
|
|
2013-07-24 17:15:21 -04:00
|
|
|
before_filter :ensure_logged_in, except: [:latest, :hot, :category, :category_feed, :latest_feed, :hot_feed, :topics_by]
|
2013-06-19 22:11:14 -04:00
|
|
|
before_filter :set_category, only: [:category, :category_feed]
|
2013-02-05 14:16:51 -05:00
|
|
|
skip_before_filter :check_xhr
|
|
|
|
|
|
|
|
# Create our filters
|
2013-03-27 16:17:49 -04:00
|
|
|
[:latest, :hot, :favorited, :read, :posted, :unread, :new].each do |filter|
|
2013-02-07 10:45:24 -05:00
|
|
|
define_method(filter) do
|
2013-06-19 22:11:14 -04:00
|
|
|
list_opts = build_topic_list_options
|
2013-08-24 13:38:02 -04:00
|
|
|
user = list_target_user
|
2013-08-21 19:18:54 -04:00
|
|
|
list = TopicQuery.new(user, list_opts).public_send("list_#{filter}")
|
2013-06-19 22:11:14 -04:00
|
|
|
list.more_topics_url = url_for(self.public_send "#{filter}_path".to_sym, list_opts.merge(format: 'json', page: next_page))
|
2013-10-11 12:35:12 -04:00
|
|
|
if [:latest, :hot].include?(filter)
|
|
|
|
@description = SiteSetting.site_description
|
|
|
|
@rss = filter
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
respond(list)
|
2013-02-07 10:45:24 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-05 16:49:06 -04:00
|
|
|
[:latest, :hot].each do |filter|
|
|
|
|
define_method("#{filter}_feed") do
|
|
|
|
anonymous_etag(@category) do
|
|
|
|
@title = "#{filter.capitalize} Topics"
|
|
|
|
@link = "#{Discourse.base_url}/#{filter}"
|
|
|
|
@description = I18n.t("rss_description.#{filter}")
|
|
|
|
@atom_link = "#{Discourse.base_url}/#{filter}.rss"
|
|
|
|
@topic_list = TopicQuery.new(current_user).public_send("list_#{filter}")
|
|
|
|
render 'list', formats: [:rss]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-24 17:15:21 -04:00
|
|
|
def topics_by
|
|
|
|
list_opts = build_topic_list_options
|
|
|
|
list = TopicQuery.new(current_user, list_opts).list_topics_by(fetch_user_from_params)
|
|
|
|
list.more_topics_url = url_for(topics_by_path(list_opts.merge(format: 'json', page: next_page)))
|
|
|
|
|
|
|
|
respond(list)
|
|
|
|
end
|
|
|
|
|
2013-08-24 16:58:16 -04:00
|
|
|
def private_messages
|
|
|
|
list_opts = build_topic_list_options
|
2013-09-30 14:35:11 -04:00
|
|
|
target_user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_see_private_messages!(target_user.id)
|
|
|
|
|
|
|
|
list = TopicQuery.new(current_user, list_opts).list_private_messages(target_user)
|
2013-08-24 16:58:16 -04:00
|
|
|
list.more_topics_url = url_for(topics_private_messages_path(list_opts.merge(format: 'json', page: next_page)))
|
|
|
|
|
|
|
|
respond(list)
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_messages_sent
|
|
|
|
list_opts = build_topic_list_options
|
2013-09-30 14:35:11 -04:00
|
|
|
target_user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_see_private_messages!(target_user.id)
|
|
|
|
|
|
|
|
list = TopicQuery.new(current_user, list_opts).list_private_messages_sent(target_user)
|
2013-08-24 16:58:16 -04:00
|
|
|
list.more_topics_url = url_for(topics_private_messages_sent_path(list_opts.merge(format: 'json', page: next_page)))
|
|
|
|
|
|
|
|
respond(list)
|
|
|
|
end
|
|
|
|
|
2013-08-30 12:32:05 -04:00
|
|
|
def private_messages_unread
|
|
|
|
list_opts = build_topic_list_options
|
2013-09-30 14:35:11 -04:00
|
|
|
target_user = fetch_user_from_params
|
|
|
|
guardian.ensure_can_see_private_messages!(target_user.id)
|
|
|
|
|
|
|
|
list = TopicQuery.new(current_user, list_opts).list_private_messages_unread(target_user)
|
2013-08-30 12:32:05 -04:00
|
|
|
list.more_topics_url = url_for(topics_private_messages_unread_path(list_opts.merge(format: 'json', page: next_page)))
|
|
|
|
|
|
|
|
respond(list)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def category
|
|
|
|
query = TopicQuery.new(current_user, page: params[:page])
|
|
|
|
|
|
|
|
# If they choose uncategorized, return topics NOT in a category
|
2013-06-19 22:11:14 -04:00
|
|
|
if request_is_for_uncategorized?
|
2013-02-05 14:16:51 -05:00
|
|
|
list = query.list_uncategorized
|
|
|
|
else
|
2013-07-08 01:42:55 -04:00
|
|
|
if !@category
|
|
|
|
raise Discourse::NotFound
|
|
|
|
return
|
|
|
|
end
|
2013-02-27 22:36:12 -05:00
|
|
|
guardian.ensure_can_see!(@category)
|
|
|
|
list = query.list_category(@category)
|
2013-09-09 11:38:09 -04:00
|
|
|
@description = @category.description
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-02 01:21:26 -04:00
|
|
|
list.more_topics_url = url_for(category_list_path(params[:category], page: next_page, format: "json"))
|
2013-02-05 14:16:51 -05:00
|
|
|
respond(list)
|
|
|
|
end
|
|
|
|
|
2013-02-27 22:36:12 -05:00
|
|
|
def category_feed
|
2013-06-19 22:11:14 -04:00
|
|
|
raise Discourse::InvalidParameters.new('Category RSS of "uncategorized"') if request_is_for_uncategorized?
|
2013-03-01 16:56:14 -05:00
|
|
|
|
2013-02-27 22:36:12 -05:00
|
|
|
guardian.ensure_can_see!(@category)
|
2013-03-01 16:56:14 -05:00
|
|
|
|
|
|
|
anonymous_etag(@category) do
|
2013-07-05 16:49:06 -04:00
|
|
|
@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"
|
2013-03-01 16:56:14 -05:00
|
|
|
@topic_list = TopicQuery.new.list_new_in_category(@category)
|
|
|
|
render 'list', formats: [:rss]
|
|
|
|
end
|
2013-02-27 22:36:12 -05:00
|
|
|
end
|
|
|
|
|
2013-03-27 16:17:49 -04: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
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
protected
|
|
|
|
|
|
|
|
def respond(list)
|
|
|
|
|
|
|
|
list.draft_key = Draft::NEW_TOPIC
|
|
|
|
list.draft_sequence = DraftSequence.current(current_user, Draft::NEW_TOPIC)
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
draft = Draft.get(current_user, list.draft_key, list.draft_sequence) if current_user
|
|
|
|
list.draft = draft
|
|
|
|
|
2013-02-06 11:55:54 -05:00
|
|
|
discourse_expires_in 1.minute
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
2013-02-07 10:45:24 -05:00
|
|
|
@list = list
|
2013-02-05 14:16:51 -05: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 10:45:24 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def next_page
|
|
|
|
params[:page].to_i + 1
|
|
|
|
end
|
|
|
|
|
2013-06-19 22:11:14 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_category
|
2013-08-22 15:46:17 -04:00
|
|
|
slug = params.fetch(:category)
|
|
|
|
@category = Category.where("slug = ?", slug).includes(:featured_users).first || Category.where("id = ?", slug.to_i).includes(:featured_users).first
|
2013-06-19 22:11:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def request_is_for_uncategorized?
|
2013-06-27 16:16:06 -04:00
|
|
|
params[:category] == Slug.for(SiteSetting.uncategorized_name) ||
|
|
|
|
params[:category] == SiteSetting.uncategorized_name ||
|
|
|
|
params[:category] == 'uncategorized'
|
2013-06-19 22:11:14 -04: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
|
|
|
|
return {
|
|
|
|
page: params[:page],
|
|
|
|
topic_ids: param_to_integer_list(:topic_ids),
|
|
|
|
exclude_category: (params[:exclude_category] || menu_item.try(:filter))
|
|
|
|
}
|
|
|
|
end
|
2013-08-24 13:38:02 -04: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-02-05 14:16:51 -05:00
|
|
|
end
|