Skip to content

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

FIX: Clear category new/unread count after a topic is removed (#40229)

Gabriel Grubba2855717ef02f
diff --git a/frontend/discourse/app/models/topic-tracking-state.js b/frontend/discourse/app/models/topic-tracking-state.js
index 98537325751..f4731941f9b 100644
--- a/frontend/discourse/app/models/topic-tracking-state.js
+++ b/frontend/discourse/app/models/topic-tracking-state.js
@@ -13,6 +13,7 @@ import Site from "discourse/models/site";
function isNew(topic) {
return (
+ !topic.deleted &&
topic.last_read_post_number === null &&
((topic.notification_level !== 0 && !topic.notification_level) ||
topic.notification_level >= NotificationLevels.TRACKING) &&
@@ -23,6 +24,7 @@ function isNew(topic) {
function isUnread(topic) {
return (
+ !topic.deleted &&
topic.last_read_post_number !== null &&
topic.last_read_post_number < topic.highest_post_number &&
topic.notification_level >= NotificationLevels.TRACKING
@@ -147,6 +149,9 @@ export default class TopicTrackingState extends EmberObject {
@bind
onDeleteMessage(msg) {
this.modifyStateProp(msg, "deleted", true);
+ if (this.newIncoming) {
+ this.clearIncoming([msg.topic_id]);
+ }
this.messageCount++;
}
diff --git a/frontend/discourse/tests/unit/models/topic-tracking-state-test.js b/frontend/discourse/tests/unit/models/topic-tracking-state-test.js
index f6bb27f9351..312d7f964c3 100644
--- a/frontend/discourse/tests/unit/models/topic-tracking-state-test.js
+++ b/frontend/discourse/tests/unit/models/topic-tracking-state-test.js
@@ -83,6 +83,21 @@ module("Unit | Model | topic-tracking-state", function (hooks) {
tags: null,
notification_level: NotificationLevels.TRACKING,
},
+ {
+ topic_id: 7,
+ deleted: true,
+ last_read_post_number: null,
+ tags: ["random"],
+ created_in_new_period: true,
+ },
+ {
+ topic_id: 8,
+ deleted: true,
+ last_read_post_number: 1,
+ highest_post_number: 7,
+ category_id: 7,
+ notification_level: NotificationLevels.TRACKING,
+ },
]);
let randomUnread = 0;
@@ -412,8 +427,29 @@ module("Unit | Model | topic-tracking-state", function (hooks) {
{
topic_id: 111,
deleted: false,
+ last_read_post_number: null,
+ notification_level: NotificationLevels.TRACKING,
+ category_id: 1,
+ created_in_new_period: true,
},
]);
+ trackingState.trackIncoming("all");
+ trackingState.notifyIncoming({
+ topic_id: 111,
+ message_type: "new_topic",
+ payload: { category_id: 1 },
+ });
+
+ assert.strictEqual(
+ trackingState.countNew({ categoryId: 1 }),
+ 1,
+ "counts the topic as new"
+ );
+ assert.strictEqual(
+ trackingState.incomingCount,
+ 1,
+ "counts the topic as incoming"
+ );
await publishToMessageBus("/delete", { topic_id: 111 });
@@ -421,6 +457,16 @@ module("Unit | Model | topic-tracking-state", function (hooks) {
trackingState.findState(111).deleted,
"marks the topic as deleted"
);
+ assert.strictEqual(
+ trackingState.countNew({ categoryId: 1 }),
+ 0,
+ "removes the deleted topic from the new count"
+ );
+ assert.strictEqual(
+ trackingState.incomingCount,
+ 0,
+ "removes the deleted topic from incoming"
+ );
assert.strictEqual(
trackingState.messageCount,
1,
diff --git a/spec/system/category_tracking_badge_spec.rb b/spec/system/category_tracking_badge_spec.rb
new file mode 100644
index 00000000000..48bdcd7cd8b
--- /dev/null
+++ b/spec/system/category_tracking_badge_spec.rb
@@ -0,0 +1,42 @@
+# frozen_string_literal: true
+
+RSpec.describe "Category tracking badge" do
+ fab!(:admin) { Fabricate(:admin, refresh_auto_groups: true) }
+ fab!(:moderator) { Fabricate(:moderator, refresh_auto_groups: true) }
+ fab!(:author) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:category)
+ fab!(:existing_topic) { Fabricate(:read_topic, category: category, current_user: admin) }
+
+ let(:category_page) { PageObjects::Pages::Category.new }
+ let(:topic_list) { PageObjects::Components::TopicList.new }
+ let(:topic_list_controls) { PageObjects::Components::TopicListControls.new }
+
+ before do
+ CategoryUser.create!(
+ category: category,
+ user: admin,
+ notification_level: CategoryUser.notification_levels[:watching],
+ )
+ end
+
+ it "stops showing a new topic after it is spam-deleted" do
+ sign_in(admin)
+
+ category_page.visit(category)
+ expect(topic_list).to have_topic(existing_topic)
+ new_post = create_post(category: category, user: author)
+
+ try_until_success(reason: "relies on MessageBus updates") do
+ expect(topic_list_controls).to have_new(count: 1)
+ end
+
+ category_page.click_new
+ expect(topic_list).to have_topic(new_post.topic)
+
+ PostActionCreator.spam(moderator, new_post).reviewable.perform(moderator, :delete_and_agree)
+
+ try_until_success(reason: "relies on MessageBus updates") do
+ expect(topic_list_controls).to have_new(count: 0)
+ end
+ end
+end