From 2c810141887b605448249ca961cc8c739bea4331 Mon Sep 17 00:00:00 2001 From: John Preston Date: Sun, 9 Apr 2017 23:21:14 +0300 Subject: [PATCH] Fix crash on precondition in PeerListBox. If a global search request was sent and we clear the query the response still was being processed which caused an Expects() fail. --- Telegram/SourceFiles/boxes/peer_list_box.cpp | 35 +++++++++----------- Telegram/SourceFiles/boxes/peer_list_box.h | 4 +-- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Telegram/SourceFiles/boxes/peer_list_box.cpp b/Telegram/SourceFiles/boxes/peer_list_box.cpp index a47afa079..81c4c3f0b 100644 --- a/Telegram/SourceFiles/boxes/peer_list_box.cpp +++ b/Telegram/SourceFiles/boxes/peer_list_box.cpp @@ -948,6 +948,7 @@ void PeerListBox::Inner::searchQueryChanged(QString query) { } } if (_searchMode == SearchMode::Global) { + _globalSearchRequestId = 0; needGlobalSearch(); } refreshRows(); @@ -978,23 +979,27 @@ bool PeerListBox::Inner::globalSearchInCache() { void PeerListBox::Inner::globalSearchOnServer() { _globalSearchQuery = _searchQuery; - _globalSearchRequestId = MTP::send(MTPcontacts_Search(MTP_string(_globalSearchQuery), MTP_int(SearchPeopleLimit)), ::rpcDone(base::lambda_guarded(this, [this](const MTPcontacts_Found &result, mtpRequestId requestId) { + _globalSearchRequestId = request(MTPcontacts_Search(MTP_string(_globalSearchQuery), MTP_int(SearchPeopleLimit))).done([this](const MTPcontacts_Found &result, mtpRequestId requestId) { globalSearchDone(result, requestId); - })), ::rpcFail(base::lambda_guarded(this, [this](const RPCError &error, mtpRequestId requestId) { - return globalSearchFail(error, requestId); - }))); + }).fail([this](const RPCError &error, mtpRequestId requestId) { + if (_globalSearchRequestId == requestId) { + _globalSearchRequestId = 0; + refreshRows(); + } + }).send(); _globalSearchQueries.emplace(_globalSearchRequestId, _globalSearchQuery); } void PeerListBox::Inner::globalSearchDone(const MTPcontacts_Found &result, mtpRequestId requestId) { auto query = _globalSearchQuery; - auto it = _globalSearchQueries.find(requestId); - if (it != _globalSearchQueries.cend()) { - query = it->second; - _globalSearchCache[query] = result; - _globalSearchQueries.erase(it); + if (requestId) { + auto it = _globalSearchQueries.find(requestId); + if (it != _globalSearchQueries.cend()) { + query = it->second; + _globalSearchCache[query] = result; + _globalSearchQueries.erase(it); + } } - if (_globalSearchRequestId == requestId) { _globalSearchRequestId = 0; if (result.type() == mtpc_contacts_found) { @@ -1023,16 +1028,6 @@ void PeerListBox::Inner::globalSearchDone(const MTPcontacts_Found &result, mtpRe } } -bool PeerListBox::Inner::globalSearchFail(const RPCError &error, mtpRequestId requestId) { - if (MTP::isDefaultHandledError(error)) return false; - - if (_globalSearchRequestId == requestId) { - _globalSearchRequestId = 0; - refreshRows(); - } - return true; -} - bool PeerListBox::Inner::globalSearchLoading() const { return (_globalSearchTimer && _globalSearchTimer->isActive()) || _globalSearchRequestId; } diff --git a/Telegram/SourceFiles/boxes/peer_list_box.h b/Telegram/SourceFiles/boxes/peer_list_box.h index f01d870b1..1c16018b0 100644 --- a/Telegram/SourceFiles/boxes/peer_list_box.h +++ b/Telegram/SourceFiles/boxes/peer_list_box.h @@ -21,6 +21,7 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org #pragma once #include "boxes/abstract_box.h" +#include "mtproto/sender.h" namespace Ui { class RippleAnimation; @@ -245,7 +246,7 @@ private: }; // This class is hold in header because it requires Qt preprocessing. -class PeerListBox::Inner : public TWidget, public RPCSender, private base::Subscriber { +class PeerListBox::Inner : public TWidget, private MTP::Sender, private base::Subscriber { Q_OBJECT public: @@ -376,7 +377,6 @@ private: bool globalSearchInCache(); void globalSearchOnServer(); void globalSearchDone(const MTPcontacts_Found &result, mtpRequestId requestId); - bool globalSearchFail(const RPCError &error, mtpRequestId requestId); bool globalSearchLoading() const; void clearGlobalSearchRows();