Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Notification/tracking changes for nested replies (#39750)

Mark VanLandingham1e5e20ffc518
diff --git a/app/assets/stylesheets/common/components/badges.scss b/app/assets/stylesheets/common/components/badges.scss
index fd609940e1c..e93bddbacbc 100644
--- a/app/assets/stylesheets/common/components/badges.scss
+++ b/app/assets/stylesheets/common/components/badges.scss
@@ -122,12 +122,14 @@
font-weight: dark-light-choose(normal, bold);
}
- &.new-topic {
+ &.new-topic,
+ &.new-replies {
background-color: transparent;
padding-left: 0.15em;
}
- &.new-topic::before {
+ &.new-topic::before,
+ &.new-replies::before {
content: "";
background: var(--tertiary-med-or-tertiary);
display: inline-block;
diff --git a/app/models/notification.rb b/app/models/notification.rb
index 97c2be4d31d..fdaecdcf24f 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -11,6 +11,11 @@ class Notification < ActiveRecord::Base
MEMBERSHIP_REQUEST_CONSOLIDATION_WINDOW_HOURS = 24
+ # Bucket key used by nested-topic :replied consolidation for replies
+ # to the topic root (post 1) and for posts with no parent. Frontend
+ # mirror: TOPIC_ROOT_BUCKET in lib/notification-types/replied.js.
+ TOPIC_ROOT_BUCKET = 1
+
validates :data, presence: true
validates :notification_type, presence: true
@@ -282,7 +287,10 @@ class Notification < ActiveRecord::Base
end
def url
- topic.presence&.relative_url(post_number)
+ return if topic.blank?
+ return consolidated_nested_replied_url if consolidated_nested_replied?
+
+ topic.relative_url(post_number)
end
def post
@@ -421,6 +429,20 @@ class Notification < ActiveRecord::Base
protected
+ # We build /n/ directly here rather than letting /t/ redirect because
+ # the redirect strips the query string we need (sort, collapse_replies).
+ def consolidated_nested_replied?
+ notification_type == Notification.types[:replied] && data_hash["consolidated_count"].to_i > 1 &&
+ data_hash["reply_to_post_number"].present?
+ end
+
+ def consolidated_nested_replied_url
+ bucket = data_hash["reply_to_post_number"].to_i
+ slug_segment = topic.slug.present? ? "/#{topic.slug}" : ""
+ bucket_segment = bucket > TOPIC_ROOT_BUCKET ? "/#{bucket}" : ""
+ "#{Discourse.base_path}/n#{slug_segment}/#{topic.id}#{bucket_segment}?sort=new&collapse_replies=true"
+ end
+
def refresh_notification_count
User.find_by(id: user_id)&.publish_notifications_state if user_id
end
diff --git a/app/models/post.rb b/app/models/post.rb
index 69df17c11c1..66f8a091f62 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -518,12 +518,18 @@ class Post < ActiveRecord::Base
)
end
+ # In a nested-view topic, a "root" reply (no reply_to_post_number) is
+ # treated as targeting post 1, so the OP gets the :replied notification.
+ # New callers that don't want this behavior should branch on the topic
+ # type before calling.
def reply_notification_target
- return if reply_to_post_number.blank?
+ target_post_number = reply_to_post_number
+ target_post_number = 1 if target_post_number.blank? && topic&.nested_view?
+ return if target_post_number.blank?
Post.find_by(
"topic_id = :topic_id AND post_number = :post_number AND user_id <> :user_id",
topic_id: topic_id,
- post_number: reply_to_post_number,
+ post_number: target_post_number,
user_id: user_id,
).try(:user)
end
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 8744538033f..f2c6fb2b14a 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -704,6 +704,20 @@ class Topic < ActiveRecord::Base
self.archetype == Archetype.default
end
+ # Single source of truth for "this topic renders as nested." Both
+ # TopicListItemSerializer and TopicViewSerializer delegate here.
+ #
+ # NOTE: does not consider category.nested_replies_default. The category
+ # override is honored at *creation time* by the initializer in
+ # config/initializers/300-nested-replies.rb (which materializes a
+ # NestedTopic record). Topics created before a category was switched on
+ # therefore won't be nested unless the site-wide default is also on.
+ def nested_view?
+ return false unless SiteSetting.nested_replies_enabled
+ return false if private_message?
+ nested_topic.present? || SiteSetting.nested_replies_default
+ end
+
def open?
!self.closed?
end
diff --git a/app/models/topic_notifier.rb b/app/models/topic_notifier.rb
index d733698d3d5..4981f5a398a 100644
--- a/app/models/topic_notifier.rb
+++ b/app/models/topic_notifier.rb
@@ -20,6 +20,10 @@ class TopicNotifier
change_level user_id, :watching, reason
end
+ def track_topic!(user_id, reason = :created_topic)
+ change_level user_id, :tracking, reason
+ end
+
# Enable/disable the mute on the topic
def toggle_mute(user_id)
change_level user_id, (muted?(user_id) ? levels[:regular] : levels[:muted])
diff --git a/app/serializers/topic_list_item_serializer.rb b/app/serializers/topic_list_item_serializer.rb
index 6c4d02098e7..b3978d34f94 100644
--- a/app/serializers/topic_list_item_serializer.rb
+++ b/app/serializers/topic_list_item_serializer.rb
@@ -20,7 +20,8 @@ class TopicListItemSerializer < ListableTopicSerializer
:allowed_user_count,
:participant_groups,
:is_hot,
- :is_nested_view
+ :is_nested_view,
+ :has_new_replies
has_many :posters, serializer: TopicPosterSerializer, embed: :objects
has_many :participants, serializer: TopicPosterSerializer, embed: :objects
@@ -149,11 +150,28 @@ class TopicListItemSerializer < ListableTopicSerializer
end
def is_nested_view
- object.nested_topic.present? || SiteSetting.nested_replies_default
+ true
end
def include_is_nested_view?
- SiteSetting.nested_replies_enabled && !object.private_message?
+ object.nested_view?
+ end
+
+ # The *only* "topic has new content" signal we render for nested topics —
+ # the existing unread_posts/new_posts/unseen badges are deliberately not
+ # shown when is_nested_view is true (see post-count-or-badges.gjs).
+ def has_new_replies
+ true
+ end
+
+ def include_has_new_replies?
+ return false unless scope.user
+ return false unless object.nested_view?
+
+ last_visited = object.user_data&.last_visited_at
+ return false if last_visited.blank?
+ return false if object.last_post_user_id == scope.user.id
+ object.bumped_at > last_visited
end
private
diff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rb
index a730b493c98..7b1581dac39 100644
--- a/app/serializers/topic_view_serializer.rb
+++ b/app/serializers/topic_view_serializer.rb
@@ -341,10 +341,10 @@ class TopicViewSerializer < ApplicationSerializer
end
def is_nested_view
- object.topic.nested_topic.present? || SiteSetting.nested_replies_default
+ true
end
def include_is_nested_view?
- SiteSetting.nested_replies_enabled && !object.topic.private_message?
+ object.topic.nested_view?
end
end
diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb
index 76eecb1e4a5..8660764a99b 100644
--- a/app/services/post_alerter.rb
+++ b/app/services/post_alerter.rb
@@ -378,12 +378,46 @@ class PostAlerter
unread_posts(user, topic).count
end
- def destroy_notifications(user, types, topic)
+ # Bucket membership is the scope here — we don't re-apply the watching/reply_to
+ # filters that #unread_posts uses, since a recipient is by definition the
+ # parent author (or OP for bucket 1).
+ def unread_posts_in_bucket(user, topic, bucket_post_number)
+ last_read =
+ TopicUser.where(user_id: user.id, topic_id: topic.id).pick(:last_read_post_number) || 0
+
+ scope =
+ Post.secured(Guardian.new(user)).where(topic_id: topic.id).where("post_number > ?", last_read)
+
+ if bucket_post_number == Notification::TOPIC_ROOT_BUCKET
+ # Post 1 is the root itself, not a reply to it.
+ scope.where("reply_to_post_number IS NULL OR reply_to_post_number = 1").where(
+ "post_number > 1",
+ )
+ else
+ scope.where(reply_to_post_number: bucket_post_number)
+ end
+ end
+
+ def first_unread_post_in_bucket(user, topic, bucket_post_number)
+ unread_posts_in_bucket(user, topic, bucket_post_number).order("post_number").first
+ end
+
+ def unread_count_in_bucket(user, topic, bucket_post_number)
+ unread_posts_in_bucket(user, topic, bucket_post_number).count
+ end
+
+ def destroy_notifications(user, types, topic, bucket_post_number: nil)
return if user.blank?
return unless Guardian.new(user).can_see?(topic)
User.transaction do
- user.notifications.where(notification_type: types, topic_id: topic.id).destroy_all
+ scope = user.notifications.where(notification_type: types, topic_id: topic.id)
+ if bucket_post_number
+ # Compare as text — `data` is stored as text and a future malformed
+ # value (e.g. empty string) would raise on a `::int` cast.
+ scope = scope.where("data::jsonb ->> 'reply_to_post_number' = ?", bucket_post_number.to_s)
+ end
+ scope.destroy_all
# Reload so notification counts sync up correctly
user.reload
@@ -543,6 +577,9 @@ class PostAlerter
end
end
+ # Keyed on the *incoming* post's number. A consolidated nested-bucket row
+ # stores the bucket's first-unread post number, so it won't collide here —
+ # the COLLAPSED branch below destroys + recreates the bucket row.
existing_notifications =
user
.notifications
@@ -572,9 +609,23 @@ class PostAlerter
end
collapsed = false
+ bucket_post_number = nil
+ consolidated_count = nil
if COLLAPSED_NOTIFICATION_TYPES.include?(type)
- destroy_notifications(user, COLLAPSED_NOTIFICATION_TYPES, topic)
+ if type == Notification.types[:replied] && topic.nested_view?
+ bucket_post_number = post.reply_to_post_number || Notification::TOPIC_ROOT_BUCKET
+ destroy_notifications(
+ user,
+ [Notification.types[:replied]],
+ topic,
+ bucket_post_number: bucket_post_number,
+ )
+ else
+ types = COLLAPSED_NOTIFICATION_TYPES
+ types -= [Notification.types[:replied]] if topic.nested_view?
+ destroy_notifications(user, types, topic)
+ end
collapsed = true
end
@@ -582,9 +633,15 @@ class PostAlerter
original_username = opts[:display_username].presence || post.username
if collapsed
- post = first_unread_post(user, topic) || post
- count = unread_count(user, topic)
+ if bucket_post_number
+ post = first_unread_post_in_bucket(user, topic, bucket_post_number) || post
+ count = unread_count_in_bucket(user, topic, bucket_post_number)
+ else
+ post = first_unread_post(user, topic) || post
+ count = unread_count(user, topic)
+ end
if count > 1
+ consolidated_count = count
I18n.with_locale(user.effective_locale) do
opts[:display_username] = I18n.t("embed.replies", count: count)
end
@@ -610,6 +667,11 @@ class PostAlerter
display_username: opts[:display_username] || post.user.username,
}
+ if bucket_post_number
+ notification_data[:reply_to_post_number] = bucket_post_number
+ notification_data[:consolidated_count] = consolidated_count if consolidated_count
+ end
+
if display_name = opts[:display_name] || post.user.name
notification_data[:display_name] = display_name
end
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index ad6f3e43daf..8dc8b90be50 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -3425,6 +3425,12 @@ en:
liked_by_multiple_users:
one: "%{username} and %{count} other"
other: "%{username} and %{count} others"
+ replied_consolidated_in_topic:
+ one: "%{count} new reply in your topic"
+ other: "%{count} new replies in your topic"
+ replied_consolidated_to_post:
+ one: "%{count} new reply to your post"
+ other: "%{count} new replies to your post"
liked_consolidated_description:
one: "liked %{count} of your posts"
other: "liked %{count} of your posts"
@@ -3997,6 +4003,7 @@ en:
list: "Topics"
new: "new topic"
+ has_new_replies: "new replies since your last visit"
unread: "unread"
new_topics:
one: "%{count} new topic"
diff --git a/frontend/discourse/app/components/nested.gjs b/frontend/discourse/app/components/nested.gjs
index 45a45fad585..3b668a22f2d 100644
--- a/frontend/discourse/app/components/nested.gjs
+++ b/frontend/discourse/app/components/nested.gjs
@@ -146,6 +146,7 @@ export default class Nested extends Component {
@getCloakingData={{this.viewportTracker.getCloakingData}}
@cloakAbove={{this.cloakAbove}}
@cloakBelow={{this.cloakBelow}}
+ @collapseFromDepth={{if @collapseReplies 0}}
/>
{{else}}
<div class="nested-view__empty">
diff --git a/frontend/discourse/app/components/nested/context-view.gjs b/frontend/discourse/app/components/nested/context-view.gjs
index c35a193d8f6..45a2b49808e 100644
--- a/frontend/discourse/app/components/nested/context-view.gjs
+++ b/frontend/discourse/app/components/nested/context-view.gjs
@@ -191,6 +191,7 @@ export default class NestedContextView extends Component {
@getCloakingData={{this.viewportTracker.getCloakingData}}
@cloakAbove={{this.cloakAbove}}
@cloakBelow={{this.cloakBelow}}
+ @collapseFromDepth={{if @collapseReplies 1}}
/>
{{/each}}
</div>
diff --git a/frontend/discourse/app/components/nested/post-children.gjs b/frontend/discourse/app/components/nested/post-children.gjs
index 689ac0e1d4b..0bff7f610ca 100644
--- a/frontend/discourse/app/components/nested/post-children.gjs
+++ b/frontend/discourse/app/components/nested/post-children.gjs
@@ -231,6 +231,7 @@ export default class NestedPostChildren extends Component {
@fetchedChildrenCache={{@fetchedChildrenCache}}
@scrollAnchor={{@scrollAnchor}}
@registerPost={{@registerPost}}
+ @collapseFromDepth={{@collapseFromDepth}}
/>
{{/each}}
diff --git a/frontend/discourse/app/components/nested/post.gjs b/frontend/discourse/app/components/nested/post.gjs
index f033e91ac4b..9a207b9f781 100644
--- a/frontend/discourse/app/components/nested/post.gjs
+++ b/frontend/discourse/app/components/nested/post.gjs
@@ -64,10 +64,22 @@ export default class NestedPost extends Component {
this.expanded = cached.expanded;
this.collapsed = cached.collapsed;
} else {
- this.expanded =
+ const wouldExpand =
(this.args.children?.length ?? 0) > 0 ||
this.args.post.deleted_post_placeholder === true ||
this.args.post.ignored_post_placeholder === true;
+
+ // collapseFromDepth is the URL-driven cutoff (set by the parent view —
+ // 0 for root view, 1 for context view). At/below it, the post renders
+ // but its children start hidden behind an "Expand X replies" button.
+ if (
+ this.args.collapseFromDepth != null &&
+ this.args.depth >= this.args.collapseFromDepth
+ ) {
+ this.expanded = false;
+ } else {
+ this.expanded = wouldExpand;
+ }
this.collapsed = false;
}
@@ -593,6 +605,7 @@ export default class NestedPost extends Component {
@fetchedChildrenCache={{@fetchedChildrenCache}}
@scrollAnchor={{@scrollAnchor}}
@registerPost={{@registerPost}}
+ @collapseFromDepth={{@collapseFromDepth}}
/>
{{/if}}
</div>
diff --git a/frontend/discourse/app/components/topic-list/item/topic-cell.gjs b/frontend/discourse/app/components/topic-list/item/topic-cell.gjs
index cb086b74daa..19a229e1211 100644
--- a/frontend/discourse/app/components/topic-list/item/topic-cell.gjs
+++ b/frontend/discourse/app/components/topic-list/item/topic-cell.gjs
@@ -3,6 +3,7 @@ import { on } from "@ember/modifier";
import { action } from "@ember/object";
import PluginOutlet from "discourse/components/plugin-outlet";
import ActionList from "discourse/components/topic-list/action-list";
+import NewRepliesDot from "discourse/components/topic-list/new-replies-dot";
import ParticipantGroups from "discourse/components/topic-list/participant-groups";
import TopicExcerpt from "discourse/components/topic-list/topic-excerpt";
import TopicLink from "discourse/components/topic-list/topic-link";
@@ -74,7 +75,11 @@ export default class TopicCell extends Component {
/>
{{~! no whitespace ~}}
<UnreadIndicator @topic={{@topic}} />
- {{~#if @showTopicPostBadges~}}
+ {{~#if @topic.is_nested_view~}}
+ {{~#if @topic.has_new_replies~}}
+ <NewRepliesDot @topic={{@topic}} />
+ {{~/if~}}
+ {{~else if @showTopicPostBadges~}}
<TopicPostBadges
@unreadPosts={{@topic.unread_posts}}
@unseen={{@topic.unseen}}
diff --git a/frontend/discourse/app/components/topic-list/new-replies-dot.gjs b/frontend/discourse/app/components/topic-list/new-replies-dot.gjs
new file mode 100644
index 00000000000..59a10a38345
--- /dev/null
+++ b/frontend/discourse/app/components/topic-list/new-replies-dot.gjs
@@ -0,0 +1,14 @@
+import { i18n } from "discourse-i18n";
+
+const NewRepliesDot = <template>
+ {{~! no whitespace ~}}
+ <span class="topic-post-badges">&nbsp;<a
+ href={{@topic.lastUnreadUrl}}
+ title={{i18n "topic.has_new_replies"}}
+ aria-label={{i18n "topic.has_new_replies"}}
+ class="badge badge-notification new-replies"
+ >&nbsp;</a></span>
+ {{~! no whitespace ~}}
+</template>;
+
+export default NewRepliesDot;
diff --git a/frontend/discourse/app/components/topic-list/post-count-or-badges.gjs b/frontend/discourse/app/components/topic-list/post-count-or-badges.gjs
index b36f99c6ed4..660e4e6b9e8 100644
--- a/frontend/discourse/app/components/topic-list/post-count-or-badges.gjs
+++ b/frontend/discourse/app/components/topic-list/post-count-or-badges.gjs
@@ -1,9 +1,16 @@
import ItemRepliesCell from "discourse/components/topic-list/item/replies-cell";
+import NewRepliesDot from "discourse/components/topic-list/new-replies-dot";
import TopicPostBadges from "discourse/components/topic-post-badges";
import { and } from "discourse/truth-helpers";
const PostCountOrBadges = <template>
- {{#if (and @postBadgesEnabled @topic.unread_posts)}}
+ {{#if @topic.is_nested_view}}
+ {{#if @topic.has_new_replies}}
+ <NewRepliesDot @topic={{@topic}} />
+ {{else}}
+ <ItemRepliesCell @topic={{@topic}} @tagName="div" />
+ {{/if}}
+ {{else if (and @postBadgesEnabled @topic.unread_posts)}}
<TopicPostBadges
@unreadPosts={{@topic.unread_posts}}
@unseen={{@topic.unseen}}
diff --git a/frontend/discourse/app/controllers/nested.js b/frontend/discourse/app/controllers/nested.js
index ec74562b9b2..71c2364f66a 100644
--- a/frontend/discourse/app/controllers/nested.js
+++ b/frontend/discourse/app/controllers/nested.js
@@ -42,7 +42,13 @@ export default class NestedController extends Controller {
@tracked newRootPostIds = [];
@tracked editingTopic = false;
@tracked pinnedPostIds = [];
- queryParams = ["sort", "context"];
+ // Persisted in the URL across in-topic navigation by design — once a
+ // user lands via a consolidated reply notification, browsing within
+ // the topic keeps the collapsed view, and the URL is shareable in that
+ // state. If we ever want to scope it to entry-only, clear after the
+ // initial render in the route.
+ @tracked collapseReplies = false;
+ queryParams = ["sort", "context", { collapseReplies: "collapse_replies" }];
// Externalized expansion state: postNumber → { expanded, collapsed }
// Components read on construction, write on toggle.
diff --git a/frontend/discourse/app/lib/notification-types-manager.js b/frontend/discourse/app/lib/notification-types-manager.js
index 4eadd4a56b7..58171d52e14 100644
--- a/frontend/discourse/app/lib/notification-types-manager.js
+++ b/frontend/discourse/app/lib/notification-types-manager.js
@@ -14,6 +14,7 @@ import MembershipRequestAccepted from "discourse/lib/notification-types/membersh
import MembershipRequestConsolidated from "discourse/lib/notification-types/membership-request-consolidated";
import MovedPost from "discourse/lib/notification-types/moved-post";
import NewFeatures from "discourse/lib/notification-types/new-features";
+import Replied from "discourse/lib/notification-types/replied";
import UpcomingChangeAutomaticallyPromoted from "discourse/lib/notification-types/upcoming-change-automatically-promoted";
import UpcomingChangeAvailable from "discourse/lib/notification-types/upcoming-change-available";
import WatchingFirstPost from "discourse/lib/notification-types/watching-first-post";
@@ -34,6 +35,7 @@ const CLASS_FOR_TYPE = {
moved_post: MovedPost,
new_features: NewFeatures,
admin_problems: AdminProblems,
+ replied: Replied,
upcoming_change_available: UpcomingChangeAvailable,
upcoming_change_automatically_promoted: UpcomingChangeAutomaticallyPromoted,
watching_first_post: WatchingFirstPost,
diff --git a/frontend/discourse/app/lib/notification-types/replied.js b/frontend/discourse/app/lib/notification-types/replied.js
new file mode 100644
index 00000000000..e71089789b1
--- /dev/null
+++ b/frontend/discourse/app/lib/notification-types/replied.js
@@ -0,0 +1,45 @@
+import getURL from "discourse/lib/get-url";
+import NotificationTypeBase from "discourse/lib/notification-types/base";
+import { i18n } from "discourse-i18n";
+
+// Mirror of Notification::TOPIC_ROOT_BUCKET on the backend
+// (app/models/notification.rb).
+export const TOPIC_ROOT_BUCKET = 1;
+
+export default class extends NotificationTypeBase {
+ get isBucketed() {
+ return this.notification.data.reply_to_post_number != null;
+ }
+
+ get isTopicBucket() {
+ return this.notification.data.reply_to_post_number === TOPIC_ROOT_BUCKET;
+ }
+
+ get consolidatedCount() {
+ return this.notification.data.consolidated_count;
+ }
+
+ // Must stay in sync with Notification#consolidated_nested_replied_url.
+ // Targets the bucket parent's level (sort=new + collapse_replies=true)
+ // rather than a single post so the user sees all the new content at once.
+ get linkHref() {
+ if (this.isBucketed && this.consolidatedCount > 1 && this.topicId) {
+ let url = getURL(`/n/${this.notification.slug}/${this.topicId}`);
+ if (!this.isTopicBucket) {
+ url += `/${this.notification.data.reply_to_post_number}`;
+ }
+ return `${url}?sort=new&collapse_replies=true`;
+ }
+ return super.linkHref;
+ }
+
+ get label() {
+ if (this.consolidatedCount > 1 && this.isBucketed) {
+ const key = this.isTopicBucket
+ ? "notifications.replied_consolidated_in_topic"
+ : "notifications.replied_consolidated_to_post";
+ return i18n(key, { count: this.consolidatedCount });
+ }
+ return super.label;
+ }
+}
diff --git a/frontend/discourse/app/routes/nested.js b/frontend/discourse/app/routes/nested.js
index bfb8c38dabc..13738305198 100644
--- a/frontend/discourse/app/routes/nested.js
+++ b/frontend/discourse/app/routes/nested.js
@@ -19,6 +19,7 @@ export default class NestedRoute extends Route {
queryParams = {
sort: { refreshModel: true },
context: { refreshModel: true },
+ collapseReplies: { refreshModel: false },
};
buildRouteInfoMetadata() {
diff --git a/frontend/discourse/app/templates/nested.gjs b/frontend/discourse/app/templates/nested.gjs
index cc581371dc2..34b7413d2a7 100644
--- a/frontend/discourse/app/templates/nested.gjs
+++ b/frontend/discourse/app/templates/nested.gjs
@@ -43,6 +43,7 @@ export default <template>
@fetchedChildrenCache={{@controller.fetchedChildrenCache}}
@scrollAnchor={{@controller.scrollAnchor}}
@showActivityLog={{@controller.showActivityLog}}
+ @collapseReplies={{@controller.collapseReplies}}
/>
{{else}}
<Nested
@@ -78,6 +79,7 @@ export default <template>
@fetchedChildrenCache={{@controller.fetchedChildrenCache}}
@scrollAnchor={{@controller.scrollAnchor}}
@showActivityLog={{@controller.showActivityLog}}
+ @collapseReplies={{@controller.collapseReplies}}
/>
{{/if}}
</template>
diff --git a/frontend/discourse/tests/integration/components/topic-list-post-count-or-badges-test.gjs b/frontend/discourse/tests/integration/components/topic-list-post-count-or-badges-test.gjs
new file mode 100644
index 00000000000..0d3be30e34f
--- /dev/null
+++ b/frontend/discourse/tests/integration/components/topic-list-post-count-or-badges-test.gjs
@@ -0,0 +1,104 @@
+import { render } from "@ember/test-helpers";
+import { module, test } from "qunit";
+import PostCountOrBadges from "discourse/components/topic-list/post-count-or-badges";
+import { setupRenderingTest } from "discourse/tests/helpers/component-test";
+
+module(
+ "Integration | Component | topic-list/post-count-or-badges",
+ function (hooks) {
+ setupRenderingTest(hooks);
+
+ test("nested topic with new replies renders the new-replies dot", async function (assert) {
+ const store = this.owner.lookup("service:store");
+ const topic = store.createRecord("topic", {
+ id: 1,
+ is_nested_view: true,
+ has_new_replies: true,
+ unread_posts: 0,
+ });
+
+ await render(
+ <template>
+ <PostCountOrBadges @topic={{topic}} @postBadgesEnabled={{true}} />
+ </template>
+ );
+
+ assert.dom(".badge-notification.new-replies").exists();
+ assert.dom(".badge-notification.unread-posts").doesNotExist();
+ });
+
+ test("nested topic without new replies falls back to the post-count cell", async function (assert) {
+ const store = this.owner.lookup("service:store");
+ const topic = store.createRecord("topic", {
+ id: 2,
+ is_nested_view: true,
+ has_new_replies: false,
+ unread_posts: 0,
+ });
+
+ await render(
+ <template>
+ <PostCountOrBadges @topic={{topic}} @postBadgesEnabled={{true}} />
+ </template>
+ );
+
+ assert.dom(".badge-notification.new-replies").doesNotExist();
+ assert.dom(".badge-notification.new-topic").doesNotExist();
+ });
+
+ test("nested topic does not render the unread-posts count even if unread_posts > 0", async function (assert) {
+ const store = this.owner.lookup("service:store");
+ const topic = store.createRecord("topic", {
+ id: 3,
+ is_nested_view: true,
+ has_new_replies: false,
+ unread_posts: 5,
+ });
+
+ await render(
+ <template>
+ <PostCountOrBadges @topic={{topic}} @postBadgesEnabled={{true}} />
+ </template>
+ );
+
+ assert.dom(".badge-notification.unread-posts").doesNotExist();
+ });
+
+ test("flat topic with unread_posts still renders the unread-posts count", async function (assert) {
+ const store = this.owner.lookup("service:store");
+ const topic = store.createRecord("topic", {
+ id: 4,
+ is_nested_view: false,
+ unread_posts: 3,
+ unseen: false,
+ });
+
+ await render(
+ <template>
+ <PostCountOrBadges @topic={{topic}} @postBadgesEnabled={{true}} />
+ </template>
+ );
+
+ assert.dom(".badge-notification.unread-posts").exists();
+ assert.dom(".badge-notification.new-replies").doesNotExist();
+ });
+
+ test("flat topic with no unread_posts falls back to the post-count cell", async function (assert) {
+ const store = this.owner.lookup("service:store");
+ const topic = store.createRecord("topic", {
+ id: 5,
+ is_nested_view: false,
+ unread_posts: 0,
+ });
+
+ await render(
+ <template>
+ <PostCountOrBadges @topic={{topic}} @postBadgesEnabled={{true}} />
+ </template>
+ );
+
+ assert.dom(".badge-notification.unread-posts").doesNotExist();
+ assert.dom(".badge-notification.new-replies").doesNotExist();
+ });
+ }
+);
diff --git a/frontend/discourse/tests/unit/lib/notification-types/replied-test.js b/frontend/discourse/tests/unit/lib/notification-types/replied-test.js
new file mode 100644
index 00000000000..683a57427c9
--- /dev/null
+++ b/frontend/discourse/tests/unit/lib/notification-types/replied-test.js
@@ -0,0 +1,174 @@
+import { getOwner } from "@ember/owner";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import { deepMerge } from "discourse/lib/object";
+import Notification from "discourse/models/notification";
+import { NOTIFICATION_TYPES } from "discourse/tests/fixtures/concerns/notification-types";
+import { createRenderDirector } from "discourse/tests/helpers/notification-types-helper";
+import { i18n } from "discourse-i18n";
+
+function getNotification(overrides = {}) {
+ return Notification.create(
+ deepMerge(
+ {
+ id: 11,
+ user_id: 1,
+ notification_type: NOTIFICATION_TYPES.replied,
+ read: false,
+ high_priority: false,
+ created_at: "2022-07-01T06:00:32.173Z",
+ topic_id: 100,
+ post_number: 5,
+ slug: "test-topic",
+ fancy_title: "Test Topic",
+ data: {
+ topic_title: "Test Topic",
+ original_post_id: 999,
+ original_post_type: 1,
+ original_username: "replier",
+ display_username: "replier",
+ },
+ },
+ overrides
+ )
+ );
+}
+
+module("Unit | Notification Types | replied", function (hooks) {
+ setupTest(hooks);
+
+ test("linkHref points at the post for a flat-topic reply", function (assert) {
+ const notification = getNotification();
+ const director = createRenderDirector(
+ notification,
+ "replied",
+ getOwner(this).lookup("service:site-settings")
+ );
+ assert.strictEqual(director.linkHref, "/t/test-topic/100/5");
+ });
+
+ test("linkHref points at the specific post for a singular nested-topic-bucket reply", function (assert) {
+ const notification = getNotification({
+ data: { reply_to_post_number: 1 },
+ });
+ const director = createRenderDirector(
+ notification,
+ "replied",
+ getOwner(this).lookup("service:site-settings")
+ );
+ assert.strictEqual(director.linkHref, "/t/test-topic/100/5");
+ });
+
+ test("linkHref points at the specific post for a singular per-post-bucket reply", function (assert) {
+ const notification = getNotification({
+ data: { reply_to_post_number: 4 },
+ });
+ const director = createRenderDirector(
+ notification,
+ "replied",
+ getOwner(this).lookup("service:site-settings")
+ );
+ assert.strictEqual(director.linkHref, "/t/test-topic/100/5");
+ });
+
+ test("linkHref targets the topic root with sort=new&collapse_replies=true for a consolidated topic-bucket notification", function (assert) {
+ const notification = getNotification({
+ data: { reply_to_post_number: 1, consolidated_count: 3 },
+ });
+ const director = createRenderDirector(
+ notification,
+ "replied",
+ getOwner(this).lookup("service:site-settings")
+ );
+ assert.strictEqual(
+ director.linkHref,
+ "/n/test-topic/100?sort=new&collapse_replies=true"
+ );
+ });
+
+ test("linkHref targets the bucket parent's context with sort=new&collapse_replies=true for a consolidated per-post-bucket notification", function (assert) {
+ const notification = getNotification({
+ data: { reply_to_post_number: 4, consolidated_count: 2 },
+ });
+ const director = createRenderDirector(
+ notification,
+ "replied",
+ getOwner(this).lookup("service:site-settings")
+ );
+ assert.strictEqual(
+ director.linkHref,
+ "/n/test-topic/100/4?sort=new&collapse_replies=true"
+ );
+ });
+
+ test("linkHref is unchanged for flat-topic consolidated notifications", function (assert) {
+ // Flat topics never get the bucket fields (regression guard).
+ const notification = getNotification();
+ const director = createRenderDirector(
+ notification,
+ "replied",
+ getOwner(this).lookup("service:site-settings")
+ );
+ assert.strictEqual(director.linkHref, "/t/test-topic/100/5");
+ });
+
+ test("label uses replied_consolidated_in_topic for the topic bucket when consolidated", function (assert) {
+ const notification = getNotification({
+ data: {
+ reply_to_post_number: 1,
+ consolidated_count: 3,
+ display_username: "3 replies",
+ },
+ });
+ const director = createRenderDirector(
+ notification,
+ "replied",
+ getOwner(this).lookup("service:site-settings")
+ );
+ assert.strictEqual(
+ director.label,
+ i18n("notifications.replied_consolidated_in_topic", { count: 3 })
+ );
+ });
+
+ test("label uses replied_consolidated_to_post for a per-post bucket when consolidated", function (assert) {
+ const notification = getNotification({
+ data: {
+ reply_to_post_number: 4,
+ consolidated_count: 2,
+ display_username: "2 replies",
+ },
+ });
+ const director = createRenderDirector(
+ notification,
+ "replied",
+ getOwner(this).lookup("service:site-settings")
+ );
+ assert.strictEqual(
+ director.label,
+ i18n("notifications.replied_consolidated_to_post", { count: 2 })
+ );
+ });
+
+ test("label falls back to display_username for a single (non-consolidated) reply", function (assert) {
+ const notification = getNotification({
+ data: { reply_to_post_number: 1 },
+ });
+ const director = createRenderDirector(
+ notification,
+ "replied",
+ getOwner(this).lookup("service:site-settings")
+ );
+ assert.strictEqual(director.label, "replier");
+ });
+
+ test("label falls back to display_username for flat topics (no bucket)", function (assert) {
+ const notification = getNotification();
+ const director = createRenderDirector(
+ notification,
+ "replied",
+ getOwner(this).lookup("service:site-settings")
+ );
+ assert.strictEqual(director.label, "replier");
+ });
+});
diff --git a/lib/topic_creator.rb b/lib/topic_creator.rb
index cffceb9ecf5..0a1b99040d0 100644
--- a/lib/topic_creator.rb
+++ b/lib/topic_creator.rb
@@ -69,7 +69,8 @@ class TopicCreator
process_private_message(topic)
save_topic(topic)
create_warning(topic)
- watch_topic(topic)
+ set_author_notification_level(topic)
+ apply_pm_recipient_notification_levels(topic)
create_shared_draft(topic)
UserActionManager.topic_created(topic)
@@ -111,15 +112,25 @@ class TopicCreator
UserWarning.create(topic: topic, user: @added_users.first, created_by: @user)
end
- def watch_topic(topic)
- topic.notifier.watch_topic!(topic.user_id) unless @opts[:auto_track] == false
+ def set_author_notification_level(topic)
+ return if @opts[:auto_track] == false
+
+ if topic.nested_view?
+ topic.notifier.track_topic!(topic.user_id)
+ else
+ topic.notifier.watch_topic!(topic.user_id)
+ end
+ end
+
+ def apply_pm_recipient_notification_levels(topic)
+ return unless topic.private_message?
topic.reload.topic_allowed_users.each do |tau|
next if tau.user_id == -1 || tau.user_id == topic.user_id
topic.notifier.watch!(tau.user_id)
end
- topic.reload.topic_allowed_groups.each do |topic_allowed_group|
+ topic.topic_allowed_groups.each do |topic_allowed_group|
group = topic_allowed_group.group
begin
diff --git a/spec/lib/nested_replies/nested_replies_spec.rb b/spec/lib/nested_replies/nested_replies_spec.rb
index 0e9eb9135fb..1de559bad61 100644
--- a/spec/lib/nested_replies/nested_replies_spec.rb
+++ b/spec/lib/nested_replies/nested_replies_spec.rb
@@ -98,13 +98,13 @@ RSpec.describe NestedReplies do
expect(json[:is_nested_view]).to eq(true)
end
- it "returns false for is_nested_view when not set" do
+ it "omits is_nested_view when the topic isn't nested" do
topic.nested_topic&.destroy!
topic_view = TopicView.new(topic.id, user)
json = TopicViewSerializer.new(topic_view, scope: Guardian.new(user), root: false).as_json
- expect(json[:is_nested_view]).to eq(false)
+ expect(json.key?(:is_nested_view)).to eq(false)
end
context "when topic is a private message" do
diff --git a/spec/lib/topic_creator_spec.rb b/spec/lib/topic_creator_spec.rb
index 0b1b847ade0..2a3e3cfded9 100644
--- a/spec/lib/topic_creator_spec.rb
+++ b/spec/lib/topic_creator_spec.rb
@@ -28,7 +28,7 @@ RSpec.describe TopicCreator do
context "with topic success cases" do
before do
TopicCreator.any_instance.expects(:save_topic).returns(true)
- TopicCreator.any_instance.expects(:watch_topic).returns(true)
+ TopicCreator.any_instance.expects(:apply_pm_recipient_notification_levels).returns(true)
SiteSetting.allow_duplicate_topic_titles = true
end
@@ -550,7 +550,7 @@ RSpec.describe TopicCreator do
context "with success cases" do
before do
TopicCreator.any_instance.expects(:save_topic).returns(true)
- TopicCreator.any_instance.expects(:watch_topic).returns(true)
+ TopicCreator.any_instance.expects(:apply_pm_recipient_notification_levels).returns(true)
SiteSetting.allow_duplicate_topic_titles = true
SiteSetting.enable_staged_users = true
end
@@ -735,6 +735,102 @@ RSpec.describe TopicCreator do
end
end
end
+
+ context "with nested-view topics" do
+ before do
+ SiteSetting.nested_replies_enabled = true
+ SiteSetting.nested_replies_default = true
+ end
+
+ it "sets the OP to Tracking instead of Watching" do
+ topic = TopicCreator.create(user, Guardian.new(user), valid_attrs)
+ topic_user = TopicUser.find_by(user_id: user.id, topic_id: topic.id)
+
+ expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:tracking])
+ expect(topic_user.notifications_reason_id).to eq(
+ TopicUser.notification_reasons[:created_topic],
+ )
+ end
+
+ it "still sets the OP to Watching when the topic is flat" do
+ SiteSetting.nested_replies_default = false
+ topic = TopicCreator.create(user, Guardian.new(user), valid_attrs)
+ topic_user = TopicUser.find_by(user_id: user.id, topic_id: topic.id)
+
+ expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:watching])
+ end
+
+ it "still sets the OP to Watching when the nested feature is disabled" do
+ SiteSetting.nested_replies_enabled = false
+ topic = TopicCreator.create(user, Guardian.new(user), valid_attrs)
+ topic_user = TopicUser.find_by(user_id: user.id, topic_id: topic.id)
+
+ expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:watching])
+ end
+
+ context "when going through the full post-creation flow (TopicCreator + PostCreator)" do
+ # These tests use create_post which goes through PostCreator -> TopicCreator,
+ # exercising the same auto_notification step that runs in production.
+
+ it "respects notification_level_when_replying when the user has set it to Watching" do
+ user.user_option.update!(
+ notification_level_when_replying: TopicUser.notification_levels[:watching],
+ )
+
+ post = create_post(user: user)
+
+ topic_user = TopicUser.find_by(user_id: user.id, topic_id: post.topic_id)
+ expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:watching])
+ end
+
+ it "respects notification_level_when_replying when the user has set it to Regular" do
+ user.user_option.update!(
+ notification_level_when_replying: TopicUser.notification_levels[:regular],
+ )
+
+ post = create_post(user: user)
+
+ topic_user = TopicUser.find_by(user_id: user.id, topic_id: post.topic_id)
+ # auto_notification's guard prevents downgrading below the level
+ # TopicCreator already set, so the user stays at Tracking. This is
+ # the same behavior as flat topics with replying=:regular.
+ expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:tracking])
+ end
+
+ # Pin existing behavior: TagUser.auto_watch fires from Topic#after_save
+ # (when tags_changed), which runs *before* set_author_notification_level
+ # creates the TopicUser row, so the UPDATE matches no rows and the
+ # OP keeps the level TopicCreator set. Same shape applies for
+ # CategoryUser.auto_watch (only fires on category change). If we ever
+ # want a watched-tag/category to override the nested-OP-Tracking
+ # default, we'd need an explicit honor-tag-or-category step in
+ # set_author_notification_level. Calling that out as a known limit.
+ it "does NOT auto-bump the OP from Tracking to Watching for a watched tag" do
+ SiteSetting.tagging_enabled = true
+ tag = Fabricate(:tag, name: "watched-by-user")
+ TagUser.change(user.id, tag.id, TagUser.notification_levels[:watching])
+
+ post = create_post(user: user, tags: [tag.name])
+
+ topic_user = TopicUser.find_by(user_id: user.id, topic_id: post.topic_id)
+ expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:tracking])
+ end
+
+ it "does NOT auto-bump the OP from Tracking to Watching for a watched category" do
+ category = Fabricate(:category)
+ CategoryUser.set_notification_level_for_category(
+ user,
+ CategoryUser.notification_levels[:watching],
+ category.id,
+ )
+
+ post = create_post(user: user, category: category.id)
+
+ topic_user = TopicUser.find_by(user_id: user.id, topic_id: post.topic_id)
+ expect(topic_user.notification_level).to eq(TopicUser.notification_levels[:tracking])
+ end
+ end
+ end
end
describe "private message with target_user_ids" do
diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb
index 42ec13cddc4..ef106ba5178 100644
--- a/spec/models/notification_spec.rb
+++ b/spec/models/notification_spec.rb
@@ -295,6 +295,79 @@ RSpec.describe Notification do
end
end
+ describe "#url" do
+ fab!(:topic)
+ fab!(:post) { Fabricate(:post, topic: topic, post_number: 5) }
+
+ it "returns the relative topic url for a regular reply notification" do
+ notification =
+ Fabricate(
+ :notification,
+ notification_type: Notification.types[:replied],
+ topic: topic,
+ post_number: 5,
+ data: { topic_title: topic.title, display_username: "u" }.to_json,
+ )
+
+ expect(notification.url).to eq(topic.relative_url(5))
+ end
+
+ it "returns the topic root with sort=new&collapse_replies=true for a consolidated topic-bucket notification" do
+ notification =
+ Fabricate(
+ :notification,
+ notification_type: Notification.types[:replied],
+ topic: topic,
+ post_number: 7,
+ data: {
+ topic_title: topic.title,
+ display_username: "3 replies",
+ reply_to_post_number: 1,
+ consolidated_count: 3,
+ }.to_json,
+ )
+
+ expect(notification.url).to eq("/n/#{topic.slug}/#{topic.id}?sort=new&collapse_replies=true")
+ end
+
+ it "returns the bucket parent's context with sort=new&collapse_replies=true for a consolidated per-post-bucket notification" do
+ notification =
+ Fabricate(
+ :notification,
+ notification_type: Notification.types[:replied],
+ topic: topic,
+ post_number: 7,
+ data: {
+ topic_title: topic.title,
+ display_username: "2 replies",
+ reply_to_post_number: 4,
+ consolidated_count: 2,
+ }.to_json,
+ )
+
+ expect(notification.url).to eq(
+ "/n/#{topic.slug}/#{topic.id}/4?sort=new&collapse_replies=true",
+ )
+ end
+
+ it "uses the legacy specific-post URL for a singular nested-bucket notification" do
+ notification =
+ Fabricate(
+ :notification,
+ notification_type: Notification.types[:replied],
+ topic: topic,
+ post_number: 5,
+ data: {
+ topic_title: topic.title,
+ display_username: "u",
+ reply_to_post_number: 1,
+ }.to_json,
+ )
+
+ expect(notification.url).to eq(topic.relative_url(5))
+ end
+ end
+
describe "data" do
let(:notification) { Fabricate.build(:notification) }
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index 5bf26ccda00..1b131d3d011 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -2578,4 +2578,63 @@ RSpec.describe Post do
expect(post.reload.locale).to eq(nil)
end
end
+
+ describe "#reply_notification_target" do
+ fab!(:op_user, :user)
+ fab!(:topic) { Fabricate(:topic, user: op_user) }
+ fab!(:first_post) { Fabricate(:post, topic: topic, user: op_user, post_number: 1) }
+
+ it "returns the parent post's user when reply_to_post_number is set" do
+ replier = Fabricate(:user)
+ target_user = Fabricate(:user)
+ target_post = Fabricate(:post, topic: topic, user: target_user, post_number: 2)
+ reply = Fabricate(:post, topic: topic, user: replier, reply_to_post_number: 2)
+
+ expect(reply.reply_notification_target).to eq(target_user)
+ end
+
+ it "returns nil for a root post in a flat topic" do
+ replier = Fabricate(:user)
+ root = Fabricate(:post, topic: topic, user: replier, reply_to_post_number: nil)
+
+ expect(root.reply_notification_target).to be_nil
+ end
+
+ context "when the topic is in nested view" do
+ before do
+ SiteSetting.nested_replies_enabled = true
+ Fabricate(:nested_topic, topic: topic)
+ end
+
+ it "redirects a root post to the topic's first-post author" do
+ replier = Fabricate(:user)
+ root = Fabricate(:post, topic: topic, user: replier, reply_to_post_number: nil)
+
+ expect(root.reply_notification_target).to eq(op_user)
+ end
+
+ it "returns nil when the OP posts a root themselves" do
+ root = Fabricate(:post, topic: topic, user: op_user, reply_to_post_number: nil)
+
+ expect(root.reply_notification_target).to be_nil
+ end
+
+ it "still resolves to the parent author for a non-root reply" do
+ replier = Fabricate(:user)
+ target_user = Fabricate(:user)
+ Fabricate(:post, topic: topic, user: target_user, post_number: 2)
+ reply = Fabricate(:post, topic: topic, user: replier, reply_to_post_number: 2)
+
+ expect(reply.reply_notification_target).to eq(target_user)
+ end
+
+ it "returns nil when the first post has been deleted" do
+ first_post.trash!
+ replier = Fabricate(:user)
+ root = Fabricate(:post, topic: topic, user: replier, reply_to_post_number: nil)
+
+ expect(root.reply_notification_target).to be_nil
+ end
+ end
+ end
end
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index 838edd579d9..40e96a86ea4 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -3917,4 +3917,34 @@ RSpec.describe Topic do
expect(topic.in_user_locale?).to eq(false)
end
end
+
+ describe "#nested_view?" do
+ fab!(:topic)
+
+ it "returns false when nested_replies_enabled is off" do
+ SiteSetting.nested_replies_enabled = false
+ SiteSetting.nested_replies_default = true
+ Fabricate(:nested_topic, topic: topic)
+ expect(topic.reload.nested_view?).to eq(false)
+ end
+
+ it "returns true when the topic has a nested_topic record and the feature is enabled" do
+ SiteSetting.nested_replies_enabled = true
+ Fabricate(:nested_topic, topic: topic)
+ expect(topic.reload.nested_view?).to eq(true)
+ end
+
+ it "returns true when nested_replies_default is on, even without a nested_topic record" do
+ SiteSetting.nested_replies_enabled = true
+ SiteSetting.nested_replies_default = true
+ expect(topic.nested_view?).to eq(true)
+ end
+
+ it "returns false for private messages even when defaults are on" do
+ SiteSetting.nested_replies_enabled = true
+ SiteSetting.nested_replies_default = true
+ pm = Fabricate(:private_message_topic)
+ expect(pm.nested_view?).to eq(false)
+ end
+ end
end
diff --git a/spec/serializers/topic_list_item_serializer_spec.rb b/spec/serializers/topic_list_item_serializer_spec.rb
index ec60d1a35d3..8ab9dffd023 100644
--- a/spec/serializers/topic_list_item_serializer_spec.rb
+++ b/spec/serializers/topic_list_item_serializer_spec.rb
@@ -298,6 +298,99 @@ RSpec.describe TopicListItemSerializer do
end
end
+ describe "#has_new_replies" do
+ fab!(:user)
+ fab!(:other_user, :user)
+ fab!(:nested_topic_record, :topic) { Fabricate(:topic, user: other_user) }
+
+ before do
+ SiteSetting.nested_replies_enabled = true
+ Fabricate(:nested_topic, topic: nested_topic_record)
+ end
+
+ def serialize_with_user_data(last_visited_at:)
+ nested_topic_record.user_data =
+ TopicUser
+ .find_or_create_by(user: user, topic: nested_topic_record)
+ .tap { |tu| tu.update!(last_visited_at: last_visited_at) }
+ TopicListItemSerializer.new(
+ nested_topic_record,
+ scope: Guardian.new(user),
+ root: false,
+ ).as_json
+ end
+
+ it "is included when bumped_at is after last_visited_at and the user wasn't the last poster" do
+ nested_topic_record.update!(bumped_at: 1.minute.ago, last_post_user_id: other_user.id)
+ json = serialize_with_user_data(last_visited_at: 5.minutes.ago)
+
+ expect(json[:has_new_replies]).to eq(true)
+ end
+
+ it "is omitted when last_visited_at is more recent than bumped_at" do
+ nested_topic_record.update!(bumped_at: 5.minutes.ago, last_post_user_id: other_user.id)
+ json = serialize_with_user_data(last_visited_at: 1.minute.ago)
+
+ expect(json.key?(:has_new_replies)).to eq(false)
+ end
+
+ it "is omitted when the current user was the last poster" do
+ nested_topic_record.update!(bumped_at: 1.minute.ago, last_post_user_id: user.id)
+ json = serialize_with_user_data(last_visited_at: 5.minutes.ago)
+
+ expect(json.key?(:has_new_replies)).to eq(false)
+ end
+
+ it "is omitted when the user has never visited the topic" do
+ nested_topic_record.update!(bumped_at: 1.minute.ago, last_post_user_id: other_user.id)
+ json = serialize_with_user_data(last_visited_at: nil)
+
+ expect(json.key?(:has_new_replies)).to eq(false)
+ end
+
+ it "is omitted from the payload for flat topics" do
+ flat_topic = Fabricate(:topic, user: other_user)
+ flat_topic.user_data =
+ TopicUser
+ .find_or_create_by(user: user, topic: flat_topic)
+ .tap { |tu| tu.update!(last_visited_at: 5.minutes.ago) }
+
+ json = TopicListItemSerializer.new(flat_topic, scope: Guardian.new(user), root: false).as_json
+
+ expect(json.key?(:has_new_replies)).to eq(false)
+ end
+
+ it "is omitted from the payload when the user is anonymous" do
+ json =
+ TopicListItemSerializer.new(nested_topic_record, scope: Guardian.new, root: false).as_json
+
+ expect(json.key?(:has_new_replies)).to eq(false)
+ end
+
+ it "leaves the existing flat-topic unread payload (unread_posts, unseen) intact" do
+ flat_topic = Fabricate(:topic, user: other_user, bumped_at: 1.minute.ago)
+ Fabricate(:post, topic: flat_topic, user: other_user)
+ Fabricate(:post, topic: flat_topic, user: other_user)
+ flat_topic.reload
+ flat_topic.user_data =
+ TopicUser
+ .find_or_create_by(user: user, topic: flat_topic)
+ .tap do |tu|
+ tu.update!(
+ last_visited_at: 5.minutes.ago,
+ last_read_post_number: 1,
+ notification_level: TopicUser.notification_levels[:watching],
+ )
+ end
+
+ json = TopicListItemSerializer.new(flat_topic, scope: Guardian.new(user), root: false).as_json
+
+ expect(json[:unread_posts]).to be > 0
+ expect(json.key?(:unseen)).to eq(true)
+ expect(json.key?(:has_new_replies)).to eq(false)
+ end
+ end
+
describe "#is_hot" do
describe "including the attr based on theme modifier or plugin registry" do
fab!(:hot_topic, :topic)
diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb
index 05493c0ce9d..38db5ddd150 100644
--- a/spec/services/post_alerter_spec.rb
+++ b/spec/services/post_alerter_spec.rb
@@ -1888,6 +1888,330 @@ RSpec.describe PostAlerter do
end
end
+ context "with replies in a nested topic" do
+ fab!(:op, :user)
+ fab!(:replier, :user)
+ fab!(:third_party, :user)
+ fab!(:nested_topic_record) { Fabricate(:topic, user: op) }
+ fab!(:nested_first_post) do
+ Fabricate(:post, topic: nested_topic_record, user: op, post_number: 1)
+ end
+
+ before do
+ SiteSetting.nested_replies_enabled = true
+ Fabricate(:nested_topic, topic: nested_topic_record)
+ end
+
+ it "notifies the OP via :replied for a root post (no reply_to_post_number)" do
+ root = Fabricate(:post, topic: nested_topic_record, user: replier, reply_to_post_number: nil)
+
+ PostAlerter.post_created(root)
+
+ expect(op.notifications.where(notification_type: Notification.types[:replied]).count).to eq(1)
+ end
+
+ it "does not notify the OP about replies between third parties on someone else's post" do
+ other_root_user = Fabricate(:user)
+ other_root =
+ Fabricate(
+ :post,
+ topic: nested_topic_record,
+ user: other_root_user,
+ post_number: 2,
+ reply_to_post_number: nil,
+ )
+ PostAlerter.post_created(other_root)
+ op.notifications.destroy_all
+
+ deep_reply =
+ Fabricate(
+ :post,
+ topic: nested_topic_record,
+ user: third_party,
+ post_number: 3,
+ reply_to_post_number: 2,
+ )
+ PostAlerter.post_created(deep_reply)
+
+ expect(op.notifications.where(notification_type: Notification.types[:replied]).count).to eq(0)
+ end
+
+ it "does not notify the OP when they post a root themselves" do
+ own_root = Fabricate(:post, topic: nested_topic_record, user: op, reply_to_post_number: nil)
+
+ PostAlerter.post_created(own_root)
+
+ expect(op.notifications.where(notification_type: Notification.types[:replied]).count).to eq(0)
+ end
+
+ it "still notifies a parent-post author when someone replies directly to them" do
+ parent_user = Fabricate(:user)
+ Fabricate(
+ :post,
+ topic: nested_topic_record,
+ user: parent_user,
+ post_number: 2,
+ reply_to_post_number: nil,
+ )
+ reply =
+ Fabricate(
+ :post,
+ topic: nested_topic_record,
+ user: third_party,
+ post_number: 3,
+ reply_to_post_number: 2,
+ )
+
+ PostAlerter.post_created(reply)
+
+ expect(
+ parent_user.notifications.where(notification_type: Notification.types[:replied]).count,
+ ).to eq(1)
+ end
+
+ it "does not generate a :replied notification for a whispered root" do
+ whispered_root =
+ Fabricate(
+ :post,
+ topic: nested_topic_record,
+ user: replier,
+ post_type: Post.types[:whisper],
+ reply_to_post_number: nil,
+ )
+
+ PostAlerter.post_created(whispered_root)
+
+ expect(op.notifications.where(notification_type: Notification.types[:replied]).count).to eq(0)
+ end
+
+ it "starts redirecting only after a flat topic is converted to nested mid-life" do
+ flat_topic = Fabricate(:topic, user: op)
+ Fabricate(:post, topic: flat_topic, user: op, post_number: 1)
+
+ flat_root =
+ Fabricate(
+ :post,
+ topic: flat_topic,
+ user: replier,
+ post_number: 2,
+ reply_to_post_number: nil,
+ )
+ PostAlerter.post_created(flat_root)
+ expect(op.notifications.where(notification_type: Notification.types[:replied]).count).to eq(0)
+
+ Fabricate(:nested_topic, topic: flat_topic)
+
+ nested_root =
+ Fabricate(
+ :post,
+ topic: flat_topic,
+ user: third_party,
+ post_number: 3,
+ reply_to_post_number: nil,
+ )
+ PostAlerter.post_created(nested_root)
+ expect(op.notifications.where(notification_type: Notification.types[:replied]).count).to eq(1)
+ end
+
+ it "still emits :posted notifications for users explicitly Watching the nested topic" do
+ watcher = Fabricate(:user)
+ TopicUser.change(
+ watcher.id,
+ nested_topic_record.id,
+ notification_level: TopicUser.notification_levels[:watching],
+ )
+
+ root = Fabricate(:post, topic: nested_topic_record, user: replier, reply_to_post_number: nil)
+ PostAlerter.post_created(root)
+
+ expect(
+ watcher.notifications.where(notification_type: Notification.types[:posted]).count,
+ ).to eq(1)
+ end
+ end
+
+ context "with reply consolidation in a nested topic" do
+ fab!(:bucket_op, :user)
+ fab!(:bucket_replier, :user)
+ fab!(:bucket_other_replier, :user)
+ fab!(:bucket_topic) { Fabricate(:topic, user: bucket_op) }
+ fab!(:bucket_first_post) do
+ Fabricate(:post, topic: bucket_topic, user: bucket_op, post_number: 1)
+ end
+
+ before do
+ SiteSetting.nested_replies_enabled = true
+ Fabricate(:nested_topic, topic: bucket_topic)
+ end
+
+ def post_alert(user:, post_number:, reply_to: nil)
+ post =
+ Fabricate(
+ :post,
+ topic: bucket_topic,
+ user: user,
+ post_number: post_number,
+ reply_to_post_number: reply_to,
+ )
+ PostAlerter.post_created(post)
+ post
+ end
+
+ it "stores the resolved reply_to_post_number in notification.data for root posts" do
+ post_alert(user: bucket_replier, post_number: 2, reply_to: nil)
+
+ notification =
+ bucket_op.notifications.find_by(notification_type: Notification.types[:replied])
+ expect(notification.data_hash["reply_to_post_number"]).to eq(1)
+ end
+
+ it "stores the parent post number in notification.data for non-root replies" do
+ Fabricate(:post, topic: bucket_topic, user: bucket_op, post_number: 2)
+ post_alert(user: bucket_replier, post_number: 3, reply_to: 2)
+
+ notification =
+ bucket_op.notifications.find_by(notification_type: Notification.types[:replied])
+ expect(notification.data_hash["reply_to_post_number"]).to eq(2)
+ end
+
+ it "consolidates replies that share a bucket into one notification" do
+ post_alert(user: bucket_replier, post_number: 2, reply_to: nil)
+ post_alert(user: bucket_other_replier, post_number: 3, reply_to: nil)
+
+ notifications = bucket_op.notifications.where(notification_type: Notification.types[:replied])
+ expect(notifications.count).to eq(1)
+ expect(notifications.first.data_hash["reply_to_post_number"]).to eq(1)
+ expect(notifications.first.data_hash["display_username"]).to eq(
+ I18n.t("embed.replies", count: 2),
+ )
+ expect(notifications.first.data_hash["consolidated_count"]).to eq(2)
+ end
+
+ it "keeps separate notifications for replies in different buckets" do
+ Fabricate(:post, topic: bucket_topic, user: bucket_op, post_number: 2)
+ Fabricate(:post, topic: bucket_topic, user: bucket_op, post_number: 3)
+
+ post_alert(user: bucket_replier, post_number: 4, reply_to: 2)
+ post_alert(user: bucket_other_replier, post_number: 5, reply_to: 3)
+
+ buckets =
+ bucket_op
+ .notifications
+ .where(notification_type: Notification.types[:replied])
+ .map { |n| n.data_hash["reply_to_post_number"] }
+
+ expect(buckets.sort).to eq([2, 3])
+ end
+
+ it "does not let a non-root bucket reply collapse a root bucket notification" do
+ post_alert(user: bucket_replier, post_number: 2, reply_to: nil)
+
+ parent_post = Fabricate(:post, topic: bucket_topic, user: bucket_op, post_number: 3)
+ post_alert(user: bucket_other_replier, post_number: 4, reply_to: 3)
+
+ buckets =
+ bucket_op
+ .notifications
+ .where(notification_type: Notification.types[:replied])
+ .map { |n| n.data_hash["reply_to_post_number"] }
+
+ expect(buckets.sort).to eq([1, 3])
+ end
+
+ it "does not let a nested posted notification collapse a bucketed replied notification" do
+ watcher = Fabricate(:user)
+ TopicUser.change(
+ watcher.id,
+ bucket_topic.id,
+ notification_level: TopicUser.notification_levels[:watching],
+ )
+ Fabricate(:post, topic: bucket_topic, user: watcher, post_number: 2)
+ post_alert(user: bucket_replier, post_number: 3, reply_to: 2)
+
+ post_alert(user: bucket_other_replier, post_number: 4, reply_to: nil)
+
+ notifications = watcher.notifications.where(topic: bucket_topic)
+ expect(notifications.where(notification_type: Notification.types[:replied]).count).to eq(1)
+ expect(notifications.where(notification_type: Notification.types[:posted]).count).to eq(1)
+ end
+
+ it "does not let a nested category-watching notification collapse a bucketed replied notification" do
+ category_watcher = Fabricate(:user)
+ CategoryUser.set_notification_level_for_category(
+ category_watcher,
+ CategoryUser.notification_levels[:watching],
+ bucket_topic.category_id,
+ )
+ Fabricate(:post, topic: bucket_topic, user: category_watcher, post_number: 2)
+ post_alert(user: bucket_replier, post_number: 3, reply_to: 2)
+
+ post_alert(user: bucket_other_replier, post_number: 4, reply_to: nil)
+
+ notifications = category_watcher.notifications.where(topic: bucket_topic)
+ expect(notifications.where(notification_type: Notification.types[:replied]).count).to eq(1)
+ expect(
+ notifications.where(notification_type: Notification.types[:watching_category_or_tag]).count,
+ ).to eq(1)
+ end
+ end
+
+ context "with reply consolidation on a flat topic (regression guard)" do
+ fab!(:flat_op, :user)
+ fab!(:flat_replier_a, :user)
+ fab!(:flat_replier_b, :user)
+ fab!(:flat_topic) { Fabricate(:topic, user: flat_op) }
+ fab!(:flat_first_post) { Fabricate(:post, topic: flat_topic, user: flat_op, post_number: 1) }
+
+ # Flat topics must not pick up the new bucket fields. The frontend
+ # Replied handler keys on data.reply_to_post_number to render the
+ # nested-only "X new replies in your topic" / "to your post" copy
+ # and to append ?sort=new — flat topics must keep the legacy shape.
+
+ it "does not store reply_to_post_number or consolidated_count for a flat consolidated reply" do
+ Fabricate(:post, topic: flat_topic, user: flat_op, post_number: 2)
+ reply1 =
+ Fabricate(
+ :post,
+ topic: flat_topic,
+ user: flat_replier_a,
+ post_number: 3,
+ reply_to_post_number: 2,
+ )
+ PostAlerter.post_created(reply1)
+ reply2 =
+ Fabricate(
+ :post,
+ topic: flat_topic,
+ user: flat_replier_b,
+ post_number: 4,
+ reply_to_post_number: 2,
+ )
+ PostAlerter.post_created(reply2)
+
+ notifications = flat_op.notifications.where(notification_type: Notification.types[:replied])
+ expect(notifications.count).to eq(1)
+ data = notifications.first.data_hash
+ expect(data.key?("reply_to_post_number")).to eq(false)
+ expect(data.key?("consolidated_count")).to eq(false)
+ expect(data["display_username"]).to eq(I18n.t("embed.replies", count: 2))
+ end
+
+ it "does not store reply_to_post_number for a flat singular reply either" do
+ reply =
+ Fabricate(
+ :post,
+ topic: flat_topic,
+ user: flat_replier_a,
+ post_number: 2,
+ reply_to_post_number: 1,
+ )
+ PostAlerter.post_created(reply)
+
+ notification = flat_op.notifications.find_by(notification_type: Notification.types[:replied])
+ expect(notification.data_hash.key?("reply_to_post_number")).to eq(false)
+ end
+ end
+
context "with category" do
context "with watching" do
it "triggers :before_create_notifications_for_users" do
diff --git a/spec/system/nested_collapse_replies_spec.rb b/spec/system/nested_collapse_replies_spec.rb
new file mode 100644
index 00000000000..558a933cfcd
--- /dev/null
+++ b/spec/system/nested_collapse_replies_spec.rb
@@ -0,0 +1,66 @@
+# frozen_string_literal: true
+
+RSpec.describe "Nested view collapse_replies URL param" do
+ fab!(:op_user) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:replier_a) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:replier_b) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:replier_c) { Fabricate(:user, refresh_auto_groups: true) }
+
+ fab!(:topic) { Fabricate(:topic, user: op_user) }
+ fab!(:op_post) { Fabricate(:post, topic: topic, user: op_user, post_number: 1) }
+ fab!(:root_post) do
+ Fabricate(:post, topic: topic, user: replier_a, post_number: 2, reply_to_post_number: nil)
+ end
+ fab!(:reply_to_root) do
+ Fabricate(:post, topic: topic, user: replier_b, post_number: 3, reply_to_post_number: 2)
+ end
+ fab!(:reply_to_reply) do
+ Fabricate(:post, topic: topic, user: replier_c, post_number: 4, reply_to_post_number: 3)
+ end
+
+ let(:nested_view) { PageObjects::Pages::NestedView.new }
+
+ before do
+ SiteSetting.nested_replies_enabled = true
+ Fabricate(:nested_topic, topic: topic)
+ sign_in(op_user)
+ end
+
+ context "in the root view" do
+ it "renders all descendants by default" do
+ nested_view.visit_nested(topic)
+
+ expect(nested_view).to have_post(root_post)
+ expect(nested_view).to have_post(reply_to_root)
+ expect(nested_view).to have_post(reply_to_reply)
+ end
+
+ it "hides the root post's replies entirely when collapse_replies=true" do
+ page.visit("/n/#{topic.slug}/#{topic.id}?collapse_replies=true")
+
+ # Roots are the focal level — visible.
+ expect(nested_view).to have_post(root_post)
+
+ # Their replies (depth 1+) are hidden behind an "Expand X replies"
+ # button on the root, so the user can scan only the new roots.
+ expect(nested_view).to have_no_post(reply_to_root)
+ expect(nested_view).to have_no_post(reply_to_reply)
+ end
+ end
+
+ context "in the context view" do
+ it "renders direct replies but hides grandchildren when collapse_replies=true" do
+ page.visit("/n/#{topic.slug}/#{topic.id}/#{root_post.post_number}?collapse_replies=true")
+
+ expect(nested_view).to have_context_view
+
+ # Chain root and its direct replies (the "new content") are visible.
+ expect(nested_view).to have_post(root_post)
+ expect(nested_view).to have_post(reply_to_root)
+
+ # Grandchildren of the chain root are collapsed behind an "Expand"
+ # button on the depth-1 reply.
+ expect(nested_view).to have_no_post(reply_to_reply)
+ end
+ end
+end
diff --git a/spec/system/nested_topic_list_dot_spec.rb b/spec/system/nested_topic_list_dot_spec.rb
new file mode 100644
index 00000000000..8207631ba5c
--- /dev/null
+++ b/spec/system/nested_topic_list_dot_spec.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+RSpec.describe "Nested topic-list new-replies dot" do
+ fab!(:op_user) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:other_user) { Fabricate(:user, refresh_auto_groups: true) }
+
+ let(:topic_list) { PageObjects::Components::TopicList.new }
+ let!(:topic) do
+ post = create_post(user: op_user, raw: "Original post for the nested topic test")
+ post.topic
+ end
+
+ before do
+ SiteSetting.nested_replies_enabled = true
+ SiteSetting.nested_replies_default = true
+
+ # OP has visited and read; another user posted afterwards.
+ TopicUser.find_or_create_by(user: op_user, topic: topic).update!(
+ last_visited_at: 5.minutes.ago,
+ first_visited_at: 5.minutes.ago,
+ last_read_post_number: 1,
+ )
+ create_post(user: other_user, topic_id: topic.id, raw: "A reply from someone else")
+ topic.reload.update!(bumped_at: 1.minute.ago, last_post_user_id: other_user.id)
+
+ sign_in(op_user)
+ end
+
+ it "shows the dot on the topic list and clears it after visiting the topic" do
+ visit("/latest")
+ expect(topic_list).to have_new_replies_dot(topic)
+
+ topic_list.click_topic_title(topic)
+ expect(page).to have_css(".nested-view")
+
+ visit("/latest")
+ expect(topic_list).to have_no_new_replies_dot(topic)
+ end
+end
diff --git a/spec/system/page_objects/components/topic_list.rb b/spec/system/page_objects/components/topic_list.rb
index c51d90537cd..e5ec15a479f 100644
--- a/spec/system/page_objects/components/topic_list.rb
+++ b/spec/system/page_objects/components/topic_list.rb
@@ -58,6 +58,14 @@ module PageObjects
page.has_no_css?("#{topic_list_item_unread_badge(topic)}")
end
+ def has_new_replies_dot?(topic)
+ page.has_css?("#{topic_list_item_new_replies_dot(topic)}")
+ end
+
+ def has_no_new_replies_dot?(topic)
+ page.has_no_css?("#{topic_list_item_new_replies_dot(topic)}")
+ end
+
def has_checkbox_selected_on_row?(n)
page.has_css?("#{TOPIC_LIST_ITEM_SELECTOR}:nth-child(#{n}) input.bulk-select:checked")
end
@@ -157,6 +165,10 @@ module PageObjects
def topic_list_item_unread_badge(topic)
"#{topic_list_item_class(topic)} .topic-post-badges .unread-posts"
end
+
+ def topic_list_item_new_replies_dot(topic)
+ "#{topic_list_item_class(topic)} .topic-post-badges .new-replies"
+ end
end
end
end