diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index c462dfeb4..a0feaa15b 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -1012,34 +1012,6 @@ rpl::producer ApiWrap::dialogsLoadBlockedByDate() const { return _dialogsLoadBlockedByDate.value(); } -void ApiWrap::requestFakeChatListMessage( - not_null history) { - if (_fakeChatListRequests.contains(history)) { - return; - } - - _fakeChatListRequests.emplace(history); - request(MTPmessages_GetHistory( - history->peer->input, - MTP_int(0), // offset_id - MTP_int(0), // offset_date - MTP_int(0), // add_offset - MTP_int(2), // limit - MTP_int(0), // max_id - MTP_int(0), // min_id - MTP_int(0) - )).done([=](const MTPmessages_Messages &result) { - _fakeChatListRequests.erase(history); - history->setFakeChatListMessageFrom(result); - }).fail([=](const RPCError &error) { - _fakeChatListRequests.erase(history); - history->setFakeChatListMessageFrom(MTP_messages_messages( - MTP_vector(0), - MTP_vector(0), - MTP_vector(0))); - }).send(); -} - void ApiWrap::requestWallPaper( const QString &slug, Fn done, @@ -3908,12 +3880,12 @@ void ApiWrap::requestSharedMedia( SharedMediaType type, MsgId messageId, SliceType slice) { - auto key = std::make_tuple(peer, type, messageId, slice); + const auto key = std::make_tuple(peer, type, messageId, slice); if (_sharedMediaRequests.contains(key)) { return; } - auto prepared = Api::PrepareSearchRequest( + const auto prepared = Api::PrepareSearchRequest( peer, type, QString(), @@ -3923,17 +3895,23 @@ void ApiWrap::requestSharedMedia( return; } - auto requestId = request( - std::move(*prepared) - ).done([this, peer, type, messageId, slice]( - const MTPmessages_Messages &result) { - auto key = std::make_tuple(peer, type, messageId, slice); - _sharedMediaRequests.remove(key); - sharedMediaDone(peer, type, messageId, slice, result); - }).fail([this, key](const RPCError &error) { - _sharedMediaRequests.remove(key); - }).send(); - _sharedMediaRequests.emplace(key, requestId); + const auto history = session().data().history(peer); + auto &histories = history->owner().histories(); + const auto requestType = Data::Histories::RequestType::History; + histories.sendRequest(history, requestType, [=](Fn finish) { + return request( + std::move(*prepared) + ).done([=](const MTPmessages_Messages &result) { + const auto key = std::make_tuple(peer, type, messageId, slice); + _sharedMediaRequests.remove(key); + sharedMediaDone(peer, type, messageId, slice, result); + finish(); + }).fail([=](const RPCError &error) { + _sharedMediaRequests.remove(key); + finish(); + }).send(); + }); + _sharedMediaRequests.emplace(key); } void ApiWrap::sharedMediaDone( diff --git a/Telegram/SourceFiles/apiwrap.h b/Telegram/SourceFiles/apiwrap.h index 8f46aa7d7..c48ef2b01 100644 --- a/Telegram/SourceFiles/apiwrap.h +++ b/Telegram/SourceFiles/apiwrap.h @@ -177,7 +177,6 @@ public: //void setFeedChannels( // not_null feed, // const std::vector> &channels); - void requestFakeChatListMessage(not_null history); void requestWallPaper( const QString &slug, @@ -739,15 +738,14 @@ private: mtpRequestId _contactsRequestId = 0; mtpRequestId _contactsStatusesRequestId = 0; - base::flat_set> _fakeChatListRequests; base::flat_map, mtpRequestId> _unreadMentionsRequests; - base::flat_map, SharedMediaType, MsgId, - SliceType>, mtpRequestId> _sharedMediaRequests; + SliceType>> _sharedMediaRequests; base::flat_map, mtpRequestId> _userPhotosRequests; diff --git a/Telegram/SourceFiles/calls/calls_box_controller.h b/Telegram/SourceFiles/calls/calls_box_controller.h index d67e22c04..ae5894df4 100644 --- a/Telegram/SourceFiles/calls/calls_box_controller.h +++ b/Telegram/SourceFiles/calls/calls_box_controller.h @@ -44,7 +44,7 @@ private: MTP::Sender _api; MsgId _offsetId = 0; - mtpRequestId _loadRequestId = 0; + int _loadRequestId = 0; // Not a real mtpRequestId. bool _allLoaded = false; }; diff --git a/Telegram/SourceFiles/data/data_histories.cpp b/Telegram/SourceFiles/data/data_histories.cpp index 7d87d1a8e..3a5df6411 100644 --- a/Telegram/SourceFiles/data/data_histories.cpp +++ b/Telegram/SourceFiles/data/data_histories.cpp @@ -333,6 +333,38 @@ void Histories::changeDialogUnreadMark( )).send(); } +void Histories::requestFakeChatListMessage( + not_null history) { + if (_fakeChatListRequests.contains(history)) { + return; + } + + _fakeChatListRequests.emplace(history); + sendRequest(history, RequestType::History, [=](Fn finish) { + return session().api().request(MTPmessages_GetHistory( + history->peer->input, + MTP_int(0), // offset_id + MTP_int(0), // offset_date + MTP_int(0), // add_offset + MTP_int(2), // limit + MTP_int(0), // max_id + MTP_int(0), // min_id + MTP_int(0) + )).done([=](const MTPmessages_Messages &result) { + _fakeChatListRequests.erase(history); + history->setFakeChatListMessageFrom(result); + finish(); + }).fail([=](const RPCError &error) { + _fakeChatListRequests.erase(history); + history->setFakeChatListMessageFrom(MTP_messages_messages( + MTP_vector(0), + MTP_vector(0), + MTP_vector(0))); + finish(); + }).send(); + }); +} + void Histories::sendPendingReadInbox(not_null history) { if (const auto state = lookup(history)) { if (state->readTill diff --git a/Telegram/SourceFiles/data/data_histories.h b/Telegram/SourceFiles/data/data_histories.h index 0cbae7539..bcc068229 100644 --- a/Telegram/SourceFiles/data/data_histories.h +++ b/Telegram/SourceFiles/data/data_histories.h @@ -56,6 +56,7 @@ public: void dialogEntryApplied(not_null history); void changeDialogUnreadMark(not_null history, bool unread); //void changeDialogUnreadMark(not_null feed, bool unread); // #feed + void requestFakeChatListMessage(not_null history); void deleteMessages( not_null history, @@ -121,6 +122,8 @@ private: not_null, std::vector>> _dialogRequestsPending; + base::flat_set> _fakeChatListRequests; + }; } // namespace Data diff --git a/Telegram/SourceFiles/data/data_search_controller.cpp b/Telegram/SourceFiles/data/data_search_controller.cpp index 243654287..6ae67371f 100644 --- a/Telegram/SourceFiles/data/data_search_controller.cpp +++ b/Telegram/SourceFiles/data/data_search_controller.cpp @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_session.h" #include "data/data_messages.h" #include "data/data_channel.h" +#include "data/data_histories.h" #include "history/history.h" #include "history/history_item.h" #include "apiwrap.h" @@ -193,7 +194,8 @@ SearchController::CacheEntry::CacheEntry(const Query &query) } SearchController::SearchController(not_null session) -: _api(session->api().instance()) { +: _session(session) +, _api(session->api().instance()) { } bool SearchController::hasInCache(const Query &query) const { @@ -366,23 +368,32 @@ void SearchController::requestMore( if (!prepared) { return; } - auto requestId = _api.request( - std::move(*prepared) - ).done([=](const MTPmessages_Messages &result) { - listData->requests.remove(key); - auto parsed = ParseSearchResult( - listData->peer, - query.type, - key.aroundId, - key.direction, - result); - listData->list.addSlice( - std::move(parsed.messageIds), - parsed.noSkipRange, - parsed.fullCount); - }).send(); + auto &histories = _session->data().histories(); + const auto type = Histories::RequestType::History; + const auto history = _session->data().history(listData->peer); + auto requestId = histories.sendRequest(history, type, [=](Fn finish) { + return _api.request( + std::move(*prepared) + ).done([=](const MTPmessages_Messages &result) { + listData->requests.remove(key); + auto parsed = ParseSearchResult( + listData->peer, + query.type, + key.aroundId, + key.direction, + result); + listData->list.addSlice( + std::move(parsed.messageIds), + parsed.noSkipRange, + parsed.fullCount); + finish(); + }).fail([=](const RPCError &error) { + finish(); + }).send(); + }); listData->requests.emplace(key, [=] { - _api.request(requestId).cancel(); + auto &histories = _session->data().histories(); + histories.cancelRequest(history, requestId); }); } diff --git a/Telegram/SourceFiles/data/data_search_controller.h b/Telegram/SourceFiles/data/data_search_controller.h index fcc2b8853..a439bb38e 100644 --- a/Telegram/SourceFiles/data/data_search_controller.h +++ b/Telegram/SourceFiles/data/data_search_controller.h @@ -129,6 +129,7 @@ private: const Query &query, Data *listData); + const not_null _session; MTP::Sender _api; Cache _cache; Cache::iterator _current = _cache.end(); diff --git a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp index 8fbf57622..ab1c85d3e 100644 --- a/Telegram/SourceFiles/dialogs/dialogs_widget.cpp +++ b/Telegram/SourceFiles/dialogs/dialogs_widget.cpp @@ -1015,94 +1015,95 @@ void Widget::searchReceived( ? *_singleMessageSearch.lookup(_searchQuery) : nullptr; - if (_searchRequest == requestId) { - switch (result.type()) { - case mtpc_messages_messages: { - auto &d = result.c_messages_messages(); - if (_searchRequest != 0) { - // Don't apply cached data! - session().data().processUsers(d.vusers()); - session().data().processChats(d.vchats()); - } - auto &msgs = d.vmessages().v; - _inner->searchReceived(msgs, inject, type, msgs.size()); + if (_searchRequest != requestId) { + return; + } + switch (result.type()) { + case mtpc_messages_messages: { + auto &d = result.c_messages_messages(); + if (_searchRequest != 0) { + // Don't apply cached data! + session().data().processUsers(d.vusers()); + session().data().processChats(d.vchats()); + } + auto &msgs = d.vmessages().v; + _inner->searchReceived(msgs, inject, type, msgs.size()); + if (type == SearchRequestType::MigratedFromStart || type == SearchRequestType::MigratedFromOffset) { + _searchFullMigrated = true; + } else { + _searchFull = true; + } + } break; + + case mtpc_messages_messagesSlice: { + auto &d = result.c_messages_messagesSlice(); + if (_searchRequest != 0) { + // Don't apply cached data! + session().data().processUsers(d.vusers()); + session().data().processChats(d.vchats()); + } + auto &msgs = d.vmessages().v; + const auto someAdded = _inner->searchReceived(msgs, inject, type, d.vcount().v); + const auto nextRate = d.vnext_rate(); + const auto rateUpdated = nextRate && (nextRate->v != _searchNextRate); + const auto finished = (type == SearchRequestType::FromStart || type == SearchRequestType::FromOffset) + ? !rateUpdated + : !someAdded; + if (rateUpdated) { + _searchNextRate = nextRate->v; + } + if (finished) { if (type == SearchRequestType::MigratedFromStart || type == SearchRequestType::MigratedFromOffset) { _searchFullMigrated = true; } else { _searchFull = true; } - } break; + } + } break; - case mtpc_messages_messagesSlice: { - auto &d = result.c_messages_messagesSlice(); - if (_searchRequest != 0) { - // Don't apply cached data! - session().data().processUsers(d.vusers()); - session().data().processChats(d.vchats()); - } - auto &msgs = d.vmessages().v; - const auto someAdded = _inner->searchReceived(msgs, inject, type, d.vcount().v); - const auto nextRate = d.vnext_rate(); - const auto rateUpdated = nextRate && (nextRate->v != _searchNextRate); - const auto finished = (type == SearchRequestType::FromStart || type == SearchRequestType::FromOffset) - ? !rateUpdated - : !someAdded; - if (rateUpdated) { - _searchNextRate = nextRate->v; - } - if (finished) { - if (type == SearchRequestType::MigratedFromStart || type == SearchRequestType::MigratedFromOffset) { - _searchFullMigrated = true; - } else { - _searchFull = true; - } - } - } break; - - case mtpc_messages_channelMessages: { - auto &d = result.c_messages_channelMessages(); - if (const auto peer = _searchInChat.peer()) { - if (const auto channel = peer->asChannel()) { - channel->ptsReceived(d.vpts().v); - } else { - LOG(("API Error: " - "received messages.channelMessages when no channel " - "was passed! (Widget::searchReceived)")); - } + case mtpc_messages_channelMessages: { + auto &d = result.c_messages_channelMessages(); + if (const auto peer = _searchInChat.peer()) { + if (const auto channel = peer->asChannel()) { + channel->ptsReceived(d.vpts().v); } else { LOG(("API Error: " "received messages.channelMessages when no channel " "was passed! (Widget::searchReceived)")); } - if (_searchRequest != 0) { - // Don't apply cached data! - session().data().processUsers(d.vusers()); - session().data().processChats(d.vchats()); - } - auto &msgs = d.vmessages().v; - if (!_inner->searchReceived(msgs, inject, type, d.vcount().v)) { - if (type == SearchRequestType::MigratedFromStart || type == SearchRequestType::MigratedFromOffset) { - _searchFullMigrated = true; - } else { - _searchFull = true; - } - } - } break; - - case mtpc_messages_messagesNotModified: { - LOG(("API Error: received messages.messagesNotModified! (Widget::searchReceived)")); + } else { + LOG(("API Error: " + "received messages.channelMessages when no channel " + "was passed! (Widget::searchReceived)")); + } + if (_searchRequest != 0) { + // Don't apply cached data! + session().data().processUsers(d.vusers()); + session().data().processChats(d.vchats()); + } + auto &msgs = d.vmessages().v; + if (!_inner->searchReceived(msgs, inject, type, d.vcount().v)) { if (type == SearchRequestType::MigratedFromStart || type == SearchRequestType::MigratedFromOffset) { _searchFullMigrated = true; } else { _searchFull = true; } - } break; } + } break; - _searchRequest = 0; - onListScroll(); - update(); + case mtpc_messages_messagesNotModified: { + LOG(("API Error: received messages.messagesNotModified! (Widget::searchReceived)")); + if (type == SearchRequestType::MigratedFromStart || type == SearchRequestType::MigratedFromOffset) { + _searchFullMigrated = true; + } else { + _searchFull = true; + } + } break; } + + _searchRequest = 0; + onListScroll(); + update(); } void Widget::peerSearchReceived( diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 80a17e2be..bbe4cbd93 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -2480,7 +2480,7 @@ void History::setFakeChatListMessage() { if (const auto chat = peer->asChat()) { // In chats we try to take the item before the 'last', which // is the empty-displayed migration message. - session().api().requestFakeChatListMessage(this); + owner().histories().requestFakeChatListMessage(this); } else if (const auto from = migrateFrom()) { // In megagroups we just try to use // the message from the original group. diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 15deb8f4f..78c364636 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -1716,9 +1716,8 @@ void HistoryWidget::showHistory( session().data().stopPlayingVideoFiles(); clearReplyReturns(); - clearAllLoadRequests(); - if (_history) { + clearAllLoadRequests(); if (Ui::InFocusChain(_list)) { // Removing focus from list clears selected and updates top bar. setFocus(); @@ -1917,19 +1916,34 @@ void HistoryWidget::showHistory( } void HistoryWidget::clearDelayedShowAt() { + Expects(_history != nullptr); + _delayedShowAtMsgId = -1; if (_delayedShowAtRequest) { - MTP::cancel(_delayedShowAtRequest); + _history->owner().histories().cancelRequest( + _history, + _delayedShowAtRequest); _delayedShowAtRequest = 0; } } void HistoryWidget::clearAllLoadRequests() { + Expects(_history != nullptr); + + auto &histories = _history->owner().histories(); clearDelayedShowAt(); - if (_firstLoadRequest) MTP::cancel(_firstLoadRequest); - if (_preloadRequest) MTP::cancel(_preloadRequest); - if (_preloadDownRequest) MTP::cancel(_preloadDownRequest); - _preloadRequest = _preloadDownRequest = _firstLoadRequest = 0; + if (_firstLoadRequest) { + histories.cancelRequest(_history, _firstLoadRequest); + _firstLoadRequest = 0; + } + if (_preloadRequest) { + histories.cancelRequest(_history, _preloadRequest); + _preloadRequest = 0; + } + if (_preloadDownRequest) { + histories.cancelRequest(_history, _preloadDownRequest); + _preloadDownRequest = 0; + } } void HistoryWidget::updateFieldSubmitSettings() { @@ -2330,8 +2344,10 @@ void HistoryWidget::unreadCountUpdated() { } } -bool HistoryWidget::messagesFailed(const RPCError &error, mtpRequestId requestId) { - if (MTP::isDefaultHandledError(error)) return false; +bool HistoryWidget::messagesFailed(const RPCError &error, int requestId) { + if (MTP::isDefaultHandledError(error)) { + return false; + } if (error.type() == qstr("CHANNEL_PRIVATE") || error.type() == qstr("CHANNEL_PUBLIC_GROUP_NA") @@ -2356,15 +2372,20 @@ bool HistoryWidget::messagesFailed(const RPCError &error, mtpRequestId requestId return true; } -void HistoryWidget::messagesReceived(PeerData *peer, const MTPmessages_Messages &messages, mtpRequestId requestId) { - if (!_history) { - _preloadRequest = _preloadDownRequest = _firstLoadRequest = _delayedShowAtRequest = 0; - return; - } +void HistoryWidget::messagesReceived(PeerData *peer, const MTPmessages_Messages &messages, int requestId) { + Expects(_history != nullptr); bool toMigrated = (peer == _peer->migrateFrom()); if (peer != _peer && !toMigrated) { - _preloadRequest = _preloadDownRequest = _firstLoadRequest = _delayedShowAtRequest = 0; + if (_preloadRequest == requestId) { + _preloadRequest = 0; + } else if (_preloadDownRequest == requestId) { + _preloadDownRequest = 0; + } else if (_firstLoadRequest == requestId) { + _firstLoadRequest = 0; + } else if (_delayedShowAtRequest == requestId) { + _delayedShowAtRequest = 0; + } return; } @@ -2456,10 +2477,7 @@ void HistoryWidget::messagesReceived(PeerData *peer, const MTPmessages_Messages _delayedShowAtRequest = 0; _history->getReadyFor(_delayedShowAtMsgId); if (_history->isEmpty()) { - if (_preloadRequest) MTP::cancel(_preloadRequest); - if (_preloadDownRequest) MTP::cancel(_preloadDownRequest); - if (_firstLoadRequest) MTP::cancel(_firstLoadRequest); - _preloadRequest = _preloadDownRequest = 0; + clearAllLoadRequests(); _firstLoadRequest = -1; // hack - don't updateListSize yet addMessagesToFront(peer, *histList); _firstLoadRequest = 0; @@ -2520,14 +2538,14 @@ void HistoryWidget::firstLoadMessages() { return; } - auto from = _peer; + auto from = _history; auto offsetId = 0; auto offset = 0; auto loadCount = kMessagesPerPage; if (_showAtMsgId == ShowAtUnreadMsgId) { if (const auto around = _migrated ? _migrated->loadAroundId() : 0) { _history->getReadyFor(_showAtMsgId); - from = _migrated->peer; + from = _migrated; offset = -loadCount / 2; offsetId = around; } else if (const auto around = _history->loadAroundId()) { @@ -2547,7 +2565,7 @@ void HistoryWidget::firstLoadMessages() { } else if (_showAtMsgId < 0 && _history->isChannel()) { if (_showAtMsgId < 0 && -_showAtMsgId < ServerMaxMsgId && _migrated) { _history->getReadyFor(_showAtMsgId); - from = _migrated->peer; + from = _migrated; offset = -loadCount / 2; offsetId = -_showAtMsgId; } else if (_showAtMsgId == SwitchAtTopMsgId) { @@ -2560,18 +2578,27 @@ void HistoryWidget::firstLoadMessages() { auto minId = 0; auto historyHash = 0; - _firstLoadRequest = MTP::send( - MTPmessages_GetHistory( - from->input, + const auto history = from; + const auto type = Data::Histories::RequestType::History; + auto &histories = history->owner().histories(); + _firstLoadRequest = histories.sendRequest(history, type, [=](Fn finish) { + return history->session().api().request(MTPmessages_GetHistory( + history->peer->input, MTP_int(offsetId), MTP_int(offsetDate), MTP_int(offset), MTP_int(loadCount), MTP_int(maxId), MTP_int(minId), - MTP_int(historyHash)), - rpcDone(&HistoryWidget::messagesReceived, from), - rpcFail(&HistoryWidget::messagesFailed)); + MTP_int(historyHash) + )).done([=](const MTPmessages_Messages &result) { + messagesReceived(history->peer, result, _firstLoadRequest); + finish(); + }).fail([=](const RPCError &error) { + messagesFailed(error, _firstLoadRequest); + finish(); + }).send(); + }); } void HistoryWidget::loadMessages() { @@ -2602,18 +2629,27 @@ void HistoryWidget::loadMessages() { auto minId = 0; auto historyHash = 0; - _preloadRequest = MTP::send( - MTPmessages_GetHistory( - from->peer->input, + const auto history = from; + const auto type = Data::Histories::RequestType::History; + auto &histories = history->owner().histories(); + _preloadRequest = histories.sendRequest(history, type, [=](Fn finish) { + return history->session().api().request(MTPmessages_GetHistory( + history->peer->input, MTP_int(offsetId), MTP_int(offsetDate), MTP_int(addOffset), MTP_int(loadCount), MTP_int(maxId), MTP_int(minId), - MTP_int(historyHash)), - rpcDone(&HistoryWidget::messagesReceived, from->peer.get()), - rpcFail(&HistoryWidget::messagesFailed)); + MTP_int(historyHash) + )).done([=](const MTPmessages_Messages &result) { + messagesReceived(history->peer, result, _preloadRequest); + finish(); + }).fail([=](const RPCError &error) { + messagesFailed(error, _preloadRequest); + finish(); + }).send(); + }); } void HistoryWidget::loadMessagesDown() { @@ -2644,18 +2680,27 @@ void HistoryWidget::loadMessagesDown() { auto minId = 0; auto historyHash = 0; - _preloadDownRequest = MTP::send( - MTPmessages_GetHistory( - from->peer->input, + const auto history = from; + const auto type = Data::Histories::RequestType::History; + auto &histories = history->owner().histories(); + _preloadDownRequest = histories.sendRequest(history, type, [=](Fn finish) { + return history->session().api().request(MTPmessages_GetHistory( + history->peer->input, MTP_int(offsetId + 1), MTP_int(offsetDate), MTP_int(addOffset), MTP_int(loadCount), MTP_int(maxId), MTP_int(minId), - MTP_int(historyHash)), - rpcDone(&HistoryWidget::messagesReceived, from->peer.get()), - rpcFail(&HistoryWidget::messagesFailed)); + MTP_int(historyHash) + )).done([=](const MTPmessages_Messages &result) { + messagesReceived(history->peer, result, _preloadDownRequest); + finish(); + }).fail([=](const RPCError &error) { + messagesFailed(error, _preloadDownRequest); + finish(); + }).send(); + }); } void HistoryWidget::delayedShowAt(MsgId showAtMsgId) { @@ -2667,13 +2712,13 @@ void HistoryWidget::delayedShowAt(MsgId showAtMsgId) { clearDelayedShowAt(); _delayedShowAtMsgId = showAtMsgId; - auto from = _peer; + auto from = _history; auto offsetId = 0; auto offset = 0; auto loadCount = kMessagesPerPage; if (_delayedShowAtMsgId == ShowAtUnreadMsgId) { if (const auto around = _migrated ? _migrated->loadAroundId() : 0) { - from = _migrated->peer; + from = _migrated; offset = -loadCount / 2; offsetId = around; } else if (const auto around = _history->loadAroundId()) { @@ -2689,7 +2734,7 @@ void HistoryWidget::delayedShowAt(MsgId showAtMsgId) { offsetId = _delayedShowAtMsgId; } else if (_delayedShowAtMsgId < 0 && _history->isChannel()) { if (_delayedShowAtMsgId < 0 && -_delayedShowAtMsgId < ServerMaxMsgId && _migrated) { - from = _migrated->peer; + from = _migrated; offset = -loadCount / 2; offsetId = -_delayedShowAtMsgId; } @@ -2699,18 +2744,27 @@ void HistoryWidget::delayedShowAt(MsgId showAtMsgId) { auto minId = 0; auto historyHash = 0; - _delayedShowAtRequest = MTP::send( - MTPmessages_GetHistory( - from->input, + const auto history = from; + const auto type = Data::Histories::RequestType::History; + auto &histories = history->owner().histories(); + _delayedShowAtRequest = histories.sendRequest(history, type, [=](Fn finish) { + return history->session().api().request(MTPmessages_GetHistory( + history->peer->input, MTP_int(offsetId), MTP_int(offsetDate), MTP_int(offset), MTP_int(loadCount), MTP_int(maxId), MTP_int(minId), - MTP_int(historyHash)), - rpcDone(&HistoryWidget::messagesReceived, from), - rpcFail(&HistoryWidget::messagesFailed)); + MTP_int(historyHash) + )).done([=](const MTPmessages_Messages &result) { + messagesReceived(history->peer, result, _delayedShowAtRequest); + finish(); + }).fail([=](const RPCError &error) { + messagesFailed(error, _delayedShowAtRequest); + finish(); + }).send(); + }); } void HistoryWidget::onScroll() { @@ -2884,7 +2938,8 @@ void HistoryWidget::saveEditMsg() { sendFlags |= MTPmessages_EditMessage::Flag::f_entities; } - _saveEditMsgRequestId = MTP::send( + const auto history = _history; + _saveEditMsgRequestId = history->session().api().request( MTPmessages_EditMessage( MTP_flags(sendFlags), _history->peer->input, @@ -2893,9 +2948,12 @@ void HistoryWidget::saveEditMsg() { MTPInputMedia(), MTPReplyMarkup(), sentEntities, - MTP_int(0)), // schedule_date - rpcDone(&HistoryWidget::saveEditMsgDone, _history), - rpcFail(&HistoryWidget::saveEditMsgFail, _history)); + MTP_int(0) + )).done([=](const MTPUpdates &result, mtpRequestId requestId) { + saveEditMsgDone(history, result, requestId); + }).fail([=](const RPCError &error, mtpRequestId requestId) { + saveEditMsgFail(history, error, requestId); + }).send(); } void HistoryWidget::saveEditMsgDone(History *history, const MTPUpdates &updates, mtpRequestId req) { @@ -2913,7 +2971,9 @@ void HistoryWidget::saveEditMsgDone(History *history, const MTPUpdates &updates, } bool HistoryWidget::saveEditMsgFail(History *history, const RPCError &error, mtpRequestId req) { - if (MTP::isDefaultHandledError(error)) return false; + if (MTP::isDefaultHandledError(error)) { + return false; + } if (req == _saveEditMsgRequestId) { _saveEditMsgRequestId = 0; } @@ -3733,7 +3793,9 @@ void HistoryWidget::inlineBotResolveDone( } bool HistoryWidget::inlineBotResolveFail(QString name, const RPCError &error) { - if (MTP::isDefaultHandledError(error)) return false; + if (MTP::isDefaultHandledError(error)) { + return false; + } _inlineBotResolveRequestId = 0; // Notify::inlineBotRequesting(false); @@ -6085,7 +6147,7 @@ void HistoryWidget::cancelEdit() { applyDraft(); if (_saveEditMsgRequestId) { - MTP::cancel(_saveEditMsgRequestId); + _history->session().api().request(_saveEditMsgRequestId).cancel(); _saveEditMsgRequestId = 0; } @@ -6970,5 +7032,6 @@ void HistoryWidget::synteticScrollToY(int y) { } HistoryWidget::~HistoryWidget() { + clearAllLoadRequests(); setTabbedPanel(nullptr); } diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index f8b720792..f672559e1 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -105,7 +105,6 @@ public: void start(); - void messagesReceived(PeerData *peer, const MTPmessages_Messages &messages, mtpRequestId requestId); void historyLoaded(); void windowShown(); @@ -569,7 +568,8 @@ private: void checkPreview(); void requestPreview(); void gotPreview(QString links, const MTPMessageMedia &media, mtpRequestId req); - bool messagesFailed(const RPCError &error, mtpRequestId requestId); + void messagesReceived(PeerData *peer, const MTPmessages_Messages &messages, int requestId); + bool messagesFailed(const RPCError &error, int requestId); void addMessagesToFront(PeerData *peer, const QVector &messages); void addMessagesToBack(PeerData *peer, const QVector &messages); @@ -681,12 +681,12 @@ private: bool _canSendMessages = false; MsgId _showAtMsgId = ShowAtUnreadMsgId; - mtpRequestId _firstLoadRequest = 0; - mtpRequestId _preloadRequest = 0; - mtpRequestId _preloadDownRequest = 0; + int _firstLoadRequest = 0; // Not real mtpRequestId. + int _preloadRequest = 0; // Not real mtpRequestId. + int _preloadDownRequest = 0; // Not real mtpRequestId. MsgId _delayedShowAtMsgId = -1; - mtpRequestId _delayedShowAtRequest = 0; + int _delayedShowAtRequest = 0; // Not real mtpRequestId. object_ptr _topBar; object_ptr _scroll;