From 8550b783b31d7aeb8ac6de4c1e2d013ddca377b3 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Fri, 10 May 2024 13:11:04 +0300 Subject: =?UTF-8?q?fixes=20#4410=20(Discord:=20=D0=B7=D0=B0=D0=B3=D1=80?= =?UTF-8?q?=D1=83=D0=B7=D0=BA=D0=B0=20=D0=B8=D1=81=D1=82=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D0=B8=20=D1=81=20=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B6=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=D0=BD=D0=B5=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=B5?= =?UTF-8?q?=20=D1=81=D0=BE=D0=B1=D1=8B=D1=82=D0=B8=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- protocols/Discord/src/dispatch.cpp | 42 ++---------------------------------- protocols/Discord/src/guilds.cpp | 2 +- protocols/Discord/src/proto.h | 1 + protocols/Discord/src/server.cpp | 4 ++-- protocols/Discord/src/stdafx.h | 1 - protocols/Discord/src/utils.cpp | 44 ++++++++++++++++++++++++++++++++++++-- 6 files changed, 48 insertions(+), 46 deletions(-) (limited to 'protocols/Discord/src') diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index f251abaa53..e2dcb9d30d 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -444,10 +444,9 @@ void CDiscordProto::OnCommandMessageUpdate(const JSONNode &pRoot) void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) { - CMStringW wszMessageId = pRoot["id"].as_mstring(); CMStringA szUserId = pRoot["author"]["id"].as_mstring(); SnowFlake userId = _atoi64(szUserId); - SnowFlake msgId = _wtoi64(wszMessageId); + SnowFlake msgId = ::getId(pRoot["id"]); // try to find a sender by his channel SnowFlake channelId = ::getId(pRoot["channel_id"]); @@ -474,43 +473,7 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) debugLogA("skipping own message with nonce=%lld, id=%lld", ownMsg.nonce, msgId); } else { - CMStringW wszText = PrepareMessageText(pRoot), wszMentioned, wszAuthor = getName(pRoot["author"]); - SnowFlake mentionId = 0; - - for (auto &it : pRoot["mentions"]) { - wszMentioned = getName(it); - mentionId = _wtoi64(it["id"].as_mstring()); - break; - } - - switch (pRoot["type"].as_int()) { - case 1: // user was added to chat - if (mentionId != userId) - wszText.Format(TranslateT("%s added %s to the group"), wszAuthor.c_str(), wszMentioned.c_str()); - else - wszText.Format(TranslateT("%s joined the group"), wszMentioned.c_str()); - break; - - case 2: // user was removed from chat - if (mentionId != userId) - wszText.Format(TranslateT("%s removed %s from the group"), wszAuthor.c_str(), wszMentioned.c_str()); - else - wszText.Format(TranslateT("%s left the group"), wszMentioned.c_str()); - break; - - case 3: // call - break; - - case 4: // chat was renamed - if (pUser->si) - setWString(pUser->si->hContact, "Nick", wszText); - return; - - case 5: // chat icon is changed - wszText.Format(TranslateT("%s changed the group icon"), wszAuthor.c_str()); - break; - } - + CMStringW wszText = PrepareMessageText(pRoot, pUser); if (wszText.IsEmpty()) return; @@ -552,7 +515,6 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) ProtoChainRecvMsg(pUser->hContact, dbei); } } - } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp index 67fb04928b..a4e5a8ee3b 100644 --- a/protocols/Discord/src/guilds.cpp +++ b/protocols/Discord/src/guilds.cpp @@ -226,7 +226,7 @@ CDiscordUser* CDiscordProto::ProcessGuildChannel(CDiscordGuild *pGuild, const JS pUser->wszTopic = pch["topic"].as_mstring(); pUser->pGuild = pGuild; pUser->lastMsgId = ::getId(pch["last_message_id"]); - pUser->parentId = _wtoi64(pch["parent_id"].as_mstring()); + pUser->parentId = ::getId(pch["parent_id"]); return pUser; } diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 48664c424d..7099600f52 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -347,6 +347,7 @@ class CDiscordProto : public PROTO CDiscordUser* FindUser(const wchar_t *pwszUsername, int iDiscriminator); CDiscordUser* FindUserByChannel(SnowFlake channelId); + CMStringW PrepareMessageText(const JSONNode &pRoot, CDiscordUser *pUser); void PreparePrivateChannel(const JSONNode &); CDiscordUser* PrepareUser(const JSONNode &); diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp index c083e9b91d..0b48dc3ab9 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -85,10 +85,10 @@ void CDiscordProto::OnReceiveHistory(MHttpResponse *pReply, AsyncHttpRequest *pR for (auto &it : arNodes) { auto &pNode = *it; - if (pNode["type"].as_int() != 0) + CMStringW wszText = PrepareMessageText(pNode, pUser); + if (wszText.IsEmpty()) continue; - CMStringW wszText = PrepareMessageText(pNode); CMStringA szUserId = pNode["author"]["id"].as_mstring(); SnowFlake msgid = ::getId(pNode["id"]); SnowFlake authorid = _atoi64(szUserId); diff --git a/protocols/Discord/src/stdafx.h b/protocols/Discord/src/stdafx.h index 7bfd1425e9..1623cbafa6 100644 --- a/protocols/Discord/src/stdafx.h +++ b/protocols/Discord/src/stdafx.h @@ -80,7 +80,6 @@ void CopyId(const CMStringW &nick); SnowFlake getId(const JSONNode &pNode); CMStringW getName(const JSONNode &pNode); CMStringW getNick(const JSONNode &pNode); -CMStringW PrepareMessageText(const JSONNode &pRoot); int StrToStatus(const CMStringW &str); time_t StringToDate(const CMStringW &str); int SerialNext(void); diff --git a/protocols/Discord/src/utils.cpp b/protocols/Discord/src/utils.cpp index 952e5d8261..2e7c5defe7 100644 --- a/protocols/Discord/src/utils.cpp +++ b/protocols/Discord/src/utils.cpp @@ -114,6 +114,8 @@ SnowFlake CDiscordProto::getId(MCONTACT hContact, const char *szSetting) case DBVT_WCHAR: result = _wtoi64(dbv.pwszVal); break; + default: + result = 0; } db_free(&dbv); return result; @@ -230,7 +232,7 @@ void CDiscordProto::PreparePrivateChannel(const JSONNode &root) Chat_AddGroup(si, LPGENW("Owners")); Chat_AddGroup(si, LPGENW("Participants")); { - SnowFlake ownerId = _wtoi64(root["owner_id"].as_mstring()); + SnowFlake ownerId = ::getId(root["owner_id"]); setId(pUser->hContact, DB_KEY_OWNERID, ownerId); CheckAvatarChange(si->hContact, root["icon"].as_mstring()); @@ -339,7 +341,7 @@ CDiscordUser* CDiscordProto::PrepareUser(const JSONNode &user) ///////////////////////////////////////////////////////////////////////////////////////// -CMStringW PrepareMessageText(const JSONNode &pRoot) +CMStringW CDiscordProto::PrepareMessageText(const JSONNode &pRoot, CDiscordUser *pUser) { CMStringW wszText = pRoot["content"].as_mstring(); @@ -374,6 +376,44 @@ CMStringW PrepareMessageText(const JSONNode &pRoot) wszText.AppendFormat(L"\n%s: %s", TranslateT("Preview"), str.c_str()); } + auto &nAuthor = pRoot["author"]; + SnowFlake mentionId = 0, userId = ::getId(nAuthor["id"]); + CMStringW wszMentioned, wszAuthor = getName(nAuthor); + + for (auto &it : pRoot["mentions"]) { + wszMentioned = getName(it); + mentionId = ::getId(it["id"]); + break; + } + + switch (pRoot["type"].as_int()) { + case 1: // user was added to chat + if (mentionId != userId) + wszText.Format(TranslateT("%s added %s to the group"), wszAuthor.c_str(), wszMentioned.c_str()); + else + wszText.Format(TranslateT("%s joined the group"), wszMentioned.c_str()); + break; + + case 2: // user was removed from chat + if (mentionId != userId) + wszText.Format(TranslateT("%s removed %s from the group"), wszAuthor.c_str(), wszMentioned.c_str()); + else + wszText.Format(TranslateT("%s left the group"), wszMentioned.c_str()); + break; + + case 3: // call + break; + + case 4: // chat was renamed + if (pUser->si) + setWString(pUser->si->hContact, "Nick", wszText); + break; + + case 5: // chat icon is changed + wszText.Format(TranslateT("%s changed the group icon"), wszAuthor.c_str()); + break; + } + return wszText; } -- cgit v1.2.3