2013-02-05 13:16:51 -06:00
class ListController < ApplicationController
2013-03-28 08:01:13 -05:00
before_filter :ensure_logged_in , except : [ :latest , :hot , :category , :category_feed ]
2013-02-05 13:16:51 -06:00
skip_before_filter :check_xhr
# Create our filters
2013-03-27 15:17:49 -05:00
[ :latest , :hot , :favorited , :read , :posted , :unread , :new ] . each do | filter |
2013-02-07 09:45:24 -06:00
define_method ( filter ) do
2013-05-28 02:52:52 -05:00
list_opts = {
page : params [ :page ]
}
if params [ :topic_ids ]
list_opts [ :topic_ids ] = params [ :topic_ids ] . split ( " , " ) . map ( & :to_i )
end
2013-02-05 13:16:51 -06:00
# html format means we need to farm exclude from the site options
2013-03-04 18:42:44 -06:00
if params [ :format ] . blank? || params [ :format ] == " html "
2013-02-05 13:16:51 -06:00
#TODO objectify this stuff
SiteSetting . top_menu . split ( '|' ) . each do | f |
s = f . split ( " , " )
2013-03-28 08:01:13 -05:00
if s [ 0 ] == action_name || ( action_name == " index " && s [ 0 ] == SiteSetting . homepage )
2013-02-05 13:16:51 -06:00
list_opts [ :exclude_category ] = s [ 1 ] [ 1 .. - 1 ] if s . length == 2
end
end
end
list_opts [ :exclude_category ] = params [ :exclude_category ] if params [ :exclude_category ] . present?
list = TopicQuery . new ( current_user , list_opts ) . send ( " list_ #{ filter } " )
list . more_topics_url = url_for ( self . send " #{ filter } _path " . to_sym , list_opts . merge ( format : 'json' , page : next_page ) )
2013-02-07 09:45:24 -06:00
2013-02-05 13:16:51 -06:00
respond ( list )
2013-02-07 09:45:24 -06:00
end
2013-02-05 13:16:51 -06:00
end
def category
query = TopicQuery . new ( current_user , page : params [ :page ] )
list = nil
# If they choose uncategorized, return topics NOT in a category
2013-05-29 11:06:21 -05:00
if params [ :category ] == 'uncategorized'
2013-02-05 13:16:51 -06:00
list = query . list_uncategorized
else
2013-02-27 21:36:12 -06:00
@category = Category . where ( " slug = ? or id = ? " , params [ :category ] , params [ :category ] . to_i ) . includes ( :featured_users ) . first
guardian . ensure_can_see! ( @category )
list = query . list_category ( @category )
2013-02-05 13:16:51 -06:00
end
list . more_topics_url = url_for ( category_path ( params [ :category ] , page : next_page , format : " json " ) )
respond ( list )
end
2013-02-27 21:36:12 -06:00
def category_feed
raise Discourse :: InvalidParameters . new ( 'Category RSS of "uncategorized"' ) if params [ :category ] == Slug . for ( SiteSetting . uncategorized_name ) || params [ :category ] == SiteSetting . uncategorized_name
@category = Category . where ( " slug = ? " , params [ :category ] ) . includes ( :featured_users ) . first
2013-03-01 15:56:14 -06:00
2013-02-27 21:36:12 -06:00
guardian . ensure_can_see! ( @category )
2013-03-01 15:56:14 -06:00
anonymous_etag ( @category ) do
@topic_list = TopicQuery . new . list_new_in_category ( @category )
render 'list' , formats : [ :rss ]
end
2013-02-27 21:36:12 -06:00
end
2013-03-27 15:17:49 -05: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 13:16:51 -06:00
protected
def respond ( list )
list . draft_key = Draft :: NEW_TOPIC
list . draft_sequence = DraftSequence . current ( current_user , Draft :: NEW_TOPIC )
2013-02-07 09:45:24 -06:00
2013-02-05 13:16:51 -06:00
draft = Draft . get ( current_user , list . draft_key , list . draft_sequence ) if current_user
list . draft = draft
2013-02-06 10:55:54 -06:00
discourse_expires_in 1 . minute
2013-02-05 13:16:51 -06:00
respond_to do | format |
format . html do
2013-02-07 09:45:24 -06:00
@list = list
2013-02-05 13:16:51 -06: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 09:45:24 -06:00
2013-02-05 13:16:51 -06:00
def next_page
params [ :page ] . to_i + 1
end
end