From f7921376928e87122877bddbff1af0fbc55e7685 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 22 Mar 2023 01:17:07 +0300 Subject: [PATCH] Add DialogFilter::need_dialog. --- td/telegram/DialogFilter.cpp | 69 +++++++++++++++++++++++++++++++++ td/telegram/DialogFilter.h | 3 ++ td/telegram/MessagesManager.cpp | 67 ++------------------------------ 3 files changed, 75 insertions(+), 64 deletions(-) diff --git a/td/telegram/DialogFilter.cpp b/td/telegram/DialogFilter.cpp index 3ddfff01b..ac0ad0cfc 100644 --- a/td/telegram/DialogFilter.cpp +++ b/td/telegram/DialogFilter.cpp @@ -541,6 +541,75 @@ vector DialogFilter::get_folder_ids() const { return {FolderId::main(), FolderId::archive()}; } +bool DialogFilter::need_dialog(const Td *td, DialogId dialog_id, bool has_unread_mentions, bool is_muted, + bool has_unread_messages, FolderId folder_id) const { + if (InputDialogId::contains(pinned_dialog_ids, dialog_id)) { + return true; + } + if (InputDialogId::contains(included_dialog_ids, dialog_id)) { + return true; + } + if (InputDialogId::contains(excluded_dialog_ids, dialog_id)) { + return false; + } + if (dialog_id.get_type() == DialogType::SecretChat) { + auto user_id = td->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id()); + if (user_id.is_valid()) { + auto user_dialog_id = DialogId(user_id); + if (InputDialogId::contains(pinned_dialog_ids, user_dialog_id)) { + return true; + } + if (InputDialogId::contains(included_dialog_ids, user_dialog_id)) { + return true; + } + if (InputDialogId::contains(excluded_dialog_ids, user_dialog_id)) { + return false; + } + } + } + if (!has_unread_mentions) { + if (exclude_muted && is_muted) { + return false; + } + if (exclude_read && !has_unread_messages) { + return false; + } + } + if (exclude_archived && folder_id == FolderId::archive()) { + return false; + } + switch (dialog_id.get_type()) { + case DialogType::User: { + auto user_id = dialog_id.get_user_id(); + if (td->contacts_manager_->is_user_bot(user_id)) { + return include_bots; + } + if (user_id == td->contacts_manager_->get_my_id() || td->contacts_manager_->is_user_contact(user_id)) { + return include_contacts; + } + return include_non_contacts; + } + case DialogType::Chat: + return include_groups; + case DialogType::Channel: + return td->contacts_manager_->is_broadcast_channel(dialog_id.get_channel_id()) ? include_channels + : include_groups; + case DialogType::SecretChat: { + auto user_id = td->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id()); + if (td->contacts_manager_->is_user_bot(user_id)) { + return include_bots; + } + if (td->contacts_manager_->is_user_contact(user_id)) { + return include_contacts; + } + return include_non_contacts; + } + default: + UNREACHABLE(); + return false; + } +} + vector DialogFilter::get_dialog_filter_ids(const vector> &dialog_filters, int32 main_dialog_list_position) { auto result = transform(dialog_filters, [](const auto &dialog_filter) { return dialog_filter->dialog_filter_id; }); diff --git a/td/telegram/DialogFilter.h b/td/telegram/DialogFilter.h index e0cb76fee..12fcce791 100644 --- a/td/telegram/DialogFilter.h +++ b/td/telegram/DialogFilter.h @@ -93,6 +93,9 @@ class DialogFilter { vector get_folder_ids() const; + bool need_dialog(const Td *td, DialogId dialog_id, bool has_unread_mentions, bool is_muted, bool has_unread_messages, + FolderId folder_id) const; + static vector get_dialog_filter_ids(const vector> &dialog_filters, int32 main_dialog_list_position); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 4a81defc4..2bef53d18 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -39111,70 +39111,9 @@ bool MessagesManager::need_dialog_in_filter(const Dialog *d, const DialogFilter CHECK(filter != nullptr); CHECK(d->order != DEFAULT_ORDER); - if (InputDialogId::contains(filter->pinned_dialog_ids, d->dialog_id)) { - return true; - } - if (InputDialogId::contains(filter->included_dialog_ids, d->dialog_id)) { - return true; - } - if (InputDialogId::contains(filter->excluded_dialog_ids, d->dialog_id)) { - return false; - } - if (d->dialog_id.get_type() == DialogType::SecretChat) { - auto user_id = td_->contacts_manager_->get_secret_chat_user_id(d->dialog_id.get_secret_chat_id()); - if (user_id.is_valid()) { - auto dialog_id = DialogId(user_id); - if (InputDialogId::contains(filter->pinned_dialog_ids, dialog_id)) { - return true; - } - if (InputDialogId::contains(filter->included_dialog_ids, dialog_id)) { - return true; - } - if (InputDialogId::contains(filter->excluded_dialog_ids, dialog_id)) { - return false; - } - } - } - if (d->unread_mention_count == 0 || is_dialog_mention_notifications_disabled(d)) { - if (filter->exclude_muted && is_dialog_muted(d)) { - return false; - } - if (filter->exclude_read && d->server_unread_count + d->local_unread_count == 0 && !d->is_marked_as_unread) { - return false; - } - } - if (filter->exclude_archived && d->folder_id == FolderId::archive()) { - return false; - } - switch (d->dialog_id.get_type()) { - case DialogType::User: { - auto user_id = d->dialog_id.get_user_id(); - if (td_->contacts_manager_->is_user_bot(user_id)) { - return filter->include_bots; - } - if (user_id == td_->contacts_manager_->get_my_id() || td_->contacts_manager_->is_user_contact(user_id)) { - return filter->include_contacts; - } - return filter->include_non_contacts; - } - case DialogType::Chat: - return filter->include_groups; - case DialogType::Channel: - return is_broadcast_channel(d->dialog_id) ? filter->include_channels : filter->include_groups; - case DialogType::SecretChat: { - auto user_id = td_->contacts_manager_->get_secret_chat_user_id(d->dialog_id.get_secret_chat_id()); - if (td_->contacts_manager_->is_user_bot(user_id)) { - return filter->include_bots; - } - if (td_->contacts_manager_->is_user_contact(user_id)) { - return filter->include_contacts; - } - return filter->include_non_contacts; - } - default: - UNREACHABLE(); - return false; - } + return filter->need_dialog( + td_, d->dialog_id, d->unread_mention_count != 0 && !is_dialog_mention_notifications_disabled(d), + is_dialog_muted(d), d->server_unread_count + d->local_unread_count != 0 || d->is_marked_as_unread, d->folder_id); } bool MessagesManager::need_dialog_in_list(const Dialog *d, const DialogList &list) const {