Skip to content

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

FIX: ensures we fetch the direct message channel (#37767)

Joffrey JAFFEUX18a1e0ff20e8
diff --git a/plugins/chat/app/controllers/chat/direct_messages_controller.rb b/plugins/chat/app/controllers/chat/direct_messages_controller.rb
index 2ab75d8b95b..ad04923e561 100644
--- a/plugins/chat/app/controllers/chat/direct_messages_controller.rb
+++ b/plugins/chat/app/controllers/chat/direct_messages_controller.rb
@@ -8,7 +8,7 @@ module Chat
direct_message = Chat::DirectMessage.for_user_ids(users.map(&:id).uniq)
if direct_message
- chat_channel = Chat::Channel.find_by(chatable_id: direct_message)
+ chat_channel = direct_message.direct_message_channel
render_serialized(
chat_channel,
Chat::ChannelSerializer,
diff --git a/plugins/chat/spec/requests/direct_messages_controller_spec.rb b/plugins/chat/spec/requests/direct_messages_controller_spec.rb
index 42387098b3a..ab2f2be4238 100644
--- a/plugins/chat/spec/requests/direct_messages_controller_spec.rb
+++ b/plugins/chat/spec/requests/direct_messages_controller_spec.rb
@@ -37,6 +37,34 @@ RSpec.describe Chat::DirectMessagesController do
end
end
+ context "when a category channel exists with the same chatable_id as the direct message" do
+ it "returns the direct message channel, not the category channel" do
+ category = Fabricate(:category, name: "Secret Category")
+ cat_channel =
+ Chat::CategoryChannel.create!(
+ chatable: category,
+ chatable_type: "Category",
+ name: "Secret Category",
+ )
+
+ dm = Chat::DirectMessage.create!
+ dm.direct_message_users.create!(user_id: user.id)
+ dm.direct_message_users.create!(user_id: user1.id)
+ dm_channel = Chat::DirectMessageChannel.create!(chatable: dm)
+
+ DB.exec(
+ "UPDATE chat_channels SET chatable_id = :target_id WHERE id = :channel_id",
+ target_id: dm.id,
+ channel_id: cat_channel.id,
+ )
+
+ get "/chat/direct_messages.json", params: { usernames: user1.username }
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["channel"]["id"]).to eq(dm_channel.id)
+ expect(response.parsed_body["channel"]["chatable_type"]).to eq("DirectMessage")
+ end
+ end
+
context "when channel exists" do
let!(:channel) do
direct_messages_channel = Chat::DirectMessage.create!