From f667c9138be6ebb88972501c553384ab46c19a6d Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 29 Nov 2023 20:23:00 +0300 Subject: Telegram: message ids are unique only inside the same chat, not globally --- protocols/Telegram/src/avatars.cpp | 4 +-- protocols/Telegram/src/proto.cpp | 12 ++++----- protocols/Telegram/src/proto.h | 11 ++++---- protocols/Telegram/src/server.cpp | 55 +++++++++++++++----------------------- protocols/Telegram/src/utils.cpp | 26 ++++++++++++------ protocols/Telegram/src/utils.h | 3 +++ 6 files changed, 55 insertions(+), 56 deletions(-) (limited to 'protocols/Telegram/src') diff --git a/protocols/Telegram/src/avatars.cpp b/protocols/Telegram/src/avatars.cpp index c640478e14..e304fd6e0b 100644 --- a/protocols/Telegram/src/avatars.cpp +++ b/protocols/Telegram/src/avatars.cpp @@ -238,9 +238,7 @@ void CTelegramProto::ProcessFile(TD::updateFile *pObj) for (auto &it : m_arOwnMsg) { if (it->tmpFileId == pFile->id_) { if (!pFile->remote_->id_.empty()) { - char szId[100]; - _i64toa(it->tmpMsgId, szId, 10); - if (auto hDbEvent = db_event_getById(m_szModuleName, szId)) { + if (auto hDbEvent = db_event_getById(m_szModuleName, it->szMsgId)) { DBVARIANT dbv = { DBVT_UTF8 }; dbv.pszVal = (char *)pFile->remote_->id_.c_str(); db_event_setJson(hDbEvent, "u", &dbv); diff --git a/protocols/Telegram/src/proto.cpp b/protocols/Telegram/src/proto.cpp index 0e5accd74c..9222617a7a 100644 --- a/protocols/Telegram/src/proto.cpp +++ b/protocols/Telegram/src/proto.cpp @@ -25,7 +25,7 @@ static int CompareUsers(const TG_USER *p1, const TG_USER *p2) } static int CompareOwnMsg(const TG_OWN_MESSAGE *p1, const TG_OWN_MESSAGE *p2) -{ return CompareId(p1->tmpMsgId, p2->tmpMsgId); +{ return strcmp(p1->szMsgId, p2->szMsgId); } static int CompareBasicGroups(const TG_BASIC_GROUP *p1, const TG_BASIC_GROUP *p2) @@ -423,13 +423,13 @@ void CTelegramProto::ProcessFileMessage(TG_FILE_REQUEST *ft, const TD::message * return; } - auto *pOwnMsg = new TG_OWN_MESSAGE(pUser->hContact, 0, pMsg->id_); + char szUserId[100]; + auto szMsgId(msg2id(pMsg)); + + auto *pOwnMsg = new TG_OWN_MESSAGE(pUser->hContact, 0, szMsgId); pOwnMsg->tmpFileId = pFile->id_; m_arOwnMsg.insert(pOwnMsg); - char szMsgId[40], szUserId[100]; - _i64toa(pMsg->id_, szMsgId, 10); - if (!GetGcUserId(pUser, pMsg, szUserId)) szUserId[0] = 0; @@ -611,7 +611,7 @@ int CTelegramProto::SendMsg(MCONTACT hContact, const char *pszMessage) int msgid = SendTextMessage(pUser->chatId, pszMessage); if (msgid != -1) - m_arOwnMsg.insert(new TG_OWN_MESSAGE(hContact, (HANDLE)msgid, -1)); + m_arOwnMsg.insert(new TG_OWN_MESSAGE(hContact, (HANDLE)msgid, "")); return msgid; } diff --git a/protocols/Telegram/src/proto.h b/protocols/Telegram/src/proto.h index 6731a512aa..8ce67e3463 100644 --- a/protocols/Telegram/src/proto.h +++ b/protocols/Telegram/src/proto.h @@ -133,15 +133,16 @@ struct TG_BASIC_GROUP struct TG_OWN_MESSAGE { - TG_OWN_MESSAGE(MCONTACT _1, HANDLE _2, int64_t _3) : + TG_OWN_MESSAGE(MCONTACT _1, HANDLE _2, const char *_3) : hContact(_1), hAck(_2), - tmpMsgId(_3) + szMsgId(_3) {} - int64_t tmpMsgId, tmpFileId = -1; - HANDLE hAck; - MCONTACT hContact; + int64_t tmpFileId = -1; + HANDLE hAck; + MCONTACT hContact; + CMStringA szMsgId; }; class CTelegramProto : public PROTO diff --git a/protocols/Telegram/src/server.cpp b/protocols/Telegram/src/server.cpp index 3a81fb4aa3..19d5a3100b 100644 --- a/protocols/Telegram/src/server.cpp +++ b/protocols/Telegram/src/server.cpp @@ -222,23 +222,17 @@ void CTelegramProto::ProcessResponse(td::ClientManager::Response response) { auto *pMessage = (TD::updateMessageSendSucceeded *)response.object.get(); - if (pMessage->old_message_id_) { - char szId[100]; - _i64toa(pMessage->old_message_id_, szId, 10); - if (auto hDbEvent = db_event_getById(m_szModuleName, szId)) { - _i64toa(pMessage->message_->id_, szId, 10); - db_event_updateId(hDbEvent, szId); - } - } + auto szOldId = msg2id(pMessage->message_->chat_id_, pMessage->old_message_id_); + if (pMessage->old_message_id_) + if (auto hDbEvent = db_event_getById(m_szModuleName, szOldId)) + db_event_updateId(hDbEvent, msg2id(pMessage->message_.get())); ProcessMessage(pMessage->message_.get()); - if (auto *pOwnMsg = m_arOwnMsg.find((TG_OWN_MESSAGE *)&pMessage->old_message_id_)) { - if (pOwnMsg->hAck) { - char szMsgId[100]; - _i64toa(pMessage->message_->id_, szMsgId, 10); - ProtoBroadcastAck(pOwnMsg->hContact ? pOwnMsg->hContact : m_iSavedMessages, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, pOwnMsg->hAck, (LPARAM)szMsgId); - } + TG_OWN_MESSAGE tmp(0, 0, szOldId); + if (auto *pOwnMsg = m_arOwnMsg.find(&tmp)) { + if (pOwnMsg->hAck) + ProtoBroadcastAck(pOwnMsg->hContact ? pOwnMsg->hContact : m_iSavedMessages, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, pOwnMsg->hAck, (LPARAM)pOwnMsg->szMsgId.c_str()); m_arOwnMsg.remove(pOwnMsg); } @@ -252,7 +246,8 @@ void CTelegramProto::ProcessResponse(td::ClientManager::Response response) case TD::updateNewMessage::ID: { auto *pMessage = ((TD::updateNewMessage *)response.object.get())->message_.get(); - if (!m_arOwnMsg.find((TG_OWN_MESSAGE*)&pMessage->id_)) + TG_OWN_MESSAGE tmp(0, 0, msg2id(pMessage)); + if (!m_arOwnMsg.find(&tmp)) ProcessMessage(pMessage); } break; @@ -297,7 +292,7 @@ void CTelegramProto::OnSendMessage(td::ClientManager::Response &response) for (auto &it : m_arOwnMsg) if (it->hAck == (HANDLE)response.request_id) { auto *pMessage = ((TD::message *)response.object.get()); - it->tmpMsgId = pMessage->id_; + it->szMsgId = msg2id(pMessage); break; } break; @@ -373,8 +368,8 @@ void CTelegramProto::OnGetHistory(td::ClientManager::Response &response, void *p if (pMsg->id_ < lastMsgId) lastMsgId = pMsg->id_; - char szMsgId[100], szUserId[100]; - _i64toa(pMsg->id_, szMsgId, 10); + char szUserId[100]; + auto szMsgId(msg2id(pMsg)); if (db_event_getById(m_szModuleName, szMsgId)) continue; @@ -666,12 +661,9 @@ void CTelegramProto::ProcessDeleteMessage(TD::updateDeleteMessages *pObj) return; } - for (auto &it : pObj->message_ids_) { - char id[100]; - _i64toa(it, id, 10); - if (MEVENT hEvent = db_event_getById(m_szModuleName, id)) + for (auto &it : pObj->message_ids_) + if (MEVENT hEvent = db_event_getById(m_szModuleName, msg2id(pObj->chat_id_, it))) db_event_delete(hEvent, true); - } } void CTelegramProto::ProcessGroups(TD::updateChatFolders *pObj) @@ -707,9 +699,7 @@ void CTelegramProto::ProcessMarkRead(TD::updateChatReadInbox *pObj) return; } - char szId[100]; - _i64toa(pObj->last_read_inbox_message_id_, szId, 10); - MEVENT hLastRead = db_event_getById(m_szModuleName, szId); + MEVENT hLastRead = db_event_getById(m_szModuleName, msg2id(pObj->chat_id_, pObj->last_read_inbox_message_id_)); if (hLastRead == 0) { debugLogA("unknown event, ignored"); return; @@ -743,9 +733,9 @@ void CTelegramProto::ProcessMessage(const TD::message *pMessage) if (pMessage->sending_state_->get_id() == TD::messageSendingStatePending::ID) return; - char szId[100], szUserId[100], szReplyId[100]; - _i64toa(pMessage->id_, szId, 10); - if (db_event_getById(m_szModuleName, szId)) + char szUserId[100], szReplyId[100]; + auto szMsgId(msg2id(pMessage)); + if (db_event_getById(m_szModuleName, szMsgId)) return; CMStringA szText(GetMessageText(pUser, pMessage)); @@ -767,7 +757,7 @@ void CTelegramProto::ProcessMessage(const TD::message *pMessage) PROTORECVEVENT pre = {}; pre.szMessage = szText.GetBuffer(); - pre.szMsgId = szId; + pre.szMsgId = szMsgId; pre.timestamp = pMessage->date_; if (pMessage->is_outgoing_) pre.flags |= PREF_SENT; @@ -788,10 +778,7 @@ void CTelegramProto::ProcessMessageContent(TD::updateMessageContent *pObj) return; } - char szMsgId[100]; - _i64toa(pObj->message_id_, szMsgId, 10); - - MEVENT hDbEvent = db_event_getById(m_szModuleName, szMsgId); + MEVENT hDbEvent = db_event_getById(m_szModuleName, msg2id(pObj->chat_id_, pObj->message_id_)); if (hDbEvent == 0) { debugLogA("Unknown message with id=%lld (chat id %lld, ignored", pObj->message_id_, pObj->chat_id_); return; diff --git a/protocols/Telegram/src/utils.cpp b/protocols/Telegram/src/utils.cpp index 414a9bdc7a..28d8a01136 100644 --- a/protocols/Telegram/src/utils.cpp +++ b/protocols/Telegram/src/utils.cpp @@ -102,6 +102,16 @@ static CMStringA getFormattedText(TD::object_ptr &pText) ///////////////////////////////////////////////////////////////////////////////////////// +CMStringA msg2id(TD::int53 chatId, TD::int53 msgId) +{ + return CMStringA(FORMAT, "%lld_%lld", chatId, msgId); +} + +CMStringA msg2id(const TD::message *pMsg) +{ + return CMStringA(FORMAT, "%lld_%lld", pMsg->chat_id_, pMsg->id_); +} + const char *getName(const TD::usernames *pName) { return (pName == nullptr) ? TranslateU("none") : pName->editable_username_.c_str(); @@ -445,8 +455,8 @@ CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, const TD::message *pMsg { const TD::MessageContent *pBody = pMsg->content_.get(); - char szId[100], szUserId[100], *pszUserId = nullptr; - _i64toa(pMsg->id_, szId, 10); + char szUserId[100], *pszUserId = nullptr; + auto szMsgId(msg2id(pMsg)); if (GetGcUserId(pUser, pMsg, szUserId)) pszUserId = szUserId; @@ -473,7 +483,7 @@ CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, const TD::message *pMsg } CMStringA fileName(FORMAT, "%s (%d x %d)", TranslateU("Picture"), pPhoto->width_, pPhoto->height_); - GetMessageFile(TG_FILE_REQUEST::PICTURE, pUser, pPhoto->photo_.get(), fileName, pDoc->caption_->text_, szId, pszUserId, pMsg); + GetMessageFile(TG_FILE_REQUEST::PICTURE, pUser, pPhoto->photo_.get(), fileName, pDoc->caption_->text_, szMsgId, pszUserId, pMsg); } break; @@ -487,7 +497,7 @@ CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, const TD::message *pMsg caption += " "; caption += pDoc->caption_->text_; } - GetMessageFile(TG_FILE_REQUEST::VIDEO, pUser, pAudio->audio_.get(), pAudio->file_name_.c_str(), caption, szId, pszUserId, pMsg); + GetMessageFile(TG_FILE_REQUEST::VIDEO, pUser, pAudio->audio_.get(), pAudio->file_name_.c_str(), caption, szMsgId, pszUserId, pMsg); } break; @@ -501,7 +511,7 @@ CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, const TD::message *pMsg caption += " "; caption += pDoc->caption_->text_; } - GetMessageFile(TG_FILE_REQUEST::VIDEO, pUser, pVideo->video_.get(), pVideo->file_name_.c_str(), caption, szId, pszUserId, pMsg); + GetMessageFile(TG_FILE_REQUEST::VIDEO, pUser, pVideo->video_.get(), pVideo->file_name_.c_str(), caption, szMsgId, pszUserId, pMsg); } break; @@ -515,7 +525,7 @@ CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, const TD::message *pMsg caption += " "; caption += pDoc->caption_->text_; } - GetMessageFile(TG_FILE_REQUEST::VIDEO, pUser, pVideo->animation_.get(), pVideo->file_name_.c_str(), caption, szId, pszUserId, pMsg); + GetMessageFile(TG_FILE_REQUEST::VIDEO, pUser, pVideo->animation_.get(), pVideo->file_name_.c_str(), caption, szMsgId, pszUserId, pMsg); } break; @@ -523,14 +533,14 @@ CMStringA CTelegramProto::GetMessageText(TG_USER *pUser, const TD::message *pMsg { auto *pDoc = (TD::messageVoiceNote *)pBody; CMStringA fileName(FORMAT, "%s (%d %s)", TranslateU("Voice message"), pDoc->voice_note_->duration_, TranslateU("seconds")); - GetMessageFile(TG_FILE_REQUEST::VOICE, pUser, pDoc->voice_note_->voice_.get(), fileName, pDoc->caption_->text_, szId, pszUserId, pMsg); + GetMessageFile(TG_FILE_REQUEST::VOICE, pUser, pDoc->voice_note_->voice_.get(), fileName, pDoc->caption_->text_, szMsgId, pszUserId, pMsg); } break; case TD::messageDocument::ID: { auto *pDoc = (TD::messageDocument *)pBody; - GetMessageFile(TG_FILE_REQUEST::FILE, pUser, pDoc->document_->document_.get(), pDoc->document_->file_name_.c_str(), pDoc->caption_->text_, szId, pszUserId, pMsg); + GetMessageFile(TG_FILE_REQUEST::FILE, pUser, pDoc->document_->document_.get(), pDoc->document_->file_name_.c_str(), pDoc->caption_->text_, szMsgId, pszUserId, pMsg); } break; diff --git a/protocols/Telegram/src/utils.h b/protocols/Telegram/src/utils.h index d924a4a137..1af5f4ee4c 100644 --- a/protocols/Telegram/src/utils.h +++ b/protocols/Telegram/src/utils.h @@ -7,3 +7,6 @@ TD::object_ptr makeFile(const wchar_t *pwszFilename); TD::object_ptr formatBbcodes(const char *pszText); TG_FILE_REQUEST::Type AutoDetectType(const wchar_t *pwszFilename); + +CMStringA msg2id(const TD::message *pMsg); +CMStringA msg2id(TD::int53 chatId, TD::int53 msgId); -- cgit v1.2.3