From 6b97b9289e5caa368f86a5c77be2d138032cab6e Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 22 Feb 2024 21:23:19 +0300 Subject: [PATCH] Add updateQuickReplyShortcut and updateQuickReplyShortcutDeleted. --- td/generate/scheme/td_api.tl | 7 +++++ td/telegram/QuickReplyManager.cpp | 51 +++++++++++++++++++++++++++++++ td/telegram/QuickReplyManager.h | 10 ++++++ 3 files changed, 68 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index e8ec25f88..2d8944b4d 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -6583,6 +6583,13 @@ updateSavedMessagesTopic topic:savedMessagesTopic = Update; //@description Number of Saved Messages topics has changed @topic_count Approximate total number of Saved Messages topics updateSavedMessagesTopicCount topic_count:int32 = Update; +//@description Basic information about a quick reply shortcut has changed. This update is guaranteed to come before the quick shortcut identifier is returned to the application +//@shortcut New data about the shortcut +updateQuickReplyShortcut shortcut:quickReplyShortcut = Update; + +//@description A quick reply shortcut was deleted @name The name of the deleted shortcut +updateQuickReplyShortcutDeleted name:string = Update; + //@description Basic information about a topic in a forum chat was changed @chat_id Chat identifier @info New information about the topic updateForumTopicInfo chat_id:int53 info:forumTopicInfo = Update; diff --git a/td/telegram/QuickReplyManager.cpp b/td/telegram/QuickReplyManager.cpp index 5714a7182..d6505e5b1 100644 --- a/td/telegram/QuickReplyManager.cpp +++ b/td/telegram/QuickReplyManager.cpp @@ -306,10 +306,15 @@ void QuickReplyManager::on_reload_quick_reply_shortcuts( message_id_to_message[message_id] = std::move(message); } + FlatHashSet old_shortcut_ids; + for (auto &shortcut : shortcuts_.shortcuts_) { + old_shortcut_ids.insert(shortcut->shortcut_id_); + } FlatHashSet added_shortcut_ids; FlatHashSet added_shortcut_names; vector> new_shortcuts; vector changed_shortcut_ids; + vector deleted_shortcut_ids; for (auto &quick_reply : shortcuts->quick_replies_) { if (quick_reply->shortcut_id_ <= 0 || quick_reply->shortcut_.empty() || quick_reply->count_ <= 0 || quick_reply->top_message_ <= 0) { @@ -349,11 +354,38 @@ void QuickReplyManager::on_reload_quick_reply_shortcuts( if (old_shortcut == nullptr || update_shortcut_from(shortcut.get(), old_shortcut, true)) { changed_shortcut_ids.push_back(shortcut->shortcut_id_); } + old_shortcut_ids.erase(shortcut->shortcut_id_); new_shortcuts.push_back(std::move(shortcut)); } + for (auto shortcut_id : old_shortcut_ids) { + auto old_shortcut = get_shortcut(shortcut_id); + CHECK(old_shortcut != nullptr); + auto is_changed = td::remove_if(old_shortcut->messages_, [](const unique_ptr &message) { + return message->message_id.is_server(); + }); + if (old_shortcut->messages_.empty()) { + CHECK(is_changed); + send_update_quick_reply_shortcut_deleted(old_shortcut); + } else { + // some local messages has left + auto shortcut = td::make_unique(); + shortcut->name_ = std::move(old_shortcut->name_); + shortcut->shortcut_id_ = old_shortcut->shortcut_id_; + shortcut->total_count_ = static_cast(old_shortcut->messages_.size()); + shortcut->messages_ = std::move(old_shortcut->messages_); + if (is_changed) { + send_update_quick_reply_shortcut(shortcut.get(), "on_reload_quick_reply_shortcuts 1"); + } + new_shortcuts.push_back(std::move(shortcut)); + } + } shortcuts_.shortcuts_ = std::move(new_shortcuts); shortcuts_.are_inited_ = true; + + for (auto shortcut_id : changed_shortcut_ids) { + send_update_quick_reply_shortcut(get_shortcut(shortcut_id), "on_reload_quick_reply_shortcuts 2"); + } break; } } @@ -450,4 +482,23 @@ bool QuickReplyManager::update_shortcut_from(Shortcut *new_shortcut, Shortcut *o old_message_count != get_shortcut_message_count(new_shortcut); } +td_api::object_ptr QuickReplyManager::get_update_quick_reply_shortcut_object( + const Shortcut *s, const char *source) const { + return td_api::make_object(get_quick_reply_shortcut_object(s, source)); +} + +void QuickReplyManager::send_update_quick_reply_shortcut(const Shortcut *s, const char *source) { + send_closure(G()->td(), &Td::send_update, get_update_quick_reply_shortcut_object(s, source)); +} + +td_api::object_ptr +QuickReplyManager::get_update_quick_reply_shortcut_deleted_object(const Shortcut *s) const { + CHECK(s != nullptr); + return td_api::make_object(s->name_); +} + +void QuickReplyManager::send_update_quick_reply_shortcut_deleted(const Shortcut *s) { + send_closure(G()->td(), &Td::send_update, get_update_quick_reply_shortcut_deleted_object(s)); +} + } // namespace td diff --git a/td/telegram/QuickReplyManager.h b/td/telegram/QuickReplyManager.h index db2ff3059..a59b048b0 100644 --- a/td/telegram/QuickReplyManager.h +++ b/td/telegram/QuickReplyManager.h @@ -144,6 +144,16 @@ class QuickReplyManager final : public Actor { static bool update_shortcut_from(Shortcut *new_shortcut, Shortcut *old_shortcut, bool is_partial); + td_api::object_ptr get_update_quick_reply_shortcut_object(const Shortcut *s, + const char *source) const; + + void send_update_quick_reply_shortcut(const Shortcut *s, const char *source); + + td_api::object_ptr get_update_quick_reply_shortcut_deleted_object( + const Shortcut *s) const; + + void send_update_quick_reply_shortcut_deleted(const Shortcut *s); + Shortcuts shortcuts_; Td *td_;