From 51c1dc20e1923aadd24f9e2e38609a1854ddcd17 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 9 Sep 2019 11:13:41 +0300 Subject: [PATCH] Unregister randomId if send message fails. --- Telegram/SourceFiles/api/api_sending.cpp | 4 ++-- Telegram/SourceFiles/apiwrap.cpp | 23 +++++++++++-------- Telegram/SourceFiles/apiwrap.h | 1 + .../SourceFiles/storage/localimageloader.cpp | 1 + .../SourceFiles/storage/localimageloader.h | 1 + 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/api/api_sending.cpp b/Telegram/SourceFiles/api/api_sending.cpp index 0d7b4575c..47940a2e9 100644 --- a/Telegram/SourceFiles/api/api_sending.cpp +++ b/Telegram/SourceFiles/api/api_sending.cpp @@ -135,11 +135,11 @@ void SendExistingMedia( if (media->fileReference() != usedFileReference) { performRequest(); } else { - api->sendMessageFail(error, peer, newId); + api->sendMessageFail(error, peer, randomId, newId); } }); } else { - api->sendMessageFail(error, peer, newId); + api->sendMessageFail(error, peer, randomId, newId); } }; performRequest(); diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index e9e751d65..6f772e5d2 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -541,6 +541,7 @@ void ApiWrap::toggleHistoryArchived( void ApiWrap::sendMessageFail( const RPCError &error, not_null peer, + uint64 randomId, FullMsgId itemId) { if (error.type() == qstr("PEER_FLOOD")) { Ui::show(Box( @@ -567,6 +568,8 @@ void ApiWrap::sendMessageFail( } } if (const auto item = _session->data().message(itemId)) { + Assert(randomId != 0); + session().data().unregisterMessageRandomId(randomId); item->sendFailed(); } } @@ -4449,7 +4452,7 @@ void ApiWrap::forwardMessages( auto currentGroupId = items.front()->groupId(); auto ids = QVector(); auto randomIds = QVector(); - auto localIds = std::unique_ptr>(); + auto localIds = std::unique_ptr>(); const auto sendAccumulated = [&] { if (shared) { @@ -4474,8 +4477,8 @@ void ApiWrap::forwardMessages( } }).fail([=, ids = std::move(localIds)](const RPCError &error) { if (ids) { - for (const auto &itemId : *ids) { - sendMessageFail(error, peer, itemId); + for (const auto &[randomId, itemId] : *ids) { + sendMessageFail(error, peer, randomId, itemId); } } else { sendMessageFail(error, peer); @@ -4492,7 +4495,7 @@ void ApiWrap::forwardMessages( ids.reserve(count); randomIds.reserve(count); for (const auto item : items) { - auto randomId = rand_value(); + const auto randomId = rand_value(); if (genClientSideMessage) { if (const auto message = item->toHistoryMessage()) { const auto newId = FullMsgId( @@ -4515,9 +4518,9 @@ void ApiWrap::forwardMessages( message); _session->data().registerMessageRandomId(randomId, newId); if (!localIds) { - localIds = std::make_unique>(); + localIds = std::make_unique>(); } - localIds->push_back(newId); + localIds->emplace(randomId, newId); } } const auto newFrom = item->history()->peer; @@ -5034,7 +5037,7 @@ void ApiWrap::sendMessage(MessageToSend &&message) { if (error.type() == qstr("MESSAGE_EMPTY")) { lastMessage->destroy(); } else { - sendMessageFail(error, peer, newId); + sendMessageFail(error, peer, randomId, newId); } history->clearSentDraftText(QString()); }).afterRequest(history->sendRequestId @@ -5156,7 +5159,7 @@ void ApiWrap::sendInlineResult( applyUpdates(result, randomId); history->clearSentDraftText(QString()); }).fail([=](const RPCError &error) { - sendMessageFail(error, peer, newId); + sendMessageFail(error, peer, randomId, newId); history->clearSentDraftText(QString()); }).afterRequest(history->sendRequestId ).send(); @@ -5294,7 +5297,7 @@ void ApiWrap::sendMediaWithRandomId( )).done([=](const MTPUpdates &result) { applyUpdates(result); }).fail([=](const RPCError &error) { - sendMessageFail(error, peer, itemId); + sendMessageFail(error, peer, randomId, itemId); }).afterRequest( history->sendRequestId ).send(); @@ -5388,7 +5391,7 @@ void ApiWrap::sendAlbumIfReady(not_null album) { }).fail([=](const RPCError &error) { if (const auto album = _sendingAlbums.take(groupId)) { for (const auto &item : (*album)->items) { - sendMessageFail(error, peer, item.msgId); + sendMessageFail(error, peer, item.randomId, item.msgId); } } else { sendMessageFail(error, peer); diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index 8d8c00487..0656d974b 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -441,6 +441,7 @@ public: void sendMessageFail( const RPCError &error, not_null peer, + uint64 randomId = 0, FullMsgId itemId = FullMsgId()); void uploadPeerPhoto(not_null peer, QImage &&image); diff --git a/Telegram/SourceFiles/storage/localimageloader.cpp b/Telegram/SourceFiles/storage/localimageloader.cpp index ea06108b2..0a94bdbd9 100644 --- a/Telegram/SourceFiles/storage/localimageloader.cpp +++ b/Telegram/SourceFiles/storage/localimageloader.cpp @@ -403,6 +403,7 @@ void SendingAlbum::fillMedia( const auto i = FindAlbumItem(items, item); Assert(!i->media); + i->randomId = randomId; i->media = PrepareAlbumItemMedia(item, media, randomId); } diff --git a/Telegram/SourceFiles/storage/localimageloader.h b/Telegram/SourceFiles/storage/localimageloader.h index 05ae9d4e8..1f4ada86b 100644 --- a/Telegram/SourceFiles/storage/localimageloader.h +++ b/Telegram/SourceFiles/storage/localimageloader.h @@ -160,6 +160,7 @@ struct SendingAlbum { } TaskId taskId; + uint64 randomId = 0; FullMsgId msgId; std::optional media; };