2017-10-30 19:24:20 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-10-30 19:24:20 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-10-30 19:24:20 +00:00
|
|
|
*/
|
2017-11-06 07:31:18 +00:00
|
|
|
#include "data/data_search_controller.h"
|
2017-10-30 19:24:20 +00:00
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2018-01-04 10:22:53 +00:00
|
|
|
#include "data/data_session.h"
|
2018-01-09 17:08:31 +00:00
|
|
|
#include "data/data_messages.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_channel.h"
|
2020-02-21 11:51:37 +00:00
|
|
|
#include "data/data_histories.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2018-01-11 19:33:26 +00:00
|
|
|
#include "history/history_item.h"
|
2019-11-27 08:02:56 +00:00
|
|
|
#include "apiwrap.h"
|
2017-10-30 19:24:20 +00:00
|
|
|
|
|
|
|
namespace Api {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kSharedMediaLimit = 100;
|
2022-05-16 09:20:31 +00:00
|
|
|
constexpr auto kFirstSharedMediaLimit = 0;
|
2024-05-03 10:47:08 +00:00
|
|
|
constexpr auto kHistoryLimit = 50;
|
2019-02-19 06:57:53 +00:00
|
|
|
constexpr auto kDefaultSearchTimeoutMs = crl::time(200);
|
2017-10-30 19:24:20 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2022-05-16 09:20:31 +00:00
|
|
|
std::optional<SearchRequest> PrepareSearchRequest(
|
2017-10-30 19:24:20 +00:00
|
|
|
not_null<PeerData*> peer,
|
2022-10-11 15:08:19 +00:00
|
|
|
MsgId topicRootId,
|
2017-10-30 19:24:20 +00:00
|
|
|
Storage::SharedMediaType type,
|
|
|
|
const QString &query,
|
|
|
|
MsgId messageId,
|
2018-01-09 17:08:31 +00:00
|
|
|
Data::LoadDirection direction) {
|
2017-12-28 10:11:50 +00:00
|
|
|
const auto filter = [&] {
|
2017-10-30 19:24:20 +00:00
|
|
|
using Type = Storage::SharedMediaType;
|
|
|
|
switch (type) {
|
|
|
|
case Type::Photo:
|
|
|
|
return MTP_inputMessagesFilterPhotos();
|
|
|
|
case Type::Video:
|
|
|
|
return MTP_inputMessagesFilterVideo();
|
2017-12-28 10:11:50 +00:00
|
|
|
case Type::PhotoVideo:
|
|
|
|
return MTP_inputMessagesFilterPhotoVideo();
|
2017-10-30 19:24:20 +00:00
|
|
|
case Type::MusicFile:
|
|
|
|
return MTP_inputMessagesFilterMusic();
|
|
|
|
case Type::File:
|
|
|
|
return MTP_inputMessagesFilterDocument();
|
|
|
|
case Type::VoiceFile:
|
|
|
|
return MTP_inputMessagesFilterVoice();
|
|
|
|
case Type::RoundVoiceFile:
|
|
|
|
return MTP_inputMessagesFilterRoundVoice();
|
|
|
|
case Type::RoundFile:
|
|
|
|
return MTP_inputMessagesFilterRoundVideo();
|
|
|
|
case Type::GIF:
|
|
|
|
return MTP_inputMessagesFilterGif();
|
|
|
|
case Type::Link:
|
|
|
|
return MTP_inputMessagesFilterUrl();
|
|
|
|
case Type::ChatPhoto:
|
|
|
|
return MTP_inputMessagesFilterChatPhotos();
|
2020-10-20 16:29:24 +00:00
|
|
|
case Type::Pinned:
|
|
|
|
return MTP_inputMessagesFilterPinned();
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
return MTP_inputMessagesFilterEmpty();
|
|
|
|
}();
|
2019-07-05 13:38:38 +00:00
|
|
|
if (query.isEmpty() && filter.type() == mtpc_inputMessagesFilterEmpty) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2017-10-30 19:24:20 +00:00
|
|
|
|
2017-12-28 10:11:50 +00:00
|
|
|
const auto minId = 0;
|
|
|
|
const auto maxId = 0;
|
2022-05-16 09:20:31 +00:00
|
|
|
const auto limit = messageId ? kSharedMediaLimit : kFirstSharedMediaLimit;
|
2017-12-28 10:11:50 +00:00
|
|
|
const auto offsetId = [&] {
|
2017-10-30 19:24:20 +00:00
|
|
|
switch (direction) {
|
2018-01-09 17:08:31 +00:00
|
|
|
case Data::LoadDirection::Before:
|
|
|
|
case Data::LoadDirection::Around: return messageId;
|
|
|
|
case Data::LoadDirection::After: return messageId + 1;
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
Unexpected("Direction in PrepareSearchRequest");
|
|
|
|
}();
|
2017-12-28 10:11:50 +00:00
|
|
|
const auto addOffset = [&] {
|
2017-10-30 19:24:20 +00:00
|
|
|
switch (direction) {
|
2018-01-09 17:08:31 +00:00
|
|
|
case Data::LoadDirection::Before: return 0;
|
|
|
|
case Data::LoadDirection::Around: return -limit / 2;
|
|
|
|
case Data::LoadDirection::After: return -limit;
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
Unexpected("Direction in PrepareSearchRequest");
|
|
|
|
}();
|
2021-08-25 08:15:05 +00:00
|
|
|
const auto hash = uint64(0);
|
2017-10-30 19:24:20 +00:00
|
|
|
|
2021-12-02 09:54:30 +00:00
|
|
|
const auto mtpOffsetId = int(std::clamp(
|
|
|
|
offsetId.bare,
|
|
|
|
int64(0),
|
|
|
|
int64(0x3FFFFFFF)));
|
2022-10-11 15:08:19 +00:00
|
|
|
using Flag = MTPmessages_Search::Flag;
|
2017-10-30 19:24:20 +00:00
|
|
|
return MTPmessages_Search(
|
2022-10-11 15:08:19 +00:00
|
|
|
MTP_flags(topicRootId ? Flag::f_top_msg_id : Flag(0)),
|
2017-10-30 19:24:20 +00:00
|
|
|
peer->input,
|
|
|
|
MTP_string(query),
|
2020-10-06 12:58:16 +00:00
|
|
|
MTP_inputPeerEmpty(),
|
2023-12-26 22:31:21 +00:00
|
|
|
MTPInputPeer(), // saved_peer_id
|
2024-01-03 10:46:34 +00:00
|
|
|
MTPVector<MTPReaction>(), // saved_reaction
|
2022-10-11 15:08:19 +00:00
|
|
|
MTP_int(topicRootId),
|
2017-10-30 19:24:20 +00:00
|
|
|
filter,
|
2021-08-25 08:15:05 +00:00
|
|
|
MTP_int(0), // min_date
|
|
|
|
MTP_int(0), // max_date
|
2021-12-02 09:54:30 +00:00
|
|
|
MTP_int(mtpOffsetId),
|
2017-10-30 19:24:20 +00:00
|
|
|
MTP_int(addOffset),
|
|
|
|
MTP_int(limit),
|
|
|
|
MTP_int(maxId),
|
2018-01-18 11:04:50 +00:00
|
|
|
MTP_int(minId),
|
2021-08-25 08:15:05 +00:00
|
|
|
MTP_long(hash));
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SearchResult ParseSearchResult(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
Storage::SharedMediaType type,
|
|
|
|
MsgId messageId,
|
2018-01-09 17:08:31 +00:00
|
|
|
Data::LoadDirection direction,
|
2022-05-16 09:20:31 +00:00
|
|
|
const SearchRequestResult &data) {
|
2017-10-30 19:24:20 +00:00
|
|
|
auto result = SearchResult();
|
2017-11-20 19:54:05 +00:00
|
|
|
result.noSkipRange = MsgRange{ messageId, messageId };
|
|
|
|
|
|
|
|
auto messages = [&] {
|
2017-10-30 19:24:20 +00:00
|
|
|
switch (data.type()) {
|
|
|
|
case mtpc_messages_messages: {
|
|
|
|
auto &d = data.c_messages_messages();
|
2019-07-05 13:38:38 +00:00
|
|
|
peer->owner().processUsers(d.vusers());
|
|
|
|
peer->owner().processChats(d.vchats());
|
|
|
|
result.fullCount = d.vmessages().v.size();
|
|
|
|
return &d.vmessages().v;
|
2017-10-30 19:24:20 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messages_messagesSlice: {
|
|
|
|
auto &d = data.c_messages_messagesSlice();
|
2019-07-05 13:38:38 +00:00
|
|
|
peer->owner().processUsers(d.vusers());
|
|
|
|
peer->owner().processChats(d.vchats());
|
|
|
|
result.fullCount = d.vcount().v;
|
|
|
|
return &d.vmessages().v;
|
2017-10-30 19:24:20 +00:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messages_channelMessages: {
|
2022-11-08 11:36:46 +00:00
|
|
|
const auto &d = data.c_messages_channelMessages();
|
|
|
|
if (const auto channel = peer->asChannel()) {
|
2019-07-05 13:38:38 +00:00
|
|
|
channel->ptsReceived(d.vpts().v);
|
2022-11-08 11:36:46 +00:00
|
|
|
channel->processTopics(d.vtopics());
|
2017-10-30 19:24:20 +00:00
|
|
|
} else {
|
2017-12-28 10:11:50 +00:00
|
|
|
LOG(("API Error: received messages.channelMessages when "
|
|
|
|
"no channel was passed! (ParseSearchResult)"));
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
peer->owner().processUsers(d.vusers());
|
|
|
|
peer->owner().processChats(d.vchats());
|
|
|
|
result.fullCount = d.vcount().v;
|
|
|
|
return &d.vmessages().v;
|
2017-10-30 19:24:20 +00:00
|
|
|
} break;
|
2017-11-20 19:54:05 +00:00
|
|
|
|
|
|
|
case mtpc_messages_messagesNotModified: {
|
2017-12-28 10:11:50 +00:00
|
|
|
LOG(("API Error: received messages.messagesNotModified! "
|
|
|
|
"(ParseSearchResult)"));
|
2017-11-20 19:54:05 +00:00
|
|
|
return (const QVector<MTPMessage>*)nullptr;
|
|
|
|
} break;
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
Unexpected("messages.Messages type in ParseSearchResult()");
|
|
|
|
}();
|
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
if (!messages) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-04-25 12:45:15 +00:00
|
|
|
const auto addType = NewMessageType::Existing;
|
2017-11-20 19:54:05 +00:00
|
|
|
result.messageIds.reserve(messages->size());
|
2019-01-03 12:36:01 +00:00
|
|
|
for (const auto &message : *messages) {
|
2019-08-09 19:19:23 +00:00
|
|
|
const auto item = peer->owner().addNewMessage(
|
|
|
|
message,
|
2021-07-28 11:55:02 +00:00
|
|
|
MessageFlags(),
|
2019-08-09 19:19:23 +00:00
|
|
|
addType);
|
|
|
|
if (item) {
|
|
|
|
const auto itemId = item->id;
|
2017-10-30 19:24:20 +00:00
|
|
|
if ((type == Storage::SharedMediaType::kCount)
|
|
|
|
|| item->sharedMediaTypes().test(type)) {
|
|
|
|
result.messageIds.push_back(itemId);
|
|
|
|
}
|
2017-11-23 09:58:12 +00:00
|
|
|
accumulate_min(result.noSkipRange.from, itemId);
|
|
|
|
accumulate_max(result.noSkipRange.till, itemId);
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (messageId && result.messageIds.empty()) {
|
|
|
|
result.noSkipRange = [&]() -> MsgRange {
|
|
|
|
switch (direction) {
|
2018-01-09 17:08:31 +00:00
|
|
|
case Data::LoadDirection::Before: // All old loaded.
|
2017-10-30 19:24:20 +00:00
|
|
|
return { 0, result.noSkipRange.till };
|
2018-01-09 17:08:31 +00:00
|
|
|
case Data::LoadDirection::Around: // All loaded.
|
2017-10-30 19:24:20 +00:00
|
|
|
return { 0, ServerMaxMsgId };
|
2018-01-09 17:08:31 +00:00
|
|
|
case Data::LoadDirection::After: // All new loaded.
|
2017-10-30 19:24:20 +00:00
|
|
|
return { result.noSkipRange.from, ServerMaxMsgId };
|
|
|
|
}
|
|
|
|
Unexpected("Direction in ParseSearchResult");
|
|
|
|
}();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-05-03 10:47:08 +00:00
|
|
|
HistoryRequest PrepareHistoryRequest(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
MsgId messageId,
|
|
|
|
Data::LoadDirection direction) {
|
|
|
|
const auto minId = 0;
|
|
|
|
const auto maxId = 0;
|
|
|
|
const auto limit = kHistoryLimit;
|
|
|
|
const auto offsetId = [&] {
|
|
|
|
switch (direction) {
|
|
|
|
case Data::LoadDirection::Before:
|
|
|
|
case Data::LoadDirection::Around: return messageId;
|
|
|
|
case Data::LoadDirection::After: return messageId + 1;
|
|
|
|
}
|
|
|
|
Unexpected("Direction in PrepareSearchRequest");
|
|
|
|
}();
|
|
|
|
const auto addOffset = [&] {
|
|
|
|
switch (direction) {
|
|
|
|
case Data::LoadDirection::Before: return 0;
|
|
|
|
case Data::LoadDirection::Around: return -limit / 2;
|
|
|
|
case Data::LoadDirection::After: return -limit;
|
|
|
|
}
|
|
|
|
Unexpected("Direction in PrepareSearchRequest");
|
|
|
|
}();
|
|
|
|
const auto hash = uint64(0);
|
|
|
|
const auto offsetDate = int32(0);
|
|
|
|
|
|
|
|
const auto mtpOffsetId = int(std::clamp(
|
|
|
|
offsetId.bare,
|
|
|
|
int64(0),
|
|
|
|
int64(0x3FFFFFFF)));
|
|
|
|
return MTPmessages_GetHistory(
|
|
|
|
peer->input,
|
|
|
|
MTP_int(mtpOffsetId),
|
|
|
|
MTP_int(offsetDate),
|
|
|
|
MTP_int(addOffset),
|
|
|
|
MTP_int(limit),
|
|
|
|
MTP_int(maxId),
|
|
|
|
MTP_int(minId),
|
|
|
|
MTP_long(hash));
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryResult ParseHistoryResult(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
MsgId messageId,
|
|
|
|
Data::LoadDirection direction,
|
|
|
|
const HistoryRequestResult &data) {
|
|
|
|
return ParseSearchResult(
|
|
|
|
peer,
|
|
|
|
Storage::SharedMediaType::kCount,
|
|
|
|
messageId,
|
|
|
|
direction,
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
|
2020-06-08 09:06:50 +00:00
|
|
|
SearchController::CacheEntry::CacheEntry(
|
|
|
|
not_null<Main::Session*> session,
|
|
|
|
const Query &query)
|
|
|
|
: peerData(session->data().peer(query.peerId))
|
2017-10-31 07:03:18 +00:00
|
|
|
, migratedData(query.migratedPeerId
|
2020-06-08 09:06:50 +00:00
|
|
|
? base::make_optional(Data(session->data().peer(query.migratedPeerId)))
|
2018-09-21 16:28:46 +00:00
|
|
|
: std::nullopt) {
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 08:02:56 +00:00
|
|
|
SearchController::SearchController(not_null<Main::Session*> session)
|
2020-02-25 12:12:04 +00:00
|
|
|
: _session(session) {
|
2019-11-27 08:02:56 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 07:03:18 +00:00
|
|
|
bool SearchController::hasInCache(const Query &query) const {
|
|
|
|
return query.query.isEmpty() || _cache.contains(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchController::setQuery(const Query &query) {
|
|
|
|
if (query.query.isEmpty()) {
|
|
|
|
_cache.clear();
|
|
|
|
_current = _cache.end();
|
|
|
|
} else {
|
|
|
|
_current = _cache.find(query);
|
|
|
|
}
|
|
|
|
if (_current == _cache.end()) {
|
|
|
|
_current = _cache.emplace(
|
|
|
|
query,
|
2020-06-08 09:06:50 +00:00
|
|
|
std::make_unique<CacheEntry>(_session, query)).first;
|
2017-10-31 07:03:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<SparseIdsMergedSlice> SearchController::idsSlice(
|
2017-10-30 19:24:20 +00:00
|
|
|
SparseIdsMergedSlice::UniversalMsgId aroundId,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
2017-10-31 07:03:18 +00:00
|
|
|
Expects(_current != _cache.cend());
|
|
|
|
|
|
|
|
auto query = (const Query&)_current->first;
|
|
|
|
auto createSimpleViewer = [=](
|
2017-10-30 19:24:20 +00:00
|
|
|
PeerId peerId,
|
2022-10-11 15:08:19 +00:00
|
|
|
MsgId topicRootId,
|
2017-10-30 19:24:20 +00:00
|
|
|
SparseIdsSlice::Key simpleKey,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
|
|
|
return simpleIdsSlice(
|
|
|
|
peerId,
|
2022-10-11 15:08:19 +00:00
|
|
|
topicRootId,
|
2017-10-30 19:24:20 +00:00
|
|
|
simpleKey,
|
2017-10-31 07:03:18 +00:00
|
|
|
query,
|
2017-10-30 19:24:20 +00:00
|
|
|
limitBefore,
|
|
|
|
limitAfter);
|
|
|
|
};
|
|
|
|
return SparseIdsMergedSlice::CreateViewer(
|
|
|
|
SparseIdsMergedSlice::Key(
|
2017-10-31 07:03:18 +00:00
|
|
|
query.peerId,
|
2022-10-11 15:08:19 +00:00
|
|
|
query.topicRootId,
|
2017-10-31 07:03:18 +00:00
|
|
|
query.migratedPeerId,
|
2017-10-30 19:24:20 +00:00
|
|
|
aroundId),
|
|
|
|
limitBefore,
|
|
|
|
limitAfter,
|
|
|
|
std::move(createSimpleViewer));
|
|
|
|
}
|
|
|
|
|
2017-10-31 07:03:18 +00:00
|
|
|
rpl::producer<SparseIdsSlice> SearchController::simpleIdsSlice(
|
2017-10-30 19:24:20 +00:00
|
|
|
PeerId peerId,
|
2022-10-11 15:08:19 +00:00
|
|
|
MsgId topicRootId,
|
2017-10-30 19:24:20 +00:00
|
|
|
MsgId aroundId,
|
2017-10-31 07:03:18 +00:00
|
|
|
const Query &query,
|
2017-10-30 19:24:20 +00:00
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
|
|
|
Expects(peerId != 0);
|
|
|
|
Expects(IsServerMsgId(aroundId) || (aroundId == 0));
|
|
|
|
Expects((aroundId != 0)
|
|
|
|
|| (limitBefore == 0 && limitAfter == 0));
|
2022-10-11 15:08:19 +00:00
|
|
|
Expects((query.peerId == peerId && query.topicRootId == topicRootId)
|
|
|
|
|| (query.migratedPeerId == peerId && MsgId(0) == topicRootId));
|
2017-10-31 07:03:18 +00:00
|
|
|
|
|
|
|
auto it = _cache.find(query);
|
|
|
|
if (it == _cache.end()) {
|
|
|
|
return [=](auto) { return rpl::lifetime(); };
|
|
|
|
}
|
2017-10-30 19:24:20 +00:00
|
|
|
|
2017-10-31 07:03:18 +00:00
|
|
|
auto listData = (peerId == query.peerId)
|
|
|
|
? &it->second->peerData
|
|
|
|
: &*it->second->migratedData;
|
2017-10-30 19:24:20 +00:00
|
|
|
return [=](auto consumer) {
|
|
|
|
auto lifetime = rpl::lifetime();
|
|
|
|
auto builder = lifetime.make_state<SparseIdsSliceBuilder>(
|
|
|
|
aroundId,
|
|
|
|
limitBefore,
|
|
|
|
limitAfter);
|
2017-12-22 07:05:20 +00:00
|
|
|
builder->insufficientAround(
|
|
|
|
) | rpl::start_with_next([=](
|
|
|
|
const SparseIdsSliceBuilder::AroundData &data) {
|
|
|
|
requestMore(data, query, listData);
|
|
|
|
}, lifetime);
|
2017-10-30 19:24:20 +00:00
|
|
|
|
|
|
|
auto pushNextSnapshot = [=] {
|
|
|
|
consumer.put_next(builder->snapshot());
|
|
|
|
};
|
|
|
|
|
2017-12-22 07:05:20 +00:00
|
|
|
listData->list.sliceUpdated(
|
|
|
|
) | rpl::filter([=](const SliceUpdate &update) {
|
|
|
|
return builder->applyUpdate(update);
|
|
|
|
}) | rpl::start_with_next(pushNextSnapshot, lifetime);
|
|
|
|
|
2020-06-08 09:06:50 +00:00
|
|
|
_session->data().itemRemoved(
|
2017-12-22 07:05:20 +00:00
|
|
|
) | rpl::filter([=](not_null<const HistoryItem*> item) {
|
2022-10-11 15:08:19 +00:00
|
|
|
return (item->history()->peer->id == peerId)
|
|
|
|
&& (!topicRootId || item->topicRootId() == topicRootId);
|
2017-12-22 07:05:20 +00:00
|
|
|
}) | rpl::filter([=](not_null<const HistoryItem*> item) {
|
|
|
|
return builder->removeOne(item->id);
|
|
|
|
}) | rpl::start_with_next(pushNextSnapshot, lifetime);
|
|
|
|
|
2020-06-08 09:06:50 +00:00
|
|
|
_session->data().historyCleared(
|
2017-12-22 07:05:20 +00:00
|
|
|
) | rpl::filter([=](not_null<const History*> history) {
|
|
|
|
return (history->peer->id == peerId);
|
|
|
|
}) | rpl::filter([=] {
|
|
|
|
return builder->removeAll();
|
|
|
|
}) | rpl::start_with_next(pushNextSnapshot, lifetime);
|
2017-10-30 19:24:20 +00:00
|
|
|
|
2017-10-31 07:03:18 +00:00
|
|
|
using Result = Storage::SparseIdsListResult;
|
|
|
|
listData->list.query(Storage::SparseIdsListQuery(
|
|
|
|
aroundId,
|
|
|
|
limitBefore,
|
2017-12-22 07:05:20 +00:00
|
|
|
limitAfter
|
|
|
|
)) | rpl::filter([=](const Result &result) {
|
|
|
|
return builder->applyInitial(result);
|
|
|
|
}) | rpl::start_with_next_done(
|
|
|
|
pushNextSnapshot,
|
|
|
|
[=] { builder->checkInsufficient(); },
|
|
|
|
lifetime);
|
2017-10-30 19:24:20 +00:00
|
|
|
|
|
|
|
return lifetime;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-03 15:47:08 +00:00
|
|
|
auto SearchController::saveState() -> SavedState {
|
|
|
|
auto result = SavedState();
|
|
|
|
if (_current != _cache.end()) {
|
|
|
|
result.query = _current->first;
|
|
|
|
result.peerList = std::move(_current->second->peerData.list);
|
|
|
|
if (auto &migrated = _current->second->migratedData) {
|
|
|
|
result.migratedList = std::move(migrated->list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchController::restoreState(SavedState &&state) {
|
|
|
|
if (!state.query.peerId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = _cache.find(state.query);
|
|
|
|
if (it == _cache.end()) {
|
|
|
|
it = _cache.emplace(
|
|
|
|
state.query,
|
2020-06-08 09:06:50 +00:00
|
|
|
std::make_unique<CacheEntry>(_session, state.query)).first;
|
2017-11-03 15:47:08 +00:00
|
|
|
}
|
|
|
|
auto replace = Data(it->second->peerData.peer);
|
|
|
|
replace.list = std::move(state.peerList);
|
|
|
|
it->second->peerData = std::move(replace);
|
|
|
|
if (auto &migrated = state.migratedList) {
|
|
|
|
Assert(it->second->migratedData.has_value());
|
|
|
|
auto replace = Data(it->second->migratedData->peer);
|
|
|
|
replace.list = std::move(*migrated);
|
|
|
|
it->second->migratedData = std::move(replace);
|
|
|
|
}
|
|
|
|
_current = it;
|
|
|
|
}
|
|
|
|
|
2017-10-31 07:03:18 +00:00
|
|
|
void SearchController::requestMore(
|
2017-10-30 19:24:20 +00:00
|
|
|
const SparseIdsSliceBuilder::AroundData &key,
|
2017-10-31 07:03:18 +00:00
|
|
|
const Query &query,
|
2017-10-30 19:24:20 +00:00
|
|
|
Data *listData) {
|
|
|
|
if (listData->requests.contains(key)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
auto prepared = PrepareSearchRequest(
|
2017-10-30 19:24:20 +00:00
|
|
|
listData->peer,
|
2022-10-11 15:08:19 +00:00
|
|
|
query.topicRootId,
|
2017-10-31 07:03:18 +00:00
|
|
|
query.type,
|
|
|
|
query.query,
|
2017-10-30 19:24:20 +00:00
|
|
|
key.aroundId,
|
2019-07-05 13:38:38 +00:00
|
|
|
key.direction);
|
|
|
|
if (!prepared) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-21 11:51:37 +00:00
|
|
|
auto &histories = _session->data().histories();
|
2020-02-21 12:57:06 +00:00
|
|
|
const auto type = ::Data::Histories::RequestType::History;
|
2020-02-21 11:51:37 +00:00
|
|
|
const auto history = _session->data().history(listData->peer);
|
|
|
|
auto requestId = histories.sendRequest(history, type, [=](Fn<void()> finish) {
|
2020-02-25 12:12:04 +00:00
|
|
|
return _session->api().request(
|
2020-02-21 11:51:37 +00:00
|
|
|
std::move(*prepared)
|
2022-05-16 09:20:31 +00:00
|
|
|
).done([=](const SearchRequestResult &result) {
|
2020-02-21 11:51:37 +00:00
|
|
|
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();
|
2021-11-26 20:46:53 +00:00
|
|
|
}).fail([=] {
|
2020-02-21 11:51:37 +00:00
|
|
|
finish();
|
|
|
|
}).send();
|
|
|
|
});
|
2017-10-31 07:03:18 +00:00
|
|
|
listData->requests.emplace(key, [=] {
|
2020-02-21 12:57:06 +00:00
|
|
|
_session->data().histories().cancelRequest(requestId);
|
2017-10-31 07:03:18 +00:00
|
|
|
});
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 08:02:56 +00:00
|
|
|
DelayedSearchController::DelayedSearchController(
|
|
|
|
not_null<Main::Session*> session)
|
|
|
|
: _controller(session) {
|
2017-10-30 19:24:20 +00:00
|
|
|
_timer.setCallback([this] { setQueryFast(_nextQuery); });
|
|
|
|
}
|
|
|
|
|
|
|
|
void DelayedSearchController::setQuery(const Query &query) {
|
2017-10-31 07:03:18 +00:00
|
|
|
setQuery(query, kDefaultSearchTimeoutMs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DelayedSearchController::setQuery(
|
|
|
|
const Query &query,
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time delay) {
|
2017-11-03 15:47:08 +00:00
|
|
|
if (currentQuery() == query) {
|
|
|
|
_timer.cancel();
|
|
|
|
return;
|
|
|
|
}
|
2017-10-31 07:03:18 +00:00
|
|
|
if (_controller.hasInCache(query)) {
|
|
|
|
setQueryFast(query);
|
|
|
|
} else {
|
|
|
|
_nextQuery = query;
|
|
|
|
_timer.callOnce(delay);
|
|
|
|
}
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DelayedSearchController::setQueryFast(const Query &query) {
|
|
|
|
_controller.setQuery(query);
|
2017-11-14 17:22:44 +00:00
|
|
|
_currentQueryChanges.fire_copy(query.query);
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Api
|