From c1309d7657b2e1d9920c7ab5f8f3450ac2471f55 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 12 Oct 2020 11:33:16 +0300 Subject: [PATCH] Do not return deleted secret chats and replace migrated basic groups with corresponding supergroups in recently found chats. GitOrigin-RevId: e50390583e3956a4eb8fdb92ec8a82f04cc7914f --- td/telegram/ContactsManager.cpp | 8 ++++++ td/telegram/ContactsManager.h | 1 + td/telegram/MessagesManager.cpp | 49 ++++++++++++++++++++++++++++++++- td/telegram/MessagesManager.h | 5 +++- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index deec1976e..3afc4822f 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -12815,6 +12815,14 @@ bool ContactsManager::get_chat_is_active(ChatId chat_id) const { return c->is_active; } +ChannelId ContactsManager::get_chat_migrated_to_channel_id(ChatId chat_id) const { + auto c = get_chat(chat_id); + if (c == nullptr) { + return ChannelId(); + } + return c->migrated_to_channel_id; +} + DialogParticipantStatus ContactsManager::get_chat_status(ChatId chat_id) const { auto c = get_chat(chat_id); if (c == nullptr) { diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index c43beeeee..197fed8c8 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -464,6 +464,7 @@ class ContactsManager : public Actor { void reload_chat_full(ChatId chat_id, Promise &&promise); bool get_chat_is_active(ChatId chat_id) const; + ChannelId get_chat_migrated_to_channel_id(ChatId chat_id) const; DialogParticipantStatus get_chat_status(ChatId chat_id) const; DialogParticipantStatus get_chat_permissions(ChatId chat_id) const; bool is_appointed_chat_administrator(ChatId chat_id) const; diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 8fea014fb..08c3d7cb8 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -15404,6 +15404,9 @@ std::pair> MessagesManager::search_dialogs(const string } promise.set_value(Unit()); + + update_recently_found_dialogs(); + size_t result_size = min(static_cast(limit), recently_found_dialog_ids_.size()); return {narrow_cast(recently_found_dialog_ids_.size()), vector(recently_found_dialog_ids_.begin(), recently_found_dialog_ids_.begin() + result_size)}; @@ -35219,6 +35222,7 @@ void MessagesManager::save_recently_found_dialogs() { } value += to_string(dialog_id.get()); } + LOG(DEBUG) << "Save recently found chats " << value; G()->td_db()->get_binlog_pmc()->set("recently_found_dialog_usernames_and_ids", value); } @@ -35237,6 +35241,7 @@ bool MessagesManager::load_recently_found_dialogs(Promise &promise) { return true; } + LOG(DEBUG) << "Loaded recently found chats " << found_dialogs_str; auto found_dialogs = full_split(found_dialogs_str, ','); if (recently_found_dialogs_loaded_ == 1 && resolve_recently_found_dialogs_multipromise_.promise_count() == 0) { // queries was sent and have already been finished @@ -35342,7 +35347,7 @@ bool MessagesManager::add_recently_found_dialog_internal(DialogId dialog_id) { // TODO create function auto it = std::find(recently_found_dialog_ids_.begin(), recently_found_dialog_ids_.end(), dialog_id); if (it == recently_found_dialog_ids_.end()) { - if (narrow_cast(recently_found_dialog_ids_.size()) == MAX_RECENT_FOUND_DIALOGS) { + if (narrow_cast(recently_found_dialog_ids_.size()) == MAX_RECENTLY_FOUND_DIALOGS) { CHECK(!recently_found_dialog_ids_.empty()); recently_found_dialog_ids_.back() = dialog_id; } else { @@ -35359,6 +35364,48 @@ bool MessagesManager::remove_recently_found_dialog_internal(DialogId dialog_id) return td::remove(recently_found_dialog_ids_, dialog_id); } +void MessagesManager::update_recently_found_dialogs() { + vector dialog_ids; + for (auto dialog_id : recently_found_dialog_ids_) { + const Dialog *d = get_dialog(dialog_id); + if (d == nullptr) { + continue; + } + switch (dialog_id.get_type()) { + case DialogType::User: + // always keep + break; + case DialogType::Chat: { + auto channel_id = td_->contacts_manager_->get_chat_migrated_to_channel_id(dialog_id.get_chat_id()); + if (channel_id.is_valid() && get_dialog(DialogId(channel_id)) != nullptr) { + dialog_id = DialogId(channel_id); + } + break; + } + case DialogType::Channel: + // always keep + break; + case DialogType::SecretChat: + if (is_deleted_secret_chat(d)) { + dialog_id = DialogId(); + } + break; + case DialogType::None: + default: + UNREACHABLE(); + break; + } + if (dialog_id.is_valid()) { + dialog_ids.push_back(dialog_id); + } + } + + if (dialog_ids != recently_found_dialog_ids_) { + recently_found_dialog_ids_ = std::move(dialog_ids); + save_recently_found_dialogs(); + } +} + void MessagesManager::suffix_load_loop(Dialog *d) { if (d->suffix_load_has_query_) { return; diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index f6a0333c2..6ee4299a4 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -1644,7 +1644,7 @@ class MessagesManager : public Actor { static constexpr int32 MIN_CHANNEL_DIFFERENCE = 10; static constexpr int32 MAX_CHANNEL_DIFFERENCE = 100; static constexpr int32 MAX_BOT_CHANNEL_DIFFERENCE = 100000; // server side limit - static constexpr int32 MAX_RECENT_FOUND_DIALOGS = 20; // some reasonable value + static constexpr int32 MAX_RECENTLY_FOUND_DIALOGS = 30; // some reasonable value static constexpr size_t MAX_TITLE_LENGTH = 128; // server side limit for chat title static constexpr size_t MAX_DESCRIPTION_LENGTH = 255; // server side limit for chat description static constexpr size_t MAX_DIALOG_FILTER_TITLE_LENGTH = 12; // server side limit for dialog filter title @@ -2831,7 +2831,10 @@ class MessagesManager : public Actor { bool remove_recently_found_dialog_internal(DialogId dialog_id); + void update_recently_found_dialogs(); + void save_recently_found_dialogs(); + bool load_recently_found_dialogs(Promise &promise); void reget_message_from_server_if_needed(DialogId dialog_id, const Message *m);