diff --git a/plugins/chat/assets/javascripts/discourse/chat-route-map.js b/plugins/chat/assets/javascripts/discourse/chat-route-map.js index bd3826c0adc..9e63f4962e1 100644 --- a/plugins/chat/assets/javascripts/discourse/chat-route-map.js +++ b/plugins/chat/assets/javascripts/discourse/chat-route-map.js @@ -15,6 +15,8 @@ export default function () { this.route("channels"); this.route("threads"); + this.route("new-message"); + this.route( "channel.info", { path: "/c/:channelTitle/:channelId/info" }, diff --git a/plugins/chat/assets/javascripts/discourse/controllers/chat-new-message.js b/plugins/chat/assets/javascripts/discourse/controllers/chat-new-message.js new file mode 100644 index 00000000000..0d20da040c9 --- /dev/null +++ b/plugins/chat/assets/javascripts/discourse/controllers/chat-new-message.js @@ -0,0 +1,5 @@ +import Controller from "@ember/controller"; + +export default class ChatNewMessageController extends Controller { + queryParams = ["recipients"]; +} diff --git a/plugins/chat/assets/javascripts/discourse/routes/chat-new-message.js b/plugins/chat/assets/javascripts/discourse/routes/chat-new-message.js new file mode 100644 index 00000000000..1c7107bdd07 --- /dev/null +++ b/plugins/chat/assets/javascripts/discourse/routes/chat-new-message.js @@ -0,0 +1,20 @@ +import { inject as service } from "@ember/service"; +import DiscourseRoute from "discourse/routes/discourse"; + +export default class ChatNewMessageRoute extends DiscourseRoute { + @service chat; + @service router; + + beforeModel(transition) { + const recipients = this.paramsFor(this.routeName).recipients?.split(","); + + if (!recipients) { + transition.abort(); + return this.router.transitionTo("chat"); + } + + this.chat.upsertDmChannel({ usernames: recipients }).then((channel) => { + this.router.transitionTo("chat.channel", channel.title, channel.id); + }); + } +} diff --git a/plugins/chat/config/routes.rb b/plugins/chat/config/routes.rb index 9f03bfbd868..8e49e12f4c8 100644 --- a/plugins/chat/config/routes.rb +++ b/plugins/chat/config/routes.rb @@ -75,6 +75,7 @@ Chat::Engine.routes.draw do # chat_controller routes get "/" => "chat#respond" + get "/new-message" => "chat#respond" get "/direct-messages" => "chat#respond" get "/channels" => "chat#respond" get "/threads" => "chat#respond" diff --git a/plugins/chat/spec/system/chat_new_message_spec.rb b/plugins/chat/spec/system/chat_new_message_spec.rb new file mode 100644 index 00000000000..1ee12b31d9f --- /dev/null +++ b/plugins/chat/spec/system/chat_new_message_spec.rb @@ -0,0 +1,47 @@ +# frozen_string_literal: true + +RSpec.describe "Chat New Message from params", type: :system do + fab!(:current_user) { Fabricate(:user) } + fab!(:user_1) { Fabricate(:user) } + fab!(:user_2) { Fabricate(:user) } + fab!(:public_channel) { Fabricate(:chat_channel) } + fab!(:user_1_channel) { Fabricate(:direct_message_channel, users: [current_user, user_1]) } + let(:chat_page) { PageObjects::Pages::Chat.new } + + before do + chat_system_bootstrap + public_channel.add(current_user) + sign_in(current_user) + end + + context "with a single user" do + it "redirects to existing chat channel" do + chat_page.visit_new_message(user_1) + + expect(page).to have_current_path("/chat/c/#{user_1.username}/#{user_1_channel.id}") + end + + it "creates a dm channel and redirects if none exists" do + chat_page.visit_new_message(user_2) + + expect(page).to have_current_path("/chat/c/#{user_2.username}/#{Chat::Channel.last.id}") + end + + it "redirects to chat channel if recipients param is missing" do + visit("/chat/new-message") + + # chat service selects public channel from getIdealFirstChannelId + expect(page).to have_current_path("/chat/c/#{public_channel.slug}/#{public_channel.id}") + end + end + + context "with multiple users" do + it "creates a dm channel with multiple users" do + chat_page.visit_new_message([user_1, user_2]) + + expect(page).to have_current_path( + "/chat/c/#{user_1.username}-#{user_2.username}/#{Chat::Channel.last.id}", + ) + end + end +end diff --git a/plugins/chat/spec/system/page_objects/chat/chat.rb b/plugins/chat/spec/system/page_objects/chat/chat.rb index 8df22f8c623..f86e1d3660d 100644 --- a/plugins/chat/spec/system/page_objects/chat/chat.rb +++ b/plugins/chat/spec/system/page_objects/chat/chat.rb @@ -91,6 +91,16 @@ module PageObjects PageObjects::Pages::ChatBrowse.new.has_finished_loading? end + def visit_new_message(recipients) + if recipients.is_a?(Array) + recipients = recipients.map(&:username).join(",") + elsif recipients.respond_to?(:username) + recipients = recipients.username + end + + visit("/chat/new-message?recipients=#{recipients}") + end + def has_finished_loading? has_no_css?(".chat-channel--not-loaded-once") has_no_css?(".chat-skeleton")