A platform for community discussion. Free, open, simple.
FIX: regression in channel list and make improvements (#36436)
Keegan Georgef8f15651ec22
diff --git a/plugins/chat/assets/javascripts/discourse/components/channels-list-direct.gjs b/plugins/chat/assets/javascripts/discourse/components/channels-list-direct.gjsindex 15b67ba92a4..f093d12797e 100644--- a/plugins/chat/assets/javascripts/discourse/components/channels-list-direct.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/channels-list-direct.gjs@@ -20,6 +20,7 @@ export default class ChannelsListDirect extends Component {@service chatChannelsManager;@service site;@service modal;+ @service siteSettings;get inSidebar() {return this.args.inSidebar ?? false;@@ -47,6 +48,13 @@ export default class ChannelsListDirect extends Component {return this.chatChannelsManager.directMessageChannels?.length === 0;}+ get channelList() {+ if (!this.inSidebar && this.siteSettings.star_chat_channels) {+ return this.chatChannelsManager.truncatedUnstarredDirectMessageChannels;+ }+ return this.chatChannelsManager.truncatedDirectMessageChannels;+ }+@actiontoggleChannelSection(section) {this.args.toggleSection(section);@@ -118,10 +126,7 @@ export default class ChannelsListDirect extends Component {@ctaAction={{this.openNewMessageModal}}/>{{else}}- {{#each- this.chatChannelsManager.truncatedDirectMessageChannels- as |channel|- }}+ {{#each this.channelList as |channel|}}<ChatChannelRow@channel={{channel}}@options={{hash leaveButton=true}}diff --git a/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs b/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjsindex 79a31ee8c49..173d66afb74 100644--- a/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/channels-list-public.gjs@@ -19,6 +19,7 @@ export default class ChannelsListPublic extends Component {@service chatTrackingStateManager;@service site;@service router;+ @service siteSettings;get inSidebar() {return this.args.inSidebar ?? false;@@ -33,6 +34,9 @@ export default class ChannelsListPublic extends Component {}get channelList() {+ if (!this.inSidebar && this.siteSettings.star_chat_channels) {+ return this.chatChannelsManager.unstarredPublicMessageChannelsByActivity;+ }return this.chatChannelsManager.publicMessageChannelsByActivity;}diff --git a/plugins/chat/assets/javascripts/discourse/components/channels-list-starred.gjs b/plugins/chat/assets/javascripts/discourse/components/channels-list-starred.gjsindex 9f9211ef716..69bd1811956 100644--- a/plugins/chat/assets/javascripts/discourse/components/channels-list-starred.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/channels-list-starred.gjs@@ -7,8 +7,18 @@ import ChatChannelRow from "./chat-channel-row";export default class ChannelsListStarred extends Component {@service chatChannelsManager;+ get inSidebar() {+ return this.args.inSidebar ?? false;+ }+get channelList() {- return this.args.channels || this.chatChannelsManager.starredChannels;+ if (this.args.channels) {+ return this.args.channels;+ }+ if (!this.inSidebar) {+ return this.chatChannelsManager.starredChannelsByActivity;+ }+ return this.chatChannelsManager.starredChannels;}<template>diff --git a/plugins/chat/assets/javascripts/discourse/components/chat/footer/unread-indicator.gjs b/plugins/chat/assets/javascripts/discourse/components/chat/footer/unread-indicator.gjsindex 03b97ec9c1c..38b042aedd2 100644--- a/plugins/chat/assets/javascripts/discourse/components/chat/footer/unread-indicator.gjs+++ b/plugins/chat/assets/javascripts/discourse/components/chat/footer/unread-indicator.gjs@@ -26,17 +26,15 @@ export const UnreadThreadsIndicator = <template>export default class FooterUnreadIndicator extends Component {@service chatChannelsManager;@service chatTrackingStateManager;+ @service siteSettings;badgeType = this.args.badgeType;get urgentCount() {if (this.badgeType === CHANNELS_TAB) {- return this.chatTrackingStateManager.publicChannelMentionCount;+ return this.unstarredPublicChannelMentionCount;} else if (this.badgeType === DMS_TAB) {- return (- this.chatTrackingStateManager.directMessageUnreadCount +- this.chatTrackingStateManager.directMessageMentionCount- );+ return this.unstarredDMUrgentCount;} else if (this.badgeType === STARRED_TAB) {return this.starredChannelsUrgentCount;} else if (this.badgeType === THREADS_TAB) {@@ -48,7 +46,7 @@ export default class FooterUnreadIndicator extends Component {get unreadCount() {if (this.badgeType === CHANNELS_TAB) {- return this.chatTrackingStateManager.publicChannelUnreadCount;+ return this.unstarredPublicChannelUnreadCount;} else if (this.badgeType === STARRED_TAB) {return this.starredChannelsUnreadCount;} else if (this.badgeType === THREADS_TAB) {@@ -58,6 +56,40 @@ export default class FooterUnreadIndicator extends Component {}}+ get unstarredPublicChannelMentionCount() {+ if (!this.siteSettings.star_chat_channels) {+ return this.chatTrackingStateManager.publicChannelMentionCount;+ }+ return this.chatChannelsManager.unstarredPublicMessageChannels.reduce(+ (count, channel) => count + channel.tracking.mentionCount,+ 0+ );+ }++ get unstarredPublicChannelUnreadCount() {+ if (!this.siteSettings.star_chat_channels) {+ return this.chatTrackingStateManager.publicChannelUnreadCount;+ }+ return this.chatChannelsManager.unstarredPublicMessageChannels.reduce(+ (count, channel) => count + channel.tracking.unreadCount,+ 0+ );+ }++ get unstarredDMUrgentCount() {+ if (!this.siteSettings.star_chat_channels) {+ return (+ this.chatTrackingStateManager.directMessageUnreadCount ++ this.chatTrackingStateManager.directMessageMentionCount+ );+ }+ return this.chatChannelsManager.unstarredDirectMessageChannels.reduce(+ (count, channel) =>+ count + channel.tracking.unreadCount + channel.tracking.mentionCount,+ 0+ );+ }+get showUrgent() {return this.urgentCount > 0;}diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.jsindex 97ef1881986..056798d1c89 100644--- a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js+++ b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js@@ -179,6 +179,28 @@ export default class ChatChannelsManager extends Service {return this.#sortChannelsByActivity([...this.publicMessageChannels]);}+ /**+ * Returns public message channels that are not starred, sorted by activity.+ * Falls back to all public channels by activity if starring is disabled.+ * Channels with unreads appear first, then sorted alphabetically.+ *+ * @returns {ChatChannel[]} Array of unstarred public channels sorted by activity+ */+ get unstarredPublicMessageChannelsByActivity() {+ if (!this.siteSettings.star_chat_channels) {+ return this.publicMessageChannelsByActivity;+ }++ return this.#sortChannelsByActivity(+ this.channels.filter(+ (channel) =>+ channel.isCategoryChannel &&+ channel.currentUserMembership?.following &&+ !channel.currentUserMembership?.starred+ )+ );+ }+@cachedget directMessageChannels() {return this.#sortDirectMessageChannels(@@ -238,6 +260,81 @@ export default class ChatChannelsManager extends Service {return this.starredChannels.length > 0;}+ /**+ * Returns all starred channels sorted by activity (unreads first).+ * Channels with urgent notifications (mentions, DM unreads) come first,+ * then channels with regular unreads, then by most recent message.+ *+ * @returns {ChatChannel[]} Array of starred channels sorted by activity+ */+ get starredChannelsByActivity() {+ if (!this.siteSettings.star_chat_channels) {+ return [];+ }++ const starredChannels = this.channels.filter(+ (channel) =>+ channel.currentUserMembership?.following &&+ channel.currentUserMembership?.starred+ );++ return starredChannels.sort((a, b) => {+ const aUrgent = this.#getChannelUrgentCount(a);+ const bUrgent = this.#getChannelUrgentCount(b);++ // Channels with urgent notifications come first+ if (aUrgent > 0 && bUrgent > 0) {+ return this.#compareByLastActivity(a, b);+ }+ if (aUrgent > 0 || bUrgent > 0) {+ return aUrgent > bUrgent ? -1 : 1;+ }++ const aUnread = this.#getChannelUnreadCount(a);+ const bUnread = this.#getChannelUnreadCount(b);++ // Channels with unreads come next+ if (aUnread > 0 && bUnread > 0) {+ return this.#compareByLastActivity(a, b);+ }+ if (aUnread > 0 || bUnread > 0) {+ return aUnread > bUnread ? -1 : 1;+ }++ // Sort remaining by last activity+ return this.#compareByLastActivity(a, b);+ });+ }++ #getChannelUrgentCount(channel) {+ if (channel.isDirectMessageChannel) {+ return (+ channel.tracking.unreadCount ++ channel.tracking.mentionCount ++ channel.tracking.watchedThreadsUnreadCount+ );+ }+ return (+ channel.tracking.mentionCount + channel.tracking.watchedThreadsUnreadCount+ );+ }++ #getChannelUnreadCount(channel) {+ return (+ channel.tracking.unreadCount + channel.unreadThreadsCountSinceLastViewed+ );+ }++ #compareByLastActivity(a, b) {+ const aDate = a.lastMessage?.createdAt+ ? new Date(a.lastMessage.createdAt)+ : new Date(0);+ const bDate = b.lastMessage?.createdAt+ ? new Date(b.lastMessage.createdAt)+ : new Date(0);+ return bDate - aDate;+ }+/*** Returns public message channels that are not starred.* Falls back to all public channels if starring is disabled.diff --git a/plugins/chat/test/javascripts/unit/services/chat-channels-manager-test.js b/plugins/chat/test/javascripts/unit/services/chat-channels-manager-test.jsindex 2eed04967fc..e8c9640ae5f 100644--- a/plugins/chat/test/javascripts/unit/services/chat-channels-manager-test.js+++ b/plugins/chat/test/javascripts/unit/services/chat-channels-manager-test.js@@ -150,6 +150,214 @@ module(});});+ module("#unstarredPublicMessageChannelsByActivity", function () {+ test("returns all channels sorted by activity when starring is disabled", function (assert) {+ this.siteSettings.star_chat_channels = false;++ const channelA = this.fabricators.channel({+ chatable: this.fabricators.coreFabricators.category({+ slug: "channel-a",+ }),+ });+ const channelB = this.fabricators.channel({+ chatable: this.fabricators.coreFabricators.category({+ slug: "channel-b",+ }),+ });++ channelA.currentUserMembership = UserChatChannelMembership.create({+ following: true,+ starred: true,+ });+ channelB.currentUserMembership = UserChatChannelMembership.create({+ following: true,+ starred: false,+ });++ this.subject.store(channelA);+ this.subject.store(channelB);++ const result = this.subject.unstarredPublicMessageChannelsByActivity;++ assert.strictEqual(result.length, 2, "returns all channels");+ });++ test("excludes starred channels when starring is enabled", function (assert) {+ const starredChannel = this.fabricators.channel({+ chatable: this.fabricators.coreFabricators.category({+ slug: "starred-channel",+ }),+ });+ const unstarredChannel = this.fabricators.channel({+ chatable: this.fabricators.coreFabricators.category({+ slug: "unstarred-channel",+ }),+ });++ starredChannel.currentUserMembership = UserChatChannelMembership.create(+ {+ following: true,+ starred: true,+ }+ );+ unstarredChannel.currentUserMembership =+ UserChatChannelMembership.create({+ following: true,+ starred: false,+ });++ this.subject.store(starredChannel);+ this.subject.store(unstarredChannel);++ const result = this.subject.unstarredPublicMessageChannelsByActivity;++ assert.strictEqual(result.length, 1, "returns only unstarred channels");+ assert.strictEqual(+ result[0].id,+ unstarredChannel.id,+ "returns the unstarred channel"+ );+ });++ test("sorts unstarred channels by activity with unreads first", function (assert) {+ const channelWithUnread = this.fabricators.channel({+ chatable: this.fabricators.coreFabricators.category({+ slug: "channel-with-unread",+ }),+ });+ const channelNoUnread = this.fabricators.channel({+ chatable: this.fabricators.coreFabricators.category({+ slug: "channel-no-unread",+ }),+ });++ channelWithUnread.currentUserMembership =+ UserChatChannelMembership.create({+ following: true,+ starred: false,+ });+ channelNoUnread.currentUserMembership =+ UserChatChannelMembership.create({+ following: true,+ starred: false,+ });++ channelWithUnread.tracking.unreadCount = 5;+ channelNoUnread.tracking.unreadCount = 0;++ this.subject.store(channelNoUnread);+ this.subject.store(channelWithUnread);++ const result = this.subject.unstarredPublicMessageChannelsByActivity;++ assert.strictEqual(+ result[0].id,+ channelWithUnread.id,+ "channel with unreads comes first"+ );+ assert.strictEqual(+ result[1].id,+ channelNoUnread.id,+ "channel without unreads comes second"+ );+ });+ });++ module("#starredChannelsByActivity", function () {+ test("returns empty array when starring is disabled", function (assert) {+ this.siteSettings.star_chat_channels = false;++ const channel = this.fabricators.channel({+ chatable: this.fabricators.coreFabricators.category({+ slug: "test",+ }),+ });+ channel.currentUserMembership = UserChatChannelMembership.create({+ following: true,+ starred: true,+ });++ this.subject.store(channel);++ assert.strictEqual(+ this.subject.starredChannelsByActivity.length,+ 0,+ "returns empty array"+ );+ });++ test("sorts starred channels with unreads first", function (assert) {+ const channelWithUnread = this.fabricators.channel({+ chatable: this.fabricators.coreFabricators.category({+ slug: "channel-with-unread",+ }),+ });+ const channelNoUnread = this.fabricators.channel({+ chatable: this.fabricators.coreFabricators.category({+ slug: "channel-no-unread",+ }),+ });++ channelWithUnread.currentUserMembership =+ UserChatChannelMembership.create({+ following: true,+ starred: true,+ });+ channelNoUnread.currentUserMembership =+ UserChatChannelMembership.create({+ following: true,+ starred: true,+ });++ channelWithUnread.tracking.unreadCount = 5;+ channelNoUnread.tracking.unreadCount = 0;++ this.subject.store(channelNoUnread);+ this.subject.store(channelWithUnread);++ const result = this.subject.starredChannelsByActivity;++ assert.strictEqual(+ result[0].id,+ channelWithUnread.id,+ "channel with unreads comes first"+ );+ assert.strictEqual(+ result[1].id,+ channelNoUnread.id,+ "channel without unreads comes second"+ );+ });++ test("includes both public and DM starred channels", function (assert) {+ const publicChannel = this.fabricators.channel({+ chatable: this.fabricators.coreFabricators.category({+ slug: "public",+ }),+ });+ const dmChannel = this.fabricators.channel({+ chatable: this.fabricators.directMessage(),+ title: "DM User",+ });++ publicChannel.currentUserMembership = UserChatChannelMembership.create({+ following: true,+ starred: true,+ });+ dmChannel.currentUserMembership = UserChatChannelMembership.create({+ following: true,+ starred: true,+ });++ this.subject.store(publicChannel);+ this.subject.store(dmChannel);++ const result = this.subject.starredChannelsByActivity;++ assert.strictEqual(result.length, 2, "returns both channels");+ });+ });+module("#sortDirectMessageChannels with starred channels", function () {test("prioritizes starred DM channels over unstarred", function (assert) {const dmA = this.fabricators.channel({