From f79e67cab8a86d37b425d60449963141025d894a Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 27 Oct 2022 19:03:58 +0300 Subject: [PATCH] Apply edited data from service message about forum topics. --- td/telegram/ForumTopicEditedData.h | 2 ++ td/telegram/ForumTopicIcon.cpp | 8 ++++++++ td/telegram/ForumTopicIcon.h | 2 ++ td/telegram/ForumTopicInfo.cpp | 16 ++++++++++++++++ td/telegram/ForumTopicInfo.h | 3 +++ td/telegram/ForumTopicManager.cpp | 9 +++++++++ td/telegram/ForumTopicManager.h | 4 ++++ td/telegram/MessageContent.cpp | 16 ++++++++++++++++ td/telegram/MessageContent.h | 3 +++ td/telegram/MessagesManager.cpp | 1 + 10 files changed, 64 insertions(+) diff --git a/td/telegram/ForumTopicEditedData.h b/td/telegram/ForumTopicEditedData.h index db2f416cc..9c310275f 100644 --- a/td/telegram/ForumTopicEditedData.h +++ b/td/telegram/ForumTopicEditedData.h @@ -25,6 +25,8 @@ class ForumTopicEditedData { friend StringBuilder &operator<<(StringBuilder &string_builder, const ForumTopicEditedData &topic_edited_data); + friend class ForumTopicInfo; + public: ForumTopicEditedData() = default; diff --git a/td/telegram/ForumTopicIcon.cpp b/td/telegram/ForumTopicIcon.cpp index 395bbae02..556f1b8cb 100644 --- a/td/telegram/ForumTopicIcon.cpp +++ b/td/telegram/ForumTopicIcon.cpp @@ -12,6 +12,14 @@ ForumTopicIcon::ForumTopicIcon(int32 color, int64 custom_emoji_id) : color_(color & 0xFFFFFF), custom_emoji_id_(custom_emoji_id) { } +bool ForumTopicIcon::edit_custom_emoji_id(CustomEmojiId custom_emoji_id) { + if (custom_emoji_id_ != custom_emoji_id) { + custom_emoji_id_ = custom_emoji_id; + return true; + } + return false; +} + td_api::object_ptr ForumTopicIcon::get_forum_topic_icon_object() const { return td_api::make_object(color_, custom_emoji_id_.get()); } diff --git a/td/telegram/ForumTopicIcon.h b/td/telegram/ForumTopicIcon.h index 8ba9af1cb..3f7932916 100644 --- a/td/telegram/ForumTopicIcon.h +++ b/td/telegram/ForumTopicIcon.h @@ -26,6 +26,8 @@ class ForumTopicIcon { ForumTopicIcon() = default; ForumTopicIcon(int32 color, int64 custom_emoji_id); + bool edit_custom_emoji_id(CustomEmojiId custom_emoji_id); + td_api::object_ptr get_forum_topic_icon_object() const; template diff --git a/td/telegram/ForumTopicInfo.cpp b/td/telegram/ForumTopicInfo.cpp index 7e84c8004..e24577d9e 100644 --- a/td/telegram/ForumTopicInfo.cpp +++ b/td/telegram/ForumTopicInfo.cpp @@ -35,6 +35,22 @@ ForumTopicInfo::ForumTopicInfo(const tl_object_ptr &fo } } +bool ForumTopicInfo::apply_edited_data(const ForumTopicEditedData &edited_data) { + bool is_changed = false; + if (!edited_data.title_.empty() && edited_data.title_ != title_) { + title_ = edited_data.title_; + is_changed = true; + } + if (edited_data.edit_icon_custom_emoji_id_ && icon_.edit_custom_emoji_id(edited_data.icon_custom_emoji_id_)) { + is_changed = true; + } + if (edited_data.edit_is_closed_ && edited_data.is_closed_ != is_closed_) { + is_closed_ = edited_data.is_closed_; + is_changed = true; + } + return is_changed; +} + td_api::object_ptr ForumTopicInfo::get_forum_topic_info_object(Td *td) const { if (is_empty()) { return nullptr; diff --git a/td/telegram/ForumTopicInfo.h b/td/telegram/ForumTopicInfo.h index 71871b376..924144cd4 100644 --- a/td/telegram/ForumTopicInfo.h +++ b/td/telegram/ForumTopicInfo.h @@ -7,6 +7,7 @@ #pragma once #include "td/telegram/DialogId.h" +#include "td/telegram/ForumTopicEditedData.h" #include "td/telegram/ForumTopicIcon.h" #include "td/telegram/MessageId.h" #include "td/telegram/td_api.h" @@ -54,6 +55,8 @@ class ForumTopicInfo { return top_thread_message_id_; } + bool apply_edited_data(const ForumTopicEditedData &edited_data); + td_api::object_ptr get_forum_topic_info_object(Td *td) const; }; diff --git a/td/telegram/ForumTopicManager.cpp b/td/telegram/ForumTopicManager.cpp index fe79b8ea8..b54a0258c 100644 --- a/td/telegram/ForumTopicManager.cpp +++ b/td/telegram/ForumTopicManager.cpp @@ -264,6 +264,15 @@ void ForumTopicManager::toggle_forum_topic_is_closed(DialogId dialog_id, Message td_->create_handler(std::move(promise))->send(channel_id, top_thread_message_id, is_closed); } +void ForumTopicManager::on_forum_topic_edited(DialogId dialog_id, MessageId top_thread_message_id, + const ForumTopicEditedData &edited_data) { + auto topic_info = get_topic_info(dialog_id, top_thread_message_id); + if (topic_info == nullptr) { + return; + } + topic_info->apply_edited_data(edited_data); +} + Status ForumTopicManager::is_forum(DialogId dialog_id) { if (!td_->messages_manager_->have_dialog_force(dialog_id, "ForumTopicManager::is_forum")) { return Status::Error(400, "Chat not found"); diff --git a/td/telegram/ForumTopicManager.h b/td/telegram/ForumTopicManager.h index 3019eb15e..a7aa66083 100644 --- a/td/telegram/ForumTopicManager.h +++ b/td/telegram/ForumTopicManager.h @@ -8,6 +8,7 @@ #include "td/telegram/CustomEmojiId.h" #include "td/telegram/DialogId.h" +#include "td/telegram/ForumTopicEditedData.h" #include "td/telegram/ForumTopicInfo.h" #include "td/telegram/td_api.h" @@ -42,6 +43,9 @@ class ForumTopicManager final : public Actor { void toggle_forum_topic_is_closed(DialogId dialog_id, MessageId top_thread_message_id, bool is_closed, Promise &&promise); + void on_forum_topic_edited(DialogId dialog_id, MessageId top_thread_message_id, + const ForumTopicEditedData &edited_data); + private: static constexpr size_t MAX_FORUM_TOPIC_TITLE_LENGTH = 128; // server side limit for forum topic title diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 4955787cc..f77e0c6b6 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -33,6 +33,7 @@ #include "td/telegram/ForumTopicEditedData.hpp" #include "td/telegram/ForumTopicIcon.h" #include "td/telegram/ForumTopicIcon.hpp" +#include "td/telegram/ForumTopicManager.h" #include "td/telegram/Game.h" #include "td/telegram/Game.hpp" #include "td/telegram/Global.h" @@ -6102,6 +6103,21 @@ void add_message_content_dependencies(Dependencies &dependencies, const MessageC add_formatted_text_dependencies(dependencies, get_message_content_text(message_content)); } +void update_forum_topic_info_by_service_message_content(Td *td, const MessageContent *content, DialogId dialog_id, + MessageId top_thread_message_id) { + if (!top_thread_message_id.is_valid()) { + return; + } + switch (content->get_type()) { + case MessageContentType::TopicEdit: + return td->forum_topic_manager_->on_forum_topic_edited( + dialog_id, top_thread_message_id, static_cast(content)->edited_data); + default: + // nothing to do + return; + } +} + void on_sent_message_content(Td *td, const MessageContent *content) { switch (content->get_type()) { case MessageContentType::Animation: diff --git a/td/telegram/MessageContent.h b/td/telegram/MessageContent.h index 2d759cb4f..22438bc88 100644 --- a/td/telegram/MessageContent.h +++ b/td/telegram/MessageContent.h @@ -252,6 +252,9 @@ void update_failed_to_send_message_content(Td *td, unique_ptr &c void add_message_content_dependencies(Dependencies &dependencies, const MessageContent *message_content); +void update_forum_topic_info_by_service_message_content(Td *td, const MessageContent *content, DialogId dialog_id, + MessageId top_thread_message_id); + void on_sent_message_content(Td *td, const MessageContent *content); void move_message_content_sticker_set_to_top(Td *td, const MessageContent *content); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 267bb87c3..89e88b23a 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -35644,6 +35644,7 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq if (from_update) { speculatively_update_active_group_call_id(d, m); speculatively_update_channel_participants(dialog_id, m); + update_forum_topic_info_by_service_message_content(td_, m->content.get(), dialog_id, m->top_thread_message_id); update_sent_message_contents(dialog_id, m); update_used_hashtags(dialog_id, m); update_top_dialogs(dialog_id, m);