A platform for community discussion. Free, open, simple.
FIX: Improve the reliability of the unread channel keyboard shortcuts (#29814)
Gary Pendergastf06a5635b3fb
diff --git a/plugins/chat/assets/javascripts/discourse/models/chat-channel.js b/plugins/chat/assets/javascripts/discourse/models/chat-channel.jsindex 209941f5bc5..440f9f84a62 100644--- a/plugins/chat/assets/javascripts/discourse/models/chat-channel.js+++ b/plugins/chat/assets/javascripts/discourse/models/chat-channel.js@@ -201,6 +201,16 @@ export default class ChatChannel {return this.meta.can_join_chat_channel;}+ get hasUnread() {+ return (+ this.tracking.unreadCount ++ this.tracking.mentionCount ++ this.tracking.watchedThreadsUnreadCount ++ this.threadsManager.unreadThreadCount >+ 0+ );+ }+async stageMessage(message) {message.id = guid();message.staged = true;diff --git a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.jsindex fbfd0d552d5..b06fa4e6913 100644--- a/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js+++ b/plugins/chat/assets/javascripts/discourse/services/chat-channels-manager.js@@ -133,14 +133,7 @@ export default class ChatChannelsManager extends Service {@cachedget publicMessageChannelsWithActivity() {- return this.publicMessageChannels.filter(- (channel) =>- channel.tracking.unreadCount +- channel.tracking.mentionCount +- channel.tracking.watchedThreadsUnreadCount +- channel.threadsManager.unreadThreadCount >- 0- );+ return this.publicMessageChannels.filter((channel) => channel.hasUnread);}get publicMessageChannelsByActivity() {@@ -159,14 +152,7 @@ export default class ChatChannelsManager extends Service {@cachedget directMessageChannelsWithActivity() {- return this.directMessageChannels.filter(- (channel) =>- channel.tracking.unreadCount +- channel.tracking.mentionCount +- channel.tracking.watchedThreadsUnreadCount +- channel.threadsManager.unreadThreadCount >- 0- );+ return this.directMessageChannels.filter((channel) => channel.hasUnread);}get truncatedDirectMessageChannels() {diff --git a/plugins/chat/assets/javascripts/discourse/services/chat.js b/plugins/chat/assets/javascripts/discourse/services/chat.jsindex f976fb7615e..d5c39bad8f5 100644--- a/plugins/chat/assets/javascripts/discourse/services/chat.js+++ b/plugins/chat/assets/javascripts/discourse/services/chat.js@@ -291,6 +291,33 @@ export default class Chat extends Service {this.chatChannelsManager.publicMessageChannelsWithActivity;directChannels =this.chatChannelsManager.directMessageChannelsWithActivity;++ // If the active channel has no unread messages, we need to manually insert it into+ // the list, so we can find the next/previous unread channel.+ if (!activeChannel.hasUnread) {+ const allChannels = activeChannel.isDirectMessageChannel+ ? this.chatChannelsManager.directMessageChannels+ : this.chatChannelsManager.publicMessageChannels;++ // Find the ID of the channel before the active channel, which is unread+ let checkChannelIndex =+ allChannels.findIndex((c) => c.id === activeChannel.id) - 1;++ // If we get back to the start of the list, we can stop+ while (checkChannelIndex >= 0) {+ if (allChannels[checkChannelIndex].hasUnread) {+ break;+ }+ checkChannelIndex--;+ }++ // Insert the active channel after unread channel we found (or at the start of the list)+ if (activeChannel.isDirectMessageChannel) {+ directChannels.splice(checkChannelIndex + 1, 0, activeChannel);+ } else {+ publicChannels.splice(checkChannelIndex + 1, 0, activeChannel);+ }+ }} else {publicChannels = this.chatChannelsManager.publicMessageChannels;directChannels = this.chatChannelsManager.directMessageChannels;diff --git a/plugins/chat/spec/system/page_objects/sidebar/sidebar.rb b/plugins/chat/spec/system/page_objects/sidebar/sidebar.rbindex c95bb847741..f1ae291fcf1 100644--- a/plugins/chat/spec/system/page_objects/sidebar/sidebar.rb+++ b/plugins/chat/spec/system/page_objects/sidebar/sidebar.rb@@ -64,14 +64,24 @@ module PageObjectsenddef has_unread_channel?(channel)- has_css?(".sidebar-section-link.channel-#{channel.id} .sidebar-section-link-suffix.unread")+ has_css?(+ ".sidebar-section-link.channel-#{channel.id} .sidebar-section-link-suffix:is(.unread, .urgent)",+ )enddef has_no_unread_channel?(channel)has_no_css?(- ".sidebar-section-link.channel-#{channel.id} .sidebar-section-link-suffix.unread",+ ".sidebar-section-link.channel-#{channel.id} .sidebar-section-link-suffix:is(.unread, .urgent)",)end++ def has_active_channel?(channel)+ has_css?(".sidebar-section-link.channel-#{channel.id}.active")+ end++ def has_no_active_channel?(channel)+ has_no_css?(".sidebar-section-link.channel-#{channel.id}.active")+ endendendenddiff --git a/plugins/chat/spec/system/shortcuts/sidebar_spec.rb b/plugins/chat/spec/system/shortcuts/sidebar_spec.rbindex 71dcf74397e..48956303dd0 100644--- a/plugins/chat/spec/system/shortcuts/sidebar_spec.rb+++ b/plugins/chat/spec/system/shortcuts/sidebar_spec.rb@@ -4,6 +4,7 @@ RSpec.describe "Shortcuts | sidebar", type: :system dofab!(:current_user) { Fabricate(:admin) }let(:chat) { PageObjects::Pages::Chat.new }+ let(:sidebar_page) { PageObjects::Pages::Sidebar.new }before doSiteSetting.navigation_menu = "sidebar"@@ -22,41 +23,43 @@ RSpec.describe "Shortcuts | sidebar", type: :system dovisit("/")find("body").send_keys(%i[alt arrow_down])- expect(page).to have_no_selector(".channel-#{channel_1.id}.active")- expect(page).to have_no_selector(".channel-#{dm_channel_1.id}.active")+ expect(sidebar_page).to have_no_active_channel(channel_1)+ expect(sidebar_page).to have_no_active_channel(dm_channel_1)endendcontext "when on chat page" doit "navigates through the channels" dochat.visit_channel(channel_1)-- expect(page).to have_selector(".channel-#{channel_1.id}.active")+ expect(sidebar_page).to have_active_channel(channel_1)find("body").send_keys(%i[alt arrow_down])- expect(page).to have_selector(".channel-#{dm_channel_1.id}.active")+ expect(sidebar_page).to have_active_channel(dm_channel_1)find("body").send_keys(%i[alt arrow_down])- expect(page).to have_selector(".channel-#{channel_1.id}.active")+ expect(sidebar_page).to have_active_channel(channel_1)find("body").send_keys(%i[alt arrow_up])- expect(page).to have_selector(".channel-#{dm_channel_1.id}.active")+ expect(sidebar_page).to have_active_channel(dm_channel_1)endendendcontext "when using Alt+Shift+Up/Down arrows" do- fab!(:channel_1) { Fabricate(:chat_channel) }- fab!(:channel_2) { Fabricate(:chat_channel) }+ fab!(:channel_1) { Fabricate(:chat_channel, name: "Channel 1") }+ fab!(:channel_2) { Fabricate(:chat_channel, name: "Channel 2") }+ fab!(:channel_3) { Fabricate(:chat_channel, name: "Channel 3") }fab!(:dm_channel_1) { Fabricate(:direct_message_channel, users: [current_user]) }- fab!(:dm_channel_2) { Fabricate(:direct_message_channel, users: [current_user]) }+ fab!(:other_user) { Fabricate(:user) }+ fab!(:dm_channel_2) { Fabricate(:direct_message_channel, users: [current_user, other_user]) }before dochannel_1.add(current_user)channel_2.add(current_user)+ channel_3.add(current_user)endcontext "when on homepage" do@@ -64,34 +67,37 @@ RSpec.describe "Shortcuts | sidebar", type: :system dovisit("/")find("body").send_keys(%i[alt shift arrow_down])- expect(page).to have_no_selector(".channel-#{channel_1.id}.active")- expect(page).to have_no_selector(".channel-#{channel_2.id}.active")- expect(page).to have_no_selector(".channel-#{dm_channel_1.id}.active")- expect(page).to have_no_selector(".channel-#{dm_channel_2.id}.active")+ expect(sidebar_page).to have_no_active_channel(channel_1)+ expect(sidebar_page).to have_no_active_channel(channel_2)+ expect(sidebar_page).to have_no_active_channel(dm_channel_1)+ expect(sidebar_page).to have_no_active_channel(dm_channel_2)endendcontext "when on chat page" doit "does nothing when no channels have activity" dochat.visit_channel(channel_1)-- expect(page).to have_selector(".channel-#{channel_1.id}.active")+ expect(sidebar_page).to have_active_channel(channel_1)find("body").send_keys(%i[alt shift arrow_down])- expect(page).to have_selector(".channel-#{channel_1.id}.active")+ expect(sidebar_page).to have_active_channel(channel_1)find("body").send_keys(%i[alt shift arrow_down])- expect(page).to have_selector(".channel-#{channel_1.id}.active")+ expect(sidebar_page).to have_active_channel(channel_1)find("body").send_keys(%i[alt shift arrow_up])- expect(page).to have_selector(".channel-#{channel_1.id}.active")+ expect(sidebar_page).to have_active_channel(channel_1)endit "navigates through the channels with activity" do+ chat.visit_channel(channel_1)+ expect(sidebar_page).to have_active_channel(channel_1)+Fabricate(:chat_message, chat_channel: channel_2, message: "hello!", use_service: true)+ expect(sidebar_page).to have_unread_channel(channel_2)Fabricate(:chat_message,@@ -99,18 +105,17 @@ RSpec.describe "Shortcuts | sidebar", type: :system domessage: "hello here!",use_service: true,)-- chat.visit_channel(channel_1)-- expect(page).to have_selector(".channel-#{channel_1.id}.active")+ expect(sidebar_page).to have_unread_channel(dm_channel_2)find("body").send_keys(%i[alt shift arrow_down])- expect(page).to have_selector(".channel-#{channel_2.id}.active")+ expect(sidebar_page).to have_active_channel(channel_2)+ expect(sidebar_page).to have_no_unread_channel(channel_2)find("body").send_keys(%i[alt shift arrow_down])- expect(page).to have_selector(".channel-#{dm_channel_2.id}.active")+ expect(sidebar_page).to have_active_channel(dm_channel_2)+ expect(sidebar_page).to have_no_unread_channel(dm_channel_2)Fabricate(:chat_message,@@ -118,16 +123,36 @@ RSpec.describe "Shortcuts | sidebar", type: :system domessage: "hello again!",use_service: true,)+ expect(sidebar_page).to have_unread_channel(channel_1)find("body").send_keys(%i[alt shift arrow_up])- expect(page).to have_selector(".channel-#{channel_1.id}.active")+ expect(sidebar_page).to have_active_channel(channel_1)+ expect(sidebar_page).to have_no_unread_channel(channel_1)Fabricate(:chat_message, chat_channel: dm_channel_1, message: "bye now!", use_service: true)+ expect(sidebar_page).to have_unread_channel(dm_channel_1)find("body").send_keys(%i[alt shift arrow_up])- expect(page).to have_selector(".channel-#{dm_channel_1.id}.active")+ expect(sidebar_page).to have_active_channel(dm_channel_1)+ expect(sidebar_page).to have_no_unread_channel(dm_channel_1)+ end++ it "remembers where the current channel is, even if that channel is unread" do+ chat.visit_channel(channel_2)+ expect(sidebar_page).to have_active_channel(channel_2)++ Fabricate(:chat_message, chat_channel: channel_1, message: "hello!", use_service: true)+ expect(sidebar_page).to have_unread_channel(channel_1)++ Fabricate(:chat_message, chat_channel: channel_3, message: "hello!", use_service: true)+ expect(sidebar_page).to have_unread_channel(channel_3)++ find("body").send_keys(%i[alt shift arrow_down])++ expect(sidebar_page).to have_active_channel(channel_3)+ expect(sidebar_page).to have_no_unread_channel(channel_3)endendend