Skip to content

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

DEV: thread count should be 0 if threading is disabled in chat (#38383)

Gabriel Grubba83f77449c622
diff --git a/plugins/chat/assets/javascripts/discourse/models/chat-channel.js b/plugins/chat/assets/javascripts/discourse/models/chat-channel.js
index 8ef662bc83d..7cb61a97a92 100644
--- a/plugins/chat/assets/javascripts/discourse/models/chat-channel.js
+++ b/plugins/chat/assets/javascripts/discourse/models/chat-channel.js
@@ -111,6 +111,10 @@ export default class ChatChannel {
}
get unreadThreadsCountSinceLastViewed() {
+ if (!this.threadingEnabled) {
+ return 0;
+ }
+
return Array.from(this.threadsManager.unreadThreadOverview.values()).filter(
(lastReplyCreatedAt) =>
lastReplyCreatedAt >= this.currentUserMembership.lastViewedAt
@@ -132,6 +136,10 @@ export default class ChatChannel {
}
get watchedThreadsUnreadCount() {
+ if (!this.threadingEnabled) {
+ return 0;
+ }
+
return this.threadsManager.threads.reduce((unreadCount, thread) => {
return unreadCount + thread.tracking.watchedThreadsUnreadCount;
}, 0);
diff --git a/plugins/chat/test/javascripts/unit/models/chat-channel-test.js b/plugins/chat/test/javascripts/unit/models/chat-channel-test.js
new file mode 100644
index 00000000000..7f4c929547d
--- /dev/null
+++ b/plugins/chat/test/javascripts/unit/models/chat-channel-test.js
@@ -0,0 +1,81 @@
+import { getOwner } from "@ember/owner";
+import { setupTest } from "ember-qunit";
+import { module, test } from "qunit";
+import ChatFabricators from "discourse/plugins/chat/discourse/lib/fabricators";
+
+module("Discourse Chat | Unit | Models | chat-channel", function (hooks) {
+ setupTest(hooks);
+
+ module("unreadThreadsCount", function () {
+ test("returns 0 when threading is disabled", function (assert) {
+ const channel = new ChatFabricators(getOwner(this)).channel();
+ channel.threadingEnabled = false;
+ channel.threadsManager.markThreadUnread(1, new Date());
+
+ assert.strictEqual(channel.unreadThreadsCount, 0);
+ });
+
+ test("returns count when threading is enabled", function (assert) {
+ const channel = new ChatFabricators(getOwner(this)).channel();
+ channel.threadingEnabled = true;
+ channel.threadsManager.markThreadUnread(1, new Date());
+ channel.threadsManager.markThreadUnread(2, new Date());
+
+ assert.strictEqual(channel.unreadThreadsCount, 2);
+ });
+ });
+
+ module("unreadThreadsCountSinceLastViewed", function () {
+ test("returns 0 when threading is disabled", function (assert) {
+ const channel = new ChatFabricators(getOwner(this)).channel();
+ channel.threadingEnabled = false;
+ channel.currentUserMembership.lastViewedAt = new Date(2000, 0, 1);
+ channel.threadsManager.markThreadUnread(1, new Date());
+
+ assert.strictEqual(channel.unreadThreadsCountSinceLastViewed, 0);
+ });
+
+ test("returns count of threads since last viewed when threading is enabled", function (assert) {
+ const channel = new ChatFabricators(getOwner(this)).channel();
+ channel.threadingEnabled = true;
+
+ const lastViewed = new Date(2024, 0, 15);
+ channel.currentUserMembership.lastViewedAt = lastViewed;
+
+ channel.threadsManager.markThreadUnread(1, new Date(2024, 0, 10));
+ channel.threadsManager.markThreadUnread(2, new Date(2024, 0, 20));
+ channel.threadsManager.markThreadUnread(3, new Date(2024, 0, 25));
+
+ assert.strictEqual(channel.unreadThreadsCountSinceLastViewed, 2);
+ });
+ });
+
+ module("watchedThreadsUnreadCount", function () {
+ test("returns 0 when threading is disabled", function (assert) {
+ const fabricators = new ChatFabricators(getOwner(this));
+ const channel = fabricators.channel();
+ channel.threadingEnabled = false;
+
+ const thread = fabricators.thread({ channel });
+ channel.threadsManager.add(channel, thread);
+ thread.tracking.watchedThreadsUnreadCount = 3;
+
+ assert.strictEqual(channel.watchedThreadsUnreadCount, 0);
+ });
+
+ test("returns sum of watched threads unread counts when threading is enabled", function (assert) {
+ const fabricators = new ChatFabricators(getOwner(this));
+ const channel = fabricators.channel();
+ channel.threadingEnabled = true;
+
+ const thread1 = fabricators.thread({ channel });
+ const thread2 = fabricators.thread({ channel });
+ channel.threadsManager.add(channel, thread1);
+ channel.threadsManager.add(channel, thread2);
+ thread1.tracking.watchedThreadsUnreadCount = 3;
+ thread2.tracking.watchedThreadsUnreadCount = 5;
+
+ assert.strictEqual(channel.watchedThreadsUnreadCount, 8);
+ });
+ });
+});