diff --git a/td/telegram/ForumTopic.h b/td/telegram/ForumTopic.h index c13f5cd30..1d0f797d8 100644 --- a/td/telegram/ForumTopic.h +++ b/td/telegram/ForumTopic.h @@ -46,6 +46,14 @@ class ForumTopic { bool update_last_read_inbox_message_id(MessageId last_read_inbox_message_id, int32 unread_count); + bool set_is_pinned(bool is_pinned) { + if (is_pinned_ == is_pinned) { + return false; + } + is_pinned_ = is_pinned; + return true; + } + DialogNotificationSettings *get_notification_settings() { return ¬ification_settings_; } diff --git a/td/telegram/ForumTopicManager.cpp b/td/telegram/ForumTopicManager.cpp index 86169b64d..796e37308 100644 --- a/td/telegram/ForumTopicManager.cpp +++ b/td/telegram/ForumTopicManager.cpp @@ -29,6 +29,7 @@ #include "td/telegram/telegram_api.h" #include "td/telegram/UpdatesManager.h" +#include "td/utils/algorithm.h" #include "td/utils/buffer.h" #include "td/utils/logging.h" #include "td/utils/Random.h" @@ -600,6 +601,53 @@ void ForumTopicManager::on_update_forum_topic_notify_settings( std::move(notification_settings)); } +void ForumTopicManager::on_update_forum_topic_is_pinned(DialogId dialog_id, MessageId top_thread_message_id, + bool is_pinned) { + if (!can_be_forum(dialog_id)) { + LOG(ERROR) << "Receive pinned topics in " << dialog_id; + return; + } + + if (td_->auth_manager_->is_bot()) { + return; + } + + auto topic = get_topic(dialog_id, top_thread_message_id); + if (topic == nullptr || topic->topic_ == nullptr) { + return; + } + if (topic->topic_->set_is_pinned(is_pinned)) { + topic->need_save_to_database_ = true; + save_topic_to_database(dialog_id, topic); + } +} + +void ForumTopicManager::on_update_pinned_forum_topics(DialogId dialog_id, vector top_thread_message_ids) { + if (!can_be_forum(dialog_id)) { + LOG(ERROR) << "Receive pinned topics in " << dialog_id; + return; + } + + if (td_->auth_manager_->is_bot()) { + return; + } + + auto dialog_topics = get_dialog_topics(dialog_id); + if (dialog_topics == nullptr) { + return; + } + + dialog_topics->topics_.foreach([&](const MessageId &top_thread_message_id, unique_ptr &topic) { + if (topic->topic_ == nullptr) { + return; + } + if (topic->topic_->set_is_pinned(contains(top_thread_message_ids, top_thread_message_id))) { + topic->need_save_to_database_ = true; + save_topic_to_database(dialog_id, topic.get()); + } + }); +} + Status ForumTopicManager::set_forum_topic_notification_settings( DialogId dialog_id, MessageId top_thread_message_id, tl_object_ptr &¬ification_settings) { @@ -995,6 +1043,10 @@ ForumTopicManager::DialogTopics *ForumTopicManager::add_dialog_topics(DialogId d return dialog_topics; } +ForumTopicManager::DialogTopics *ForumTopicManager::get_dialog_topics(DialogId dialog_id) { + return dialog_topics_.get_pointer(dialog_id); +} + ForumTopicManager::Topic *ForumTopicManager::add_topic(DialogTopics *dialog_topics, MessageId top_thread_message_id) { auto topic = dialog_topics->topics_.get_pointer(top_thread_message_id); if (topic == nullptr) { @@ -1008,6 +1060,10 @@ ForumTopicManager::Topic *ForumTopicManager::add_topic(DialogTopics *dialog_topi return topic; } +ForumTopicManager::Topic *ForumTopicManager::get_topic(DialogTopics *dialog_topics, MessageId top_thread_message_id) { + return dialog_topics->topics_.get_pointer(top_thread_message_id); +} + ForumTopicManager::Topic *ForumTopicManager::add_topic(DialogId dialog_id, MessageId top_thread_message_id) { return add_topic(add_dialog_topics(dialog_id), top_thread_message_id); } diff --git a/td/telegram/ForumTopicManager.h b/td/telegram/ForumTopicManager.h index 10aad63ac..92a87e648 100644 --- a/td/telegram/ForumTopicManager.h +++ b/td/telegram/ForumTopicManager.h @@ -95,6 +95,10 @@ class ForumTopicManager final : public Actor { tl_object_ptr &&peer_notify_settings, const char *source); + void on_update_forum_topic_is_pinned(DialogId dialog_id, MessageId top_thread_message_id, bool is_pinned); + + void on_update_pinned_forum_topics(DialogId dialog_id, vector top_thread_message_ids); + void on_forum_topic_edited(DialogId dialog_id, MessageId top_thread_message_id, const ForumTopicEditedData &edited_data); @@ -141,8 +145,12 @@ class ForumTopicManager final : public Actor { DialogTopics *add_dialog_topics(DialogId dialog_id); + DialogTopics *get_dialog_topics(DialogId dialog_id); + static Topic *add_topic(DialogTopics *dialog_topics, MessageId top_thread_message_id); + static Topic *get_topic(DialogTopics *dialog_topics, MessageId top_thread_message_id); + Topic *add_topic(DialogId dialog_id, MessageId top_thread_message_id); Topic *get_topic(DialogId dialog_id, MessageId top_thread_message_id); diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 932a42e6a..04fd7aa13 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -3043,6 +3043,27 @@ void UpdatesManager::on_update(tl_object_ptr update, Promise &&promise) { + td_->forum_topic_manager_->on_update_forum_topic_is_pinned( + DialogId(ChannelId(update->channel_id_)), MessageId(ServerMessageId(update->topic_id_)), update->pinned_); + promise.set_value(Unit()); +} + +void UpdatesManager::on_update(tl_object_ptr update, Promise &&promise) { + vector top_thread_message_ids; + for (auto &server_message_id : update->order_) { + auto message_id = MessageId(ServerMessageId(server_message_id)); + if (!message_id.is_valid()) { + LOG(ERROR) << "Receive " << to_string(update); + break; + } + top_thread_message_ids.push_back(message_id); + } + td_->forum_topic_manager_->on_update_pinned_forum_topics(DialogId(ChannelId(update->channel_id_)), + std::move(top_thread_message_ids)); + promise.set_value(Unit()); +} + void UpdatesManager::on_update(tl_object_ptr update, Promise &&promise) { int new_pts = update->pts_; int pts_count = update->pts_count_; @@ -3799,12 +3820,4 @@ void UpdatesManager::on_update(tl_object_ptr update, Promise &&promise) { - promise.set_value(Unit()); -} - -void UpdatesManager::on_update(tl_object_ptr update, Promise &&promise) { - promise.set_value(Unit()); -} - } // namespace td diff --git a/td/telegram/UpdatesManager.h b/td/telegram/UpdatesManager.h index a0baa8689..954b9f30b 100644 --- a/td/telegram/UpdatesManager.h +++ b/td/telegram/UpdatesManager.h @@ -480,6 +480,9 @@ class UpdatesManager final : public Actor { void on_update(tl_object_ptr update, Promise &&promise); void on_update(tl_object_ptr update, Promise &&promise); + void on_update(tl_object_ptr update, Promise &&promise); + void on_update(tl_object_ptr update, Promise &&promise); + void on_update(tl_object_ptr update, Promise &&promise); void on_update(tl_object_ptr update, Promise &&promise); @@ -564,10 +567,6 @@ class UpdatesManager final : public Actor { void on_update(tl_object_ptr update, Promise &&promise); // unsupported updates - - void on_update(tl_object_ptr update, Promise &&promise); - - void on_update(tl_object_ptr update, Promise &&promise); }; } // namespace td