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
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "mtproto/sender.h"
|
2017-11-06 07:31:18 +00:00
|
|
|
#include "data/data_sparse_ids.h"
|
2017-10-30 19:24:20 +00:00
|
|
|
#include "storage/storage_sparse_ids_list.h"
|
|
|
|
#include "storage/storage_shared_media.h"
|
2017-10-31 07:03:18 +00:00
|
|
|
#include "base/value_ordering.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "base/timer.h"
|
2017-10-30 19:24:20 +00:00
|
|
|
|
2018-01-09 17:08:31 +00:00
|
|
|
namespace Data {
|
|
|
|
enum class LoadDirection : char;
|
|
|
|
} // namespace Data
|
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
namespace Api {
|
|
|
|
|
|
|
|
struct SearchResult {
|
|
|
|
std::vector<MsgId> messageIds;
|
|
|
|
MsgRange noSkipRange;
|
|
|
|
int fullCount = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
MTPmessages_Search PrepareSearchRequest(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
Storage::SharedMediaType type,
|
|
|
|
const QString &query,
|
|
|
|
MsgId messageId,
|
2018-01-09 17:08:31 +00:00
|
|
|
Data::LoadDirection direction);
|
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,
|
2017-10-30 19:24:20 +00:00
|
|
|
const MTPmessages_Messages &data);
|
|
|
|
|
2017-10-31 07:03:18 +00:00
|
|
|
class SearchController : private MTP::Sender {
|
2017-10-30 19:24:20 +00:00
|
|
|
public:
|
2017-11-03 15:47:08 +00:00
|
|
|
using IdsList = Storage::SparseIdsList;
|
2017-10-30 19:24:20 +00:00
|
|
|
struct Query {
|
|
|
|
using MediaType = Storage::SharedMediaType;
|
|
|
|
|
|
|
|
PeerId peerId = 0;
|
|
|
|
PeerId migratedPeerId = 0;
|
|
|
|
MediaType type = MediaType::kCount;
|
|
|
|
QString query;
|
|
|
|
// from_id, min_date, max_date
|
2017-10-31 07:03:18 +00:00
|
|
|
|
|
|
|
friend inline auto value_ordering_helper(const Query &value) {
|
|
|
|
return std::tie(
|
|
|
|
value.peerId,
|
|
|
|
value.migratedPeerId,
|
|
|
|
value.type,
|
|
|
|
value.query);
|
|
|
|
}
|
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
};
|
2017-11-03 15:47:08 +00:00
|
|
|
struct SavedState {
|
|
|
|
Query query;
|
|
|
|
IdsList peerList;
|
|
|
|
base::optional<IdsList> migratedList;
|
|
|
|
};
|
2017-10-30 19:24:20 +00:00
|
|
|
|
2017-10-31 07:03:18 +00:00
|
|
|
void setQuery(const Query &query);
|
|
|
|
bool hasInCache(const Query &query) const;
|
|
|
|
|
|
|
|
Query query() const {
|
|
|
|
Expects(_current != _cache.cend());
|
|
|
|
return _current->first;
|
|
|
|
}
|
2017-10-30 19:24:20 +00:00
|
|
|
|
|
|
|
rpl::producer<SparseIdsMergedSlice> idsSlice(
|
|
|
|
SparseIdsMergedSlice::UniversalMsgId aroundId,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter);
|
|
|
|
|
2017-11-03 15:47:08 +00:00
|
|
|
SavedState saveState();
|
|
|
|
void restoreState(SavedState &&state);
|
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
private:
|
|
|
|
struct Data {
|
|
|
|
explicit Data(not_null<PeerData*> peer) : peer(peer) {
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<PeerData*> peer;
|
2017-11-03 15:47:08 +00:00
|
|
|
IdsList list;
|
2017-10-30 19:24:20 +00:00
|
|
|
base::flat_map<
|
|
|
|
SparseIdsSliceBuilder::AroundData,
|
2017-10-31 07:03:18 +00:00
|
|
|
rpl::lifetime> requests;
|
2017-10-30 19:24:20 +00:00
|
|
|
};
|
|
|
|
using SliceUpdate = Storage::SparseIdsSliceUpdate;
|
|
|
|
|
2017-10-31 07:03:18 +00:00
|
|
|
struct CacheEntry {
|
|
|
|
CacheEntry(const Query &query);
|
|
|
|
|
|
|
|
Data peerData;
|
|
|
|
base::optional<Data> migratedData;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CacheLess {
|
|
|
|
inline bool operator()(const Query &a, const Query &b) const {
|
|
|
|
return (a < b);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
using Cache = base::flat_map<
|
|
|
|
Query,
|
|
|
|
std::unique_ptr<CacheEntry>,
|
|
|
|
CacheLess>;
|
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
rpl::producer<SparseIdsSlice> simpleIdsSlice(
|
|
|
|
PeerId peerId,
|
|
|
|
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);
|
|
|
|
void requestMore(
|
|
|
|
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);
|
|
|
|
|
2017-10-31 07:03:18 +00:00
|
|
|
Cache _cache;
|
|
|
|
Cache::iterator _current = _cache.end();
|
2017-10-30 19:24:20 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class DelayedSearchController {
|
|
|
|
public:
|
|
|
|
DelayedSearchController();
|
|
|
|
|
2017-10-31 07:03:18 +00:00
|
|
|
using Query = SearchController::Query;
|
2017-11-03 15:47:08 +00:00
|
|
|
using SavedState = SearchController::SavedState;
|
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
void setQuery(const Query &query);
|
2017-10-31 07:03:18 +00:00
|
|
|
void setQuery(const Query &query, TimeMs delay);
|
2017-10-30 19:24:20 +00:00
|
|
|
void setQueryFast(const Query &query);
|
|
|
|
|
|
|
|
Query currentQuery() const {
|
|
|
|
return _controller.query();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<SparseIdsMergedSlice> idsSlice(
|
|
|
|
SparseIdsMergedSlice::UniversalMsgId aroundId,
|
|
|
|
int limitBefore,
|
|
|
|
int limitAfter) {
|
|
|
|
return _controller.idsSlice(
|
|
|
|
aroundId,
|
|
|
|
limitBefore,
|
|
|
|
limitAfter);
|
|
|
|
}
|
|
|
|
|
2017-11-14 17:22:44 +00:00
|
|
|
rpl::producer<QString> currentQueryValue() const {
|
|
|
|
return _currentQueryChanges.events_starting_with(
|
|
|
|
currentQuery().query);
|
2017-10-30 19:24:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 15:47:08 +00:00
|
|
|
SavedState saveState() {
|
|
|
|
return _controller.saveState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void restoreState(SavedState &&state) {
|
|
|
|
_controller.restoreState(std::move(state));
|
|
|
|
}
|
|
|
|
|
2017-10-30 19:24:20 +00:00
|
|
|
private:
|
|
|
|
SearchController _controller;
|
|
|
|
Query _nextQuery;
|
|
|
|
base::Timer _timer;
|
2017-11-14 17:22:44 +00:00
|
|
|
rpl::event_stream<QString> _currentQueryChanges;
|
2017-10-30 19:24:20 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Api
|