From 3c842f1ecbaf1bb2533466dc15ee80636c6b2bdd Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 6 Dec 2022 15:23:11 +0300 Subject: [PATCH] Improve get_dialog_notification_settings(tl_object_ptr). --- td/telegram/DialogNotificationSettings.cpp | 30 ++++++++++++++++------ td/telegram/DialogNotificationSettings.h | 5 +--- td/telegram/ForumTopic.cpp | 8 +++--- td/telegram/ForumTopic.h | 3 ++- td/telegram/ForumTopicManager.cpp | 13 +++++----- td/telegram/MessagesManager.cpp | 6 ++--- 6 files changed, 38 insertions(+), 27 deletions(-) diff --git a/td/telegram/DialogNotificationSettings.cpp b/td/telegram/DialogNotificationSettings.cpp index fc1d6d398..9502df6dd 100644 --- a/td/telegram/DialogNotificationSettings.cpp +++ b/td/telegram/DialogNotificationSettings.cpp @@ -69,18 +69,32 @@ Result get_dialog_notification_settings( } DialogNotificationSettings get_dialog_notification_settings(tl_object_ptr &&settings, - bool old_use_default_disable_pinned_message_notifications, - bool old_disable_pinned_message_notifications, - bool old_use_default_disable_mention_notifications, - bool old_disable_mention_notifications) { - if (settings == nullptr) { - return DialogNotificationSettings(); + const DialogNotificationSettings *old_settings) { + bool old_use_default_disable_pinned_message_notifications = true; + bool old_disable_pinned_message_notifications = false; + bool old_use_default_disable_mention_notifications = true; + bool old_disable_mention_notifications = false; + if (old_settings != nullptr) { + old_use_default_disable_pinned_message_notifications = + old_settings->use_default_disable_pinned_message_notifications; + old_disable_pinned_message_notifications = old_settings->disable_pinned_message_notifications; + old_use_default_disable_mention_notifications = old_settings->use_default_disable_mention_notifications; + old_disable_mention_notifications = old_settings->disable_mention_notifications; } + + if (settings == nullptr) { + auto result = DialogNotificationSettings(); + result.use_default_disable_pinned_message_notifications = old_use_default_disable_pinned_message_notifications; + result.disable_pinned_message_notifications = old_disable_pinned_message_notifications; + result.use_default_disable_mention_notifications = old_use_default_disable_mention_notifications; + result.disable_mention_notifications = old_disable_mention_notifications; + return result; + } + bool use_default_mute_until = (settings->flags_ & telegram_api::peerNotifySettings::MUTE_UNTIL_MASK) == 0; bool use_default_show_preview = (settings->flags_ & telegram_api::peerNotifySettings::SHOW_PREVIEWS_MASK) == 0; auto mute_until = use_default_mute_until || settings->mute_until_ <= G()->unix_time() ? 0 : settings->mute_until_; - bool silent_send_message = - (settings->flags_ & telegram_api::peerNotifySettings::SILENT_MASK) == 0 ? false : settings->silent_; + bool silent_send_message = settings->silent_; return {use_default_mute_until, mute_until, get_notification_sound(settings.get()), diff --git a/td/telegram/DialogNotificationSettings.h b/td/telegram/DialogNotificationSettings.h index 3222f2a8a..e566e56e3 100644 --- a/td/telegram/DialogNotificationSettings.h +++ b/td/telegram/DialogNotificationSettings.h @@ -64,10 +64,7 @@ Result get_dialog_notification_settings( td_api::object_ptr &¬ification_settings, bool old_silent_send_message); DialogNotificationSettings get_dialog_notification_settings(tl_object_ptr &&settings, - bool old_use_default_disable_pinned_message_notifications, - bool old_disable_pinned_message_notifications, - bool old_use_default_disable_mention_notifications, - bool old_disable_mention_notifications); + const DialogNotificationSettings *old_settings); bool are_default_dialog_notification_settings(const DialogNotificationSettings &settings, bool compare_sound); diff --git a/td/telegram/ForumTopic.cpp b/td/telegram/ForumTopic.cpp index af1588af5..91cd6f204 100644 --- a/td/telegram/ForumTopic.cpp +++ b/td/telegram/ForumTopic.cpp @@ -16,7 +16,8 @@ namespace td { -ForumTopic::ForumTopic(Td *td, tl_object_ptr &&forum_topic_ptr) { +ForumTopic::ForumTopic(Td *td, tl_object_ptr &&forum_topic_ptr, + const DialogNotificationSettings *current_notification_settings) { CHECK(forum_topic_ptr != nullptr); if (forum_topic_ptr->get_id() != telegram_api::forumTopic::ID) { LOG(INFO) << "Receive " << to_string(forum_topic_ptr); @@ -27,7 +28,7 @@ ForumTopic::ForumTopic(Td *td, tl_object_ptr &&forum_t is_short_ = forum_topic->short_; is_pinned_ = forum_topic->pinned_; notification_settings_ = - get_dialog_notification_settings(std::move(forum_topic->notify_settings_), true, false, true, false); + get_dialog_notification_settings(std::move(forum_topic->notify_settings_), current_notification_settings); draft_message_ = get_draft_message(td->contacts_manager_.get(), std::move(forum_topic->draft_)); if (is_short_) { @@ -49,7 +50,8 @@ td_api::object_ptr ForumTopic::get_forum_topic_object(Td *td } // TODO draft_message = can_send_message(dialog_id, info_.get_top_thread_message_id()).is_ok() ? ... : nullptr; - auto last_message = td->messages_manager_->get_message_object({dialog_id, last_message_id_}, "get_forum_topic_object"); + auto last_message = + td->messages_manager_->get_message_object({dialog_id, last_message_id_}, "get_forum_topic_object"); auto draft_message = get_draft_message_object(draft_message_); return td_api::make_object( info.get_forum_topic_info_object(td), std::move(last_message), is_pinned_, unread_count_, diff --git a/td/telegram/ForumTopic.h b/td/telegram/ForumTopic.h index 0c9c501d0..d975b09cb 100644 --- a/td/telegram/ForumTopic.h +++ b/td/telegram/ForumTopic.h @@ -34,7 +34,8 @@ class ForumTopic { public: ForumTopic() = default; - ForumTopic(Td *td, tl_object_ptr &&forum_topic_ptr); + ForumTopic(Td *td, tl_object_ptr &&forum_topic_ptr, + const DialogNotificationSettings *current_notification_settings); bool is_short() const { return is_short_; diff --git a/td/telegram/ForumTopicManager.cpp b/td/telegram/ForumTopicManager.cpp index cc9ac70f3..cbae6290a 100644 --- a/td/telegram/ForumTopicManager.cpp +++ b/td/telegram/ForumTopicManager.cpp @@ -410,10 +410,7 @@ void ForumTopicManager::on_update_forum_topic_notify_settings( return; } - auto notification_settings = get_dialog_notification_settings( - std::move(peer_notify_settings), current_settings->use_default_disable_pinned_message_notifications, - current_settings->disable_pinned_message_notifications, - current_settings->use_default_disable_mention_notifications, current_settings->disable_mention_notifications); + auto notification_settings = get_dialog_notification_settings(std::move(peer_notify_settings), current_settings); if (!notification_settings.is_synchronized) { return; } @@ -658,13 +655,15 @@ MessageId ForumTopicManager::on_get_forum_topic(DialogId dialog_id, } case telegram_api::forumTopic::ID: { auto forum_topic_info = td::make_unique(forum_topic); - auto forum_topic_full = td::make_unique(td_, std::move(forum_topic)); + MessageId top_thread_message_id = forum_topic_info->get_top_thread_message_id(); + Topic *topic = add_topic(dialog_id, top_thread_message_id); + auto current_notification_settings = + topic->topic_ == nullptr ? nullptr : topic->topic_->get_notification_settings(); + auto forum_topic_full = td::make_unique(td_, std::move(forum_topic), current_notification_settings); if (forum_topic_full->is_short()) { LOG(ERROR) << "Receive short " << to_string(forum_topic); return MessageId(); } - MessageId top_thread_message_id = forum_topic_info->get_top_thread_message_id(); - Topic *topic = add_topic(dialog_id, top_thread_message_id); if (topic->topic_ == nullptr || true) { topic->topic_ = std::move(forum_topic_full); topic->need_save_to_database_ = true; // temporary diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 03611f033..f45b4e508 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -8445,10 +8445,8 @@ void MessagesManager::on_update_dialog_notify_settings( return; } - DialogNotificationSettings notification_settings = ::td::get_dialog_notification_settings( - std::move(peer_notify_settings), current_settings->use_default_disable_pinned_message_notifications, - current_settings->disable_pinned_message_notifications, - current_settings->use_default_disable_mention_notifications, current_settings->disable_mention_notifications); + DialogNotificationSettings notification_settings = + ::td::get_dialog_notification_settings(std::move(peer_notify_settings), current_settings); if (!notification_settings.is_synchronized) { return; }