2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-05-15 10:41:40 +08:00
|
|
|
class TopicList < DraftableList
|
2017-09-29 23:04:05 +08:00
|
|
|
cattr_accessor :preloaded_custom_fields
|
|
|
|
self.preloaded_custom_fields = Set.new
|
2015-08-05 14:01:52 +08:00
|
|
|
|
2017-02-15 05:32:33 +08:00
|
|
|
def self.on_preload(&blk)
|
2017-09-29 23:04:05 +08:00
|
|
|
(@preload ||= Set.new) << blk
|
2017-02-15 05:29:06 +08:00
|
|
|
end
|
|
|
|
|
2017-02-15 05:32:33 +08:00
|
|
|
def self.cancel_preload(&blk)
|
2017-09-29 23:04:05 +08:00
|
|
|
if @preload
|
|
|
|
@preload.delete blk
|
|
|
|
if @preload.length == 0
|
|
|
|
@preload = nil
|
|
|
|
end
|
|
|
|
end
|
2017-02-15 05:29:06 +08:00
|
|
|
end
|
|
|
|
|
2017-02-16 01:04:02 +08:00
|
|
|
def self.preload(topics, object)
|
2017-09-29 23:04:05 +08:00
|
|
|
if @preload
|
|
|
|
@preload.each { |preload| preload.call(topics, object) }
|
|
|
|
end
|
2017-02-15 05:29:06 +08:00
|
|
|
end
|
|
|
|
|
2018-03-29 03:36:12 +08:00
|
|
|
attr_accessor(
|
|
|
|
:more_topics_url,
|
|
|
|
:prev_topics_url,
|
|
|
|
:filter,
|
|
|
|
:for_period,
|
|
|
|
:per_page,
|
|
|
|
:top_tags,
|
|
|
|
:current_user,
|
|
|
|
:tags,
|
|
|
|
:shared_drafts,
|
2019-08-27 20:09:00 +08:00
|
|
|
:category,
|
|
|
|
:publish_read_state
|
2018-03-29 03:36:12 +08:00
|
|
|
)
|
2013-04-03 04:52:51 +08:00
|
|
|
|
2014-10-09 00:44:47 +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
|
2014-10-09 00:44:47 +08: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
|
2018-01-13 05:35:16 +08:00
|
|
|
|
|
|
|
if @opts[:tags]
|
|
|
|
@tags = Tag.where(id: @opts[:tags]).all
|
|
|
|
end
|
2019-08-27 20:09:00 +08:00
|
|
|
|
|
|
|
@publish_read_state = !!@opts[:publish_read_state]
|
2020-05-15 10:41:40 +08:00
|
|
|
|
|
|
|
super(current_user)
|
2014-10-09 00:44:47 +08:00
|
|
|
end
|
|
|
|
|
2018-01-13 04:26:13 +08:00
|
|
|
def top_tags
|
2016-07-07 21:17:56 +08:00
|
|
|
opts = @category ? { category: @category } : {}
|
2016-09-23 03:23:10 +08:00
|
|
|
opts[:guardian] = Guardian.new(@current_user)
|
2020-11-03 22:57:58 +08:00
|
|
|
Tag.top_tags(**opts)
|
2016-07-07 21:17:56 +08:00
|
|
|
end
|
|
|
|
|
2014-10-09 00:44:47 +08:00
|
|
|
def preload_key
|
2016-07-07 21:17:56 +08:00
|
|
|
if @category
|
2020-06-18 16:32:14 +08:00
|
|
|
"topic_list_#{@category.url.sub(/^\//, '')}/l/#{@filter}"
|
2016-07-07 21:17:56 +08:00
|
|
|
else
|
|
|
|
"topic_list_#{@filter}"
|
2014-10-09 00:44:47 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Lazy initialization
|
|
|
|
def topics
|
2015-06-15 14:25:36 +08:00
|
|
|
@topics ||= load_topics
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-06-15 14:25:36 +08:00
|
|
|
def load_topics
|
2015-02-23 13:50:52 +08:00
|
|
|
@topics = @topics_input
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
# Attach some data for serialization to each topic
|
2015-01-09 08:41:10 +08:00
|
|
|
@topic_lookup = TopicUser.lookup_for(@current_user, @topics) if @current_user
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-01-07 15:20:10 +08:00
|
|
|
post_action_type =
|
2015-01-09 08:41:10 +08:00
|
|
|
if @current_user
|
2015-01-07 15:20:10 +08: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 08:41:10 +08: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 15:20:10 +08:00
|
|
|
|
|
|
|
# Data for bookmarks or likes
|
|
|
|
post_action_lookup = PostAction.lookup_for(@current_user, @topics, post_action_type) if post_action_type
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Create a lookup for all the user ids we need
|
|
|
|
user_ids = []
|
2013-02-07 23:45:24 +08:00
|
|
|
@topics.each do |ft|
|
2014-05-12 15:32:49 +08:00
|
|
|
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
|
|
|
|
|
2020-07-17 17:48:08 +08:00
|
|
|
user_lookup = UserLookup.new(user_ids)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
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?
|
2020-09-11 16:26:04 +08:00
|
|
|
|
|
|
|
if ft.regular? && category_user_lookup.present?
|
|
|
|
ft.category_user_data = @category_user_lookup[ft.category_id]
|
|
|
|
end
|
2015-01-07 15:20:10 +08: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
|
|
|
|
|
2017-02-18 06:54:43 +08:00
|
|
|
ft.posters = ft.posters_summary(
|
2020-07-17 17:48:08 +08:00
|
|
|
user_lookup: user_lookup
|
2017-02-18 06:54:43 +08:00
|
|
|
)
|
|
|
|
|
2020-07-17 17:48:08 +08:00
|
|
|
ft.participants = ft.participants_summary(
|
|
|
|
user_lookup: user_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
|
|
|
|
|
2020-11-16 21:23:49 +08:00
|
|
|
ActiveRecord::Associations::Preloader.new.preload(@topics, [:image_upload, topic_thumbnails: :optimized_image])
|
|
|
|
|
2017-09-29 23:04:05 +08:00
|
|
|
if preloaded_custom_fields.present?
|
|
|
|
Topic.preload_custom_fields(@topics, preloaded_custom_fields)
|
2015-08-05 14:01:52 +08:00
|
|
|
end
|
|
|
|
|
2017-02-16 01:04:02 +08:00
|
|
|
TopicList.preload(@topics, self)
|
2017-02-15 05:29:06 +08:00
|
|
|
|
2014-05-12 15:32:49 +08:00
|
|
|
@topics
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def attributes
|
2013-11-20 03:08:45 +08:00
|
|
|
{ 'more_topics_url' => page }
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2020-09-11 16:26:04 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def category_user_lookup
|
|
|
|
@category_user_lookup ||= begin
|
|
|
|
if @current_user
|
|
|
|
CategoryUser.lookup_for(@current_user, @topics.map(&:category_id).uniq)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|