From 6e83622d2af1cec3c759f4cff6efe4df2fe3328c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 3 Jun 2023 18:31:18 +0300 Subject: Telegram: fix for a trick with temporary message id that led to duplicated messages --- protocols/Telegram/src/proto.cpp | 5 +++++ protocols/Telegram/src/proto.h | 16 +++++++++++++++- protocols/Telegram/src/server.cpp | 26 +++++++++++++++++++------- 3 files changed, 39 insertions(+), 8 deletions(-) (limited to 'protocols/Telegram') diff --git a/protocols/Telegram/src/proto.cpp b/protocols/Telegram/src/proto.cpp index a7fe012792..2dc3c0a908 100644 --- a/protocols/Telegram/src/proto.cpp +++ b/protocols/Telegram/src/proto.cpp @@ -24,6 +24,10 @@ static int CompareUsers(const TG_USER *p1, const TG_USER *p2) { return CompareId(p1->id, p2->id); } +static int CompareOwnMsg(const TG_OWN_MESSAGE *p1, const TG_OWN_MESSAGE *p2) +{ return CompareId(p1->tmpMsgId, p2->tmpMsgId); +} + static int CompareBasicGroups(const TG_BASIC_GROUP *p1, const TG_BASIC_GROUP *p2) { return CompareId(p1->id, p2->id); } @@ -38,6 +42,7 @@ CTelegramProto::CTelegramProto(const char* protoName, const wchar_t* userName) : m_arFiles(1, PtrKeySortT), m_arChats(100, CompareChats), m_arUsers(100, CompareUsers), + m_arOwnMsg(1, CompareOwnMsg), m_arRequests(10, CompareRequests), m_arBasicGroups(10, CompareBasicGroups), m_arSuperGroups(10, CompareSuperGroups), diff --git a/protocols/Telegram/src/proto.h b/protocols/Telegram/src/proto.h index e03405560a..7181a92bce 100644 --- a/protocols/Telegram/src/proto.h +++ b/protocols/Telegram/src/proto.h @@ -125,6 +125,19 @@ struct TG_BASIC_GROUP TD::object_ptr group; }; +struct TG_OWN_MESSAGE +{ + TG_OWN_MESSAGE(MCONTACT _1, HANDLE _2, int64_t _3) : + hContact(_1), + hAck(_2), + tmpMsgId(_3) + {} + + int64_t tmpMsgId; + HANDLE hAck; + MCONTACT hContact; +}; + class CTelegramProto : public PROTO { friend class CForwardDlg; @@ -179,7 +192,8 @@ class CTelegramProto : public PROTO bool m_bAuthorized, m_bTerminated, m_bUnregister = false, m_bSmileyAdd = false; int32_t m_iClientId, m_iMsgId; int64_t m_iQueryId; - + + OBJLIST m_arOwnMsg; OBJLIST m_arRequests; mir_cs m_csFiles; diff --git a/protocols/Telegram/src/server.cpp b/protocols/Telegram/src/server.cpp index 493b88bb7e..2bd4283763 100644 --- a/protocols/Telegram/src/server.cpp +++ b/protocols/Telegram/src/server.cpp @@ -181,7 +181,18 @@ void CTelegramProto::ProcessResponse(td::ClientManager::Response response) break; case TD::updateMessageSendSucceeded::ID: - ProcessMessage(((TD::updateMessageSendSucceeded *)response.object.get())->message_.get()); + { + auto *pMessage = (TD::updateMessageSendSucceeded *)response.object.get(); + ProcessMessage(pMessage->message_.get()); + + if (auto *pOwnMsg = m_arOwnMsg.find((TG_OWN_MESSAGE *)&pMessage->old_message_id_)) { + char szMsgId[100]; + _i64toa(pMessage->message_->id_, szMsgId, 10); + ProtoBroadcastAck(pOwnMsg->hContact ? pOwnMsg->hContact : m_iSavedMessages, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, pOwnMsg->hAck, (LPARAM)szMsgId); + + m_arOwnMsg.remove(pOwnMsg); + } + } break; case TD::updateNewChat::ID: @@ -189,7 +200,11 @@ void CTelegramProto::ProcessResponse(td::ClientManager::Response response) break; case TD::updateNewMessage::ID: - ProcessMessage(((TD::updateNewMessage *)response.object.get())->message_.get()); + { + auto *pMessage = ((TD::updateNewMessage *)response.object.get())->message_.get(); + if (!m_arOwnMsg.find((TG_OWN_MESSAGE*)&pMessage->id_)) + ProcessMessage(pMessage); + } break; case TD::updateOption::ID: @@ -224,11 +239,8 @@ void CTelegramProto::OnSendMessage(td::ClientManager::Response &response, void * auto *pMessage = ((TD::message *)response.object.get()); auto *pUser = FindChat(pMessage->chat_id_); - if (pUser) { - char szMsgId[100]; - _i64toa(pMessage->id_, szMsgId, 10); - ProtoBroadcastAck(pUser->hContact ? pUser->hContact : m_iSavedMessages, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, pUserInfo, (LPARAM)szMsgId); - } + if (pUser) + m_arOwnMsg.insert(new TG_OWN_MESSAGE(pUser->hContact, pUserInfo, pMessage->id_)); } int CTelegramProto::SendTextMessage(int64_t chatId, const char *pszMessage) -- cgit v1.2.3