FIX: Stabilize direct message channel creation (#219)

FIX: Stabilize direct message channel creation (#219)

diff --git a/assets/javascripts/discourse/components/dm-creator.js b/assets/javascripts/discourse/components/dm-creator.js
index 3555d10..9060cf5 100644
--- a/assets/javascripts/discourse/components/dm-creator.js
+++ b/assets/javascripts/discourse/components/dm-creator.js
@@ -20,9 +20,8 @@ export default Component.extend({
       data: { usernames: this.usernames.uniq().join(",") },
     }).then((response) => {
       this.set("usernames", null);
-      this.chat.forceRefreshChannels().then(() => {
-        this.afterCreate(response.chat_channel);
-      });
+      this.chat.addDirectMessageChannel(response.chat_channel);
+      this.afterCreate(response.chat_channel);
     });
   },
 
diff --git a/assets/javascripts/discourse/services/chat.js b/assets/javascripts/discourse/services/chat.js
index 293745c..d8406c3 100644
--- a/assets/javascripts/discourse/services/chat.js
+++ b/assets/javascripts/discourse/services/chat.js
@@ -6,6 +6,7 @@ import { ajax } from "discourse/lib/ajax";
 import { A } from "@ember/array";
 import { defaultHomepage } from "discourse/lib/utilities";
 import { generateCookFunction } from "discourse/lib/text";
+import { next } from "@ember/runloop";
 import { Promise } from "rsvp";
 import simpleCategoryHashMentionTransform from "discourse/plugins/discourse-topic-chat/discourse/lib/simple-category-hash-mention-transform";
 
@@ -162,11 +163,13 @@ export default Service.extend({
   },
 
   _updatePresence() {
-    if (this.fullScreenChatOpen || this.chatOpen) {
-      this.presenceChannel.enter();
-    } else {
-      this.presenceChannel.leave();
-    }
+    next(() => {
+      if (this.fullScreenChatOpen || this.chatOpen) {
+        this.presenceChannel.enter();
+      } else {
+        this.presenceChannel.leave();
+      }
+    });
   },
 
   setHasUnreadMessages(value) {
@@ -354,20 +357,22 @@ export default Service.extend({
     );
   },
 
+  addDirectMessageChannel(channel) {
+    if (this.directMessageChannels.findBy("id", channel.id)) {
+      return; // User is already tracking this channel. return!
+    }
+    this.directMessageChannels.pushObject(this.processChannel(channel));
+    this.currentUser.chat_channel_tracking_state[channel.id] = {
+      unread_count: 0,
+      unread_mentions: 0,
+      chatable_type: "DirectMessageChannel",
+    };
+    this.userChatChannelTrackingStateChanged();
+  },
+
   _subscribeToNewDmChannelUpdates() {
     this.messageBus.subscribe("/chat/new-direct-message-channel", (busData) => {
-      if (this.directMessageChannels.findBy("id", busData.chat_channel.id)) {
-        return; // User is already tracking this channel. return!
-      }
-      this.directMessageChannels.pushObject(
-        this.processChannel(busData.chat_channel)
-      );
-      this.currentUser.chat_channel_tracking_state[busData.chat_channel.id] = {
-        unread_count: 0,
-        unread_mentions: 0,
-        chatable_type: "DirectMessageChannel",
-      };
-      this.userChatChannelTrackingStateChanged();
+      this.addDirectMessageChannel(busData.chat_channel);
     });
   },
 
diff --git a/test/javascripts/acceptance/chat-test.js b/test/javascripts/acceptance/chat-test.js
index 348e251..e42ad17 100644
--- a/test/javascripts/acceptance/chat-test.js
+++ b/test/javascripts/acceptance/chat-test.js
@@ -29,6 +29,7 @@ import {
   presentUserIds,
 } from "discourse/tests/helpers/presence-pretender";
 import User from "discourse/models/user";
+import selectKit from "discourse/tests/helpers/select-kit-helper";
 
 const baseChatPretenders = (server, helper) => {
   server.get("/chat/:chatChannelId/messages.json", () =>
@@ -138,6 +139,36 @@ acceptance("Discourse Chat - without unread", function (needs) {
     siteChannelPretender(server, helper);
     directMessageChannelPretender(server, helper);
     chatChannelPretender(server, helper);
+    const hawkAsJson = {
+      username: "hawk",
+      id: 2,
+      name: "hawk",
+      avatar_template:
+        "https://avatars.discourse.org/v3/letter/t/41988e/{size}.png",
+    };
+    server.get("/u/search/users", () => {
+      return helper.response({
+        users: [hawkAsJson],
+      });
+    });
+
+    server.post("/chat/direct_messages/create.json", () => {
+      return helper.response({
+        chat_channel: {
+          chat_channels: [],
+          chatable: { users: [hawkAsJson] },
+          chatable_id: 16,
+          chatable_type: "DirectMessageChannel",
+          chatable_url: null,
+          id: 18,
+          last_read_message_id: null,
+          title: "@hawk",
+          unread_count: 0,
+          unread_mentions: 0,
+          updated_at: "2021-11-08T21:26:05.710Z",
+        },
+      });
+    });
   });
 
   test("Clicking mention notification from outside chat opens the float", async function (assert) {
@@ -498,6 +529,22 @@ acceptance("Discourse Chat - without unread", function (needs) {
     await click("#chat-move-to-topic-btn");
     assert.ok(exists(".move-chat-to-topic-modal"));
   });
+
+  test("creating a new direct message channel works", async function (assert) {
+    await visit("/chat/channel/Site");
+    await click(".new-dm");
+    let users = selectKit(".dm-user-chooser");
+    await click(".dm-user-chooser");
+    await users.expand();
+    await fillIn(".dm-user-chooser input.filter-input", "hawk");
+    await users.selectRowByValue("hawk");
+    await click("button.create-dm");
+    assert.equal(currentURL(), "/chat/channel/@hawk");
+    assert.notOk(
+      query(".join-channel-btn"),
+      "Join channel button is not present"
+    );
+  });
 });
 
 acceptance(

GitHub sha: b17e6f2da6f3aa0a19a73669690966b522422b20

This commit appears in #219 which was approved by eviltrout. It was merged by markvanlan.