From 44366bd20879bd8aaa897731d98608789bd6391e Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 24 Feb 2024 03:02:42 +0300 Subject: [PATCH] Add td_api::deleteQuickReplyShortcut. --- td/generate/scheme/td_api.tl | 3 ++ td/telegram/QuickReplyManager.cpp | 49 +++++++++++++++++++++++++++++++ td/telegram/QuickReplyManager.h | 4 +++ td/telegram/Td.cpp | 6 ++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 4 +++ 6 files changed, 68 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 66d31feb7..53be9b9de 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -7732,6 +7732,9 @@ editMessageSchedulingState chat_id:int53 message_id:int53 scheduling_state:Messa //@description Loads quick reply shortcuts created by the current user. The loaded topics will be sent through updateQuickReplyShortcuts loadQuickReplyShortcuts = Ok; +//@description Deletes a quick reply shortcut @name The name of the quick reply shortcut +deleteQuickReplyShortcut name:string = Ok; + //@description Returns list of custom emojis, which can be used as forum topic icon by all users getForumTopicDefaultIcons = Stickers; diff --git a/td/telegram/QuickReplyManager.cpp b/td/telegram/QuickReplyManager.cpp index c4f7571ff..cd86d0e92 100644 --- a/td/telegram/QuickReplyManager.cpp +++ b/td/telegram/QuickReplyManager.cpp @@ -58,6 +58,31 @@ class GetQuickRepliesQuery final : public Td::ResultHandler { } }; +class DeleteQuickReplyShortcutQuery final : public Td::ResultHandler { + Promise promise_; + + public: + explicit DeleteQuickReplyShortcutQuery(Promise &&promise) : promise_(std::move(promise)) { + } + + void send(int32 shortcut_id) { + send_query(G()->net_query_creator().create(telegram_api::messages_deleteQuickReplyShortcut(shortcut_id))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + promise_.set_value(Unit()); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + QuickReplyManager::QuickReplyMessage::~QuickReplyMessage() = default; template @@ -655,6 +680,18 @@ int64 QuickReplyManager::get_shortcuts_hash() const { return get_vector_hash(numbers); } +void QuickReplyManager::delete_quick_reply_shortcut(const string &name, Promise &&promise) { + auto s = get_shortcut(name); + if (s == nullptr) { + return promise.set_error(Status::Error(400, "Shortcut not found")); + } + auto shortcut_id = s->shortcut_id_; + td::remove_if(shortcuts_.shortcuts_, + [shortcut_id](const unique_ptr &shortcut) { return shortcut->shortcut_id_ == shortcut_id; }); + + td_->create_handler(std::move(promise))->send(shortcut_id); +} + QuickReplyManager::Shortcut *QuickReplyManager::get_shortcut(int32 shortcut_id) { if (!shortcuts_.are_inited_) { return nullptr; @@ -667,6 +704,18 @@ QuickReplyManager::Shortcut *QuickReplyManager::get_shortcut(int32 shortcut_id) return nullptr; } +QuickReplyManager::Shortcut *QuickReplyManager::get_shortcut(const string &name) { + if (!shortcuts_.are_inited_) { + return nullptr; + } + for (auto &shortcut : shortcuts_.shortcuts_) { + if (shortcut->name_ == name) { + return shortcut.get(); + } + } + return nullptr; +} + void QuickReplyManager::sort_quick_reply_messages(vector> &messages) { std::sort(messages.begin(), messages.end(), [](const unique_ptr &lhs, const unique_ptr &rhs) { diff --git a/td/telegram/QuickReplyManager.h b/td/telegram/QuickReplyManager.h index c67e2fc85..c91936272 100644 --- a/td/telegram/QuickReplyManager.h +++ b/td/telegram/QuickReplyManager.h @@ -30,6 +30,8 @@ class QuickReplyManager final : public Actor { void get_quick_reply_shortcuts(Promise &&promise); + void delete_quick_reply_shortcut(const string &name, Promise &&promise); + void reload_quick_reply_shortcuts(); void get_current_state(vector> &updates) const; @@ -161,6 +163,8 @@ class QuickReplyManager final : public Actor { Shortcut *get_shortcut(int32 shortcut_id); + Shortcut *get_shortcut(const string &name); + static void sort_quick_reply_messages(vector> &messages); using QuickReplyMessageUniqueId = std::pair; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index a5e2913c7..8970f39a2 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5765,6 +5765,12 @@ void Td::on_request(uint64 id, const td_api::loadQuickReplyShortcuts &request) { quick_reply_manager_->get_quick_reply_shortcuts(std::move(promise)); } +void Td::on_request(uint64 id, const td_api::deleteQuickReplyShortcut &request) { + CHECK_IS_USER(); + CREATE_OK_REQUEST_PROMISE(); + quick_reply_manager_->delete_quick_reply_shortcut(request.name_, std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::getStory &request) { CHECK_IS_USER(); CREATE_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index dde5d7bbe..b2516f7dc 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -866,6 +866,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::loadQuickReplyShortcuts &request); + void on_request(uint64 id, const td_api::deleteQuickReplyShortcut &request); + void on_request(uint64 id, const td_api::getStory &request); void on_request(uint64 id, const td_api::getChatsToSendStories &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 8b85aa47a..6a6d56000 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -4827,6 +4827,10 @@ class CliClient final : public Actor { as_message_scheduling_state(date))); } else if (op == "lqrs") { send_request(td_api::make_object()); + } else if (op == "dqrs") { + string name; + get_args(args, name); + send_request(td_api::make_object(name)); } else if (op == "gftdi") { send_request(td_api::make_object()); } else if (op == "cft") {