diff --git a/td/telegram/DialogFilter.cpp b/td/telegram/DialogFilter.cpp index 42e8e23ac..9add01446 100644 --- a/td/telegram/DialogFilter.cpp +++ b/td/telegram/DialogFilter.cpp @@ -6,8 +6,10 @@ // #include "td/telegram/DialogFilter.h" +#include "td/telegram/ContactsManager.h" #include "td/telegram/DialogId.h" #include "td/telegram/Global.h" +#include "td/telegram/Td.h" #include "td/utils/algorithm.h" #include "td/utils/emoji.h" @@ -408,6 +410,38 @@ unique_ptr DialogFilter::merge_dialog_filter_changes(const DialogF return new_filter; } +void DialogFilter::sort_input_dialog_ids(const Td *td, const char *source) { + if (!include_contacts && !include_non_contacts && !include_bots && !include_groups && !include_channels) { + excluded_dialog_ids.clear(); + } + + auto sort_input_dialog_ids = [contacts_manager = + td->contacts_manager_.get()](vector &input_dialog_ids) { + std::sort(input_dialog_ids.begin(), input_dialog_ids.end(), + [contacts_manager](InputDialogId lhs, InputDialogId rhs) { + auto get_order = [contacts_manager](InputDialogId input_dialog_id) { + auto dialog_id = input_dialog_id.get_dialog_id(); + if (dialog_id.get_type() != DialogType::SecretChat) { + return dialog_id.get() * 10; + } + auto user_id = contacts_manager->get_secret_chat_user_id(dialog_id.get_secret_chat_id()); + return DialogId(user_id).get() * 10 + 1; + }; + return get_order(lhs) < get_order(rhs); + }); + }; + + sort_input_dialog_ids(excluded_dialog_ids); + sort_input_dialog_ids(included_dialog_ids); + + FlatHashSet all_dialog_ids; + for_each_dialog([&](const InputDialogId &input_dialog_id) { + auto dialog_id = input_dialog_id.get_dialog_id(); + CHECK(dialog_id.is_valid()); + LOG_CHECK(all_dialog_ids.insert(dialog_id).second) << source << ' ' << dialog_id << ' ' << *this; + }); +} + bool DialogFilter::are_similar(const DialogFilter &lhs, const DialogFilter &rhs) { if (lhs.title == rhs.title) { return true; diff --git a/td/telegram/DialogFilter.h b/td/telegram/DialogFilter.h index 4f248443e..9a5fef2f5 100644 --- a/td/telegram/DialogFilter.h +++ b/td/telegram/DialogFilter.h @@ -20,6 +20,8 @@ namespace td { +class Td; + class DialogFilter { public: DialogFilterId dialog_filter_id; @@ -71,6 +73,8 @@ class DialogFilter { const DialogFilter *old_server_filter, const DialogFilter *new_server_filter); + void sort_input_dialog_ids(const Td *td, const char *source); + static bool are_similar(const DialogFilter &lhs, const DialogFilter &rhs); static bool are_equivalent(const DialogFilter &lhs, const DialogFilter &rhs); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 0e7ef61dd..e3a0ca748 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -17784,7 +17784,7 @@ void MessagesManager::on_get_dialog_filters(Resultsort_input_dialog_ids(td_, "on_get_dialog_filters 1"); new_server_dialog_filters.push_back(std::move(dialog_filter)); position++; } @@ -17826,7 +17826,7 @@ void MessagesManager::on_get_dialog_filters(Resultsort_input_dialog_ids(td_, "on_get_dialog_filters 2"); if (*new_filter != *old_filter) { is_changed = true; edit_dialog_filter(std::move(new_filter), "on_get_dialog_filters"); @@ -19644,39 +19644,6 @@ InputDialogId MessagesManager::get_input_dialog_id(DialogId dialog_id) const { } } -void MessagesManager::sort_dialog_filter_input_dialog_ids(DialogFilter *dialog_filter, const char *source) const { - auto sort_input_dialog_ids = [contacts_manager = - td_->contacts_manager_.get()](vector &input_dialog_ids) { - std::sort(input_dialog_ids.begin(), input_dialog_ids.end(), - [contacts_manager](InputDialogId lhs, InputDialogId rhs) { - auto get_order = [contacts_manager](InputDialogId input_dialog_id) { - auto dialog_id = input_dialog_id.get_dialog_id(); - if (dialog_id.get_type() != DialogType::SecretChat) { - return dialog_id.get() * 10; - } - auto user_id = contacts_manager->get_secret_chat_user_id(dialog_id.get_secret_chat_id()); - return DialogId(user_id).get() * 10 + 1; - }; - return get_order(lhs) < get_order(rhs); - }); - }; - - if (!dialog_filter->include_contacts && !dialog_filter->include_non_contacts && !dialog_filter->include_bots && - !dialog_filter->include_groups && !dialog_filter->include_channels) { - dialog_filter->excluded_dialog_ids.clear(); - } - - sort_input_dialog_ids(dialog_filter->excluded_dialog_ids); - sort_input_dialog_ids(dialog_filter->included_dialog_ids); - - FlatHashSet all_dialog_ids; - dialog_filter->for_each_dialog([&](const InputDialogId &input_dialog_id) { - auto dialog_id = input_dialog_id.get_dialog_id(); - CHECK(dialog_id.is_valid()); - LOG_CHECK(all_dialog_ids.insert(dialog_id).second) << source << ' ' << dialog_id << ' ' << dialog_filter; - }); -} - Result> MessagesManager::create_dialog_filter(DialogFilterId dialog_filter_id, td_api::object_ptr filter) { CHECK(filter != nullptr); @@ -19735,7 +19702,7 @@ Result> MessagesManager::create_dialog_filter(DialogFil dialog_filter->include_channels = filter->include_channels_; TRY_STATUS(dialog_filter->check_limits()); - sort_dialog_filter_input_dialog_ids(dialog_filter.get(), "create_dialog_filter"); + dialog_filter->sort_input_dialog_ids(td_, "create_dialog_filter"); return std::move(dialog_filter); } @@ -20579,7 +20546,7 @@ Status MessagesManager::toggle_dialog_is_pinned(DialogListId dialog_list_id, Dia } TRY_STATUS(new_dialog_filter->check_limits()); - sort_dialog_filter_input_dialog_ids(new_dialog_filter.get(), "toggle_dialog_is_pinned"); + new_dialog_filter->sort_input_dialog_ids(td_, "toggle_dialog_is_pinned"); edit_dialog_filter(std::move(new_dialog_filter), "toggle_dialog_is_pinned"); save_dialog_filters(); @@ -20737,7 +20704,7 @@ Status MessagesManager::set_pinned_dialogs(DialogListId dialog_list_id, vectorincluded_dialog_ids, old_pinned_dialog_ids); TRY_STATUS(new_dialog_filter->check_limits()); - sort_dialog_filter_input_dialog_ids(new_dialog_filter.get(), "set_pinned_dialogs"); + new_dialog_filter->sort_input_dialog_ids(td_, "set_pinned_dialogs"); edit_dialog_filter(std::move(new_dialog_filter), "set_pinned_dialogs"); save_dialog_filters(); @@ -34607,7 +34574,7 @@ void MessagesManager::add_dialog_to_list(DialogId dialog_id, DialogListId dialog if (status.is_error()) { return promise.set_error(std::move(status)); } - sort_dialog_filter_input_dialog_ids(new_dialog_filter.get(), "add_dialog_to_list"); + new_dialog_filter->sort_input_dialog_ids(td_, "add_dialog_to_list"); edit_dialog_filter(std::move(new_dialog_filter), "add_dialog_to_list"); save_dialog_filters(); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 3e8c4e32e..3cd21a270 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -2861,8 +2861,6 @@ class MessagesManager final : public Actor { InputDialogId get_input_dialog_id(DialogId dialog_id) const; - void sort_dialog_filter_input_dialog_ids(DialogFilter *dialog_filter, const char *source) const; - Result> create_dialog_filter(DialogFilterId dialog_filter_id, td_api::object_ptr filter);