Move topChat methods logic to TopDialogManager.

This commit is contained in:
levlam 2021-09-25 11:37:32 +03:00
parent 5091aa68e3
commit 9115ba4aa5
5 changed files with 37 additions and 37 deletions

View File

@ -5201,14 +5201,8 @@ void Td::on_request(uint64 id, const td_api::setAutoDownloadSettings &request) {
std::move(promise));
}
void Td::on_request(uint64 id, td_api::getTopChats &request) {
void Td::on_request(uint64 id, const td_api::getTopChats &request) {
CHECK_IS_USER();
if (request.category_ == nullptr) {
return send_error_raw(id, 400, "Top chat category must be non-empty");
}
if (request.limit_ <= 0) {
return send_error_raw(id, 400, "Limit must be positive");
}
CREATE_REQUEST_PROMISE();
auto query_promise = PromiseCreator::lambda([promise = std::move(promise)](Result<vector<DialogId>> result) mutable {
if (result.is_error()) {
@ -5217,23 +5211,15 @@ void Td::on_request(uint64 id, td_api::getTopChats &request) {
promise.set_value(MessagesManager::get_chats_object(-1, result.ok()));
}
});
top_dialog_manager_->get_top_dialogs(get_top_dialog_category(*request.category_), narrow_cast<size_t>(request.limit_),
top_dialog_manager_->get_top_dialogs(get_top_dialog_category(request.category_), request.limit_,
std::move(query_promise));
}
void Td::on_request(uint64 id, const td_api::removeTopChat &request) {
CHECK_IS_USER();
if (request.category_ == nullptr) {
return send_error_raw(id, 400, "Top chat category must be non-empty");
}
DialogId dialog_id(request.chat_id_);
if (!dialog_id.is_valid()) {
return send_error_raw(id, 400, "Invalid chat identifier");
}
top_dialog_manager_->remove_dialog(get_top_dialog_category(*request.category_), dialog_id,
messages_manager_->get_input_peer(dialog_id, AccessRights::Read));
send_closure(actor_id(this), &Td::send_result, id, td_api::make_object<td_api::ok>());
CREATE_OK_REQUEST_PROMISE();
top_dialog_manager_->remove_dialog(get_top_dialog_category(request.category_), DialogId(request.chat_id_),
std::move(promise));
}
void Td::on_request(uint64 id, const td_api::loadChats &request) {

View File

@ -554,7 +554,7 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::setAutoDownloadSettings &request);
void on_request(uint64 id, td_api::getTopChats &request);
void on_request(uint64 id, const td_api::getTopChats &request);
void on_request(uint64 id, const td_api::removeTopChat &request);

View File

@ -24,8 +24,11 @@ enum class TopDialogCategory : int32 {
Size
};
inline TopDialogCategory get_top_dialog_category(const td_api::TopChatCategory &category) {
switch (category.get_id()) {
inline TopDialogCategory get_top_dialog_category(const tl_object_ptr<td_api::TopChatCategory> &category) {
if (category == nullptr) {
return TopDialogCategory::Size;
}
switch (category->get_id()) {
case td_api::topChatCategoryUsers::ID:
return TopDialogCategory::Correspondent;
case td_api::topChatCategoryBots::ID:
@ -42,6 +45,7 @@ inline TopDialogCategory get_top_dialog_category(const td_api::TopChatCategory &
return TopDialogCategory::ForwardUsers;
default:
UNREACHABLE();
return TopDialogCategory::Size;
}
}

View File

@ -6,6 +6,7 @@
//
#include "td/telegram/TopDialogManager.h"
#include "td/telegram/AccessRights.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/ConfigShared.h"
#include "td/telegram/ContactsManager.h"
@ -182,12 +183,16 @@ void TopDialogManager::on_dialog_used(TopDialogCategory category, DialogId dialo
loop();
}
void TopDialogManager::remove_dialog(TopDialogCategory category, DialogId dialog_id,
tl_object_ptr<telegram_api::InputPeer> input_peer) {
if (!is_active_ || !is_enabled_) {
return;
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"));
}
if (!is_active_ || !is_enabled_) {
return promise.set_value(Unit());
}
CHECK(dialog_id.is_valid());
if (category == TopDialogCategory::ForwardUsers && dialog_id.get_type() != DialogType::User) {
category = TopDialogCategory::ForwardChats;
@ -199,6 +204,7 @@ void TopDialogManager::remove_dialog(TopDialogCategory category, DialogId dialog
LOG(INFO) << "Remove " << top_dialog_category_name(category) << " rating of " << dialog_id;
auto input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Read);
if (input_peer != nullptr) {
auto query = telegram_api::contacts_resetTopPeerRating(get_top_peer_category(category), std::move(input_peer));
auto net_query = G()->net_query_creator().create(query);
@ -208,7 +214,7 @@ void TopDialogManager::remove_dialog(TopDialogCategory category, DialogId dialog
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()) {
return;
return promise.set_value(Unit());
}
top_dialogs.is_dirty = true;
@ -217,21 +223,26 @@ void TopDialogManager::remove_dialog(TopDialogCategory category, DialogId dialog
first_unsync_change_ = Timestamp::now_cached();
}
loop();
promise.set_value(Unit());
}
void TopDialogManager::get_top_dialogs(TopDialogCategory category, size_t limit, Promise<vector<DialogId>> promise) {
void TopDialogManager::get_top_dialogs(TopDialogCategory category, int32 limit, Promise<vector<DialogId>> promise) {
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"));
}
if (!is_active_) {
promise.set_error(Status::Error(400, "Not supported without chat info database"));
return;
return promise.set_error(Status::Error(400, "Not supported without chat info database"));
}
if (!is_enabled_) {
promise.set_error(Status::Error(400, "Top chats computation is disabled"));
return;
return promise.set_error(Status::Error(400, "Top chats computation is disabled"));
}
GetTopDialogsQuery query;
query.category = category;
query.limit = limit;
query.limit = static_cast<size_t>(limit);
query.promise = std::move(promise);
pending_get_top_dialogs_.push_back(std::move(query));
loop();

View File

@ -8,7 +8,6 @@
#include "td/telegram/DialogId.h"
#include "td/telegram/net/NetQuery.h"
#include "td/telegram/telegram_api.h"
#include "td/telegram/TopDialogCategory.h"
#include "td/actor/actor.h"
@ -33,9 +32,9 @@ class TopDialogManager final : public NetQueryCallback {
void on_dialog_used(TopDialogCategory category, DialogId dialog_id, int32 date);
void remove_dialog(TopDialogCategory category, DialogId dialog_id, tl_object_ptr<telegram_api::InputPeer> input_peer);
void remove_dialog(TopDialogCategory category, DialogId dialog_id, Promise<Unit> &&promise);
void get_top_dialogs(TopDialogCategory category, size_t limit, Promise<vector<DialogId>> promise);
void get_top_dialogs(TopDialogCategory category, int32 limit, Promise<vector<DialogId>> promise);
void update_rating_e_decay();