From f13f409e5ffaa6162013d829501909048d65bdd0 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 27 Feb 2024 18:10:11 +0300 Subject: [PATCH] Send shortcut identifiers in updates and receive them in requests. --- td/generate/scheme/td_api.tl | 16 ++++----- td/telegram/QuickReplyManager.cpp | 55 +++++++++++++++++++----------- td/telegram/QuickReplyManager.h | 8 +++-- td/telegram/QuickReplyShortcutId.h | 9 +++++ td/telegram/Td.cpp | 5 +-- td/telegram/cli.cpp | 16 +++++---- 6 files changed, 71 insertions(+), 38 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 529fa49cb..c358ee215 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -6637,11 +6637,11 @@ updateSavedMessagesTopicCount topic_count:int32 = Update; //@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 A quick reply shortcut was deleted @shortcut_id The identifier of the deleted shortcut +updateQuickReplyShortcutDeleted shortcut_id:int32 = Update; -//@description The list of quick reply shortcuts has changed @shortcut_names The new list of names of quick reply shortcuts -updateQuickReplyShortcuts shortcut_names:vector = Update; +//@description The list of quick reply shortcuts has changed @shortcut_ids The new list of identifiers of quick reply shortcuts +updateQuickReplyShortcuts shortcut_ids:vector = 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; @@ -7773,11 +7773,11 @@ 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 @shortcut_name The name of the quick reply shortcut -deleteQuickReplyShortcut shortcut_name:string = Ok; +//@description Deletes a quick reply shortcut @shortcut_id Unique identifier of the quick reply shortcut +deleteQuickReplyShortcut shortcut_id:int32 = Ok; -//@description Changes the order of quick reply shortcuts @shortcut_names The new order of quick reply shortcuts -reorderQuickReplyShortcuts shortcut_names:vector = Ok; +//@description Changes the order of quick reply shortcuts @shortcut_ids The new order of quick reply shortcuts +reorderQuickReplyShortcuts shortcut_ids:vector = Ok; //@description Returns list of custom emojis, which can be used as forum topic icon by all users diff --git a/td/telegram/QuickReplyManager.cpp b/td/telegram/QuickReplyManager.cpp index 28257e4ae..37034269a 100644 --- a/td/telegram/QuickReplyManager.cpp +++ b/td/telegram/QuickReplyManager.cpp @@ -705,7 +705,7 @@ bool QuickReplyManager::is_shortcut_list_changed(const vectorname_ != new_shortcuts[i]->name_) { + if (shortcuts_.shortcuts_[i]->shortcut_id_ != new_shortcuts[i]->shortcut_id_) { return true; } } @@ -736,12 +736,11 @@ int64 QuickReplyManager::get_shortcuts_hash() const { return get_vector_hash(numbers); } -void QuickReplyManager::delete_quick_reply_shortcut(const string &name, Promise &&promise) { - auto it = get_shortcut_it(name); +void QuickReplyManager::delete_quick_reply_shortcut(QuickReplyShortcutId shortcut_id, Promise &&promise) { + auto it = get_shortcut_it(shortcut_id); if (it == shortcuts_.shortcuts_.end()) { return promise.set_error(Status::Error(400, "Shortcut not found")); } - auto shortcut_id = (*it)->shortcut_id_; shortcuts_.shortcuts_.erase(it); if (!shortcut_id.is_server()) { @@ -759,34 +758,36 @@ void QuickReplyManager::delete_quick_reply_shortcut_from_server(QuickReplyShortc td_->create_handler(std::move(promise))->send(shortcut_id); } -void QuickReplyManager::reorder_quick_reply_shortcuts(const vector &names, Promise &&promise) { - FlatHashSet unique_names; - for (const auto &name : names) { - if (get_shortcut(name) == nullptr) { +void QuickReplyManager::reorder_quick_reply_shortcuts(const vector &shortcut_ids, + Promise &&promise) { + FlatHashSet unique_shortcut_ids; + for (const auto &shortcut_id : shortcut_ids) { + if (get_shortcut(shortcut_id) == nullptr) { return promise.set_error(Status::Error(400, "Shortcut not found")); } - unique_names.insert(name); + unique_shortcut_ids.insert(shortcut_id); } - if (unique_names.size() != names.size()) { - return promise.set_error(Status::Error(400, "Duplicate shortcut names specified")); + if (unique_shortcut_ids.size() != shortcut_ids.size()) { + return promise.set_error(Status::Error(400, "Duplicate shortcut identifiers specified")); } if (!shortcuts_.are_inited_) { return promise.set_value(Unit()); } + auto old_shortcut_ids = get_shortcut_ids(); + auto old_server_shortcut_ids = get_server_shortcut_ids(); vector> shortcuts; - for (const auto &name : names) { - auto it = get_shortcut_it(name); + for (const auto &shortcut_id : shortcut_ids) { + auto it = get_shortcut_it(shortcut_id); CHECK(it != shortcuts_.shortcuts_.end() && *it != nullptr); shortcuts.push_back(std::move(*it)); } for (auto &shortcut : shortcuts_.shortcuts_) { if (shortcut != nullptr) { - CHECK(unique_names.count(shortcut->name_) == 0); + CHECK(unique_shortcut_ids.count(shortcut->shortcut_id_) == 0); shortcuts.push_back(std::move(shortcut)); } } - auto old_server_shortcut_ids = get_server_shortcut_ids(); - bool is_list_changed = is_shortcut_list_changed(shortcuts); + bool is_list_changed = old_shortcut_ids != get_shortcut_ids(); shortcuts_.shortcuts_ = std::move(shortcuts); if (!is_list_changed) { return promise.set_value(Unit()); @@ -794,7 +795,7 @@ void QuickReplyManager::reorder_quick_reply_shortcuts(const vector &name send_update_quick_reply_shortcuts(); auto new_server_shortcut_ids = get_server_shortcut_ids(); - if (new_server_shortcut_ids == old_server_shortcut_ids || new_server_shortcut_ids.empty()) { + if (new_server_shortcut_ids == old_server_shortcut_ids) { return promise.set_value(Unit()); } @@ -830,6 +831,16 @@ QuickReplyManager::Shortcut *QuickReplyManager::get_shortcut(const string &name) return nullptr; } +vector>::iterator QuickReplyManager::get_shortcut_it( + QuickReplyShortcutId shortcut_id) { + for (auto it = shortcuts_.shortcuts_.begin(); it != shortcuts_.shortcuts_.end(); ++it) { + if ((*it)->shortcut_id_ == shortcut_id) { + return it; + } + } + return shortcuts_.shortcuts_.end(); +} + vector>::iterator QuickReplyManager::get_shortcut_it(const string &name) { for (auto it = shortcuts_.shortcuts_.begin(); it != shortcuts_.shortcuts_.end(); ++it) { if ((*it)->name_ == name) { @@ -839,6 +850,10 @@ vector>::iterator QuickReplyManager::get return shortcuts_.shortcuts_.end(); } +vector QuickReplyManager::get_shortcut_ids() const { + return transform(shortcuts_.shortcuts_, [](const unique_ptr &shortcut) { return shortcut->shortcut_id_; }); +} + vector QuickReplyManager::get_server_shortcut_ids() const { vector shortcut_ids; for (auto &shortcut : shortcuts_.shortcuts_) { @@ -936,7 +951,7 @@ void QuickReplyManager::send_update_quick_reply_shortcut(const Shortcut *s, cons 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_); + return td_api::make_object(s->shortcut_id_.get()); } void QuickReplyManager::send_update_quick_reply_shortcut_deleted(const Shortcut *s) { @@ -946,8 +961,8 @@ void QuickReplyManager::send_update_quick_reply_shortcut_deleted(const Shortcut td_api::object_ptr QuickReplyManager::get_update_quick_reply_shortcuts_object() const { CHECK(shortcuts_.are_inited_); - return td_api::make_object( - transform(shortcuts_.shortcuts_, [](const unique_ptr &shortcut) { return shortcut->name_; })); + return td_api::make_object(transform( + shortcuts_.shortcuts_, [](const unique_ptr &shortcut) { return shortcut->shortcut_id_.get(); })); } void QuickReplyManager::send_update_quick_reply_shortcuts() { diff --git a/td/telegram/QuickReplyManager.h b/td/telegram/QuickReplyManager.h index df488c497..7cf3dac83 100644 --- a/td/telegram/QuickReplyManager.h +++ b/td/telegram/QuickReplyManager.h @@ -33,9 +33,9 @@ class QuickReplyManager final : public Actor { void get_quick_reply_shortcuts(Promise &&promise); - void delete_quick_reply_shortcut(const string &name, Promise &&promise); + void delete_quick_reply_shortcut(QuickReplyShortcutId shortcut_id, Promise &&promise); - void reorder_quick_reply_shortcuts(const vector &names, Promise &&promise); + void reorder_quick_reply_shortcuts(const vector &shortcut_ids, Promise &&promise); void reload_quick_reply_shortcuts(); @@ -170,10 +170,14 @@ class QuickReplyManager final : public Actor { Shortcut *get_shortcut(const string &name); + vector>::iterator get_shortcut_it(QuickReplyShortcutId shortcut_id); + vector>::iterator get_shortcut_it(const string &name); bool is_shortcut_list_changed(const vector> &new_shortcuts) const; + vector get_shortcut_ids() const; + vector get_server_shortcut_ids() const; static void sort_quick_reply_messages(vector> &messages); diff --git a/td/telegram/QuickReplyShortcutId.h b/td/telegram/QuickReplyShortcutId.h index 7b8b5b5e7..7347672a8 100644 --- a/td/telegram/QuickReplyShortcutId.h +++ b/td/telegram/QuickReplyShortcutId.h @@ -41,6 +41,15 @@ class QuickReplyShortcutId { return input_quick_reply_shortcut_ids; } + static vector get_quick_reply_shortcut_ids(const vector &shortcut_ids) { + vector quick_reply_shortcut_ids; + quick_reply_shortcut_ids.reserve(shortcut_ids.size()); + for (auto &shortcut_id : shortcut_ids) { + quick_reply_shortcut_ids.emplace_back(shortcut_id); + } + return quick_reply_shortcut_ids; + } + bool operator==(const QuickReplyShortcutId &other) const { return id == other.id; } diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 639eb4577..9560d195f 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5770,13 +5770,14 @@ void Td::on_request(uint64 id, const td_api::loadQuickReplyShortcuts &request) { 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.shortcut_name_, std::move(promise)); + quick_reply_manager_->delete_quick_reply_shortcut(QuickReplyShortcutId(request.shortcut_id_), std::move(promise)); } void Td::on_request(uint64 id, const td_api::reorderQuickReplyShortcuts &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - quick_reply_manager_->reorder_quick_reply_shortcuts(request.shortcut_names_, std::move(promise)); + quick_reply_manager_->reorder_quick_reply_shortcuts( + QuickReplyShortcutId::get_quick_reply_shortcut_ids(request.shortcut_ids_), std::move(promise)); } void Td::on_request(uint64 id, const td_api::getStory &request) { diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 1d1db00de..a6074dfa3 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -940,6 +940,10 @@ class CliClient final : public Actor { return to_integer(trim(str)); } + vector as_shortcut_ids(Slice shortcut_ids) const { + return transform(autosplit(shortcut_ids), [](Slice str) { return as_shortcut_id(str); }); + } + td_api::object_ptr get_input_message_reply_to() const { if (reply_message_id_ != 0) { td_api::object_ptr quote; @@ -4844,13 +4848,13 @@ class CliClient final : public Actor { } 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)); + ShortcutId shortcut_id; + get_args(args, shortcut_id); + send_request(td_api::make_object(shortcut_id)); } else if (op == "rqrs") { - string names; - get_args(args, names); - send_request(td_api::make_object(autosplit_str(names))); + string shortcut_ids; + get_args(args, shortcut_ids); + send_request(td_api::make_object(as_shortcut_ids(shortcut_ids))); } else if (op == "gftdi") { send_request(td_api::make_object()); } else if (op == "cft") {