discourse/app/models/topic_list.rb

75 lines
1.9 KiB
Ruby
Raw Normal View History

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,
:for_period
2013-04-03 04:52:51 +08:00
def initialize(filter, current_user, topics, opts=nil)
2013-04-03 04:52:51 +08:00
@filter = filter
2013-02-06 03:16:51 +08:00
@current_user = current_user
@topics_input = topics
@opts = opts || {}
end
def preload_key
if @opts[:category]
c = Category.where(id: @opts[:category_id]).first
return "topic_list_#{c.url.sub(/^\//, '')}/l/#{@filter}" if c
end
"topic_list_#{@filter}"
2013-02-06 03:16:51 +08:00
end
# Lazy initialization
def topics
return @topics if @topics.present?
# copy side-loaded data (allowed users) before dumping it with the .to_a
@topics_input.each do |t|
2014-05-12 16:11:05 +08:00
t.allowed_user_ids = if @filter == :private_messages
t.allowed_users.map { |u| u.id }.to_a
else
[]
end
end
2013-02-06 03:16:51 +08: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?
# Create a lookup for all the user ids we need
user_ids = []
2013-02-07 23:45:24 +08:00
@topics.each do |ft|
user_ids << ft.user_id << ft.last_post_user_id << ft.featured_user_ids << ft.allowed_user_ids
2013-02-06 03:16:51 +08:00
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?
ft.posters = ft.posters_summary(avatar_lookup: avatar_lookup)
ft.participants = ft.participants_summary(avatar_lookup: avatar_lookup, user: @current_user)
2013-04-03 04:52:51 +08:00
ft.topic_list = self
2013-02-06 03:16:51 +08:00
end
@topics
2013-02-06 03:16:51 +08:00
end
def topic_ids
return [] unless @topics_input
@topics_input.pluck(:id)
end
2013-02-06 03:16:51 +08:00
def attributes
{'more_topics_url' => page}
2013-02-06 03:16:51 +08:00
end
end