From 9b76086084dcba0ef24ea2b0a94f438127f9eff5 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 4 May 2024 18:05:48 +0300 Subject: =?UTF-8?q?fixes=20#4399=20(Discord:=20=D0=BF=D0=BE=D0=BA=D0=B0?= =?UTF-8?q?=D0=B7=D1=8B=D0=B2=D0=B0=D1=82=D1=8C,=20=D0=BA=D1=82=D0=BE=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=B3=D0=BB=D0=B0=D1=81=D0=B8=D0=BB=20=D0=B8?= =?UTF-8?q?=D0=BB=D0=B8=20=D0=B2=D1=8B=D0=B3=D0=BD=D0=B0=D0=BB=20=D1=83?= =?UTF-8?q?=D1=87=D0=B0=D1=81=D1=82=D0=BD=D0=B8=D0=BA=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- protocols/Discord/src/dispatch.cpp | 43 ++++++++++++++++++++++++++++++-------- protocols/Discord/src/stdafx.h | 1 + protocols/Discord/src/utils.cpp | 16 +++++++++----- 3 files changed, 46 insertions(+), 14 deletions(-) (limited to 'protocols') diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index 0ba56aa29b..df6c9b2d7d 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -133,6 +133,7 @@ void CDiscordProto::OnCommandChannelUserAdded(const JSONNode &pRoot) CMStringW wszNick = getNick(nUser); GCEVENT gce = { pUser->si, GC_EVENT_JOIN }; + gce.dwFlags = GCEF_SILENT; gce.pszUID.w = wszUserId; gce.pszNick.w = wszNick; gce.time = time(0); @@ -148,6 +149,7 @@ void CDiscordProto::OnCommandChannelUserLeft(const JSONNode &pRoot) CMStringW wszUserId = pRoot["user"]["id"].as_mstring(); GCEVENT gce = { pUser->si, GC_EVENT_PART }; + gce.dwFlags = GCEF_SILENT; gce.pszUID.w = wszUserId; gce.time = time(0); Chat_Event(&gce); @@ -432,6 +434,13 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) return; } + // shift & store LastMsgId field + pUser->lastMsgId = msgId; + + SnowFlake lastId = getId(pUser->hContact, DB_KEY_LASTMSGID); // as stored in a database + if (lastId < msgId) + setId(pUser->hContact, DB_KEY_LASTMSGID, msgId); + char szMsgId[100]; _i64toa_s(msgId, szMsgId, _countof(szMsgId), 10); @@ -442,13 +451,34 @@ 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); - if (wszText.IsEmpty()) + CMStringW wszText = PrepareMessageText(pRoot), wszMentioned; + for (auto &it : pRoot["mentions"]) { + wszMentioned = getName(it); + break; + } + + switch (pRoot["type"].as_int()) { + case 4: // chat was renamed + if (pUser->si) + setWString(pUser->si->hContact, "Nick", wszText); return; - if (pRoot["type"].as_int() == 4 && pUser->si) { - setWString(pUser->si->hContact, "Nick", wszText); + case 1: // user was added to chat + wszText.Format(TranslateT("%s added %s to the group"), getName(pRoot["author"]).c_str(), wszMentioned.c_str()); + break; + + case 2: // user was removed from chat + wszText.Format(TranslateT("%s removed %s from the group"), getName(pRoot["author"]).c_str(), wszMentioned.c_str()); + break; + + case 3: // user left chat + wszText.Format(TranslateT("%s left group"), wszMentioned.c_str()); + break; } + + if (wszText.IsEmpty()) + return; + else { // old message? try to restore it from database bool bOurMessage = userId == m_ownId; @@ -492,11 +522,6 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) } } - pUser->lastMsgId = msgId; - - SnowFlake lastId = getId(pUser->hContact, DB_KEY_LASTMSGID); // as stored in a database - if (lastId < msgId) - setId(pUser->hContact, DB_KEY_LASTMSGID, msgId); } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/Discord/src/stdafx.h b/protocols/Discord/src/stdafx.h index dfea7e61dd..6e241a4dc6 100644 --- a/protocols/Discord/src/stdafx.h +++ b/protocols/Discord/src/stdafx.h @@ -77,6 +77,7 @@ void BuildStatusList(const CDiscordGuild *pGuild, SESSION_INFO *si); 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); diff --git a/protocols/Discord/src/utils.cpp b/protocols/Discord/src/utils.cpp index a3cf8f9f5d..bcc5c5225b 100644 --- a/protocols/Discord/src/utils.cpp +++ b/protocols/Discord/src/utils.cpp @@ -59,6 +59,14 @@ int SerialNext() ///////////////////////////////////////////////////////////////////////////////////////// +CMStringW getName(const JSONNode &pNode) +{ + CMStringW wszNick = pNode["global_name"].as_mstring(); + if (wszNick.IsEmpty()) + wszNick = pNode["username"].as_mstring(); + return wszNick; +} + CMStringW getNick(const JSONNode &pNode) { CMStringW name = pNode["username"].as_mstring(), discriminator = pNode["discriminator"].as_mstring(); @@ -191,15 +199,13 @@ void CDiscordProto::PreparePrivateChannel(const JSONNode &root) if (pUser->wszChannelName.IsEmpty()) { int i = 0; for (auto &it : root["recipients"]) { - CMStringW wszNick = it["global_name"].as_mstring(); - if (wszNick.IsEmpty()) - wszNick = it["username"].as_mstring(); - if (wszNick.IsEmpty()) + CMStringW wszName = getName(it); + if (wszName.IsEmpty()) continue; if (!pUser->wszChannelName.IsEmpty()) pUser->wszChannelName += L", "; - pUser->wszChannelName += wszNick; + pUser->wszChannelName += wszName; if (i++ > 3) break; -- cgit v1.2.3