mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-04-11 04:01:18 +00:00
Added caching of result of search messages in dialogs.
This commit is contained in:
parent
03c5e67047
commit
069413c19c
@ -69,6 +69,14 @@ void MessagesSearch::searchMore() {
|
||||
void MessagesSearch::searchRequest() {
|
||||
const auto nextToken = _query
|
||||
+ QString::number(_from ? _from->id.value : 0);
|
||||
if (!_offsetId) {
|
||||
const auto it = _cacheOfStartByToken.find(nextToken);
|
||||
if (it != end(_cacheOfStartByToken)) {
|
||||
_requestId = 0;
|
||||
searchReceived(it->second, _requestId, nextToken);
|
||||
return;
|
||||
}
|
||||
}
|
||||
auto callback = [=](Fn<void()> finish) {
|
||||
const auto flags = _from
|
||||
? MTP_flags(MTPmessages_Search::Flag::f_from_id)
|
||||
@ -90,7 +98,7 @@ void MessagesSearch::searchRequest() {
|
||||
MTP_int(0), // max_id
|
||||
MTP_int(0), // min_id
|
||||
MTP_long(0) // hash
|
||||
)).done([=](const MTPmessages_Messages &result, mtpRequestId id) {
|
||||
)).done([=](const TLMessages &result, mtpRequestId id) {
|
||||
_searchInHistoryRequest = 0;
|
||||
searchReceived(result, id, nextToken);
|
||||
finish();
|
||||
@ -114,7 +122,7 @@ void MessagesSearch::searchRequest() {
|
||||
}
|
||||
|
||||
void MessagesSearch::searchReceived(
|
||||
const MTPmessages_Messages &result,
|
||||
const TLMessages &result,
|
||||
mtpRequestId requestId,
|
||||
const QString &nextToken) {
|
||||
if (requestId != _requestId) {
|
||||
@ -159,6 +167,9 @@ void MessagesSearch::searchReceived(
|
||||
}, [](const MTPDmessages_messagesNotModified &data) {
|
||||
return FoundMessages{};
|
||||
});
|
||||
if (!_offsetId) {
|
||||
_cacheOfStartByToken.emplace(nextToken, result);
|
||||
}
|
||||
_requestId = 0;
|
||||
_offsetId = found.messages.empty()
|
||||
? MsgId()
|
||||
|
@ -37,9 +37,10 @@ public:
|
||||
[[nodiscard]] rpl::producer<FoundMessages> messagesFounds() const;
|
||||
|
||||
private:
|
||||
using TLMessages = MTPmessages_Messages;
|
||||
void searchRequest();
|
||||
void searchReceived(
|
||||
const MTPmessages_Messages &result,
|
||||
const TLMessages &result,
|
||||
mtpRequestId requestId,
|
||||
const QString &nextToken);
|
||||
|
||||
@ -47,6 +48,8 @@ private:
|
||||
const not_null<History*> _history;
|
||||
MTP::Sender _api;
|
||||
|
||||
base::flat_map<QString, TLMessages> _cacheOfStartByToken;
|
||||
|
||||
QString _query;
|
||||
PeerData *_from = nullptr;
|
||||
MsgId _offsetId;
|
||||
|
@ -260,7 +260,7 @@ public:
|
||||
|
||||
private:
|
||||
void clearItems();
|
||||
void requestSearch();
|
||||
void requestSearch(bool cache = true);
|
||||
void requestSearchDelayed();
|
||||
|
||||
base::unique_qptr<Ui::IconButton> _cancel;
|
||||
@ -270,6 +270,8 @@ private:
|
||||
|
||||
base::Timer _searchTimer;
|
||||
|
||||
std::vector<SearchRequest> _typedRequests;
|
||||
|
||||
rpl::event_stream<SearchRequest> _searchRequests;
|
||||
rpl::event_stream<> _queryChanges;
|
||||
};
|
||||
@ -337,11 +339,23 @@ void TopBar::clearItems() {
|
||||
});
|
||||
}
|
||||
|
||||
void TopBar::requestSearch() {
|
||||
_searchRequests.fire({ _select->getQuery(), _from.current() });
|
||||
void TopBar::requestSearch(bool cache) {
|
||||
const auto search = SearchRequest{ _select->getQuery(), _from.current() };
|
||||
if (cache) {
|
||||
_typedRequests.push_back(search);
|
||||
}
|
||||
_searchRequests.fire_copy(search);
|
||||
}
|
||||
|
||||
void TopBar::requestSearchDelayed() {
|
||||
// Check cached queries.
|
||||
for (const auto &t : _typedRequests) {
|
||||
if (t.query == _select->getQuery() && t.from == _from.current()) {
|
||||
requestSearch(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_searchTimer.callOnce(AutoSearchTimeout);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user