From 9f8de1d146712431219e332dda16c0669bb62d4e Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 19 May 2022 16:57:11 +0300 Subject: [PATCH] Add main_chat_list_position to updateChatFilters. --- td/generate/scheme/td_api.tl | 6 +- td/telegram/MessagesManager.cpp | 117 ++++++++++++++++++++++++++++++-- td/telegram/MessagesManager.h | 3 + td/telegram/Version.h | 1 + 4 files changed, 120 insertions(+), 7 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 3bcf14b36..ac34c710a 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4073,8 +4073,8 @@ updateChatIsBlocked chat_id:int53 is_blocked:Bool = Update; //@description A chat was marked as unread or was read @chat_id Chat identifier @is_marked_as_unread New value of is_marked_as_unread updateChatIsMarkedAsUnread chat_id:int53 is_marked_as_unread:Bool = Update; -//@description The list of chat filters or a chat filter has changed @chat_filters The new list of chat filters -updateChatFilters chat_filters:vector = Update; +//@description The list of chat filters or a chat filter has changed @chat_filters The new list of chat filters @main_chat_list_position Position of the main chat list among chat filters, 0-based +updateChatFilters chat_filters:vector main_chat_list_position:int32 = Update; //@description The number of online group members has changed. This update with non-zero number of online group members is sent only for currently opened chats. There is no guarantee that it will be sent just after the number of online users has changed @chat_id Identifier of the chat @online_member_count New number of online members in the chat, or 0 if unknown updateChatOnlineMemberCount chat_id:int53 online_member_count:int32 = Update; @@ -6213,7 +6213,7 @@ addStickerToSet user_id:int53 name:string sticker:inputSticker = StickerSet; setStickerSetThumbnail user_id:int53 name:string thumbnail:InputFile = StickerSet; //@description Changes the position of a sticker in the set to which it belongs; for bots only. The sticker set must have been created by the bot -//@sticker Sticker @position New position of the sticker in the set, zero-based +//@sticker Sticker @position New position of the sticker in the set, 0-based setStickerPositionInSet sticker:InputFile position:int32 = Ok; //@description Removes a sticker from the set to which it belongs; for bots only. The sticker set must have been created by the bot @sticker Sticker diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 7300df945..6b423e4d4 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -13049,6 +13049,7 @@ void MessagesManager::loop() { class MessagesManager::DialogFiltersLogEvent { public: + int32 main_dialog_list_position = 0; int32 updated_date = 0; const vector> *server_dialog_filters_in; const vector> *dialog_filters_in; @@ -13057,16 +13058,48 @@ class MessagesManager::DialogFiltersLogEvent { template void store(StorerT &storer) const { + bool has_main_dialog_list_position = main_dialog_list_position != 0; + bool has_server_dialog_filters = !server_dialog_filters_in->empty(); + bool has_dialog_filters = !dialog_filters_in->empty(); + BEGIN_STORE_FLAGS(); + STORE_FLAG(has_main_dialog_list_position); + STORE_FLAG(has_server_dialog_filters); + STORE_FLAG(has_dialog_filters); + END_STORE_FLAGS(); td::store(updated_date, storer); - td::store(*server_dialog_filters_in, storer); - td::store(*dialog_filters_in, storer); + if (has_server_dialog_filters) { + td::store(*server_dialog_filters_in, storer); + } + if (has_dialog_filters) { + td::store(*dialog_filters_in, storer); + } + if (has_main_dialog_list_position) { + td::store(main_dialog_list_position, storer); + } } template void parse(ParserT &parser) { + bool has_main_dialog_list_position = false; + bool has_server_dialog_filters = true; + bool has_dialog_filters = true; + if (parser.version() >= static_cast(Version::AddMainDialogListPosition)) { + BEGIN_PARSE_FLAGS(); + PARSE_FLAG(has_main_dialog_list_position); + PARSE_FLAG(has_server_dialog_filters); + PARSE_FLAG(has_dialog_filters); + END_PARSE_FLAGS(); + } td::parse(updated_date, parser); - td::parse(server_dialog_filters_out, parser); - td::parse(dialog_filters_out, parser); + if (has_server_dialog_filters) { + td::parse(server_dialog_filters_out, parser); + } + if (has_dialog_filters) { + td::parse(dialog_filters_out, parser); + } + if (has_main_dialog_list_position) { + td::parse(main_dialog_list_position, parser); + } } }; @@ -13124,6 +13157,12 @@ void MessagesManager::init() { if (!dialog_filters.empty()) { DialogFiltersLogEvent log_event; if (log_event_parse(log_event, dialog_filters).is_ok()) { + main_dialog_list_position_ = log_event.main_dialog_list_position; + if (main_dialog_list_position_ != 0 && !G()->shared_config().get_option_boolean("is_premium")) { + LOG(INFO) << "Ignore main chat list position " << main_dialog_list_position_; + main_dialog_list_position_ = 0; + } + dialog_filters_updated_date_ = G()->ignore_background_updates() ? 0 : log_event.updated_date; std::unordered_set server_dialog_filter_ids; for (auto &dialog_filter : log_event.server_dialog_filters_out) { @@ -17121,7 +17160,17 @@ void MessagesManager::on_get_dialog_filters(Result> new_server_dialog_filters; LOG(INFO) << "Receive " << filters.size() << " chat filters from server"; std::unordered_set new_dialog_filter_ids; + int32 server_main_dialog_list_position = -1; + int32 position = 0; for (auto &filter : filters) { + if (filter->get_id() == telegram_api::dialogFilterDefault::ID) { + if (server_main_dialog_list_position == -1) { + server_main_dialog_list_position = position; + } else { + LOG(ERROR) << "Receive duplicate dialogFilterDefault"; + } + continue; + } auto dialog_filter = DialogFilter::get_dialog_filter(std::move(filter), true); if (dialog_filter == nullptr) { continue; @@ -17133,10 +17182,20 @@ void MessagesManager::on_get_dialog_filters(Resultshared_config().get_option_boolean("is_premium")) { + LOG(INFO) << "Ignore server main chat list position " << server_main_dialog_list_position; + server_main_dialog_list_position = 0; } bool is_changed = false; dialog_filters_updated_date_ = G()->unix_time(); + auto old_server_main_dialog_list_position = get_server_main_dialog_list_position(); if (server_dialog_filters_ != new_server_dialog_filters) { LOG(INFO) << "Change server chat filters from " << get_dialog_filter_ids(server_dialog_filters_) << " to " << get_dialog_filter_ids(new_server_dialog_filters); @@ -17225,6 +17284,35 @@ void MessagesManager::on_get_dialog_filters(Resultis_empty(true)) { + current_server_position++; + } + if (current_server_position == server_main_dialog_list_position) { + main_dialog_list_position = current_position; + } + } + if (main_dialog_list_position == -1) { + LOG(INFO) << "Failed to find server position " << server_main_dialog_list_position << " in chat filters"; + main_dialog_list_position = static_cast(dialog_filters_.size()); + } + } + + if (main_dialog_list_position != main_dialog_list_position_) { + LOG(INFO) << "Change main chat list position from " << main_dialog_list_position_ << " to " + << main_dialog_list_position; + main_dialog_list_position_ = main_dialog_list_position; + is_changed = true; + } + } if (is_changed || !is_update_chat_filters_sent_) { send_update_chat_filters(); } @@ -30365,6 +30453,7 @@ void MessagesManager::save_dialog_filters() { } DialogFiltersLogEvent log_event; + log_event.main_dialog_list_position = main_dialog_list_position_; log_event.updated_date = dialog_filters_updated_date_; log_event.server_dialog_filters_in = &server_dialog_filters_; log_event.dialog_filters_in = &dialog_filters_; @@ -37351,6 +37440,25 @@ const DialogFilter *MessagesManager::get_dialog_filter(DialogFilterId dialog_fil return nullptr; } +int32 MessagesManager::get_server_main_dialog_list_position() const { + int32 current_position = 0; + int32 current_server_position = 0; + if (current_position == main_dialog_list_position_) { + return current_server_position; + } + for (const auto &dialog_filter : dialog_filters_) { + current_position++; + if (!dialog_filter->is_empty(true)) { + current_server_position++; + } + if (current_position == main_dialog_list_position_) { + return current_server_position; + } + } + LOG(WARNING) << "Failed to find server position for " << main_dialog_list_position_ << " in chat filters"; + return current_server_position; +} + vector MessagesManager::get_dialog_filter_ids(const vector> &dialog_filters) { return transform(dialog_filters, [](const auto &dialog_filter) { return dialog_filter->dialog_filter_id; }); } @@ -39823,6 +39931,7 @@ td_api::object_ptr MessagesManager::get_update_chat_f for (const auto &filter : dialog_filters_) { update->chat_filters_.push_back(filter->get_chat_filter_info_object()); } + update->main_chat_list_position_ = main_dialog_list_position_; return update; } diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 7d9fb931e..7b4ab205a 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -2794,6 +2794,8 @@ class MessagesManager final : public Actor { DialogFilter *get_dialog_filter(DialogFilterId dialog_filter_id); const DialogFilter *get_dialog_filter(DialogFilterId dialog_filter_id) const; + int32 get_server_main_dialog_list_position() const; + static vector get_dialog_filter_ids(const vector> &dialog_filters); static vector get_dialog_filter_folder_ids(const DialogFilter *filter); @@ -3540,6 +3542,7 @@ class MessagesManager final : public Actor { vector> dialog_filters_; vector recommended_dialog_filters_; vector> dialog_filter_reload_queries_; + int32 main_dialog_list_position_ = 0; FlatHashMap active_get_channel_differencies_; FlatHashMap get_channel_difference_to_log_event_id_; diff --git a/td/telegram/Version.h b/td/telegram/Version.h index bb75b1707..ade2cc932 100644 --- a/td/telegram/Version.h +++ b/td/telegram/Version.h @@ -51,6 +51,7 @@ enum class Version : int32 { AddKeyboardButtonFlags, // 35 AddAudioFlags, UseServerForwardAsCopy, + AddMainDialogListPosition, Next };