2013-02-05 14:16:51 -05:00
require_dependency 'avatar_lookup'
class TopicList
include ActiveModel :: Serialization
2015-08-05 16:01:52 +10:00
cattr_accessor :preloaded_custom_fields
2016-05-16 17:31:39 +08:00
self . preloaded_custom_fields = Set . new
2015-08-05 16:01:52 +10:00
2013-04-02 16:52:51 -04:00
attr_accessor :more_topics_url ,
2014-02-25 12:15:20 -05:00
:prev_topics_url ,
2013-04-02 16:52:51 -04:00
:draft ,
:draft_key ,
:draft_sequence ,
2014-08-28 14:34:31 -04:00
:filter ,
2014-12-15 11:54:26 -05:00
:for_period ,
2016-07-07 21:17:56 +08:00
:per_page ,
:tags
2013-04-02 16:52:51 -04:00
2014-10-08 12:44:47 -04:00
def initialize ( filter , current_user , topics , opts = nil )
2013-04-02 16:52:51 -04:00
@filter = filter
2013-02-05 14:16:51 -05:00
@current_user = current_user
@topics_input = topics
2014-10-08 12:44:47 -04:00
@opts = opts || { }
2016-05-16 17:31:39 +08:00
2016-07-07 21:17:56 +08:00
if @opts [ :category ]
@category = Category . find_by ( id : @opts [ :category_id ] )
end
2016-05-16 17:31:39 +08:00
preloaded_custom_fields << DiscourseTagging :: TAGS_FIELD_NAME if SiteSetting . tagging_enabled
2014-10-08 12:44:47 -04:00
end
2016-07-07 21:17:56 +08:00
def tags
opts = @category ? { category : @category } : { }
Tag . top_tags ( opts )
end
2014-10-08 12:44:47 -04:00
def preload_key
2016-07-07 21:17:56 +08:00
if @category
" topic_list_ #{ @category . url . sub ( / ^ \/ / , '' ) } /l/ #{ @filter } "
else
" topic_list_ #{ @filter } "
2014-10-08 12:44:47 -04:00
end
2013-02-05 14:16:51 -05:00
end
# Lazy initialization
def topics
2015-06-15 16:25:36 +10:00
@topics || = load_topics
end
2013-02-05 14:16:51 -05:00
2015-06-15 16:25:36 +10:00
def load_topics
2015-02-23 16:50:52 +11:00
@topics = @topics_input
2013-02-05 14:16:51 -05:00
# Attach some data for serialization to each topic
2015-01-09 11:41:10 +11:00
@topic_lookup = TopicUser . lookup_for ( @current_user , @topics ) if @current_user
2013-02-05 14:16:51 -05:00
2015-01-07 18:20:10 +11:00
post_action_type =
2015-01-09 11:41:10 +11:00
if @current_user
2015-01-07 18:20:10 +11:00
if @opts [ :filter ] . present?
if @opts [ :filter ] == " bookmarked "
PostActionType . types [ :bookmark ]
elsif @opts [ :filter ] == " liked "
PostActionType . types [ :like ]
end
end
end
2015-01-09 11:41:10 +11:00
# Include bookmarks if you have bookmarked topics
if @current_user && ! post_action_type
post_action_type = PostActionType . types [ :bookmark ] if @topic_lookup . any? { | _ , tu | tu && tu . bookmarked }
end
2015-01-07 18:20:10 +11:00
# Data for bookmarks or likes
post_action_lookup = PostAction . lookup_for ( @current_user , @topics , post_action_type ) if post_action_type
2013-02-05 14:16:51 -05:00
# Create a lookup for all the user ids we need
user_ids = [ ]
2013-02-07 16:45:24 +01:00
@topics . each do | ft |
2014-05-12 09:32:49 +02:00
user_ids << ft . user_id << ft . last_post_user_id << ft . featured_user_ids << ft . allowed_user_ids
2013-02-05 14:16:51 -05:00
end
avatar_lookup = AvatarLookup . new ( user_ids )
2013-02-07 16:45:24 +01:00
@topics . each do | ft |
2013-02-05 14:16:51 -05:00
ft . user_data = @topic_lookup [ ft . id ] if @topic_lookup . present?
2015-01-07 18:20:10 +11:00
if ft . user_data && post_action_lookup && actions = post_action_lookup [ ft . id ]
ft . user_data . post_action_data = { post_action_type = > actions }
end
2013-05-22 23:22:30 -07:00
ft . posters = ft . posters_summary ( avatar_lookup : avatar_lookup )
2014-05-12 09:32:49 +02:00
ft . participants = ft . participants_summary ( avatar_lookup : avatar_lookup , user : @current_user )
2013-04-02 16:52:51 -04:00
ft . topic_list = self
2013-02-05 14:16:51 -05:00
end
2016-05-16 17:31:39 +08:00
if preloaded_custom_fields . present?
Topic . preload_custom_fields ( @topics , preloaded_custom_fields )
2015-08-05 16:01:52 +10:00
end
2014-05-12 09:32:49 +02:00
@topics
2013-02-05 14:16:51 -05:00
end
def attributes
2013-11-19 14:08:45 -05:00
{ 'more_topics_url' = > page }
2013-02-05 14:16:51 -05:00
end
end