discourse/app/models/topic_list.rb

133 lines
3.3 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
require_dependency 'avatar_lookup'
require_dependency 'primary_group_lookup'
2013-02-05 14:16:51 -05:00
class TopicList
include ActiveModel::Serialization
cattr_accessor :preloaded_custom_fields
self.preloaded_custom_fields = Set.new
2017-02-14 16:32:33 -05:00
def self.on_preload(&blk)
(@preload ||= Set.new) << blk
end
2017-02-14 16:32:33 -05:00
def self.cancel_preload(&blk)
if @preload
@preload.delete blk
if @preload.length == 0
@preload = nil
end
end
end
def self.preload(topics, object)
if @preload
@preload.each { |preload| preload.call(topics, object) }
end
end
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,
:filter,
:for_period,
:per_page,
:tags,
:current_user
2013-04-02 16:52:51 -04:00
2017-07-27 21:20:09 -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
@opts = opts || {}
if @opts[:category]
@category = Category.find_by(id: @opts[:category_id])
end
end
def tags
opts = @category ? { category: @category } : {}
opts[:guardian] = Guardian.new(@current_user)
Tag.top_tags(opts)
end
def preload_key
if @category
"topic_list_#{@category.url.sub(/^\//, '')}/l/#{@filter}"
else
"topic_list_#{@filter}"
end
2013-02-05 14:16:51 -05:00
end
# Lazy initialization
def topics
@topics ||= load_topics
end
2013-02-05 14:16:51 -05:00
def load_topics
@topics = @topics_input
2013-02-05 14:16:51 -05:00
# Attach some data for serialization to each topic
@topic_lookup = TopicUser.lookup_for(@current_user, @topics) if @current_user
2013-02-05 14:16:51 -05:00
post_action_type =
if @current_user
if @opts[:filter].present?
if @opts[:filter] == "bookmarked"
PostActionType.types[:bookmark]
elsif @opts[:filter] == "liked"
PostActionType.types[:like]
end
end
end
# Include bookmarks if you have bookmarked topics
if @current_user && !post_action_type
2017-07-27 21:20:09 -04:00
post_action_type = PostActionType.types[:bookmark] if @topic_lookup.any? { |_, tu| tu && tu.bookmarked }
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|
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)
2017-02-22 12:20:50 -05:00
primary_group_lookup = PrimaryGroupLookup.new(user_ids)
2013-02-05 14:16:51 -05:00
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?
if ft.user_data && post_action_lookup && actions = post_action_lookup[ft.id]
2017-07-27 21:20:09 -04:00
ft.user_data.post_action_data = { post_action_type => actions }
end
ft.posters = ft.posters_summary(
avatar_lookup: avatar_lookup,
2017-02-22 12:20:50 -05:00
primary_group_lookup: primary_group_lookup
)
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
if preloaded_custom_fields.present?
Topic.preload_custom_fields(@topics, preloaded_custom_fields)
end
TopicList.preload(@topics, self)
@topics
2013-02-05 14:16:51 -05:00
end
def attributes
2017-07-27 21:20:09 -04:00
{ 'more_topics_url' => page }
2013-02-05 14:16:51 -05:00
end
end