From 943e68e8a709b7bfa906399ee4641e64f8d51f31 Mon Sep 17 00:00:00 2001
From: George Hazan <george.hazan@gmail.com>
Date: Thu, 2 May 2024 21:16:38 +0300
Subject: fixes #4382 (Discord: Groupchat support)

---
 protocols/Discord/src/utils.cpp | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

(limited to 'protocols')

diff --git a/protocols/Discord/src/utils.cpp b/protocols/Discord/src/utils.cpp
index b703fc63bb..42fd0bd4d9 100644
--- a/protocols/Discord/src/utils.cpp
+++ b/protocols/Discord/src/utils.cpp
@@ -187,8 +187,23 @@ void CDiscordProto::PreparePrivateChannel(const JSONNode &root)
 		pUser->bIsGroup = true;
 		pUser->wszUsername = wszChannelId;
 		pUser->wszChannelName = root["name"].as_mstring();
+		if (pUser->wszChannelName.IsEmpty()) {
+			int i = 0;
+			for (auto &it : root["recipients"]) {
+				CMStringW wszNick = it["username"].as_mstring();
+				if (wszNick.IsEmpty())
+					continue;
+
+				if (!pUser->wszChannelName.IsEmpty())
+					pUser->wszChannelName += L", ";
+				pUser->wszChannelName += wszNick;
+
+				if (i++ > 3)
+					break;
+			}
+		}
 		{
-			SESSION_INFO *si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, pUser->wszUsername, pUser->wszChannelName);
+			SESSION_INFO *si = pUser->si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, pUser->wszUsername, pUser->wszChannelName);
 			pUser->hContact = si->hContact;
 
 			Chat_AddGroup(si, LPGENW("Owners"));
@@ -196,12 +211,12 @@ void CDiscordProto::PreparePrivateChannel(const JSONNode &root)
 
 			SnowFlake ownerId = _wtoi64(root["owner_id"].as_mstring());
 
-			GCEVENT gce = { pUser->si, GC_EVENT_JOIN };
+			GCEVENT gce = { si, GC_EVENT_JOIN };
 			for (auto &it : root["recipients"]) {
 				CMStringW wszId = it["id"].as_mstring();
 				CMStringW wszNick = it["nick"].as_mstring();
 				if (wszNick.IsEmpty())
-					wszNick = it["username"].as_mstring() + L"#" + it["discriminator"].as_mstring();
+					wszNick = getNick(it);
 
 				gce.pszUID.w = wszId;
 				gce.pszNick.w = wszNick;
@@ -217,8 +232,8 @@ void CDiscordProto::PreparePrivateChannel(const JSONNode &root)
 			gce.pszStatus.w = (_wtoi64(wszId) == ownerId) ? L"Owners" : L"Participants";
 			Chat_Event(&gce);
 
-			Chat_Control(pUser->si, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE);
-			Chat_Control(pUser->si, SESSION_ONLINE);
+			Chat_Control(si, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE);
+			Chat_Control(si, SESSION_ONLINE);
 		}
 		break;
 
-- 
cgit v1.2.3