mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-01 17:12:11 +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>
38 lines
1.4 KiB
Ruby
38 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module NestedReplies
|
|
# Walks the reply_to_post_number chain from `start_post_number` toward root
|
|
# in a single recursive CTE. Returns an array of result objects, each with
|
|
# .id, .post_number, .reply_to_post_number, and .depth (1 = start post).
|
|
def self.walk_ancestors(
|
|
topic_id:,
|
|
start_post_number:,
|
|
limit: 100,
|
|
exclude_deleted: true,
|
|
stop_at_op: false
|
|
)
|
|
deleted_seed = exclude_deleted ? "AND deleted_at IS NULL" : ""
|
|
deleted_recurse = exclude_deleted ? "AND p.deleted_at IS NULL" : ""
|
|
op_stop = stop_at_op ? "AND a.reply_to_post_number != 1" : ""
|
|
|
|
DB.query(<<~SQL, topic_id: topic_id, start: start_post_number, limit: limit)
|
|
WITH RECURSIVE ancestors AS (
|
|
SELECT id, post_number, reply_to_post_number, deleted_at, 1 AS depth
|
|
FROM posts
|
|
WHERE topic_id = :topic_id
|
|
AND post_number = :start
|
|
#{deleted_seed}
|
|
UNION ALL
|
|
SELECT p.id, p.post_number, p.reply_to_post_number, p.deleted_at, a.depth + 1
|
|
FROM posts p
|
|
JOIN ancestors a ON p.post_number = a.reply_to_post_number
|
|
WHERE p.topic_id = :topic_id
|
|
#{deleted_recurse}
|
|
AND a.reply_to_post_number IS NOT NULL
|
|
#{op_stop}
|
|
AND a.depth < :limit
|
|
)
|
|
SELECT id, post_number, reply_to_post_number, depth, deleted_at FROM ancestors
|
|
SQL
|
|
end
|
|
end
|