Skip to content

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

UX: Update sort order for starred channels on drawer/mobile (#36921)

Keegan George36145ed2a8bf
diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js
index 2101d32b7fe..1f49bb48544 100644
--- a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js
+++ b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js
@@ -288,8 +288,11 @@ export default class ChatChannelsManager extends Service {
/**
* 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.
+ * Prioritizes unread status over channel type:
+ * 1. Unread public channels (sorted by activity)
+ * 2. Unread DMs/Groups (sorted by activity)
+ * 3. Read public channels (sorted by activity)
+ * 4. Read DMs/Groups (sorted by activity)
*
* @returns {ChatChannel[]} Array of starred channels sorted by activity
*/
@@ -301,30 +304,39 @@ export default class ChatChannelsManager extends Service {
);
return starredChannels.sort((a, b) => {
- if (a.isDirectMessageChannel !== b.isDirectMessageChannel) {
- return a.isDirectMessageChannel ? 1 : -1;
- }
-
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);
+ const aHasActivity = aUrgent > 0 || aUnread > 0;
+ const bHasActivity = bUrgent > 0 || bUnread > 0;
+
+ // First: prioritize channels with activity over those without
+ if (aHasActivity !== bHasActivity) {
+ return aHasActivity ? -1 : 1;
}
- if (aUnread > 0 || bUnread > 0) {
- return aUnread > bUnread ? -1 : 1;
+
+ // Within the same activity state, sort by channel type (public first)
+ if (a.isDirectMessageChannel !== b.isDirectMessageChannel) {
+ return a.isDirectMessageChannel ? 1 : -1;
+ }
+
+ // Within the same channel type and activity state:
+ // If both have activity, prioritize by urgent then unread
+ if (aHasActivity && bHasActivity) {
+ if (aUrgent > 0 && bUrgent > 0) {
+ return this.#compareByLastActivity(a, b);
+ }
+ if (aUrgent > 0 || bUrgent > 0) {
+ return aUrgent > bUrgent ? -1 : 1;
+ }
+ 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
diff --git a/plugins/chat/spec/system/drawer/starred_channels_spec.rb b/plugins/chat/spec/system/drawer/starred_channels_spec.rb
index 32ea4e28a54..6fc9d5b5457 100644
--- a/plugins/chat/spec/system/drawer/starred_channels_spec.rb
+++ b/plugins/chat/spec/system/drawer/starred_channels_spec.rb
@@ -53,7 +53,7 @@ RSpec.describe "Drawer - starred channels", type: :system do
dm_channel_2.membership_for(current_user).update!(starred: true)
end
- it "sorts by unreads first, then public channels before DMs" do
+ it "sorts by activity: unread public, unread DMs, read public, read DMs" do
Fabricate(:chat_message, chat_channel: channel_2, user: user_1)
Fabricate(:chat_message, chat_channel: dm_channel_1, user: user_1)
channel_2.membership_for(current_user).update!(last_viewed_at: 1.minute.ago)
@@ -65,7 +65,7 @@ RSpec.describe "Drawer - starred channels", type: :system do
channels = page.all(".chat-channel-row")
expect(channels.map { |c| c["data-chat-channel-id"] }).to eq(
- [channel_2.id, channel_1.id, dm_channel_1.id, dm_channel_2.id].map(&:to_s),
+ [channel_2.id, dm_channel_1.id, channel_1.id, dm_channel_2.id].map(&:to_s),
)
end
end
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.js
index b6930194e7a..39a034db03d 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
@@ -301,6 +301,198 @@ module(
assert.strictEqual(result.length, 2, "returns both channels");
});
+
+ test("prioritizes unread status over channel type", function (assert) {
+ const readPublicChannel = this.fabricators.channel({
+ chatable: this.fabricators.coreFabricators.category({
+ slug: "read-public",
+ }),
+ });
+ const unreadDmChannel = this.fabricators.channel({
+ chatable: this.fabricators.directMessage(),
+ title: "Unread DM",
+ });
+
+ readPublicChannel.currentUserMembership =
+ UserChatChannelMembership.create({
+ following: true,
+ starred: true,
+ });
+ unreadDmChannel.currentUserMembership =
+ UserChatChannelMembership.create({
+ following: true,
+ starred: true,
+ });
+
+ readPublicChannel.tracking.unreadCount = 0;
+ unreadDmChannel.tracking.unreadCount = 3;
+
+ this.subject.store(readPublicChannel);
+ this.subject.store(unreadDmChannel);
+
+ const result = this.subject.starredChannelsByActivity;
+
+ assert.strictEqual(
+ result[0].id,
+ unreadDmChannel.id,
+ "unread DM comes before read public channel"
+ );
+ assert.strictEqual(
+ result[1].id,
+ readPublicChannel.id,
+ "read public channel comes after unread DM"
+ );
+ });
+
+ test("sorts unread public channels before unread DMs", function (assert) {
+ const unreadPublicChannel = this.fabricators.channel({
+ chatable: this.fabricators.coreFabricators.category({
+ slug: "unread-public",
+ }),
+ });
+ const unreadDmChannel = this.fabricators.channel({
+ chatable: this.fabricators.directMessage(),
+ title: "Unread DM",
+ });
+
+ unreadPublicChannel.currentUserMembership =
+ UserChatChannelMembership.create({
+ following: true,
+ starred: true,
+ });
+ unreadDmChannel.currentUserMembership =
+ UserChatChannelMembership.create({
+ following: true,
+ starred: true,
+ });
+
+ unreadPublicChannel.tracking.unreadCount = 2;
+ unreadDmChannel.tracking.unreadCount = 5;
+
+ this.subject.store(unreadDmChannel);
+ this.subject.store(unreadPublicChannel);
+
+ const result = this.subject.starredChannelsByActivity;
+
+ assert.strictEqual(
+ result[0].id,
+ unreadPublicChannel.id,
+ "unread public channel comes first"
+ );
+ assert.strictEqual(
+ result[1].id,
+ unreadDmChannel.id,
+ "unread DM comes second"
+ );
+ });
+
+ test("sorts read public channels before read DMs", function (assert) {
+ const readPublicChannel = this.fabricators.channel({
+ chatable: this.fabricators.coreFabricators.category({
+ slug: "read-public",
+ }),
+ });
+ const readDmChannel = this.fabricators.channel({
+ chatable: this.fabricators.directMessage(),
+ title: "Read DM",
+ });
+
+ readPublicChannel.currentUserMembership =
+ UserChatChannelMembership.create({
+ following: true,
+ starred: true,
+ });
+ readDmChannel.currentUserMembership = UserChatChannelMembership.create({
+ following: true,
+ starred: true,
+ });
+
+ readPublicChannel.tracking.unreadCount = 0;
+ readDmChannel.tracking.unreadCount = 0;
+
+ this.subject.store(readDmChannel);
+ this.subject.store(readPublicChannel);
+
+ const result = this.subject.starredChannelsByActivity;
+
+ assert.strictEqual(
+ result[0].id,
+ readPublicChannel.id,
+ "read public channel comes first"
+ );
+ assert.strictEqual(
+ result[1].id,
+ readDmChannel.id,
+ "read DM comes second"
+ );
+ });
+
+ test("complete ordering: unread public, unread DM, read public, read DM", function (assert) {
+ const readPublicChannel = this.fabricators.channel({
+ chatable: this.fabricators.coreFabricators.category({
+ slug: "read-public",
+ }),
+ });
+ const unreadPublicChannel = this.fabricators.channel({
+ chatable: this.fabricators.coreFabricators.category({
+ slug: "unread-public",
+ }),
+ });
+ const readDmChannel = this.fabricators.channel({
+ chatable: this.fabricators.directMessage(),
+ title: "Read DM",
+ });
+ const unreadDmChannel = this.fabricators.channel({
+ chatable: this.fabricators.directMessage(),
+ title: "Unread DM",
+ });
+
+ readPublicChannel.currentUserMembership =
+ UserChatChannelMembership.create({
+ following: true,
+ starred: true,
+ });
+ unreadPublicChannel.currentUserMembership =
+ UserChatChannelMembership.create({
+ following: true,
+ starred: true,
+ });
+ readDmChannel.currentUserMembership = UserChatChannelMembership.create({
+ following: true,
+ starred: true,
+ });
+ unreadDmChannel.currentUserMembership =
+ UserChatChannelMembership.create({
+ following: true,
+ starred: true,
+ });
+
+ readPublicChannel.tracking.unreadCount = 0;
+ unreadPublicChannel.tracking.unreadCount = 2;
+ readDmChannel.tracking.unreadCount = 0;
+ unreadDmChannel.tracking.unreadCount = 3;
+
+ this.subject.store(readDmChannel);
+ this.subject.store(unreadDmChannel);
+ this.subject.store(readPublicChannel);
+ this.subject.store(unreadPublicChannel);
+
+ const result = this.subject.starredChannelsByActivity;
+
+ assert.strictEqual(result.length, 4, "returns all 4 channels");
+ assert.strictEqual(
+ result[0].id,
+ unreadPublicChannel.id,
+ "1st: unread public channel"
+ );
+ assert.strictEqual(result[1].id, unreadDmChannel.id, "2nd: unread DM");
+ assert.strictEqual(
+ result[2].id,
+ readPublicChannel.id,
+ "3rd: read public channel"
+ );
+ assert.strictEqual(result[3].id, readDmChannel.id, "4th: read DM");
+ });
});
module("#sortDirectMessageChannels with starred channels", function () {