mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-04 06:55:00 +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>
51 lines
1.1 KiB
Ruby
51 lines
1.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class NestedTopic::TogglePin
|
|
include Service::Base
|
|
|
|
params do
|
|
attribute :topic_id, :integer
|
|
attribute :post_id, :integer
|
|
|
|
validates :topic_id, presence: true
|
|
validates :post_id, presence: true
|
|
end
|
|
|
|
model :topic
|
|
model :post
|
|
policy :staff_can_edit
|
|
policy :post_is_root
|
|
model :nested_topic, :find_or_create_nested_topic
|
|
policy :within_pin_limit
|
|
transaction { step :toggle_pin }
|
|
|
|
private
|
|
|
|
def fetch_topic(params:)
|
|
Topic.find_by(id: params.topic_id)
|
|
end
|
|
|
|
def fetch_post(topic:, params:)
|
|
topic.posts.find_by(id: params.post_id)
|
|
end
|
|
|
|
def staff_can_edit(guardian:, topic:)
|
|
guardian.can_edit?(topic) && guardian.is_staff?
|
|
end
|
|
|
|
def post_is_root(post:)
|
|
post.reply_to_post_number.blank? || post.reply_to_post_number == 1
|
|
end
|
|
|
|
def find_or_create_nested_topic(topic:)
|
|
topic.nested_topic || NestedTopic.find_or_create_by!(topic: topic)
|
|
end
|
|
|
|
def within_pin_limit(nested_topic:, post:)
|
|
!nested_topic.pin_limit_reached? || nested_topic.pinned_post_ids.include?(post.id)
|
|
end
|
|
|
|
def toggle_pin(nested_topic:, post:)
|
|
nested_topic.toggle_pin(post.id)
|
|
end
|
|
end
|