A platform for community discussion. Free, open, simple.
Revert "FIX: Await channel lookups before redirecting in chat index routes" (#40474)
Gabriel Grubbac9915c69f24f
diff --git a/plugins/chat/assets/javascripts/discourse/routes/chat/channels.js b/plugins/chat/assets/javascripts/discourse/routes/chat/channels.jsindex a2825f5e437..860d36abc4c 100644--- a/plugins/chat/assets/javascripts/discourse/routes/chat/channels.js+++ b/plugins/chat/assets/javascripts/discourse/routes/chat/channels.js@@ -10,23 +10,17 @@ export default class ChatChannelsRoute extends DiscourseRoute {this.chat.activeChannel = null;}- async beforeModel() {- if (!this.site.desktopView) {- return;- }-+ beforeModel() {const id = this.currentUser.custom_fields.last_chat_channel_id;- if (id) {- const channel = await this.chatChannelsManager.find(id);- if (- channel?.isCategoryChannel &&- channel.currentUserMembership?.following- ) {- return this.router.replaceWith("chat.channel", ...channel.routeModels);+ if (this.site.desktopView) {+ if (id) {+ this.chatChannelsManager.find(id).then((c) => {+ return this.router.replaceWith("chat.channel", ...c.routeModels);+ });+ } else {+ this.router.replaceWith("chat.browse.open");}}-- return this.router.replaceWith("chat.browse.open");}model() {diff --git a/plugins/chat/assets/javascripts/discourse/routes/chat/direct-messages.js b/plugins/chat/assets/javascripts/discourse/routes/chat/direct-messages.jsindex cc8d5073db4..38a274fe80d 100644--- a/plugins/chat/assets/javascripts/discourse/routes/chat/direct-messages.js+++ b/plugins/chat/assets/javascripts/discourse/routes/chat/direct-messages.js@@ -10,33 +10,20 @@ export default class ChatDirectMessagesRoute extends DiscourseRoute {this.chat.activeChannel = null;}- async beforeModel() {- if (!this.site.desktopView) {- return;- }-- await this.chat.loadChannels();-- const dmChannels = this.chatChannelsManager.directMessageChannels;- if (dmChannels.length === 0) {- return;- }-- const id = this.currentUser.custom_fields.last_chat_channel_id;- if (id) {- const channel = await this.chatChannelsManager.find(id);- if (- channel?.isDirectMessageChannel &&- channel.currentUserMembership?.following- ) {- return this.router.replaceWith("chat.channel", ...channel.routeModels);+ beforeModel() {+ if (this.site.desktopView) {+ if (this.chatChannelsManager.directMessageChannels.length === 0) {+ // first time browsing chat and the preferred index is dms+ this.router.replaceWith("chat.direct-messages");+ } else {+ // there should be at least one dm channel+ // we can reroute using the last channel id+ const id = this.currentUser.custom_fields.last_chat_channel_id;+ this.chatChannelsManager.find(id).then((c) => {+ return this.router.replaceWith("chat.channel", ...c.routeModels);+ });}}-- return this.router.replaceWith(- "chat.channel",- ...dmChannels[0].routeModels- );}model() {diff --git a/plugins/chat/spec/system/navigation_spec.rb b/plugins/chat/spec/system/navigation_spec.rbindex 55ee805ac83..7c79cfa3f9b 100644--- a/plugins/chat/spec/system/navigation_spec.rb+++ b/plugins/chat/spec/system/navigation_spec.rb@@ -88,73 +88,13 @@ RSpec.describe "Navigation" doendcontext "when visiting mobile only routes on desktop" do- it "redirects /chat/channels to the last visited channel" do+ it "redirects /chat/channels to browse" dovisit("/chat/channels")expect(page).to have_current_path(chat.channel_path(category_channel.slug, category_channel.id),)end-- context "when user has no last_chat_channel_id" do- before { current_user.upsert_custom_fields(::Chat::LAST_CHAT_CHANNEL_ID => nil) }-- it "redirects /chat/channels to browse" do- visit("/chat/channels")-- expect(page).to have_current_path("/chat/browse/open")- end- end-- context "when last_chat_channel_id points to a DM channel" do- fab!(:other_user, :user)- fab!(:dm_channel) { Fabricate(:direct_message_channel, users: [current_user, other_user]) }-- before { current_user.upsert_custom_fields(::Chat::LAST_CHAT_CHANNEL_ID => dm_channel.id) }-- it "redirects /chat/channels to browse instead of into the DM" do- visit("/chat/channels")-- expect(page).to have_current_path("/chat/browse/open")- end- end-- context "when visiting /chat/direct-messages with DM channels" do- fab!(:other_user, :user)- fab!(:dm_channel) { Fabricate(:direct_message_channel, users: [current_user, other_user]) }-- it "redirects to the last visited DM when last_chat_channel_id is a DM" do- current_user.upsert_custom_fields(::Chat::LAST_CHAT_CHANNEL_ID => dm_channel.id)- visit("/chat/direct-messages")-- expect(page).to have_current_path(%r{/chat/c/.+/#{dm_channel.id}$})- end-- it "redirects to the first DM when last_chat_channel_id is a public channel" do- current_user.upsert_custom_fields(::Chat::LAST_CHAT_CHANNEL_ID => category_channel.id)- visit("/chat/direct-messages")-- expect(page).to have_current_path(%r{/chat/c/.+/#{dm_channel.id}$})- end-- it "redirects to the first DM when last_chat_channel_id is not set" do- current_user.upsert_custom_fields(::Chat::LAST_CHAT_CHANNEL_ID => nil)- visit("/chat/direct-messages")-- expect(page).to have_current_path(%r{/chat/c/.+/#{dm_channel.id}$})- end- end-- context "when visiting /chat/direct-messages with no DM channels" do- before { current_user.upsert_custom_fields(::Chat::LAST_CHAT_CHANNEL_ID => nil) }-- it "stays on the route and renders the empty state" do- visit("/chat/direct-messages")-- expect(page).to have_current_path("/chat/direct-messages")- expect(page).to have_css(".c-routes.--direct-messages")- end- endendcontext "when opening chat" do