From d3983571a0e844947bd81db7b798a5f38de9ed81 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 25 Sep 2021 21:27:32 +0300 Subject: [PATCH] Add TopDialogCategory.cpp. --- CMakeLists.txt | 1 + td/telegram/TopDialogCategory.cpp | 108 ++++++++++++++++++++++++++++++ td/telegram/TopDialogCategory.h | 33 +++------ td/telegram/TopDialogManager.cpp | 80 ++-------------------- td/telegram/TopDialogManager.h | 4 +- 5 files changed, 126 insertions(+), 100 deletions(-) create mode 100644 td/telegram/TopDialogCategory.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c8e0052b7..6dd54fe78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -411,6 +411,7 @@ set(TDLIB_SOURCE td/telegram/TdDb.cpp td/telegram/TermsOfService.cpp td/telegram/ThemeManager.cpp + td/telegram/TopDialogCategory.cpp td/telegram/TopDialogManager.cpp td/telegram/UpdatesManager.cpp td/telegram/Venue.cpp diff --git a/td/telegram/TopDialogCategory.cpp b/td/telegram/TopDialogCategory.cpp new file mode 100644 index 000000000..10a4238f9 --- /dev/null +++ b/td/telegram/TopDialogCategory.cpp @@ -0,0 +1,108 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// +// 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/TopDialogCategory.h" + +namespace td { + +CSlice get_top_dialog_category_name(TopDialogCategory category) { + switch (category) { + case TopDialogCategory::Correspondent: + return CSlice("correspondent"); + case TopDialogCategory::BotPM: + return CSlice("bot_pm"); + case TopDialogCategory::BotInline: + return CSlice("bot_inline"); + case TopDialogCategory::Group: + return CSlice("group"); + case TopDialogCategory::Channel: + return CSlice("channel"); + case TopDialogCategory::Call: + return CSlice("call"); + case TopDialogCategory::ForwardUsers: + return CSlice("forward_users"); + case TopDialogCategory::ForwardChats: + return CSlice("forward_chats"); + default: + UNREACHABLE(); + return CSlice(); + } +} + +TopDialogCategory get_top_dialog_category(const td_api::object_ptr &category) { + if (category == nullptr) { + return TopDialogCategory::Size; + } + switch (category->get_id()) { + case td_api::topChatCategoryUsers::ID: + return TopDialogCategory::Correspondent; + case td_api::topChatCategoryBots::ID: + return TopDialogCategory::BotPM; + case td_api::topChatCategoryInlineBots::ID: + return TopDialogCategory::BotInline; + case td_api::topChatCategoryGroups::ID: + return TopDialogCategory::Group; + case td_api::topChatCategoryChannels::ID: + return TopDialogCategory::Channel; + case td_api::topChatCategoryCalls::ID: + return TopDialogCategory::Call; + case td_api::topChatCategoryForwardChats::ID: + return TopDialogCategory::ForwardUsers; + default: + UNREACHABLE(); + return TopDialogCategory::Size; + } +} +TopDialogCategory get_top_dialog_category(const telegram_api::object_ptr &category) { + CHECK(category != nullptr); + switch (category->get_id()) { + case telegram_api::topPeerCategoryCorrespondents::ID: + return TopDialogCategory::Correspondent; + case telegram_api::topPeerCategoryBotsPM::ID: + return TopDialogCategory::BotPM; + case telegram_api::topPeerCategoryBotsInline::ID: + return TopDialogCategory::BotInline; + case telegram_api::topPeerCategoryGroups::ID: + return TopDialogCategory::Group; + case telegram_api::topPeerCategoryChannels::ID: + return TopDialogCategory::Channel; + case telegram_api::topPeerCategoryPhoneCalls::ID: + return TopDialogCategory::Call; + case telegram_api::topPeerCategoryForwardUsers::ID: + return TopDialogCategory::ForwardUsers; + case telegram_api::topPeerCategoryForwardChats::ID: + return TopDialogCategory::ForwardChats; + default: + UNREACHABLE(); + return TopDialogCategory::Size; + } +} + +telegram_api::object_ptr get_input_top_peer_category(TopDialogCategory category) { + switch (category) { + case TopDialogCategory::Correspondent: + return make_tl_object(); + case TopDialogCategory::BotPM: + return make_tl_object(); + case TopDialogCategory::BotInline: + return make_tl_object(); + case TopDialogCategory::Group: + return make_tl_object(); + case TopDialogCategory::Channel: + return make_tl_object(); + case TopDialogCategory::Call: + return make_tl_object(); + case TopDialogCategory::ForwardUsers: + return make_tl_object(); + case TopDialogCategory::ForwardChats: + return make_tl_object(); + default: + UNREACHABLE(); + return nullptr; + } +} + +} // namespace td diff --git a/td/telegram/TopDialogCategory.h b/td/telegram/TopDialogCategory.h index 7fc13b643..3f3c92293 100644 --- a/td/telegram/TopDialogCategory.h +++ b/td/telegram/TopDialogCategory.h @@ -7,8 +7,10 @@ #pragma once #include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" #include "td/utils/common.h" +#include "td/utils/Slice.h" namespace td { @@ -24,29 +26,12 @@ enum class TopDialogCategory : int32 { Size }; -inline TopDialogCategory get_top_dialog_category(const tl_object_ptr &category) { - if (category == nullptr) { - return TopDialogCategory::Size; - } - switch (category->get_id()) { - case td_api::topChatCategoryUsers::ID: - return TopDialogCategory::Correspondent; - case td_api::topChatCategoryBots::ID: - return TopDialogCategory::BotPM; - case td_api::topChatCategoryInlineBots::ID: - return TopDialogCategory::BotInline; - case td_api::topChatCategoryGroups::ID: - return TopDialogCategory::Group; - case td_api::topChatCategoryChannels::ID: - return TopDialogCategory::Channel; - case td_api::topChatCategoryCalls::ID: - return TopDialogCategory::Call; - case td_api::topChatCategoryForwardChats::ID: - return TopDialogCategory::ForwardUsers; - default: - UNREACHABLE(); - return TopDialogCategory::Size; - } -} +CSlice get_top_dialog_category_name(TopDialogCategory category); + +TopDialogCategory get_top_dialog_category(const td_api::object_ptr &category); + +TopDialogCategory get_top_dialog_category(const telegram_api::object_ptr &category); + +telegram_api::object_ptr get_input_top_peer_category(TopDialogCategory category); } // namespace td diff --git a/td/telegram/TopDialogManager.cpp b/td/telegram/TopDialogManager.cpp index ef825fb57..183141ba2 100644 --- a/td/telegram/TopDialogManager.cpp +++ b/td/telegram/TopDialogManager.cpp @@ -24,6 +24,7 @@ #include "td/telegram/telegram_api.h" #include "td/utils/algorithm.h" +#include "td/utils/buffer.h" #include "td/utils/logging.h" #include "td/utils/misc.h" #include "td/utils/port/Clocks.h" @@ -64,75 +65,6 @@ class ToggleTopPeersQuery final : public Td::ResultHandler { } }; -static CSlice top_dialog_category_name(TopDialogCategory category) { - switch (category) { - case TopDialogCategory::Correspondent: - return CSlice("correspondent"); - case TopDialogCategory::BotPM: - return CSlice("bot_pm"); - case TopDialogCategory::BotInline: - return CSlice("bot_inline"); - case TopDialogCategory::Group: - return CSlice("group"); - case TopDialogCategory::Channel: - return CSlice("channel"); - case TopDialogCategory::Call: - return CSlice("call"); - case TopDialogCategory::ForwardUsers: - return CSlice("forward_users"); - case TopDialogCategory::ForwardChats: - return CSlice("forward_chats"); - default: - UNREACHABLE(); - } -} - -static TopDialogCategory get_top_dialog_category(const telegram_api::TopPeerCategory &category) { - switch (category.get_id()) { - case telegram_api::topPeerCategoryCorrespondents::ID: - return TopDialogCategory::Correspondent; - case telegram_api::topPeerCategoryBotsPM::ID: - return TopDialogCategory::BotPM; - case telegram_api::topPeerCategoryBotsInline::ID: - return TopDialogCategory::BotInline; - case telegram_api::topPeerCategoryGroups::ID: - return TopDialogCategory::Group; - case telegram_api::topPeerCategoryChannels::ID: - return TopDialogCategory::Channel; - case telegram_api::topPeerCategoryPhoneCalls::ID: - return TopDialogCategory::Call; - case telegram_api::topPeerCategoryForwardUsers::ID: - return TopDialogCategory::ForwardUsers; - case telegram_api::topPeerCategoryForwardChats::ID: - return TopDialogCategory::ForwardChats; - default: - UNREACHABLE(); - } -} - -static tl_object_ptr get_top_peer_category(TopDialogCategory category) { - switch (category) { - case TopDialogCategory::Correspondent: - return make_tl_object(); - case TopDialogCategory::BotPM: - return make_tl_object(); - case TopDialogCategory::BotInline: - return make_tl_object(); - case TopDialogCategory::Group: - return make_tl_object(); - case TopDialogCategory::Channel: - return make_tl_object(); - case TopDialogCategory::Call: - return make_tl_object(); - case TopDialogCategory::ForwardUsers: - return make_tl_object(); - case TopDialogCategory::ForwardChats: - return make_tl_object(); - default: - UNREACHABLE(); - } -} - class ResetTopPeerRatingQuery final : public Td::ResultHandler { DialogId dialog_id_; @@ -145,7 +77,7 @@ class ResetTopPeerRatingQuery final : public Td::ResultHandler { dialog_id_ = dialog_id; send_query(G()->net_query_creator().create( - telegram_api::contacts_resetTopPeerRating(get_top_peer_category(category), std::move(input_peer)))); + telegram_api::contacts_resetTopPeerRating(get_input_top_peer_category(category), std::move(input_peer)))); } void on_result(uint64 id, BufferSlice packet) final { @@ -258,7 +190,7 @@ void TopDialogManager::on_dialog_used(TopDialogCategory category, DialogId dialo it = next; } - LOG(INFO) << "Update " << top_dialog_category_name(category) << " rating of " << dialog_id << " by " << delta; + LOG(INFO) << "Update " << get_top_dialog_category_name(category) << " rating of " << dialog_id << " by " << delta; if (!first_unsync_change_) { first_unsync_change_ = Timestamp::now_cached(); @@ -522,7 +454,7 @@ void TopDialogManager::on_result(NetQueryPtr net_query) { send_closure(G()->contacts_manager(), &ContactsManager::on_get_chats, std::move(top_peers->chats_), "on get top chats"); for (auto &category : top_peers->categories_) { - auto dialog_category = get_top_dialog_category(*category->category_); + auto dialog_category = get_top_dialog_category(category->category_); auto pos = static_cast(dialog_category); CHECK(pos < by_category_.size()); auto &top_dialogs = by_category_[pos]; @@ -551,7 +483,7 @@ 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); - auto key = PSTRING() << "top_dialogs#" << top_dialog_category_name(top_dialog_category); + auto key = PSTRING() << "top_dialogs#" << get_top_dialog_category_name(top_dialog_category); auto &top_dialogs = by_category_[top_dialog_category_i]; if (!top_dialogs.is_dirty) { @@ -615,7 +547,7 @@ void TopDialogManager::try_start() { 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); - auto key = PSTRING() << "top_dialogs#" << top_dialog_category_name(top_dialog_category); + auto key = PSTRING() << "top_dialogs#" << get_top_dialog_category_name(top_dialog_category); auto value = G()->td_db()->get_binlog_pmc()->get(key); auto &top_dialogs = by_category_[top_dialog_category_i]; diff --git a/td/telegram/TopDialogManager.h b/td/telegram/TopDialogManager.h index a3f6258a9..582ccf5a7 100644 --- a/td/telegram/TopDialogManager.h +++ b/td/telegram/TopDialogManager.h @@ -49,8 +49,8 @@ class TopDialogManager final : public NetQueryCallback { Td *td_; ActorShared<> parent_; - bool is_active_{false}; - bool is_enabled_{true}; + bool is_active_ = false; + bool is_enabled_ = true; int32 rating_e_decay_ = 241920; bool have_toggle_top_peers_query_ = false;