2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-30 11:42:26 -04:00
|
|
|
class TopicList
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
2017-09-29 11:04:05 -04:00
|
|
|
cattr_accessor :preloaded_custom_fields
|
|
|
|
self.preloaded_custom_fields = Set.new
|
2015-08-05 02:01:52 -04:00
|
|
|
|
2017-02-14 16:32:33 -05:00
|
|
|
def self.on_preload(&blk)
|
2017-09-29 11:04:05 -04:00
|
|
|
(@preload ||= Set.new) << blk
|
2017-02-14 16:29:06 -05:00
|
|
|
end
|
|
|
|
|
2017-02-14 16:32:33 -05:00
|
|
|
def self.cancel_preload(&blk)
|
2017-09-29 11:04:05 -04:00
|
|
|
if @preload
|
|
|
|
@preload.delete blk
|
|
|
|
@preload = nil if @preload.length == 0
|
|
|
|
end
|
2017-02-14 16:29:06 -05:00
|
|
|
end
|
|
|
|
|
2017-02-15 12:04:02 -05:00
|
|
|
def self.preload(topics, object)
|
2017-09-29 11:04:05 -04:00
|
|
|
@preload.each { |preload| preload.call(topics, object) } if @preload
|
2017-02-14 16:29:06 -05:00
|
|
|
end
|
|
|
|
|
2021-08-25 06:16:08 -04:00
|
|
|
def self.on_preload_user_ids(&blk)
|
|
|
|
(@preload_user_ids ||= Set.new) << blk
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.preload_user_ids(topics, user_ids, object)
|
|
|
|
if @preload_user_ids
|
|
|
|
@preload_user_ids.each do |preload_user_ids|
|
|
|
|
user_ids = preload_user_ids.call(topics, user_ids, object)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
user_ids
|
|
|
|
end
|
|
|
|
|
2018-03-28 15:36:12 -04:00
|
|
|
attr_accessor(
|
|
|
|
:more_topics_url,
|
|
|
|
:prev_topics_url,
|
|
|
|
:filter,
|
|
|
|
:for_period,
|
|
|
|
:per_page,
|
|
|
|
:current_user,
|
|
|
|
:tags,
|
|
|
|
:shared_drafts,
|
2019-08-27 08:09:00 -04:00
|
|
|
:category,
|
|
|
|
:publish_read_state,
|
2018-03-28 15:36:12 -04:00
|
|
|
)
|
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 || {}
|
2016-05-16 05:31:39 -04:00
|
|
|
|
2016-07-07 09:17:56 -04:00
|
|
|
@category = Category.find_by(id: @opts[:category_id]) if @opts[:category]
|
2018-01-12 16:35:16 -05:00
|
|
|
|
2024-02-23 01:11:39 -05:00
|
|
|
@tags = Tag.where(id: @opts[:tag_ids]).all if @opts[:tag_ids].present?
|
2019-08-27 08:09:00 -04:00
|
|
|
|
|
|
|
@publish_read_state = !!@opts[:publish_read_state]
|
2014-10-08 12:44:47 -04:00
|
|
|
end
|
|
|
|
|
2018-01-12 15:26:13 -05:00
|
|
|
def top_tags
|
2016-07-07 09:17:56 -04:00
|
|
|
opts = @category ? { category: @category } : {}
|
2016-09-22 15:23:10 -04:00
|
|
|
opts[:guardian] = Guardian.new(@current_user)
|
2020-11-03 09:57:58 -05:00
|
|
|
Tag.top_tags(**opts)
|
2016-07-07 09:17:56 -04:00
|
|
|
end
|
|
|
|
|
2014-10-08 12:44:47 -04:00
|
|
|
def preload_key
|
2022-08-24 06:54:01 -04:00
|
|
|
"topic_list"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Lazy initialization
|
|
|
|
def topics
|
2015-06-15 02:25:36 -04:00
|
|
|
@topics ||= load_topics
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2023-10-17 12:06:01 -04:00
|
|
|
def categories
|
2023-10-20 06:31:20 -04:00
|
|
|
@categories ||=
|
2024-03-21 13:51:41 -04:00
|
|
|
topics.map { |t| [t.category&.parent_category, t.category] }.flatten.uniq.compact
|
2023-10-17 12:06:01 -04:00
|
|
|
end
|
|
|
|
|
2015-06-15 02:25:36 -04:00
|
|
|
def load_topics
|
2015-02-23 00:50:52 -05:00
|
|
|
@topics = @topics_input
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
# Attach some data for serialization to each topic
|
2015-01-08 19:41:10 -05:00
|
|
|
@topic_lookup = TopicUser.lookup_for(@current_user, @topics) if @current_user
|
2021-02-03 19:27:34 -05:00
|
|
|
@dismissed_topic_users_lookup =
|
|
|
|
DismissedTopicUser.lookup_for(@current_user, @topics) if @current_user
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-01-07 02:20:10 -05:00
|
|
|
post_action_type =
|
2015-01-08 19:41:10 -05:00
|
|
|
if @current_user
|
2015-01-07 02:20:10 -05:00
|
|
|
if @opts[:filter].present?
|
2022-05-09 20:42:18 -04:00
|
|
|
PostActionType.types[:like] if @opts[:filter] == "liked"
|
2015-01-07 02:20:10 -05:00
|
|
|
end
|
|
|
|
end
|
2015-01-08 19:41:10 -05:00
|
|
|
|
2015-01-07 02:20:10 -05:00
|
|
|
# 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 = []
|
2023-05-31 10:02:06 -04:00
|
|
|
group_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
|
2023-05-31 10:02:06 -04:00
|
|
|
group_ids |= (ft.allowed_group_ids || [])
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2021-08-25 06:16:08 -04:00
|
|
|
user_ids = TopicList.preload_user_ids(@topics, user_ids, self)
|
2020-07-17 05:48:08 -04:00
|
|
|
user_lookup = UserLookup.new(user_ids)
|
2023-05-31 10:02:06 -04:00
|
|
|
group_lookup = GroupLookup.new(group_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?
|
2020-09-11 04:26:04 -04:00
|
|
|
|
|
|
|
if ft.regular? && category_user_lookup.present?
|
|
|
|
ft.category_user_data = @category_user_lookup[ft.category_id]
|
|
|
|
end
|
2015-01-07 02:20:10 -05:00
|
|
|
|
2021-02-03 19:27:34 -05:00
|
|
|
ft.dismissed = @current_user && @dismissed_topic_users_lookup.include?(ft.id)
|
|
|
|
|
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
|
|
|
|
|
2017-02-17 17:54:43 -05:00
|
|
|
ft.posters = ft.posters_summary(user_lookup: user_lookup)
|
|
|
|
|
2020-07-17 05:48:08 -04:00
|
|
|
ft.participants = ft.participants_summary(user_lookup: user_lookup, user: @current_user)
|
2023-05-31 10:02:06 -04:00
|
|
|
ft.participant_groups =
|
|
|
|
ft.participant_groups_summary(group_lookup: group_lookup, group: @opts[:group])
|
2013-04-02 16:52:51 -04:00
|
|
|
ft.topic_list = self
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2023-10-17 12:06:01 -04:00
|
|
|
topic_preloader_associations = [
|
|
|
|
:image_upload,
|
|
|
|
{ topic_thumbnails: :optimized_image },
|
|
|
|
{ category: :parent_category },
|
|
|
|
]
|
|
|
|
|
2022-12-12 09:08:13 -05:00
|
|
|
topic_preloader_associations.concat(DiscoursePluginRegistry.topic_preloader_associations.to_a)
|
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
ActiveRecord::Associations::Preloader.new(
|
2022-12-12 09:08:13 -05:00
|
|
|
records: @topics,
|
|
|
|
associations: topic_preloader_associations,
|
2022-03-21 10:28:52 -04:00
|
|
|
).call
|
2020-11-16 08:23:49 -05:00
|
|
|
|
2017-09-29 11:04:05 -04:00
|
|
|
if preloaded_custom_fields.present?
|
|
|
|
Topic.preload_custom_fields(@topics, preloaded_custom_fields)
|
2015-08-05 02:01:52 -04:00
|
|
|
end
|
|
|
|
|
2017-02-15 12:04:02 -05:00
|
|
|
TopicList.preload(@topics, self)
|
2017-02-14 16:29:06 -05:00
|
|
|
|
2014-05-12 03:32:49 -04:00
|
|
|
@topics
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def attributes
|
2013-11-19 14:08:45 -05:00
|
|
|
{ "more_topics_url" => page }
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2020-09-11 04:26:04 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def category_user_lookup
|
|
|
|
@category_user_lookup ||=
|
|
|
|
begin
|
|
|
|
if @current_user
|
|
|
|
CategoryUser.lookup_for(@current_user, @topics.map(&:category_id).uniq)
|
|
|
|
else
|
|
|
|
[]
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2020-09-11 04:26:04 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|