mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-03 16:50:30 +08:00
### Super high level description: Adds a nested/threaded view for Discourse topics, allowing posts to be displayed as an indented reply tree instead of the default flat chronological stream. Backend: - New /n/:slug/:topic_id routes serving roots, children (paginated), and context (ancestor chain) endpoints - TreeLoader recursively fetches reply trees with configurable max depth, Sort supports top/new/old ordering - NestedViewPostStat caches per-post reply counts (direct + total descendants, whisper-aware) with a backfill job for existing data - NestedTopic model tracks per-topic opt-in and pinned post Frontend: - Recursive <NestedPost> / <NestedPostChildren> components with lazy-load expansion, cloaking, and scroll tracking - NestedViewCache service preserves expansion state and scroll position across back/forward navigation (15 entries, 10min TTL) - Context view for deep-linking to a specific post with its ancestor chain - Floating actions bar, sort selector, real-time MessageBus updates Site settings (hidden): nested_replies_enabled, nested_replies_default, nested_replies_default_sort, nested_replies_max_depth, nested_replies_cap_nesting_depth, nested_replies_toggle_mode_groups, plus a per-category default override. Co-authored-by: Rafael Silva <xfalcox@gmail.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Sérgio Saquetim <saquetim@discourse.org>
105 lines
3.1 KiB
Ruby
105 lines
3.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module NestedReplies
|
|
class PostPreloader
|
|
def initialize(topic_view:, topic:, current_user:, guardian:)
|
|
@topic_view = topic_view
|
|
@topic = topic
|
|
@current_user = current_user
|
|
@guardian = guardian
|
|
end
|
|
|
|
def prepare(posts)
|
|
user_ids = posts.map(&:user_id).compact.uniq
|
|
|
|
@topic_view.reset_post_collection(posts: PostsArray.new(posts))
|
|
|
|
allowed_post_fields = TopicView.allowed_post_custom_fields(@current_user, @topic)
|
|
@topic_view.post_custom_fields =
|
|
if allowed_post_fields.present?
|
|
Post.custom_fields_for_ids(posts.map(&:id).uniq, allowed_post_fields)
|
|
else
|
|
{}
|
|
end
|
|
|
|
allowed_user_fields = User.allowed_user_custom_fields(@guardian)
|
|
@topic_view.user_custom_fields =
|
|
if allowed_user_fields.present?
|
|
User.custom_fields_for_ids(user_ids, allowed_user_fields)
|
|
else
|
|
{}
|
|
end
|
|
|
|
TopicView.preload(@topic_view)
|
|
end
|
|
|
|
private
|
|
|
|
# Array subclass that handles ActiveRecord-style methods called by plugins'
|
|
# TopicView.on_preload hooks. The posts are already loaded with associations
|
|
# by TreeLoader, so we avoid re-querying. Explicitly handles the methods
|
|
# plugins actually use; unknown AR methods fall back to a real relation.
|
|
class PostsArray < Array
|
|
def includes(*associations)
|
|
ActiveRecord::Associations::Preloader.new(records: self, associations: associations).call
|
|
self
|
|
end
|
|
|
|
def pluck(*columns)
|
|
if columns.one?
|
|
map(&columns.first)
|
|
else
|
|
map { |record| columns.map { |col| record.public_send(col) } }
|
|
end
|
|
end
|
|
|
|
def where(conditions = nil, *rest)
|
|
return self if conditions.nil?
|
|
if conditions.is_a?(Hash) && rest.empty?
|
|
PostsArray.new(
|
|
select do |record|
|
|
conditions.all? do |k, v|
|
|
val = record.public_send(k)
|
|
v.is_a?(Array) ? v.include?(val) : val == v
|
|
end
|
|
end,
|
|
)
|
|
else
|
|
Post.where(id: map(&:id)).where(conditions, *rest)
|
|
end
|
|
end
|
|
|
|
def not(conditions = {})
|
|
if conditions.is_a?(Hash)
|
|
PostsArray.new(
|
|
reject do |record|
|
|
conditions.all? do |k, v|
|
|
val = record.public_send(k)
|
|
v.is_a?(Array) ? v.include?(val) : val == v
|
|
end
|
|
end,
|
|
)
|
|
else
|
|
Post.where(id: map(&:id)).where.not(conditions)
|
|
end
|
|
end
|
|
|
|
def respond_to_missing?(name, include_private = false)
|
|
Post.none.respond_to?(name, include_private) || super
|
|
end
|
|
|
|
def method_missing(name, *args, **kwargs, &block)
|
|
relation = Post.none
|
|
if relation.respond_to?(name)
|
|
Rails.logger.warn(
|
|
"NestedReplies::PostsArray falling back to DB query for .#{name} — " \
|
|
"consider adding an explicit handler",
|
|
)
|
|
Post.where(id: map(&:id)).public_send(name, *args, **kwargs, &block)
|
|
else
|
|
super
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|