From c8a01589f0570bc9f630c4b498c6fb0cc54c79a4 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 23 Apr 2023 19:36:38 +0300 Subject: Telegram: file events to store server message id & groupchat user id, if present --- protocols/Telegram/src/proto.cpp | 20 ++++++++++++++++++++ protocols/Telegram/src/proto.h | 9 +++++++-- protocols/Telegram/src/server.cpp | 14 ++++---------- protocols/Telegram/src/utils.cpp | 28 +++++++++++++++++++++++++++- 4 files changed, 58 insertions(+), 13 deletions(-) (limited to 'protocols') diff --git a/protocols/Telegram/src/proto.cpp b/protocols/Telegram/src/proto.cpp index cd88af4b70..a07932b3d4 100644 --- a/protocols/Telegram/src/proto.cpp +++ b/protocols/Telegram/src/proto.cpp @@ -307,6 +307,26 @@ int CTelegramProto::FileResume(HANDLE hTransfer, int, const wchar_t *pwszFilenam return 0; } +//////////////////////////////////////////////////////////////////////////////////////// +// RecvFile - writes down an incoming file transfer to db + +MEVENT CTelegramProto::RecvFile(MCONTACT hContact, PROTORECVFILE *pre) +{ + MEVENT hEvent = CSuper::RecvFile(hContact, pre); + if (hEvent) { + if (auto *ft = (TG_FILE_REQUEST *)pre->lParam) { + DBVARIANT dbv = { DBVT_UTF8 }; + dbv.pszVal = ft->m_uniqueId.GetBuffer(); + db_event_setJson(hEvent, "u", &dbv); + + dbv.type = DBVT_DWORD; + dbv.dVal = ft->m_fileId; + db_event_setJson(hEvent, "id", &dbv); + } + } + return hEvent; +} + ///////////////////////////////////////////////////////////////////////////////////////// void CTelegramProto::OnSearchResults(td::ClientManager::Response &response) diff --git a/protocols/Telegram/src/proto.h b/protocols/Telegram/src/proto.h index a386466176..562be0e434 100644 --- a/protocols/Telegram/src/proto.h +++ b/protocols/Telegram/src/proto.h @@ -84,7 +84,7 @@ struct TG_USER : public MZeroedObject hContact(_2), isGroupChat(_3) { - chatId = (isGroupChat) ? -1 :id; + chatId = (isGroupChat) ? -1 : id; } int64_t id, chatId; @@ -223,7 +223,7 @@ class CTelegramProto : public PROTO void ProcessSuperGroup(TD::updateSupergroup *pObj); void ProcessUser(TD::updateUser *pObj); - CMStringA GetMessageText(TG_USER *pUser, TD::MessageContent *pBody); + CMStringA GetMessageText(TG_USER *pUser, const TD::message *pMsg); void UpdateString(MCONTACT hContact, const char *pszSetting, const std::string &str); @@ -245,6 +245,8 @@ class CTelegramProto : public PROTO void Chat_SendPrivateMessage(GCHOOK *gch); void Chat_LogMenu(GCHOOK *gch); + bool GetGcUserId(TG_USER *pUser, const TD::message *pMsg, char *dest); + // Search TD::array m_searchIds; @@ -262,6 +264,7 @@ class CTelegramProto : public PROTO TG_USER* AddUser(int64_t id, bool bIsChat); TG_USER* AddFakeUser(int64_t id, bool bIsChat); TG_USER* GetSender(const TD::MessageSender *pSender); + int64_t GetId(MCONTACT); void SetId(MCONTACT, int64_t id); @@ -289,6 +292,8 @@ public: INT_PTR GetCaps(int type, MCONTACT hContact = NULL) override; + MEVENT RecvFile(MCONTACT hContact, PROTORECVFILE *pre) override; + HANDLE SearchByName(const wchar_t *nick, const wchar_t *firstName, const wchar_t *lastName) override; int SendMsg(MCONTACT hContact, int flags, const char *pszMessage) override; int SetStatus(int iNewStatus) override; diff --git a/protocols/Telegram/src/server.cpp b/protocols/Telegram/src/server.cpp index 920d772902..469db0e971 100644 --- a/protocols/Telegram/src/server.cpp +++ b/protocols/Telegram/src/server.cpp @@ -530,7 +530,7 @@ void CTelegramProto::ProcessMarkRead(TD::updateChatReadInbox *pObj) void CTelegramProto::ProcessMessage(TD::updateNewMessage *pObj) { - auto &pMessage = pObj->message_; + auto *pMessage = pObj->message_.get(); auto *pUser = FindChat(pMessage->chat_id_); if (pUser == nullptr) { @@ -538,7 +538,7 @@ void CTelegramProto::ProcessMessage(TD::updateNewMessage *pObj) return; } - CMStringA szText(GetMessageText(pUser, pMessage->content_.get())); + CMStringA szText(GetMessageText(pUser, pMessage)); if (szText.IsEmpty()) { debugLogA("this message was not processed, ignored"); return; @@ -564,14 +564,8 @@ void CTelegramProto::ProcessMessage(TD::updateNewMessage *pObj) pre.timestamp = pMessage->date_; if (pMessage->is_outgoing_) pre.flags |= PREF_SENT; - if (pUser->isGroupChat) { - if (auto *pSender = GetSender(pMessage->sender_id_.get())) { - _i64toa(pSender->id, szUserId, 10); - pre.szUserId = szUserId; - if (pUser->m_si && !pSender->wszFirstName.IsEmpty()) - g_chatApi.UM_AddUser(pUser->m_si, Utf2T(szUserId), pSender->getDisplayName(), ID_STATUS_ONLINE); - } - } + if (GetGcUserId(pUser, pMessage, szUserId)) + pre.szUserId = szUserId; ProtoChainRecvMsg(pUser->hContact, &pre); } diff --git a/protocols/Telegram/src/utils.cpp b/protocols/Telegram/src/utils.cpp index ef2636563f..28b3204cf7 100644 --- a/protocols/Telegram/src/utils.cpp +++ b/protocols/Telegram/src/utils.cpp @@ -222,8 +222,30 @@ void CTelegramProto::Popup(MCONTACT hContact, const wchar_t *szMsg, const wchar_ ///////////////////////////////////////////////////////////////////////////////////////// -CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, TD::MessageContent *pBody) +bool CTelegramProto::GetGcUserId(TG_USER *pUser, const TD::message *pMsg, char *dest) { + if (pUser->isGroupChat) { + if (auto *pSender = GetSender(pMsg->sender_id_.get())) { + _i64toa(pSender->id, dest, 10); + if (pUser->m_si && !pSender->wszFirstName.IsEmpty()) + g_chatApi.UM_AddUser(pUser->m_si, Utf2T(dest), pSender->getDisplayName(), ID_STATUS_ONLINE); + return true; + } + } + + *dest = 0; + return false; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, const TD::message *pMsg) +{ + const TD::MessageContent *pBody = pMsg->content_.get(); + + char szId[100], szUserId[100]; + _i64toa(pMsg->id_, szId, 10); + switch (pBody->get_id()) { case TD::messageDocument::ID: { @@ -253,6 +275,10 @@ CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, TD::MessageContent *pBo pre.timestamp = time(0); pre.files.a = &pszFileName; pre.lParam = (LPARAM)pRequest; + pre.szId = szId; + if (GetGcUserId(pUser, pMsg, szUserId)) + pre.szUserId = szUserId; + if (!pDoc->caption_->text_.empty()) pre.descr.a = pDoc->caption_->text_.c_str(); ProtoChainRecvFile(pUser->hContact, &pre); -- cgit v1.2.3