From 0baa1424f91c1f2b0490e68e571a64557adf755d Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 6 Feb 2024 01:21:55 +0300 Subject: [PATCH] Add updateSavedMessagesTopic and more fields about the topic. --- td/generate/scheme/td_api.tl | 11 +++++-- td/telegram/SavedMessagesManager.cpp | 45 ++++++++++++++++++++++++++-- td/telegram/SavedMessagesManager.h | 35 ++++++++++++++++++++++ td/telegram/Td.cpp | 2 ++ 4 files changed, 89 insertions(+), 4 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 89571cfeb..4775df790 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -1902,8 +1902,12 @@ savedMessagesTopicAuthorHidden = SavedMessagesTopic; savedMessagesTopicSavedFromChat chat_id:int53 = SavedMessagesTopic; -//@description Contains information about a found Saved Messages topic @topic The topic @last_message Last message in the topic; may be null if none or unknown -foundSavedMessagesTopic topic:SavedMessagesTopic last_message:message = FoundSavedMessagesTopic; +//@description Contains information about a found Saved Messages topic +//@topic The topic +//@is_pinned True, if the topic is pinned +//@order A parameter used to determine order of the topic in the topic list. Topics must be sorted by the order in descending order +//@last_message Last message in the topic; may be null if none or unknown +foundSavedMessagesTopic topic:SavedMessagesTopic is_pinned:Bool order:int64 last_message:message = FoundSavedMessagesTopic; //@description Contains a list of Saved Messages topics //@total_count Total number of Saved Messages topics found @@ -6506,6 +6510,9 @@ updateChatFolders chat_folders:vector main_chat_list_position:in //@online_member_count New number of online members in the chat, or 0 if unknown updateChatOnlineMemberCount chat_id:int53 online_member_count:int32 = Update; +//@description Basic information about a Saved Messages topic has changed @topic New information about the topic +updateSavedMessagesTopic topic:foundSavedMessagesTopic = Update; + //@description The list of pinned Saved Messages topics has changed. The app can call getPinnedSavedMessagesTopics to get the new list updatePinnedSavedMessagesTopics = Update; diff --git a/td/telegram/SavedMessagesManager.cpp b/td/telegram/SavedMessagesManager.cpp index c7a4681fa..851e5c826 100644 --- a/td/telegram/SavedMessagesManager.cpp +++ b/td/telegram/SavedMessagesManager.cpp @@ -24,6 +24,8 @@ #include "td/utils/format.h" #include "td/utils/logging.h" +#include + namespace td { class GetPinnedSavedDialogsQuery final : public Td::ResultHandler { @@ -366,11 +368,29 @@ void SavedMessagesManager::on_topic_message_deleted(SavedMessagesTopicId saved_m } void SavedMessagesManager::on_topic_changed(SavedMessagesTopicId saved_messages_topic_id, SavedMessagesTopic *topic) { + CHECK(topic != nullptr); if (!topic->is_changed_) { return; } - // TODO send updateSavedMessagesTopic + if (topic->private_order_ != 0) { + bool is_deleted = topic_list_.ordered_topics_.erase({topic->private_order_, saved_messages_topic_id}) > 0; + CHECK(is_deleted); + } + if (topic->pinned_order_ != 0) { + topic->private_order_ = topic->pinned_order_; + } else if (topic->last_message_id_ != MessageId()) { + topic->private_order_ = (static_cast(topic->last_message_date_) << 31) + + topic->last_message_id_.get_prev_server_message_id().get_server_message_id().get(); + } + if (topic->private_order_ != 0) { + bool is_inserted = topic_list_.ordered_topics_.insert({topic->private_order_, saved_messages_topic_id}).second; + CHECK(is_inserted); + } + + send_closure(G()->td(), &Td::send_update, + td_api::make_object( + get_found_saved_messages_topic_object(saved_messages_topic_id, topic))); } void SavedMessagesManager::get_pinned_saved_messages_topics( @@ -552,13 +572,18 @@ void SavedMessagesManager::on_get_saved_messages_topics( td_api::object_ptr SavedMessagesManager::get_found_saved_messages_topic_object( SavedMessagesTopicId saved_messages_topic_id, const SavedMessagesTopic *topic) const { + CHECK(topic != nullptr); td_api::object_ptr last_message_object; if (topic->last_message_id_ != MessageId()) { last_message_object = td_->messages_manager_->get_message_object( {td_->dialog_manager_->get_my_dialog_id(), topic->last_message_id_}, "get_found_saved_messages_topic_object"); } + auto public_order = TopicDate(topic->private_order_, saved_messages_topic_id) <= topic_list_.last_topic_date_ + ? topic->private_order_ + : static_cast(0); return td_api::make_object( - saved_messages_topic_id.get_saved_messages_topic_object(td_), std::move(last_message_object)); + saved_messages_topic_id.get_saved_messages_topic_object(td_), topic->pinned_order_ != 0, public_order, + std::move(last_message_object)); } int64 SavedMessagesManager::get_next_pinned_saved_messages_topic_order() { @@ -816,4 +841,20 @@ void SavedMessagesManager::on_update_pinned_saved_messages_topics() { send_closure(G()->td(), &Td::send_update, td_api::make_object()); } +void SavedMessagesManager::get_current_state(vector> &updates) const { + if (td_->auth_manager_->is_bot()) { + return; + } + + for (const auto &it : saved_messages_topics_) { + const auto *topic = it.second.get(); + updates.push_back(td_api::make_object( + get_found_saved_messages_topic_object(topic->saved_messages_topic_id_, topic))); + } +} + +const SavedMessagesManager::TopicDate SavedMessagesManager::MIN_TOPIC_DATE{std::numeric_limits::max(), + SavedMessagesTopicId()}; +const SavedMessagesManager::TopicDate SavedMessagesManager::MAX_TOPIC_DATE{0, SavedMessagesTopicId()}; + } // namespace td diff --git a/td/telegram/SavedMessagesManager.h b/td/telegram/SavedMessagesManager.h index 0be495f42..fcbf6f5b0 100644 --- a/td/telegram/SavedMessagesManager.h +++ b/td/telegram/SavedMessagesManager.h @@ -16,6 +16,8 @@ #include "td/utils/common.h" #include "td/utils/Promise.h" +#include + namespace td { class Td; @@ -57,6 +59,8 @@ class SavedMessagesManager final : public Actor { void on_update_pinned_saved_messages_topics(); + void get_current_state(vector> &updates) const; + private: static constexpr int32 MAX_GET_HISTORY = 100; // server side limit @@ -65,9 +69,38 @@ class SavedMessagesManager final : public Actor { MessageId last_message_id_; int32 last_message_date_ = 0; int64 pinned_order_ = 0; + int64 private_order_ = 0; bool is_changed_ = true; }; + class TopicDate { + int64 order_; + SavedMessagesTopicId topic_id_; + + public: + TopicDate(int64 order, SavedMessagesTopicId topic_id) : order_(order), topic_id_(topic_id) { + } + + bool operator<(const TopicDate &other) const { + return order_ > other.order_ || + (order_ == other.order_ && topic_id_.get_unique_id() > other.topic_id_.get_unique_id()); + } + + bool operator<=(const TopicDate &other) const { + return order_ > other.order_ || + (order_ == other.order_ && topic_id_.get_unique_id() >= other.topic_id_.get_unique_id()); + } + }; + + static const TopicDate MIN_TOPIC_DATE; + static const TopicDate MAX_TOPIC_DATE; + + struct TopicList { + std::set ordered_topics_; + + TopicDate last_topic_date_ = MIN_TOPIC_DATE; // in memory + }; + void tear_down() final; SavedMessagesTopic *get_topic(SavedMessagesTopicId saved_messages_topic_id); @@ -104,6 +137,8 @@ class SavedMessagesManager final : public Actor { bool are_pinned_saved_messages_topics_inited_ = false; int64 current_pinned_saved_messages_topic_order_ = static_cast(2147000000) << 32; + + TopicList topic_list_; }; } // namespace td diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 89daacf2d..2a622ba3f 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4305,6 +4305,8 @@ void Td::on_request(uint64 id, const td_api::getCurrentState &request) { notification_manager_->get_current_state(updates); + saved_messages_manager_->get_current_state(updates); + story_manager_->get_current_state(updates); config_manager_.get_actor_unsafe()->get_current_state(updates);