2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
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)
|
|
|
|
//
|
|
|
|
#include "td/telegram/TopDialogManager.h"
|
|
|
|
|
2021-09-25 10:37:32 +02:00
|
|
|
#include "td/telegram/AccessRights.h"
|
2018-12-12 16:38:20 +01:00
|
|
|
#include "td/telegram/AuthManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
|
|
|
#include "td/telegram/DialogId.h"
|
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/logevent/LogEvent.h"
|
|
|
|
#include "td/telegram/MessagesManager.h"
|
|
|
|
#include "td/telegram/misc.h"
|
|
|
|
#include "td/telegram/net/NetQuery.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
|
|
|
#include "td/telegram/StateManager.h"
|
|
|
|
#include "td/telegram/Td.h"
|
2019-01-06 20:59:17 +01:00
|
|
|
#include "td/telegram/TdDb.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2021-01-01 13:59:53 +01:00
|
|
|
#include "td/utils/algorithm.h"
|
2021-09-25 20:27:32 +02:00
|
|
|
#include "td/utils/buffer.h"
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/logging.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/port/Clocks.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/tl_helpers.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2021-09-25 20:43:59 +02:00
|
|
|
class GetTopPeersQuery final : public Td::ResultHandler {
|
|
|
|
Promise<telegram_api::object_ptr<telegram_api::contacts_TopPeers>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetTopPeersQuery(Promise<telegram_api::object_ptr<telegram_api::contacts_TopPeers>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(int64 hash) {
|
|
|
|
int32 flags =
|
|
|
|
telegram_api::contacts_getTopPeers::CORRESPONDENTS_MASK | telegram_api::contacts_getTopPeers::BOTS_PM_MASK |
|
|
|
|
telegram_api::contacts_getTopPeers::BOTS_INLINE_MASK | telegram_api::contacts_getTopPeers::GROUPS_MASK |
|
|
|
|
telegram_api::contacts_getTopPeers::CHANNELS_MASK | telegram_api::contacts_getTopPeers::PHONE_CALLS_MASK |
|
|
|
|
telegram_api::contacts_getTopPeers::FORWARD_USERS_MASK | telegram_api::contacts_getTopPeers::FORWARD_CHATS_MASK;
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::contacts_getTopPeers(
|
|
|
|
flags, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/, false /*ignored*/,
|
|
|
|
false /*ignored*/, false /*ignored*/, false /*ignored*/, 0 /*offset*/, 100 /*limit*/, hash)));
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2021-09-25 20:43:59 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::contacts_getTopPeers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2021-09-25 20:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(result_ptr.move_as_ok());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-09-25 20:43:59 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-25 19:33:51 +02:00
|
|
|
class ToggleTopPeersQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit ToggleTopPeersQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(bool is_enabled) {
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::contacts_toggleTopPeers(is_enabled)));
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2021-09-25 19:33:51 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::contacts_toggleTopPeers>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2021-09-25 19:33:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-09-25 19:33:51 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-25 20:12:36 +02:00
|
|
|
class ResetTopPeerRatingQuery final : public Td::ResultHandler {
|
|
|
|
DialogId dialog_id_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
void send(TopDialogCategory category, DialogId dialog_id) {
|
2021-11-08 13:20:38 +01:00
|
|
|
auto input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Read);
|
2021-09-25 20:12:36 +02:00
|
|
|
if (input_peer == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_id_ = dialog_id;
|
|
|
|
send_query(G()->net_query_creator().create(
|
2021-09-25 20:27:32 +02:00
|
|
|
telegram_api::contacts_resetTopPeerRating(get_input_top_peer_category(category), std::move(input_peer))));
|
2021-09-25 20:12:36 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2021-09-25 20:12:36 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::contacts_resetTopPeerRating>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2021-09-25 20:12:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ignore the result
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-11-08 13:20:38 +01:00
|
|
|
if (!td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "ResetTopPeerRatingQuery")) {
|
2021-10-21 11:51:16 +02:00
|
|
|
LOG(INFO) << "Receive error for ResetTopPeerRatingQuery: " << status;
|
2021-09-25 20:12:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-21 11:51:16 +02:00
|
|
|
TopDialogManager::TopDialogManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
2018-06-26 21:51:00 +02:00
|
|
|
void TopDialogManager::update_is_enabled(bool is_enabled) {
|
2021-09-24 22:17:38 +02:00
|
|
|
if (td_->auth_manager_ == nullptr || !td_->auth_manager_->is_authorized() || td_->auth_manager_->is_bot()) {
|
2019-03-05 23:06:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-26 21:51:00 +02:00
|
|
|
if (set_is_enabled(is_enabled)) {
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("top_peers_enabled", is_enabled ? "1" : "0");
|
|
|
|
send_toggle_top_peers(is_enabled);
|
|
|
|
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TopDialogManager::set_is_enabled(bool is_enabled) {
|
|
|
|
if (is_enabled_ == is_enabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(DEBUG) << "Change top chats is_enabled to " << is_enabled;
|
|
|
|
is_enabled_ = is_enabled;
|
2021-09-24 22:17:38 +02:00
|
|
|
try_start();
|
2018-06-26 21:51:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopDialogManager::send_toggle_top_peers(bool is_enabled) {
|
2021-09-25 19:33:51 +02:00
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-26 21:51:00 +02:00
|
|
|
if (have_toggle_top_peers_query_) {
|
|
|
|
have_pending_toggle_top_peers_query_ = true;
|
|
|
|
pending_toggle_top_peers_query_ = is_enabled;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(DEBUG) << "Send toggle top peers query to " << is_enabled;
|
|
|
|
have_toggle_top_peers_query_ = true;
|
2021-09-25 19:33:51 +02:00
|
|
|
|
|
|
|
auto promise = PromiseCreator::lambda([actor_id = actor_id(this), is_enabled](Result<Unit> result) {
|
|
|
|
send_closure(actor_id, &TopDialogManager::on_toggle_top_peers, is_enabled, std::move(result));
|
|
|
|
});
|
|
|
|
td_->create_handler<ToggleTopPeersQuery>(std::move(promise))->send(is_enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopDialogManager::on_toggle_top_peers(bool is_enabled, Result<Unit> &&result) {
|
|
|
|
CHECK(have_toggle_top_peers_query_);
|
|
|
|
have_toggle_top_peers_query_ = false;
|
|
|
|
|
|
|
|
if (have_pending_toggle_top_peers_query_) {
|
|
|
|
have_pending_toggle_top_peers_query_ = false;
|
|
|
|
if (pending_toggle_top_peers_query_ != is_enabled) {
|
|
|
|
send_toggle_top_peers(pending_toggle_top_peers_query_);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.is_ok()) {
|
|
|
|
// everything is synchronized
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase("top_peers_enabled");
|
|
|
|
} else {
|
|
|
|
// let's resend the query forever
|
|
|
|
send_toggle_top_peers(is_enabled);
|
|
|
|
}
|
2021-09-29 19:23:39 +02:00
|
|
|
loop();
|
2018-06-26 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void TopDialogManager::on_dialog_used(TopDialogCategory category, DialogId dialog_id, int32 date) {
|
2018-06-26 21:51:00 +02:00
|
|
|
if (!is_active_ || !is_enabled_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto pos = static_cast<size_t>(category);
|
|
|
|
CHECK(pos < by_category_.size());
|
|
|
|
auto &top_dialogs = by_category_[pos];
|
|
|
|
|
|
|
|
top_dialogs.is_dirty = true;
|
|
|
|
auto it = std::find_if(top_dialogs.dialogs.begin(), top_dialogs.dialogs.end(),
|
|
|
|
[&](auto &top_dialog) { return top_dialog.dialog_id == dialog_id; });
|
|
|
|
if (it == top_dialogs.dialogs.end()) {
|
|
|
|
TopDialog top_dialog;
|
|
|
|
top_dialog.dialog_id = dialog_id;
|
|
|
|
top_dialogs.dialogs.push_back(top_dialog);
|
|
|
|
it = top_dialogs.dialogs.end() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto delta = rating_add(date, top_dialogs.rating_timestamp);
|
|
|
|
it->rating += delta;
|
|
|
|
while (it != top_dialogs.dialogs.begin()) {
|
|
|
|
auto next = std::prev(it);
|
|
|
|
if (*next < *it) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::swap(*next, *it);
|
|
|
|
it = next;
|
|
|
|
}
|
|
|
|
|
2021-09-25 20:27:32 +02:00
|
|
|
LOG(INFO) << "Update " << get_top_dialog_category_name(category) << " rating of " << dialog_id << " by " << delta;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
if (!first_unsync_change_) {
|
|
|
|
first_unsync_change_ = Timestamp::now_cached();
|
|
|
|
}
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2021-09-25 10:37:32 +02:00
|
|
|
void TopDialogManager::remove_dialog(TopDialogCategory category, DialogId dialog_id, Promise<Unit> &&promise) {
|
|
|
|
if (category == TopDialogCategory::Size) {
|
|
|
|
return promise.set_error(Status::Error(400, "Top chat category must be non-empty"));
|
|
|
|
}
|
|
|
|
if (!td_->messages_manager_->have_dialog_force(dialog_id, "remove_dialog")) {
|
|
|
|
return promise.set_error(Status::Error(400, "Chat not found"));
|
|
|
|
}
|
2018-06-26 21:51:00 +02:00
|
|
|
if (!is_active_ || !is_enabled_) {
|
2021-09-25 10:37:32 +02:00
|
|
|
return promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2019-09-03 16:13:03 +02:00
|
|
|
|
|
|
|
if (category == TopDialogCategory::ForwardUsers && dialog_id.get_type() != DialogType::User) {
|
|
|
|
category = TopDialogCategory::ForwardChats;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto pos = static_cast<size_t>(category);
|
|
|
|
CHECK(pos < by_category_.size());
|
|
|
|
auto &top_dialogs = by_category_[pos];
|
|
|
|
|
2021-09-25 20:12:36 +02:00
|
|
|
td_->create_handler<ResetTopPeerRatingQuery>()->send(category, dialog_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto it = std::find_if(top_dialogs.dialogs.begin(), top_dialogs.dialogs.end(),
|
|
|
|
[&](auto &top_dialog) { return top_dialog.dialog_id == dialog_id; });
|
|
|
|
if (it == top_dialogs.dialogs.end()) {
|
2021-09-25 10:37:32 +02:00
|
|
|
return promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
top_dialogs.is_dirty = true;
|
|
|
|
top_dialogs.dialogs.erase(it);
|
|
|
|
if (!first_unsync_change_) {
|
|
|
|
first_unsync_change_ = Timestamp::now_cached();
|
|
|
|
}
|
|
|
|
loop();
|
2021-09-25 10:37:32 +02:00
|
|
|
promise.set_value(Unit());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2023-04-14 12:38:25 +02:00
|
|
|
void TopDialogManager::get_top_dialogs(TopDialogCategory category, int32 limit,
|
|
|
|
Promise<td_api::object_ptr<td_api::chats>> &&promise) {
|
2021-09-25 10:37:32 +02:00
|
|
|
if (category == TopDialogCategory::Size) {
|
|
|
|
return promise.set_error(Status::Error(400, "Top chat category must be non-empty"));
|
|
|
|
}
|
|
|
|
if (limit <= 0) {
|
|
|
|
return promise.set_error(Status::Error(400, "Limit must be positive"));
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!is_active_) {
|
2021-09-25 10:37:32 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Not supported without chat info database"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-06-26 21:51:00 +02:00
|
|
|
if (!is_enabled_) {
|
2021-09-25 10:37:32 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Top chats computation is disabled"));
|
2018-06-26 21:51:00 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
GetTopDialogsQuery query;
|
|
|
|
query.category = category;
|
2021-09-25 10:37:32 +02:00
|
|
|
query.limit = static_cast<size_t>(limit);
|
2018-12-31 20:04:05 +01:00
|
|
|
query.promise = std::move(promise);
|
|
|
|
pending_get_top_dialogs_.push_back(std::move(query));
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopDialogManager::update_rating_e_decay() {
|
|
|
|
if (!is_active_) {
|
|
|
|
return;
|
|
|
|
}
|
2022-08-17 15:11:13 +02:00
|
|
|
rating_e_decay_ = narrow_cast<int32>(G()->get_option_integer("rating_e_decay", rating_e_decay_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(const TopDialogManager::TopDialog &top_dialog, StorerT &storer) {
|
2018-12-31 20:04:05 +01:00
|
|
|
using ::td::store;
|
|
|
|
store(top_dialog.dialog_id, storer);
|
|
|
|
store(top_dialog.rating, storer);
|
|
|
|
}
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class ParserT>
|
|
|
|
void parse(TopDialogManager::TopDialog &top_dialog, ParserT &parser) {
|
2018-12-31 20:04:05 +01:00
|
|
|
using ::td::parse;
|
2019-02-21 18:54:20 +01:00
|
|
|
parse(top_dialog.dialog_id, parser);
|
|
|
|
parse(top_dialog.rating, parser);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class StorerT>
|
|
|
|
void store(const TopDialogManager::TopDialogs &top_dialogs, StorerT &storer) {
|
2018-12-31 20:04:05 +01:00
|
|
|
using ::td::store;
|
|
|
|
store(top_dialogs.rating_timestamp, storer);
|
|
|
|
store(top_dialogs.dialogs, storer);
|
|
|
|
}
|
|
|
|
|
2019-02-21 18:54:20 +01:00
|
|
|
template <class ParserT>
|
|
|
|
void parse(TopDialogManager::TopDialogs &top_dialogs, ParserT &parser) {
|
|
|
|
using ::td::parse;
|
|
|
|
parse(top_dialogs.rating_timestamp, parser);
|
|
|
|
parse(top_dialogs.dialogs, parser);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
double TopDialogManager::rating_add(double now, double rating_timestamp) const {
|
|
|
|
return std::exp((now - rating_timestamp) / rating_e_decay_);
|
|
|
|
}
|
|
|
|
|
|
|
|
double TopDialogManager::current_rating_add(double rating_timestamp) const {
|
|
|
|
return rating_add(G()->server_time_cached(), rating_timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopDialogManager::normalize_rating() {
|
|
|
|
for (auto &top_dialogs : by_category_) {
|
|
|
|
auto div_by = current_rating_add(top_dialogs.rating_timestamp);
|
|
|
|
top_dialogs.rating_timestamp = G()->server_time_cached();
|
|
|
|
for (auto &dialog : top_dialogs.dialogs) {
|
|
|
|
dialog.rating /= div_by;
|
|
|
|
}
|
|
|
|
top_dialogs.is_dirty = true;
|
|
|
|
}
|
|
|
|
db_sync_state_ = SyncState::None;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopDialogManager::do_get_top_dialogs(GetTopDialogsQuery &&query) {
|
2019-09-03 16:13:03 +02:00
|
|
|
vector<DialogId> dialog_ids;
|
|
|
|
if (query.category != TopDialogCategory::ForwardUsers) {
|
|
|
|
auto pos = static_cast<size_t>(query.category);
|
|
|
|
CHECK(pos < by_category_.size());
|
|
|
|
dialog_ids = transform(by_category_[pos].dialogs, [](const auto &x) { return x.dialog_id; });
|
|
|
|
} else {
|
|
|
|
// merge ForwardUsers and ForwardChats
|
|
|
|
auto &users = by_category_[static_cast<size_t>(TopDialogCategory::ForwardUsers)];
|
|
|
|
auto &chats = by_category_[static_cast<size_t>(TopDialogCategory::ForwardChats)];
|
|
|
|
size_t users_pos = 0;
|
|
|
|
size_t chats_pos = 0;
|
|
|
|
while (users_pos < users.dialogs.size() || chats_pos < chats.dialogs.size()) {
|
|
|
|
if (chats_pos == chats.dialogs.size() ||
|
|
|
|
(users_pos < users.dialogs.size() && users.dialogs[users_pos] < chats.dialogs[chats_pos])) {
|
|
|
|
dialog_ids.push_back(users.dialogs[users_pos++].dialog_id);
|
|
|
|
} else {
|
|
|
|
dialog_ids.push_back(chats.dialogs[chats_pos++].dialog_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-26 18:38:27 +02:00
|
|
|
auto promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this), query = std::move(query)](Result<vector<DialogId>> r_dialog_ids) mutable {
|
|
|
|
if (r_dialog_ids.is_error()) {
|
|
|
|
return query.promise.set_error(r_dialog_ids.move_as_error());
|
|
|
|
}
|
|
|
|
send_closure(actor_id, &TopDialogManager::on_load_dialogs, std::move(query), r_dialog_ids.move_as_ok());
|
2021-09-25 21:33:55 +02:00
|
|
|
});
|
2022-09-03 09:21:04 +02:00
|
|
|
send_closure(td_->messages_manager_actor_, &MessagesManager::load_dialogs, std::move(dialog_ids), std::move(promise));
|
2021-09-25 21:33:55 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-25 21:33:55 +02:00
|
|
|
void TopDialogManager::on_load_dialogs(GetTopDialogsQuery &&query, vector<DialogId> &&dialog_ids) {
|
|
|
|
auto limit = std::min({query.limit, MAX_TOP_DIALOGS_LIMIT, dialog_ids.size()});
|
|
|
|
vector<DialogId> result;
|
|
|
|
result.reserve(limit);
|
|
|
|
for (auto dialog_id : dialog_ids) {
|
|
|
|
if (dialog_id.get_type() == DialogType::User) {
|
|
|
|
auto user_id = dialog_id.get_user_id();
|
|
|
|
if (td_->contacts_manager_->is_user_deleted(user_id)) {
|
|
|
|
LOG(INFO) << "Skip deleted " << user_id;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (td_->contacts_manager_->get_my_id() == user_id) {
|
|
|
|
LOG(INFO) << "Skip self " << user_id;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (query.category == TopDialogCategory::BotInline || query.category == TopDialogCategory::BotPM) {
|
|
|
|
auto r_bot_info = td_->contacts_manager_->get_bot_data(user_id);
|
|
|
|
if (r_bot_info.is_error()) {
|
|
|
|
LOG(INFO) << "Skip not a bot " << user_id;
|
2018-03-08 16:21:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-09-25 21:33:55 +02:00
|
|
|
if (query.category == TopDialogCategory::BotInline &&
|
|
|
|
(r_bot_info.ok().username.empty() || !r_bot_info.ok().is_inline)) {
|
|
|
|
LOG(INFO) << "Skip not inline bot " << user_id;
|
2018-03-08 16:21:40 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-25 21:33:55 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-25 21:33:55 +02:00
|
|
|
result.push_back(dialog_id);
|
|
|
|
if (result.size() == limit) {
|
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-25 21:33:55 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2023-04-14 12:38:25 +02:00
|
|
|
query.promise.set_value(
|
|
|
|
td_->messages_manager_->get_chats_object(-1, std::move(result), "TopDialogManager::on_load_dialogs"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TopDialogManager::do_get_top_peers() {
|
2021-09-03 11:27:59 +02:00
|
|
|
std::vector<uint64> ids;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (auto &category : by_category_) {
|
|
|
|
for (auto &top_dialog : category.dialogs) {
|
|
|
|
auto dialog_id = top_dialog.dialog_id;
|
|
|
|
switch (dialog_id.get_type()) {
|
|
|
|
case DialogType::User:
|
|
|
|
ids.push_back(dialog_id.get_user_id().get());
|
|
|
|
break;
|
|
|
|
case DialogType::Chat:
|
|
|
|
ids.push_back(dialog_id.get_chat_id().get());
|
|
|
|
break;
|
2021-09-25 20:43:59 +02:00
|
|
|
case DialogType::Channel:
|
|
|
|
ids.push_back(dialog_id.get_channel_id().get());
|
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 20:43:59 +02:00
|
|
|
auto promise = PromiseCreator::lambda(
|
|
|
|
[actor_id = actor_id(this)](Result<telegram_api::object_ptr<telegram_api::contacts_TopPeers>> result) {
|
|
|
|
send_closure(actor_id, &TopDialogManager::on_get_top_peers, std::move(result));
|
|
|
|
});
|
|
|
|
td_->create_handler<GetTopPeersQuery>(std::move(promise))->send(get_vector_hash(ids));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-25 20:43:59 +02:00
|
|
|
void TopDialogManager::on_get_top_peers(Result<telegram_api::object_ptr<telegram_api::contacts_TopPeers>> result) {
|
2018-12-31 20:04:05 +01:00
|
|
|
normalize_rating(); // once a day too
|
|
|
|
|
2021-09-25 20:43:59 +02:00
|
|
|
if (result.is_error()) {
|
2018-06-26 21:51:00 +02:00
|
|
|
last_server_sync_ = Timestamp::in(SERVER_SYNC_RESEND_DELAY - SERVER_SYNC_DELAY);
|
2021-09-25 20:12:36 +02:00
|
|
|
loop();
|
2018-06-25 23:10:53 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-06-26 21:51:00 +02:00
|
|
|
last_server_sync_ = Timestamp::now();
|
|
|
|
server_sync_state_ = SyncState::Ok;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-09-25 20:43:59 +02:00
|
|
|
auto top_peers_parent = result.move_as_ok();
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(DEBUG) << "Receive contacts_getTopPeers result: " << to_string(top_peers_parent);
|
2018-06-26 21:51:00 +02:00
|
|
|
switch (top_peers_parent->get_id()) {
|
|
|
|
case telegram_api::contacts_topPeersNotModified::ID:
|
|
|
|
// nothing to do
|
2021-09-25 20:12:36 +02:00
|
|
|
break;
|
2018-06-26 21:51:00 +02:00
|
|
|
case telegram_api::contacts_topPeersDisabled::ID:
|
2022-08-17 15:11:13 +02:00
|
|
|
G()->set_option_boolean("disable_top_chats", true);
|
2018-06-26 21:51:00 +02:00
|
|
|
set_is_enabled(false); // apply immediately
|
2021-09-25 20:12:36 +02:00
|
|
|
break;
|
2018-06-26 21:51:00 +02:00
|
|
|
case telegram_api::contacts_topPeers::ID: {
|
2022-08-17 15:11:13 +02:00
|
|
|
G()->set_option_empty("disable_top_chats");
|
2018-06-26 21:51:00 +02:00
|
|
|
set_is_enabled(true); // apply immediately
|
|
|
|
auto top_peers = move_tl_object_as<telegram_api::contacts_topPeers>(std::move(top_peers_parent));
|
|
|
|
|
2021-09-25 21:02:55 +02:00
|
|
|
td_->contacts_manager_->on_get_users(std::move(top_peers->users_), "on get top chats");
|
|
|
|
td_->contacts_manager_->on_get_chats(std::move(top_peers->chats_), "on get top chats");
|
2018-06-26 21:51:00 +02:00
|
|
|
for (auto &category : top_peers->categories_) {
|
2021-09-25 20:27:32 +02:00
|
|
|
auto dialog_category = get_top_dialog_category(category->category_);
|
2018-06-26 21:51:00 +02:00
|
|
|
auto pos = static_cast<size_t>(dialog_category);
|
|
|
|
CHECK(pos < by_category_.size());
|
|
|
|
auto &top_dialogs = by_category_[pos];
|
|
|
|
|
|
|
|
top_dialogs.is_dirty = true;
|
|
|
|
top_dialogs.dialogs.clear();
|
|
|
|
for (auto &top_peer : category->peers_) {
|
|
|
|
TopDialog top_dialog;
|
|
|
|
top_dialog.dialog_id = DialogId(top_peer->peer_);
|
|
|
|
top_dialog.rating = top_peer->rating_;
|
|
|
|
top_dialogs.dialogs.push_back(std::move(top_dialog));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
db_sync_state_ = SyncState::None;
|
|
|
|
break;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-06-26 21:51:00 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-09-25 20:12:36 +02:00
|
|
|
|
|
|
|
G()->td_db()->get_binlog_pmc()->set("top_dialogs_ts", to_string(static_cast<uint32>(Clocks::system())));
|
|
|
|
loop();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TopDialogManager::do_save_top_dialogs() {
|
|
|
|
LOG(INFO) << "Save top chats";
|
|
|
|
for (size_t top_dialog_category_i = 0; top_dialog_category_i < by_category_.size(); top_dialog_category_i++) {
|
|
|
|
auto top_dialog_category = TopDialogCategory(top_dialog_category_i);
|
2021-09-25 20:27:32 +02:00
|
|
|
auto key = PSTRING() << "top_dialogs#" << get_top_dialog_category_name(top_dialog_category);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto &top_dialogs = by_category_[top_dialog_category_i];
|
|
|
|
if (!top_dialogs.is_dirty) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
top_dialogs.is_dirty = false;
|
|
|
|
|
|
|
|
G()->td_db()->get_binlog_pmc()->set(key, log_event_store(top_dialogs).as_slice().str());
|
|
|
|
}
|
|
|
|
db_sync_state_ = SyncState::Ok;
|
|
|
|
first_unsync_change_ = Timestamp();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopDialogManager::start_up() {
|
2021-09-24 22:17:38 +02:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopDialogManager::tear_down() {
|
|
|
|
parent_.reset();
|
2019-03-05 05:35:17 +01:00
|
|
|
}
|
|
|
|
|
2021-09-24 22:17:38 +02:00
|
|
|
void TopDialogManager::init() {
|
|
|
|
if (td_->auth_manager_ == nullptr || !td_->auth_manager_->is_authorized()) {
|
2019-03-05 05:35:17 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-13 17:47:38 +01:00
|
|
|
is_active_ = G()->use_chat_info_database() && !td_->auth_manager_->is_bot();
|
2022-08-17 15:11:13 +02:00
|
|
|
is_enabled_ = !G()->get_option_boolean("disable_top_chats");
|
2018-06-26 21:51:00 +02:00
|
|
|
update_rating_e_decay();
|
|
|
|
|
|
|
|
string need_update_top_peers = G()->td_db()->get_binlog_pmc()->get("top_peers_enabled");
|
|
|
|
if (!need_update_top_peers.empty()) {
|
|
|
|
send_toggle_top_peers(need_update_top_peers[0] == '1');
|
|
|
|
}
|
|
|
|
|
2021-09-24 22:17:38 +02:00
|
|
|
try_start();
|
2018-06-26 21:51:00 +02:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
2021-09-24 22:17:38 +02:00
|
|
|
void TopDialogManager::try_start() {
|
2018-06-26 21:51:00 +02:00
|
|
|
was_first_sync_ = false;
|
|
|
|
first_unsync_change_ = Timestamp();
|
|
|
|
server_sync_state_ = SyncState::None;
|
|
|
|
last_server_sync_ = Timestamp();
|
|
|
|
CHECK(pending_get_top_dialogs_.empty());
|
|
|
|
|
|
|
|
LOG(DEBUG) << "Init is enabled: " << is_enabled_;
|
|
|
|
if (!is_active_) {
|
2018-12-31 20:04:05 +01:00
|
|
|
G()->td_db()->get_binlog_pmc()->erase_by_prefix("top_dialogs");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto di_top_dialogs_ts = G()->td_db()->get_binlog_pmc()->get("top_dialogs_ts");
|
|
|
|
if (!di_top_dialogs_ts.empty()) {
|
|
|
|
last_server_sync_ = Timestamp::in(to_integer<uint32>(di_top_dialogs_ts) - Clocks::system());
|
|
|
|
if (last_server_sync_.is_in_past()) {
|
|
|
|
server_sync_state_ = SyncState::Ok;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 21:51:00 +02:00
|
|
|
if (is_enabled_) {
|
|
|
|
for (size_t top_dialog_category_i = 0; top_dialog_category_i < by_category_.size(); top_dialog_category_i++) {
|
|
|
|
auto top_dialog_category = TopDialogCategory(top_dialog_category_i);
|
2021-09-25 20:27:32 +02:00
|
|
|
auto key = PSTRING() << "top_dialogs#" << get_top_dialog_category_name(top_dialog_category);
|
2018-06-26 21:51:00 +02:00
|
|
|
auto value = G()->td_db()->get_binlog_pmc()->get(key);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-06-26 21:51:00 +02:00
|
|
|
auto &top_dialogs = by_category_[top_dialog_category_i];
|
|
|
|
top_dialogs.is_dirty = false;
|
|
|
|
if (value.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
log_event_parse(top_dialogs, value).ensure();
|
|
|
|
}
|
|
|
|
normalize_rating();
|
|
|
|
} else {
|
|
|
|
G()->td_db()->get_binlog_pmc()->erase_by_prefix("top_dialogs#");
|
|
|
|
for (auto &top_dialogs : by_category_) {
|
|
|
|
top_dialogs.is_dirty = false;
|
|
|
|
top_dialogs.rating_timestamp = 0;
|
|
|
|
top_dialogs.dialogs.clear();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
db_sync_state_ = SyncState::Ok;
|
|
|
|
|
|
|
|
send_closure(G()->state_manager(), &StateManager::wait_first_sync,
|
2022-06-27 01:57:56 +02:00
|
|
|
create_event_promise(self_closure(this, &TopDialogManager::on_first_sync)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TopDialogManager::on_first_sync() {
|
|
|
|
was_first_sync_ = true;
|
2021-09-24 22:17:38 +02:00
|
|
|
if (!G()->close_flag() && td_->auth_manager_->is_bot()) {
|
2018-12-12 16:38:20 +01:00
|
|
|
is_active_ = false;
|
2021-09-24 22:17:38 +02:00
|
|
|
try_start();
|
2018-12-12 16:38:20 +01:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TopDialogManager::loop() {
|
2018-06-27 20:26:52 +02:00
|
|
|
if (!is_active_ || G()->close_flag()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pending_get_top_dialogs_.empty()) {
|
|
|
|
for (auto &query : pending_get_top_dialogs_) {
|
|
|
|
do_get_top_dialogs(std::move(query));
|
|
|
|
}
|
|
|
|
pending_get_top_dialogs_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// server sync
|
|
|
|
Timestamp server_sync_timeout;
|
|
|
|
if (server_sync_state_ == SyncState::Ok) {
|
|
|
|
server_sync_timeout = Timestamp::at(last_server_sync_.at() + SERVER_SYNC_DELAY);
|
|
|
|
if (server_sync_timeout.is_in_past()) {
|
|
|
|
server_sync_state_ = SyncState::None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 16:21:40 +01:00
|
|
|
Timestamp wakeup_timeout;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (server_sync_state_ == SyncState::Ok) {
|
|
|
|
wakeup_timeout.relax(server_sync_timeout);
|
|
|
|
} else if (server_sync_state_ == SyncState::None && was_first_sync_) {
|
|
|
|
server_sync_state_ = SyncState::Pending;
|
|
|
|
do_get_top_peers();
|
|
|
|
}
|
|
|
|
|
2018-06-26 21:51:00 +02:00
|
|
|
if (is_enabled_) {
|
2018-12-13 23:48:36 +01:00
|
|
|
// database sync
|
2018-06-26 21:51:00 +02:00
|
|
|
Timestamp db_sync_timeout;
|
|
|
|
if (db_sync_state_ == SyncState::Ok) {
|
|
|
|
if (first_unsync_change_) {
|
|
|
|
db_sync_timeout = Timestamp::at(first_unsync_change_.at() + DB_SYNC_DELAY);
|
|
|
|
if (db_sync_timeout.is_in_past()) {
|
|
|
|
db_sync_state_ = SyncState::None;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 21:51:00 +02:00
|
|
|
if (db_sync_state_ == SyncState::Ok) {
|
|
|
|
wakeup_timeout.relax(db_sync_timeout);
|
|
|
|
} else if (db_sync_state_ == SyncState::None) {
|
|
|
|
if (server_sync_state_ == SyncState::Ok) {
|
|
|
|
do_save_top_dialogs();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wakeup_timeout) {
|
|
|
|
LOG(INFO) << "Wakeup in: " << wakeup_timeout.in();
|
|
|
|
set_timeout_at(wakeup_timeout.at());
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Wakeup: never";
|
|
|
|
cancel_timeout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|