From 01b319f22bc815bb9c43fc26b704274a88bba378 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 1 Jun 2020 18:05:32 +0300 Subject: [PATCH] Add recommended filters at the beginning of the list. GitOrigin-RevId: 850744bddec52d19952a599b3ac9a8fca4667bf9 --- td/telegram/MessagesManager.cpp | 64 +++++++++++++++++++++++++-------- td/telegram/MessagesManager.h | 3 +- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index dca907a51..ddf8f754a 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -4994,25 +4994,40 @@ struct MessagesManager::DialogFilter { InputDialogId::get_input_peers(excluded_dialog_ids)); } + static bool is_similar(const DialogFilter &lhs, const DialogFilter &rhs) { + if (lhs.title == rhs.title) { + return true; + } + if (!are_flags_equal(lhs, rhs)) { + return false; + } + + vector empty_input_dialog_ids; + if (InputDialogId::are_equivalent(lhs.excluded_dialog_ids, empty_input_dialog_ids) != + InputDialogId::are_equivalent(rhs.excluded_dialog_ids, empty_input_dialog_ids)) { + return false; + } + if ((InputDialogId::are_equivalent(lhs.pinned_dialog_ids, empty_input_dialog_ids) && + InputDialogId::are_equivalent(lhs.included_dialog_ids, empty_input_dialog_ids)) != + (InputDialogId::are_equivalent(rhs.pinned_dialog_ids, empty_input_dialog_ids) && + InputDialogId::are_equivalent(rhs.included_dialog_ids, empty_input_dialog_ids))) { + return false; + } + + return true; + } + static bool are_equivalent(const DialogFilter &lhs, const DialogFilter &rhs) { return lhs.title == rhs.title && lhs.emoji == rhs.emoji && InputDialogId::are_equivalent(lhs.pinned_dialog_ids, rhs.pinned_dialog_ids) && InputDialogId::are_equivalent(lhs.included_dialog_ids, rhs.included_dialog_ids) && - InputDialogId::are_equivalent(lhs.excluded_dialog_ids, rhs.excluded_dialog_ids) && - lhs.exclude_muted == rhs.exclude_muted && lhs.exclude_read == rhs.exclude_read && - lhs.exclude_archived == rhs.exclude_archived && lhs.include_contacts == rhs.include_contacts && - lhs.include_non_contacts == rhs.include_non_contacts && lhs.include_bots == rhs.include_bots && - lhs.include_groups == rhs.include_groups && lhs.include_channels == rhs.include_channels; + InputDialogId::are_equivalent(lhs.excluded_dialog_ids, rhs.excluded_dialog_ids) && are_flags_equal(lhs, rhs); } friend bool operator==(const DialogFilter &lhs, const DialogFilter &rhs) { return lhs.dialog_filter_id == rhs.dialog_filter_id && lhs.title == rhs.title && lhs.emoji == rhs.emoji && lhs.pinned_dialog_ids == rhs.pinned_dialog_ids && lhs.included_dialog_ids == rhs.included_dialog_ids && - lhs.excluded_dialog_ids == rhs.excluded_dialog_ids && lhs.exclude_muted == rhs.exclude_muted && - lhs.exclude_read == rhs.exclude_read && lhs.exclude_archived == rhs.exclude_archived && - lhs.include_contacts == rhs.include_contacts && lhs.include_non_contacts == rhs.include_non_contacts && - lhs.include_bots == rhs.include_bots && lhs.include_groups == rhs.include_groups && - lhs.include_channels == rhs.include_channels; + lhs.excluded_dialog_ids == rhs.excluded_dialog_ids && are_flags_equal(lhs, rhs); } friend bool operator!=(const DialogFilter &lhs, const DialogFilter &rhs) { @@ -5053,6 +5068,13 @@ struct MessagesManager::DialogFilter { }(); CHECK(is_inited); } + + static bool are_flags_equal(const DialogFilter &lhs, const DialogFilter &rhs) { + return lhs.exclude_muted == rhs.exclude_muted && lhs.exclude_read == rhs.exclude_read && + lhs.exclude_archived == rhs.exclude_archived && lhs.include_contacts == rhs.include_contacts && + lhs.include_non_contacts == rhs.include_non_contacts && lhs.include_bots == rhs.include_bots && + lhs.include_groups == rhs.include_groups && lhs.include_channels == rhs.include_channels; + } }; std::unordered_map MessagesManager::DialogFilter::emoji_to_icon_name_; @@ -11126,7 +11148,7 @@ void MessagesManager::init() { } } for (auto &dialog_filter : log_event.dialog_filters_out) { - add_dialog_filter(std::move(dialog_filter), "binlog"); + add_dialog_filter(std::move(dialog_filter), false, "binlog"); } LOG(INFO) << "Loaded server chat filters " << get_dialog_filter_ids(server_dialog_filters_) << " and local chat filters " << get_dialog_filter_ids(dialog_filters_); @@ -14051,6 +14073,7 @@ void MessagesManager::on_load_recommended_dialog_filters( return td_api::make_object(get_chat_filter_object(filter.dialog_filter.get()), filter.description); }); + recommended_dialog_filters_ = std::move(filters); promise.set_value(td_api::make_object(std::move(chat_filters))); } @@ -14494,7 +14517,7 @@ void MessagesManager::on_get_dialog_filters(Result(*new_server_filter), "on_get_dialog_filters"); + add_dialog_filter(make_unique(*new_server_filter), false, "on_get_dialog_filters"); } else { // the filter was added from this client // after that it could be added from another client, or edited from this client, or edited from another client @@ -15714,7 +15737,14 @@ void MessagesManager::create_dialog_filter(td_api::object_ptr> return true; } -void MessagesManager::add_dialog_filter(unique_ptr dialog_filter, const char *source) { +void MessagesManager::add_dialog_filter(unique_ptr dialog_filter, bool at_beginning, const char *source) { if (td_->auth_manager_->is_bot()) { // just in case return; @@ -15935,7 +15965,11 @@ void MessagesManager::add_dialog_filter(unique_ptr dialog_filter, auto dialog_filter_id = dialog_filter->dialog_filter_id; LOG(INFO) << "Add " << dialog_filter_id << " from " << source; CHECK(get_dialog_filter(dialog_filter_id) == nullptr); - dialog_filters_.push_back(std::move(dialog_filter)); + if (at_beginning) { + dialog_filters_.insert(dialog_filters_.begin(), std::move(dialog_filter)); + } else { + dialog_filters_.push_back(std::move(dialog_filter)); + } auto dialog_list_id = DialogListId(dialog_filter_id); CHECK(dialog_lists_.find(dialog_list_id) == dialog_lists_.end()); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 286fb4c13..420c1d38e 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -2287,7 +2287,7 @@ class MessagesManager : public Actor { void save_dialog_filters(); - void add_dialog_filter(unique_ptr dialog_filter, const char *source); + void add_dialog_filter(unique_ptr dialog_filter, bool at_beginning, const char *source); void edit_dialog_filter(unique_ptr new_dialog_filter, const char *source); @@ -2918,6 +2918,7 @@ class MessagesManager : public Actor { int32 dialog_filters_updated_date_ = 0; vector> server_dialog_filters_; vector> dialog_filters_; + vector recommended_dialog_filters_; std::unordered_map active_get_channel_differencies_; std::unordered_map get_channel_difference_to_logevent_id_;