From b7718450b0856447bb7098f73d674fbd54c16e2d Mon Sep 17 00:00:00 2001 From: George Hazan Date: Thu, 9 Feb 2023 18:22:05 +0300 Subject: database support for new protocols --- protocols/Discord/src/dispatch.cpp | 52 +++++++---------------- protocols/Discord/src/guilds.cpp | 3 +- protocols/Discord/src/proto.cpp | 2 +- protocols/Discord/src/proto.h | 2 +- protocols/Discord/src/server.cpp | 85 +++++++++++++++++--------------------- protocols/Discord/src/utils.cpp | 2 +- 6 files changed, 59 insertions(+), 87 deletions(-) (limited to 'protocols/Discord/src') diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index 2c749eb8b9..10c39e8241 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -361,8 +361,8 @@ void CDiscordProto::OnCommandMessageUpdate(const JSONNode &pRoot) void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) { CMStringW wszMessageId = pRoot["id"].as_mstring(); - CMStringW wszUserId = pRoot["author"]["id"].as_mstring(); - SnowFlake userId = _wtoi64(wszUserId); + CMStringA szUserId = pRoot["author"]["id"].as_mstring(); + SnowFlake userId = _atoi64(szUserId); SnowFlake msgId = _wtoi64(wszMessageId); // try to find a sender by his channel @@ -408,44 +408,24 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) if (!edited.isnull()) wszText.AppendFormat(L" (%s %s)", TranslateT("edited at"), edited.as_mstring().c_str()); - if (pUser->bIsPrivate && !pUser->bIsGroup) { - // if a message has myself as an author, add some flags - PROTORECVEVENT recv = {}; - if (bOurMessage) - recv.flags = PREF_CREATEREAD | PREF_SENT; + // if a message has myself as an author, add some flags + PROTORECVEVENT recv = {}; + if (bOurMessage) + recv.flags = PREF_CREATEREAD | PREF_SENT; - debugLogA("store a message from private user %lld, channel id %lld", pUser->id, pUser->channelId); - ptrA buf(mir_utf8encodeW(wszText)); + debugLogA("store a message from private user %lld, channel id %lld", pUser->id, pUser->channelId); + ptrA buf(mir_utf8encodeW(wszText)); - recv.timestamp = (uint32_t)StringToDate(pRoot["timestamp"].as_mstring()); - recv.szMessage = buf; - recv.szMsgId = szMsgId; - ProtoChainRecvMsg(pUser->hContact, &recv); - } - else { - debugLogA("store a message into the group channel id %lld", channelId); - - SESSION_INFO *si = g_chatApi.SM_FindSession(pUser->wszUsername, m_szModuleName); - if (si == nullptr) { - debugLogA("message to unknown channel %lld ignored", channelId); - return; - } - - ProcessChatUser(pUser, wszUserId, pRoot); + recv.timestamp = (uint32_t)StringToDate(pRoot["timestamp"].as_mstring()); + recv.szMessage = buf; + recv.szMsgId = szMsgId; - ParseSpecialChars(si, wszText); - wszText.Replace(L"%", L"%%"); - - GCEVENT gce = { pUser->si, GC_EVENT_MESSAGE }; - gce.dwFlags = GCEF_ADDTOLOG; - gce.pszUID.w = wszUserId; - gce.pszText.w = wszText; - gce.time = (uint32_t)StringToDate(pRoot["timestamp"].as_mstring()); - gce.bIsMe = bOurMessage; - Chat_Event(&gce); - - debugLogW(L"New channel %s message from %s: %s", si->ptszID, gce.pszUID.w, gce.pszText.w); + if (!pUser->bIsPrivate || pUser->bIsGroup) { + recv.szUserId = szUserId; + ProcessChatUser(pUser, userId, pRoot); } + + ProtoChainRecvMsg(pUser->hContact, &recv); } pUser->lastMsgId = msgId; diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp index 6095b0a234..d4b8999a13 100644 --- a/protocols/Discord/src/guilds.cpp +++ b/protocols/Discord/src/guilds.cpp @@ -318,10 +318,9 @@ CDiscordGuildMember* CDiscordProto::ProcessGuildUser(CDiscordGuild *pGuild, cons ///////////////////////////////////////////////////////////////////////////////////////// -void CDiscordProto::ProcessChatUser(CDiscordUser *pChat, const CMStringW &wszUserId, const JSONNode &pRoot) +void CDiscordProto::ProcessChatUser(CDiscordUser *pChat, SnowFlake userId, const JSONNode &pRoot) { // input data control - SnowFlake userId = _wtoi64(wszUserId); CDiscordGuild *pGuild = pChat->pGuild; if (pGuild == nullptr || userId == 0) return; diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index 470dbdf7b5..8e89312050 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -98,7 +98,7 @@ CDiscordProto::CDiscordProto(const char *proto_name, const wchar_t *username) : // Groupchat initialization GCREGISTER gcr = {}; - gcr.dwFlags = GC_TYPNOTIF | GC_CHANMGR; + gcr.dwFlags = GC_TYPNOTIF | GC_CHANMGR | GC_DATABASE; gcr.ptszDispName = m_tszUserName; gcr.pszModule = m_szModuleName; Chat_Register(&gcr); diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 683c0cbb05..04645185d2 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -331,7 +331,7 @@ class CDiscordProto : public PROTO void Chat_ProcessNickMenu(GCHOOK* gch); void CreateChat(CDiscordGuild *pGuild, CDiscordUser *pUser); - void ProcessChatUser(CDiscordUser *pChat, const CMStringW &wszUserId, const JSONNode &pRoot); + void ProcessChatUser(CDiscordUser *pChat, SnowFlake userId, const JSONNode &pRoot); void ParseSpecialChars(SESSION_INFO *si, CMStringW &str); ////////////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp index 8266e11a0b..7ac73e38bf 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -86,56 +86,49 @@ void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest for (auto &it : arNodes) { auto &pNode = *it; CMStringW wszText = PrepareMessageText(pNode); - CMStringW wszUserId = pNode["author"]["id"].as_mstring(); + CMStringA szUserId = pNode["author"]["id"].as_mstring(); SnowFlake msgid = ::getId(pNode["id"]); - SnowFlake authorid = _wtoi64(wszUserId); + SnowFlake authorid = _atoi64(szUserId); uint32_t dwTimeStamp = StringToDate(pNode["timestamp"].as_mstring()); - if (pUser->bIsPrivate) { - DBEVENTINFO dbei = {}; - dbei.szModule = m_szModuleName; - dbei.flags = DBEF_UTF; - dbei.eventType = EVENTTYPE_MESSAGE; - - if (authorid == m_ownId) - dbei.flags |= DBEF_SENT; - else - dbei.flags &= ~DBEF_SENT; - - if (msgid <= pUser->lastReadId) - dbei.flags |= DBEF_READ; - else - dbei.flags &= ~DBEF_READ; - - ptrA szBody(mir_utf8encodeW(wszText)); - dbei.timestamp = dwTimeStamp; - dbei.pBlob = (uint8_t*)szBody.get(); - dbei.cbBlob = (uint32_t)mir_strlen(szBody); - - bool bSucceeded = false; - char szMsgId[100]; - _i64toa_s(msgid, szMsgId, _countof(szMsgId), 10); - MEVENT hDbEvent = db_event_getById(m_szModuleName, szMsgId); - if (hDbEvent != 0) - bSucceeded = 0 == db_event_edit(pUser->hContact, hDbEvent, &dbei); - - if (!bSucceeded) { - dbei.szId = szMsgId; - db_event_add(pUser->hContact, &dbei); - } + DBEVENTINFO dbei = {}; + dbei.szModule = m_szModuleName; + dbei.flags = DBEF_UTF; + dbei.eventType = EVENTTYPE_MESSAGE; + dbei.timestamp = dwTimeStamp; + + if (authorid == m_ownId) + dbei.flags |= DBEF_SENT; + else + dbei.flags &= ~DBEF_SENT; + + if (msgid <= pUser->lastReadId) + dbei.flags |= DBEF_READ; + else + dbei.flags &= ~DBEF_READ; + + MBinBuffer buf; + ptrA szBody(mir_utf8encodeW(wszText)); + buf.append(szBody, mir_strlen(szBody) + 1); + + if (!pUser->bIsPrivate) { + buf.append(szUserId, szUserId.GetLength() + 1); + ProcessChatUser(pUser, _atoi64(szUserId), pNode); } - else { - ProcessChatUser(pUser, wszUserId, pNode); - - ParseSpecialChars(si, wszText); - - GCEVENT gce = { si, GC_EVENT_MESSAGE }; - gce.dwFlags = GCEF_ADDTOLOG; - gce.pszUID.w = wszUserId; - gce.pszText.w = wszText; - gce.time = dwTimeStamp; - gce.bIsMe = authorid == m_ownId; - Chat_Event(&gce); + + dbei.pBlob = buf.data(); + dbei.cbBlob = (uint32_t)buf.length(); + + bool bSucceeded = false; + char szMsgId[100]; + _i64toa_s(msgid, szMsgId, _countof(szMsgId), 10); + MEVENT hDbEvent = db_event_getById(m_szModuleName, szMsgId); + if (hDbEvent != 0) + bSucceeded = 0 == db_event_edit(pUser->hContact, hDbEvent, &dbei); + + if (!bSucceeded) { + dbei.szId = szMsgId; + db_event_add(pUser->hContact, &dbei); } if (lastId < msgid) diff --git a/protocols/Discord/src/utils.cpp b/protocols/Discord/src/utils.cpp index b9297735e1..cc2e4fff3a 100644 --- a/protocols/Discord/src/utils.cpp +++ b/protocols/Discord/src/utils.cpp @@ -334,7 +334,7 @@ void CDiscordProto::ProcessType(CDiscordUser *pUser, const JSONNode &pRoot) CMStringA szId(FORMAT, "%lld", pUser->id); DB::AUTH_BLOB blob(pUser->hContact, T2Utf(pUser->wszUsername), nullptr, nullptr, szId, nullptr); - PROTORECVEVENT pre = { 0 }; + PROTORECVEVENT pre = {}; pre.timestamp = (uint32_t)time(0); pre.lParam = blob.size(); pre.szMessage = blob; -- cgit v1.2.3