2013-02-05 14:16:51 -05:00
|
|
|
require_dependency 'avatar_lookup'
|
|
|
|
|
|
|
|
class TopicList
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
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,
|
|
|
|
:per_page
|
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 || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def preload_key
|
|
|
|
if @opts[:category]
|
2014-10-15 15:29:20 -04:00
|
|
|
c = Category.where(id: @opts[:category_id]).first
|
|
|
|
return "topic_list_#{c.url.sub(/^\//, '')}/l/#{@filter}" if c
|
2014-10-08 12:44:47 -04:00
|
|
|
end
|
2014-10-15 15:29:20 -04:00
|
|
|
|
|
|
|
"topic_list_#{@filter}"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Lazy initialization
|
|
|
|
def topics
|
|
|
|
return @topics if @topics.present?
|
|
|
|
|
2014-05-12 03:32:49 -04:00
|
|
|
# copy side-loaded data (allowed users) before dumping it with the .to_a
|
|
|
|
@topics_input.each do |t|
|
2014-05-12 04:11:05 -04:00
|
|
|
t.allowed_user_ids = if @filter == :private_messages
|
|
|
|
t.allowed_users.map { |u| u.id }.to_a
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2014-05-12 03:32:49 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
@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?
|
|
|
|
|
2015-01-07 02:20:10 -05:00
|
|
|
post_action_type =
|
|
|
|
if @current_user.present?
|
|
|
|
if @opts[:filter].present?
|
|
|
|
if @opts[:filter] == "bookmarked"
|
|
|
|
PostActionType.types[:bookmark]
|
|
|
|
elsif @opts[:filter] == "liked"
|
|
|
|
PostActionType.types[:like]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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 10:45:24 -05:00
|
|
|
@topics.each do |ft|
|
2014-05-12 03:32:49 -04: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 10:45:24 -05: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 02:20:10 -05: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-23 02:22:30 -04:00
|
|
|
ft.posters = ft.posters_summary(avatar_lookup: avatar_lookup)
|
2014-05-12 03:32:49 -04: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
|
|
|
|
|
2014-05-12 03:32:49 -04:00
|
|
|
@topics
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-28 14:54:00 -04:00
|
|
|
def topic_ids
|
2014-01-29 18:32:04 -05:00
|
|
|
return [] unless @topics_input
|
|
|
|
@topics_input.pluck(:id)
|
2013-05-28 14:54:00 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def attributes
|
2013-11-19 14:08:45 -05:00
|
|
|
{'more_topics_url' => page}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|