2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/telegram/DialogId.h"
|
2021-10-21 11:51:16 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2020-01-27 01:19:54 +01:00
|
|
|
#include "td/telegram/TopDialogCategory.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/actor/actor.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/common.h"
|
2021-10-21 11:51:16 +02:00
|
|
|
#include "td/utils/Status.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2019-01-06 22:06:52 +01:00
|
|
|
|
2021-09-24 22:17:38 +02:00
|
|
|
class Td;
|
|
|
|
|
2021-09-25 20:43:59 +02:00
|
|
|
class TopDialogManager final : public Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-21 11:51:16 +02:00
|
|
|
TopDialogManager(Td *td, ActorShared<> parent);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-24 22:17:38 +02:00
|
|
|
void init();
|
2019-03-05 05:35:17 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void on_dialog_used(TopDialogCategory category, DialogId dialog_id, int32 date);
|
|
|
|
|
2021-09-25 10:37:32 +02:00
|
|
|
void remove_dialog(TopDialogCategory category, DialogId dialog_id, Promise<Unit> &&promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-25 10:37:32 +02:00
|
|
|
void get_top_dialogs(TopDialogCategory category, int32 limit, Promise<vector<DialogId>> promise);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void update_rating_e_decay();
|
|
|
|
|
2018-06-26 21:51:00 +02:00
|
|
|
void update_is_enabled(bool is_enabled);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
static constexpr size_t MAX_TOP_DIALOGS_LIMIT = 30;
|
2018-06-26 21:51:00 +02:00
|
|
|
static constexpr int32 SERVER_SYNC_DELAY = 86400; // seconds
|
|
|
|
static constexpr int32 SERVER_SYNC_RESEND_DELAY = 60; // seconds
|
|
|
|
static constexpr int32 DB_SYNC_DELAY = 5; // seconds
|
2021-09-24 22:17:38 +02:00
|
|
|
|
|
|
|
Td *td_;
|
2018-12-31 20:04:05 +01:00
|
|
|
ActorShared<> parent_;
|
|
|
|
|
2021-09-25 20:27:32 +02:00
|
|
|
bool is_active_ = false;
|
|
|
|
bool is_enabled_ = true;
|
2018-06-26 21:51:00 +02:00
|
|
|
int32 rating_e_decay_ = 241920;
|
|
|
|
|
|
|
|
bool have_toggle_top_peers_query_ = false;
|
|
|
|
bool have_pending_toggle_top_peers_query_ = false;
|
|
|
|
bool pending_toggle_top_peers_query_ = false;
|
2018-12-31 20:04:05 +01:00
|
|
|
bool was_first_sync_{false};
|
2018-04-19 15:08:30 +02:00
|
|
|
enum class SyncState : int32 { None, Pending, Ok };
|
2018-12-31 20:04:05 +01:00
|
|
|
SyncState db_sync_state_ = SyncState::None;
|
|
|
|
Timestamp first_unsync_change_;
|
|
|
|
SyncState server_sync_state_ = SyncState::None;
|
|
|
|
Timestamp last_server_sync_;
|
|
|
|
|
|
|
|
struct GetTopDialogsQuery {
|
|
|
|
TopDialogCategory category;
|
|
|
|
size_t limit;
|
|
|
|
Promise<vector<DialogId>> promise;
|
|
|
|
};
|
2021-10-21 11:51:16 +02:00
|
|
|
vector<GetTopDialogsQuery> pending_get_top_dialogs_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
struct TopDialog {
|
|
|
|
DialogId dialog_id;
|
|
|
|
double rating = 0;
|
|
|
|
bool operator<(const TopDialog &other) const {
|
|
|
|
return std::make_pair(-rating, dialog_id.get()) < std::make_pair(-other.rating, other.dialog_id.get());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TopDialogs {
|
|
|
|
bool is_dirty = false;
|
|
|
|
double rating_timestamp = 0;
|
2021-10-21 11:51:16 +02:00
|
|
|
vector<TopDialog> dialogs;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class StorerT>
|
|
|
|
friend void store(const TopDialog &top_dialog, StorerT &storer);
|
|
|
|
template <class ParserT>
|
|
|
|
friend void parse(TopDialog &top_dialog, ParserT &parser);
|
|
|
|
template <class StorerT>
|
|
|
|
friend void store(const TopDialogs &top_dialogs, StorerT &storer);
|
|
|
|
template <class ParserT>
|
|
|
|
friend void parse(TopDialogs &top_dialogs, ParserT &parser);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
std::array<TopDialogs, static_cast<size_t>(TopDialogCategory::Size)> by_category_;
|
|
|
|
|
|
|
|
double rating_add(double now, double rating_timestamp) const;
|
|
|
|
double current_rating_add(double rating_timestamp) const;
|
|
|
|
void normalize_rating();
|
|
|
|
|
2018-06-26 21:51:00 +02:00
|
|
|
bool set_is_enabled(bool is_enabled);
|
2021-09-25 19:33:51 +02:00
|
|
|
|
2018-06-26 21:51:00 +02:00
|
|
|
void send_toggle_top_peers(bool is_enabled);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-25 19:33:51 +02:00
|
|
|
void on_toggle_top_peers(bool is_enabled, Result<Unit> &&result);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void do_get_top_dialogs(GetTopDialogsQuery &&query);
|
|
|
|
|
2021-09-25 21:33:55 +02:00
|
|
|
void on_load_dialogs(GetTopDialogsQuery &&query, vector<DialogId> &&dialog_ids);
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void do_get_top_peers();
|
2021-09-25 21:33:55 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void do_save_top_dialogs();
|
|
|
|
|
|
|
|
void on_first_sync();
|
|
|
|
|
2021-09-25 20:43:59 +02:00
|
|
|
void on_get_top_peers(Result<telegram_api::object_ptr<telegram_api::contacts_TopPeers>> result);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-24 22:17:38 +02:00
|
|
|
void try_start();
|
2018-06-26 21:51:00 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final;
|
2021-09-24 22:17:38 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final;
|
2021-09-24 22:17:38 +02:00
|
|
|
|
|
|
|
void tear_down() final;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2019-01-06 22:06:52 +01:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|