Move recommended filters to DialogFilterManager.

This commit is contained in:
levlam 2023-03-27 11:50:40 +03:00
parent 800258fa57
commit 0ff733edad
5 changed files with 142 additions and 120 deletions

View File

@ -6,8 +6,45 @@
// //
#include "td/telegram/DialogFilterManager.h" #include "td/telegram/DialogFilterManager.h"
#include "td/telegram/AuthManager.h"
#include "td/telegram/DialogFilter.h"
#include "td/telegram/Global.h"
#include "td/telegram/MessagesManager.h"
#include "td/telegram/Td.h"
#include "td/actor/MultiPromise.h"
#include "td/utils/algorithm.h"
#include "td/utils/buffer.h"
namespace td { namespace td {
class GetSuggestedDialogFiltersQuery final : public Td::ResultHandler {
Promise<vector<tl_object_ptr<telegram_api::dialogFilterSuggested>>> promise_;
public:
explicit GetSuggestedDialogFiltersQuery(Promise<vector<tl_object_ptr<telegram_api::dialogFilterSuggested>>> &&promise)
: promise_(std::move(promise)) {
}
void send() {
send_query(G()->net_query_creator().create(telegram_api::messages_getSuggestedDialogFilters()));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_getSuggestedDialogFilters>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
promise_.set_value(result_ptr.move_as_ok());
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
DialogFilterManager::DialogFilterManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { DialogFilterManager::DialogFilterManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
} }
@ -15,4 +52,77 @@ void DialogFilterManager::tear_down() {
parent_.reset(); parent_.reset();
} }
bool DialogFilterManager::is_recommended_dialog_filter(const DialogFilter *dialog_filter) {
for (const auto &recommended_dialog_filter : recommended_dialog_filters_) {
if (DialogFilter::are_similar(*recommended_dialog_filter.dialog_filter, *dialog_filter)) {
return true;
}
}
return false;
}
void DialogFilterManager::get_recommended_dialog_filters(
Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise) {
CHECK(!td_->auth_manager_->is_bot());
auto query_promise = PromiseCreator::lambda(
[actor_id = actor_id(this), promise = std::move(promise)](
Result<vector<telegram_api::object_ptr<telegram_api::dialogFilterSuggested>>> result) mutable {
send_closure(actor_id, &DialogFilterManager::on_get_recommended_dialog_filters, std::move(result),
std::move(promise));
});
td_->create_handler<GetSuggestedDialogFiltersQuery>(std::move(query_promise))->send();
}
void DialogFilterManager::on_get_recommended_dialog_filters(
Result<vector<telegram_api::object_ptr<telegram_api::dialogFilterSuggested>>> result,
Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise) {
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
CHECK(!td_->auth_manager_->is_bot());
auto suggested_filters = result.move_as_ok();
MultiPromiseActorSafe mpas{"LoadRecommendedFiltersMultiPromiseActor"};
mpas.add_promise(Promise<Unit>());
auto lock = mpas.get_promise();
vector<RecommendedDialogFilter> filters;
for (auto &suggested_filter : suggested_filters) {
RecommendedDialogFilter recommended_dialog_filter;
recommended_dialog_filter.dialog_filter =
DialogFilter::get_dialog_filter(std::move(suggested_filter->filter_), false);
CHECK(recommended_dialog_filter.dialog_filter != nullptr);
td_->messages_manager_->load_dialog_filter(recommended_dialog_filter.dialog_filter.get(), false,
mpas.get_promise());
recommended_dialog_filter.description = std::move(suggested_filter->description_);
filters.push_back(std::move(recommended_dialog_filter));
}
mpas.add_promise(PromiseCreator::lambda([actor_id = actor_id(this), filters = std::move(filters),
promise = std::move(promise)](Result<Unit> &&result) mutable {
send_closure(actor_id, &DialogFilterManager::on_load_recommended_dialog_filters, std::move(result),
std::move(filters), std::move(promise));
}));
lock.set_value(Unit());
}
void DialogFilterManager::on_load_recommended_dialog_filters(
Result<Unit> &&result, vector<RecommendedDialogFilter> &&filters,
Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
CHECK(!td_->auth_manager_->is_bot());
auto chat_filters = transform(filters, [this](const RecommendedDialogFilter &recommended_dialog_filter) {
return td_api::make_object<td_api::recommendedChatFilter>(
td_->messages_manager_->get_chat_filter_object(recommended_dialog_filter.dialog_filter.get()),
recommended_dialog_filter.description);
});
recommended_dialog_filters_ = std::move(filters);
promise.set_value(td_api::make_object<td_api::recommendedChatFilters>(std::move(chat_filters)));
}
} // namespace td } // namespace td

View File

@ -6,21 +6,45 @@
// //
#pragma once #pragma once
#include "td/telegram/td_api.h"
#include "td/telegram/telegram_api.h"
#include "td/actor/actor.h" #include "td/actor/actor.h"
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/Promise.h"
#include "td/utils/Status.h"
namespace td { namespace td {
class DialogFilter;
class Td; class Td;
class DialogFilterManager final : public Actor { class DialogFilterManager final : public Actor {
public: public:
DialogFilterManager(Td *td, ActorShared<> parent); DialogFilterManager(Td *td, ActorShared<> parent);
bool is_recommended_dialog_filter(const DialogFilter *dialog_filter);
void get_recommended_dialog_filters(Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise);
private: private:
void tear_down() final; void tear_down() final;
struct RecommendedDialogFilter {
unique_ptr<DialogFilter> dialog_filter;
string description;
};
void on_get_recommended_dialog_filters(
Result<vector<telegram_api::object_ptr<telegram_api::dialogFilterSuggested>>> result,
Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise);
void on_load_recommended_dialog_filters(Result<Unit> &&result, vector<RecommendedDialogFilter> &&filters,
Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise);
vector<RecommendedDialogFilter> recommended_dialog_filters_;
Td *td_; Td *td_;
ActorShared<> parent_; ActorShared<> parent_;
}; };

View File

@ -16,6 +16,7 @@
#include "td/telegram/DialogDb.h" #include "td/telegram/DialogDb.h"
#include "td/telegram/DialogFilter.h" #include "td/telegram/DialogFilter.h"
#include "td/telegram/DialogFilter.hpp" #include "td/telegram/DialogFilter.hpp"
#include "td/telegram/DialogFilterManager.h"
#include "td/telegram/DialogLocation.h" #include "td/telegram/DialogLocation.h"
#include "td/telegram/DialogNotificationSettings.hpp" #include "td/telegram/DialogNotificationSettings.hpp"
#include "td/telegram/DownloadManager.h" #include "td/telegram/DownloadManager.h"
@ -182,32 +183,6 @@ class UpdateDialogFiltersOrderQuery final : public Td::ResultHandler {
} }
}; };
class GetSuggestedDialogFiltersQuery final : public Td::ResultHandler {
Promise<vector<tl_object_ptr<telegram_api::dialogFilterSuggested>>> promise_;
public:
explicit GetSuggestedDialogFiltersQuery(Promise<vector<tl_object_ptr<telegram_api::dialogFilterSuggested>>> &&promise)
: promise_(std::move(promise)) {
}
void send() {
send_query(G()->net_query_creator().create(telegram_api::messages_getSuggestedDialogFilters()));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_getSuggestedDialogFilters>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
promise_.set_value(result_ptr.move_as_ok());
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class GetOnlinesQuery final : public Td::ResultHandler { class GetOnlinesQuery final : public Td::ResultHandler {
DialogId dialog_id_; DialogId dialog_id_;
@ -17127,68 +17102,6 @@ void MessagesManager::delete_dialogs_from_filter(const DialogFilter *dialog_filt
} }
} }
void MessagesManager::get_recommended_dialog_filters(
Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise) {
CHECK(!td_->auth_manager_->is_bot());
auto query_promise =
PromiseCreator::lambda([actor_id = actor_id(this), promise = std::move(promise)](
Result<vector<tl_object_ptr<telegram_api::dialogFilterSuggested>>> result) mutable {
send_closure(actor_id, &MessagesManager::on_get_recommended_dialog_filters, std::move(result),
std::move(promise));
});
td_->create_handler<GetSuggestedDialogFiltersQuery>(std::move(query_promise))->send();
}
void MessagesManager::on_get_recommended_dialog_filters(
Result<vector<tl_object_ptr<telegram_api::dialogFilterSuggested>>> result,
Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise) {
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
CHECK(!td_->auth_manager_->is_bot());
auto suggested_filters = result.move_as_ok();
MultiPromiseActorSafe mpas{"LoadRecommendedFiltersMultiPromiseActor"};
mpas.add_promise(Promise<Unit>());
auto lock = mpas.get_promise();
vector<RecommendedDialogFilter> filters;
for (auto &suggested_filter : suggested_filters) {
RecommendedDialogFilter recommended_dialog_filter;
recommended_dialog_filter.dialog_filter =
DialogFilter::get_dialog_filter(std::move(suggested_filter->filter_), false);
CHECK(recommended_dialog_filter.dialog_filter != nullptr);
load_dialog_filter(recommended_dialog_filter.dialog_filter.get(), false, mpas.get_promise());
recommended_dialog_filter.description = std::move(suggested_filter->description_);
filters.push_back(std::move(recommended_dialog_filter));
}
mpas.add_promise(PromiseCreator::lambda([actor_id = actor_id(this), filters = std::move(filters),
promise = std::move(promise)](Result<Unit> &&result) mutable {
send_closure(actor_id, &MessagesManager::on_load_recommended_dialog_filters, std::move(result), std::move(filters),
std::move(promise));
}));
lock.set_value(Unit());
}
void MessagesManager::on_load_recommended_dialog_filters(
Result<Unit> &&result, vector<RecommendedDialogFilter> &&filters,
Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
CHECK(!td_->auth_manager_->is_bot());
auto chat_filters = transform(filters, [this](const RecommendedDialogFilter &recommended_dialog_filter) {
return td_api::make_object<td_api::recommendedChatFilter>(
get_chat_filter_object(recommended_dialog_filter.dialog_filter.get()), recommended_dialog_filter.description);
});
recommended_dialog_filters_ = std::move(filters);
promise.set_value(td_api::make_object<td_api::recommendedChatFilters>(std::move(chat_filters)));
}
Result<DialogDate> MessagesManager::get_dialog_list_last_date(DialogListId dialog_list_id) { Result<DialogDate> MessagesManager::get_dialog_list_last_date(DialogListId dialog_list_id) {
CHECK(!td_->auth_manager_->is_bot()); CHECK(!td_->auth_manager_->is_bot());
@ -19733,13 +19646,7 @@ void MessagesManager::create_dialog_filter(td_api::object_ptr<td_api::chatFilter
CHECK(dialog_filter != nullptr); CHECK(dialog_filter != nullptr);
auto chat_filter_info = dialog_filter->get_chat_filter_info_object(); auto chat_filter_info = dialog_filter->get_chat_filter_info_object();
bool at_beginning = false; bool at_beginning = td_->dialog_filter_manager_->is_recommended_dialog_filter(dialog_filter.get());
for (const auto &recommended_dialog_filter : recommended_dialog_filters_) {
if (DialogFilter::are_similar(*recommended_dialog_filter.dialog_filter, *dialog_filter)) {
at_beginning = true;
}
}
add_dialog_filter(std::move(dialog_filter), at_beginning, "create_dialog_filter"); add_dialog_filter(std::move(dialog_filter), at_beginning, "create_dialog_filter");
if (at_beginning && main_dialog_list_position_ != 0) { if (at_beginning && main_dialog_list_position_ != 0) {
main_dialog_list_position_++; main_dialog_list_position_++;
@ -19814,12 +19721,7 @@ void MessagesManager::on_update_dialog_filter(unique_ptr<DialogFilter> dialog_fi
} }
if (!is_edited) { if (!is_edited) {
bool at_beginning = false; bool at_beginning = td_->dialog_filter_manager_->is_recommended_dialog_filter(dialog_filter.get());
for (const auto &recommended_dialog_filter : recommended_dialog_filters_) {
if (DialogFilter::are_similar(*recommended_dialog_filter.dialog_filter, *dialog_filter)) {
at_beginning = true;
}
}
if (at_beginning) { if (at_beginning) {
server_dialog_filters_.insert(server_dialog_filters_.begin(), std::move(dialog_filter)); server_dialog_filters_.insert(server_dialog_filters_.begin(), std::move(dialog_filter));
} else { } else {

View File

@ -581,7 +581,7 @@ class MessagesManager final : public Actor {
void load_dialog_filter(DialogFilterId dialog_filter_id, bool force, Promise<Unit> &&promise); void load_dialog_filter(DialogFilterId dialog_filter_id, bool force, Promise<Unit> &&promise);
void get_recommended_dialog_filters(Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise); void load_dialog_filter(const DialogFilter *dialog_filter, bool force, Promise<Unit> &&promise);
Result<DialogDate> get_dialog_list_last_date(DialogListId dialog_list_id); Result<DialogDate> get_dialog_list_last_date(DialogListId dialog_list_id);
@ -748,7 +748,9 @@ class MessagesManager final : public Actor {
static tl_object_ptr<td_api::chats> get_chats_object(const std::pair<int32, vector<DialogId>> &dialog_ids); static tl_object_ptr<td_api::chats> get_chats_object(const std::pair<int32, vector<DialogId>> &dialog_ids);
tl_object_ptr<td_api::chatFilter> get_chat_filter_object(DialogFilterId dialog_filter_id); td_api::object_ptr<td_api::chatFilter> get_chat_filter_object(DialogFilterId dialog_filter_id);
td_api::object_ptr<td_api::chatFilter> get_chat_filter_object(const DialogFilter *dialog_filter);
tl_object_ptr<td_api::messages> get_dialog_history(DialogId dialog_id, MessageId from_message_id, int32 offset, tl_object_ptr<td_api::messages> get_dialog_history(DialogId dialog_id, MessageId from_message_id, int32 offset,
int32 limit, int left_tries, bool only_local, int32 limit, int left_tries, bool only_local,
@ -1461,11 +1463,6 @@ class MessagesManager final : public Actor {
void parse(ParserT &parser); void parse(ParserT &parser);
}; };
struct RecommendedDialogFilter {
unique_ptr<DialogFilter> dialog_filter;
string description;
};
struct DialogList { struct DialogList {
DialogListId dialog_list_id; DialogListId dialog_list_id;
bool is_message_unread_count_inited_ = false; bool is_message_unread_count_inited_ = false;
@ -2841,24 +2838,14 @@ class MessagesManager final : public Actor {
void update_dialogs_hints(const Dialog *d); void update_dialogs_hints(const Dialog *d);
void update_dialogs_hints_rating(const Dialog *d); void update_dialogs_hints_rating(const Dialog *d);
td_api::object_ptr<td_api::chatFilter> get_chat_filter_object(const DialogFilter *dialog_filter);
void load_dialog_filter_dialogs(DialogFilterId dialog_filter_id, vector<InputDialogId> &&input_dialog_ids, void load_dialog_filter_dialogs(DialogFilterId dialog_filter_id, vector<InputDialogId> &&input_dialog_ids,
Promise<Unit> &&promise); Promise<Unit> &&promise);
void on_load_dialog_filter_dialogs(DialogFilterId dialog_filter_id, vector<DialogId> &&dialog_ids, void on_load_dialog_filter_dialogs(DialogFilterId dialog_filter_id, vector<DialogId> &&dialog_ids,
Promise<Unit> &&promise); Promise<Unit> &&promise);
void load_dialog_filter(const DialogFilter *dialog_filter, bool force, Promise<Unit> &&promise);
void delete_dialogs_from_filter(const DialogFilter *dialog_filter, vector<DialogId> &&dialog_ids, const char *source); void delete_dialogs_from_filter(const DialogFilter *dialog_filter, vector<DialogId> &&dialog_ids, const char *source);
void on_get_recommended_dialog_filters(Result<vector<tl_object_ptr<telegram_api::dialogFilterSuggested>>> result,
Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise);
void on_load_recommended_dialog_filters(Result<Unit> &&result, vector<RecommendedDialogFilter> &&filters,
Promise<td_api::object_ptr<td_api::recommendedChatFilters>> &&promise);
Result<unique_ptr<DialogFilter>> create_dialog_filter(DialogFilterId dialog_filter_id, Result<unique_ptr<DialogFilter>> create_dialog_filter(DialogFilterId dialog_filter_id,
td_api::object_ptr<td_api::chatFilter> filter); td_api::object_ptr<td_api::chatFilter> filter);
@ -3623,7 +3610,6 @@ class MessagesManager final : public Actor {
int32 dialog_filters_updated_date_ = 0; int32 dialog_filters_updated_date_ = 0;
vector<unique_ptr<DialogFilter>> server_dialog_filters_; vector<unique_ptr<DialogFilter>> server_dialog_filters_;
vector<unique_ptr<DialogFilter>> dialog_filters_; vector<unique_ptr<DialogFilter>> dialog_filters_;
vector<RecommendedDialogFilter> recommended_dialog_filters_;
vector<Promise<Unit>> dialog_filter_reload_queries_; vector<Promise<Unit>> dialog_filter_reload_queries_;
int32 server_main_dialog_list_position_ = 0; // position of the main dialog list stored on the server int32 server_main_dialog_list_position_ = 0; // position of the main dialog list stored on the server
int32 main_dialog_list_position_ = 0; // local position of the main dialog list stored on the server int32 main_dialog_list_position_ = 0; // local position of the main dialog list stored on the server

View File

@ -6120,7 +6120,7 @@ void Td::on_request(uint64 id, const td_api::getChatFilter &request) {
void Td::on_request(uint64 id, const td_api::getRecommendedChatFilters &request) { void Td::on_request(uint64 id, const td_api::getRecommendedChatFilters &request) {
CHECK_IS_USER(); CHECK_IS_USER();
CREATE_REQUEST_PROMISE(); CREATE_REQUEST_PROMISE();
messages_manager_->get_recommended_dialog_filters(std::move(promise)); dialog_filter_manager_->get_recommended_dialog_filters(std::move(promise));
} }
void Td::on_request(uint64 id, td_api::createChatFilter &request) { void Td::on_request(uint64 id, td_api::createChatFilter &request) {