2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 14:38:15 +00:00

Add data component for RecentPeers.

This commit is contained in:
John Preston
2024-04-11 18:30:14 +04:00
parent e24ab4f1ab
commit 2e0529bd9a
4 changed files with 126 additions and 3 deletions

View File

@@ -7,7 +7,17 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "data/components/recent_peers.h"
#include "main/main_session.h"
#include "storage/serialize_common.h"
#include "storage/serialize_peer.h"
#include "storage/storage_account.h"
namespace Data {
namespace {
constexpr auto kLimit = 48;
} // namespace
RecentPeers::RecentPeers(not_null<Main::Session*> session)
: _session(session) {
@@ -15,4 +25,98 @@ RecentPeers::RecentPeers(not_null<Main::Session*> session)
RecentPeers::~RecentPeers() = default;
const std::vector<not_null<PeerData*>> &RecentPeers::list() const {
_session->local().readSearchSuggestions();
return _list;
}
rpl::producer<> RecentPeers::updates() const {
return _updates.events();
}
void RecentPeers::remove(not_null<PeerData*> peer) {
const auto i = ranges::find(_list, peer);
if (i != end(_list)) {
_list.erase(i);
_updates.fire({});
}
_session->local().writeSearchSuggestionsDelayed();
}
void RecentPeers::bump(not_null<PeerData*> peer) {
_session->local().readSearchSuggestions();
if (!_list.empty() && _list.front() == peer) {
return;
}
auto i = ranges::find(_list, peer);
if (i == end(_list)) {
_list.push_back(peer);
i = end(_list) - 1;
}
ranges::rotate(begin(_list), i, i + 1);
_updates.fire({});
_session->local().writeSearchSuggestionsDelayed();
}
void RecentPeers::clear() {
_session->local().readSearchSuggestions();
_list.clear();
_updates.fire({});
_session->local().writeSearchSuggestionsDelayed();
}
QByteArray RecentPeers::serialize() const {
_session->local().readSearchSuggestions();
if (_list.empty()) {
return {};
}
auto size = 2 * sizeof(quint32); // AppVersion, count
const auto count = std::min(int(_list.size()), kLimit);
auto &&list = _list | ranges::views::take(count);
for (const auto &peer : list) {
size += Serialize::peerSize(peer);
}
auto stream = Serialize::ByteArrayWriter(size);
stream
<< quint32(AppVersion)
<< quint32(_list.size());
for (const auto &peer : list) {
Serialize::writePeer(stream, peer);
}
return std::move(stream).result();
}
void RecentPeers::applyLocal(QByteArray serialized) {
_list.clear();
if (serialized.isEmpty()) {
return;
}
auto stream = Serialize::ByteArrayReader(serialized);
auto streamAppVersion = quint32();
auto count = quint32();
stream >> streamAppVersion >> count;
if (!stream.ok()) {
return;
}
_list.reserve(count);
for (auto i = 0; i != int(count); ++i) {
const auto peer = Serialize::readPeer(
_session,
streamAppVersion,
stream);
if (stream.ok() && peer) {
_list.push_back(peer);
} else {
_list.clear();
return;
}
}
}
} // namespace Data

View File

@@ -18,9 +18,22 @@ public:
explicit RecentPeers(not_null<Main::Session*> session);
~RecentPeers();
[[nodiscard]] const std::vector<not_null<PeerData*>> &list() const;
[[nodiscard]] rpl::producer<> updates() const;
void remove(not_null<PeerData*> peer);
void bump(not_null<PeerData*> peer);
void clear();
[[nodiscard]] QByteArray serialize() const;
void applyLocal(QByteArray serialized);
private:
const not_null<Main::Session*> _session;
std::vector<not_null<PeerData*>> _list;
rpl::event_stream<> _updates;
};
} // namespace Data

View File

@@ -243,7 +243,12 @@ QByteArray TopPeers::serialize() const {
}
void TopPeers::applyLocal(QByteArray serialized) {
if (_lastReceived || serialized.isEmpty()) {
if (_lastReceived) {
return;
}
_list.clear();
_disabled = false;
if (serialized.isEmpty()) {
return;
}
auto stream = Serialize::ByteArrayReader(serialized);