Move sort_input_dialog_ids to class DialogFilter.

This commit is contained in:
levlam 2023-03-21 16:26:06 +03:00
parent 51c4c1368b
commit 9acf785389
4 changed files with 44 additions and 41 deletions

View File

@ -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> 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<InputDialogId> &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<DialogId, DialogIdHash> 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;

View File

@ -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);

View File

@ -17784,7 +17784,7 @@ void MessagesManager::on_get_dialog_filters(Result<vector<tl_object_ptr<telegram
continue;
}
sort_dialog_filter_input_dialog_ids(dialog_filter.get(), "on_get_dialog_filters 1");
dialog_filter->sort_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(Result<vector<tl_object_ptr<telegram
LOG(INFO) << "Old server filter: " << *old_server_filter;
LOG(INFO) << "New server filter: " << *new_server_filter;
LOG(INFO) << "New local filter: " << *new_filter;
sort_dialog_filter_input_dialog_ids(new_filter.get(), "on_get_dialog_filters 2");
new_filter->sort_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<InputDialogId> &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<DialogId, DialogIdHash> 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<unique_ptr<DialogFilter>> MessagesManager::create_dialog_filter(DialogFilterId dialog_filter_id,
td_api::object_ptr<td_api::chatFilter> filter) {
CHECK(filter != nullptr);
@ -19735,7 +19702,7 @@ Result<unique_ptr<DialogFilter>> 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, vector<D
append(new_dialog_filter->included_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();

View File

@ -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<unique_ptr<DialogFilter>> create_dialog_filter(DialogFilterId dialog_filter_id,
td_api::object_ptr<td_api::chatFilter> filter);