Unregister randomId if send message fails.

This commit is contained in:
John Preston 2019-09-09 11:13:41 +03:00
parent 3a3bf84cfc
commit 51c1dc20e1
5 changed files with 18 additions and 12 deletions

View File

@ -135,11 +135,11 @@ void SendExistingMedia(
if (media->fileReference() != usedFileReference) { if (media->fileReference() != usedFileReference) {
performRequest(); performRequest();
} else { } else {
api->sendMessageFail(error, peer, newId); api->sendMessageFail(error, peer, randomId, newId);
} }
}); });
} else { } else {
api->sendMessageFail(error, peer, newId); api->sendMessageFail(error, peer, randomId, newId);
} }
}; };
performRequest(); performRequest();

View File

@ -541,6 +541,7 @@ void ApiWrap::toggleHistoryArchived(
void ApiWrap::sendMessageFail( void ApiWrap::sendMessageFail(
const RPCError &error, const RPCError &error,
not_null<PeerData*> peer, not_null<PeerData*> peer,
uint64 randomId,
FullMsgId itemId) { FullMsgId itemId) {
if (error.type() == qstr("PEER_FLOOD")) { if (error.type() == qstr("PEER_FLOOD")) {
Ui::show(Box<InformBox>( Ui::show(Box<InformBox>(
@ -567,6 +568,8 @@ void ApiWrap::sendMessageFail(
} }
} }
if (const auto item = _session->data().message(itemId)) { if (const auto item = _session->data().message(itemId)) {
Assert(randomId != 0);
session().data().unregisterMessageRandomId(randomId);
item->sendFailed(); item->sendFailed();
} }
} }
@ -4449,7 +4452,7 @@ void ApiWrap::forwardMessages(
auto currentGroupId = items.front()->groupId(); auto currentGroupId = items.front()->groupId();
auto ids = QVector<MTPint>(); auto ids = QVector<MTPint>();
auto randomIds = QVector<MTPlong>(); auto randomIds = QVector<MTPlong>();
auto localIds = std::unique_ptr<std::vector<FullMsgId>>(); auto localIds = std::unique_ptr<base::flat_map<uint64, FullMsgId>>();
const auto sendAccumulated = [&] { const auto sendAccumulated = [&] {
if (shared) { if (shared) {
@ -4474,8 +4477,8 @@ void ApiWrap::forwardMessages(
} }
}).fail([=, ids = std::move(localIds)](const RPCError &error) { }).fail([=, ids = std::move(localIds)](const RPCError &error) {
if (ids) { if (ids) {
for (const auto &itemId : *ids) { for (const auto &[randomId, itemId] : *ids) {
sendMessageFail(error, peer, itemId); sendMessageFail(error, peer, randomId, itemId);
} }
} else { } else {
sendMessageFail(error, peer); sendMessageFail(error, peer);
@ -4492,7 +4495,7 @@ void ApiWrap::forwardMessages(
ids.reserve(count); ids.reserve(count);
randomIds.reserve(count); randomIds.reserve(count);
for (const auto item : items) { for (const auto item : items) {
auto randomId = rand_value<uint64>(); const auto randomId = rand_value<uint64>();
if (genClientSideMessage) { if (genClientSideMessage) {
if (const auto message = item->toHistoryMessage()) { if (const auto message = item->toHistoryMessage()) {
const auto newId = FullMsgId( const auto newId = FullMsgId(
@ -4515,9 +4518,9 @@ void ApiWrap::forwardMessages(
message); message);
_session->data().registerMessageRandomId(randomId, newId); _session->data().registerMessageRandomId(randomId, newId);
if (!localIds) { if (!localIds) {
localIds = std::make_unique<std::vector<FullMsgId>>(); localIds = std::make_unique<base::flat_map<uint64, FullMsgId>>();
} }
localIds->push_back(newId); localIds->emplace(randomId, newId);
} }
} }
const auto newFrom = item->history()->peer; const auto newFrom = item->history()->peer;
@ -5034,7 +5037,7 @@ void ApiWrap::sendMessage(MessageToSend &&message) {
if (error.type() == qstr("MESSAGE_EMPTY")) { if (error.type() == qstr("MESSAGE_EMPTY")) {
lastMessage->destroy(); lastMessage->destroy();
} else { } else {
sendMessageFail(error, peer, newId); sendMessageFail(error, peer, randomId, newId);
} }
history->clearSentDraftText(QString()); history->clearSentDraftText(QString());
}).afterRequest(history->sendRequestId }).afterRequest(history->sendRequestId
@ -5156,7 +5159,7 @@ void ApiWrap::sendInlineResult(
applyUpdates(result, randomId); applyUpdates(result, randomId);
history->clearSentDraftText(QString()); history->clearSentDraftText(QString());
}).fail([=](const RPCError &error) { }).fail([=](const RPCError &error) {
sendMessageFail(error, peer, newId); sendMessageFail(error, peer, randomId, newId);
history->clearSentDraftText(QString()); history->clearSentDraftText(QString());
}).afterRequest(history->sendRequestId }).afterRequest(history->sendRequestId
).send(); ).send();
@ -5294,7 +5297,7 @@ void ApiWrap::sendMediaWithRandomId(
)).done([=](const MTPUpdates &result) { )).done([=](const MTPUpdates &result) {
applyUpdates(result); applyUpdates(result);
}).fail([=](const RPCError &error) { }).fail([=](const RPCError &error) {
sendMessageFail(error, peer, itemId); sendMessageFail(error, peer, randomId, itemId);
}).afterRequest( }).afterRequest(
history->sendRequestId history->sendRequestId
).send(); ).send();
@ -5388,7 +5391,7 @@ void ApiWrap::sendAlbumIfReady(not_null<SendingAlbum*> album) {
}).fail([=](const RPCError &error) { }).fail([=](const RPCError &error) {
if (const auto album = _sendingAlbums.take(groupId)) { if (const auto album = _sendingAlbums.take(groupId)) {
for (const auto &item : (*album)->items) { for (const auto &item : (*album)->items) {
sendMessageFail(error, peer, item.msgId); sendMessageFail(error, peer, item.randomId, item.msgId);
} }
} else { } else {
sendMessageFail(error, peer); sendMessageFail(error, peer);

View File

@ -441,6 +441,7 @@ public:
void sendMessageFail( void sendMessageFail(
const RPCError &error, const RPCError &error,
not_null<PeerData*> peer, not_null<PeerData*> peer,
uint64 randomId = 0,
FullMsgId itemId = FullMsgId()); FullMsgId itemId = FullMsgId());
void uploadPeerPhoto(not_null<PeerData*> peer, QImage &&image); void uploadPeerPhoto(not_null<PeerData*> peer, QImage &&image);

View File

@ -403,6 +403,7 @@ void SendingAlbum::fillMedia(
const auto i = FindAlbumItem(items, item); const auto i = FindAlbumItem(items, item);
Assert(!i->media); Assert(!i->media);
i->randomId = randomId;
i->media = PrepareAlbumItemMedia(item, media, randomId); i->media = PrepareAlbumItemMedia(item, media, randomId);
} }

View File

@ -160,6 +160,7 @@ struct SendingAlbum {
} }
TaskId taskId; TaskId taskId;
uint64 randomId = 0;
FullMsgId msgId; FullMsgId msgId;
std::optional<MTPInputSingleMedia> media; std::optional<MTPInputSingleMedia> media;
}; };