2013-02-06 03:16:51 +08:00
|
|
|
require_dependency 'avatar_lookup'
|
|
|
|
|
|
|
|
class TopicList
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
2013-04-03 04:52:51 +08:00
|
|
|
attr_accessor :more_topics_url,
|
2014-02-26 01:15:20 +08:00
|
|
|
:prev_topics_url,
|
2013-04-03 04:52:51 +08:00
|
|
|
:draft,
|
|
|
|
:draft_key,
|
|
|
|
:draft_sequence,
|
|
|
|
:filter
|
|
|
|
|
|
|
|
def initialize(filter, current_user, topics)
|
|
|
|
@filter = filter
|
2013-02-06 03:16:51 +08:00
|
|
|
@current_user = current_user
|
|
|
|
@topics_input = topics
|
|
|
|
end
|
|
|
|
|
|
|
|
# Lazy initialization
|
|
|
|
def topics
|
|
|
|
return @topics if @topics.present?
|
|
|
|
|
|
|
|
@topics = @topics_input.to_a
|
|
|
|
|
|
|
|
# Attach some data for serialization to each topic
|
|
|
|
@topic_lookup = TopicUser.lookup_for(@current_user, @topics) if @current_user.present?
|
|
|
|
|
|
|
|
# Create a lookup for all the user ids we need
|
|
|
|
user_ids = []
|
2013-02-07 23:45:24 +08:00
|
|
|
@topics.each do |ft|
|
2013-02-06 03:16:51 +08:00
|
|
|
user_ids << ft.user_id << ft.last_post_user_id << ft.featured_user_ids
|
|
|
|
end
|
|
|
|
|
|
|
|
avatar_lookup = AvatarLookup.new(user_ids)
|
|
|
|
|
2013-02-07 23:45:24 +08:00
|
|
|
@topics.each do |ft|
|
2013-02-06 03:16:51 +08:00
|
|
|
ft.user_data = @topic_lookup[ft.id] if @topic_lookup.present?
|
2013-05-23 14:22:30 +08:00
|
|
|
ft.posters = ft.posters_summary(avatar_lookup: avatar_lookup)
|
2013-04-03 04:52:51 +08:00
|
|
|
ft.topic_list = self
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
return @topics
|
|
|
|
end
|
|
|
|
|
2013-05-29 02:54:00 +08:00
|
|
|
def topic_ids
|
2014-01-30 07:32:04 +08:00
|
|
|
return [] unless @topics_input
|
|
|
|
@topics_input.pluck(:id)
|
2013-05-29 02:54:00 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def attributes
|
2013-11-20 03:08:45 +08:00
|
|
|
{'more_topics_url' => page}
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|