From f3d655f1ace6b775735733ae14d3ef9edfb874c5 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 15 Apr 2024 23:36:27 +0300 Subject: [PATCH] Create local qucik reply shortcut if it doesn't exist yet. --- td/telegram/QuickReplyManager.cpp | 29 +++++++++++++++++++++++++---- td/telegram/QuickReplyManager.h | 4 +++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/td/telegram/QuickReplyManager.cpp b/td/telegram/QuickReplyManager.cpp index 6e65d31da..91edb39b1 100644 --- a/td/telegram/QuickReplyManager.cpp +++ b/td/telegram/QuickReplyManager.cpp @@ -1499,25 +1499,43 @@ vector>::iterator QuickReplyMan return s->messages_.end(); } -Status QuickReplyManager::check_new_shortcut_name(const string &name, int32 new_message_count) { +Result QuickReplyManager::create_new_local_shortcut(const string &name, + int32 new_message_count) { TRY_STATUS(check_shortcut_name(name)); load_quick_reply_shortcuts(); - const auto *s = get_shortcut(name); + if (!shortcuts_.are_inited_) { + return Status::Error(400, "Quick reply shortcuts must be loaded first"); + } + + auto *s = get_shortcut(name); auto max_message_count = td_->option_manager_->get_option_integer("quick_reply_shortcut_message_count_max"); if (s != nullptr) { max_message_count -= s->server_total_count_ + s->local_total_count_; } else { auto max_shortcut_count = td_->option_manager_->get_option_integer("quick_reply_shortcut_count_max"); - if (static_cast(shortcuts_.shortcuts_.size()) == max_shortcut_count) { + if (static_cast(shortcuts_.shortcuts_.size()) >= max_shortcut_count) { return Status::Error(400, "Quick reply shortcut count exceeded"); } } if (new_message_count > max_message_count) { return Status::Error(400, "Quick reply message count exceeded"); } + if (s != nullptr) { + return s; + } + if (next_local_shortcut_id_ >= std::numeric_limits::max() - 10) { + return Status::Error(400, "Too many local shortcuts created"); + } - return Status::OK(); + auto shortcut = td::make_unique(); + shortcut->name_ = name; + shortcut->shortcut_id_ = QuickReplyShortcutId(next_local_shortcut_id_++); + s = shortcut.get(); + + shortcuts_.shortcuts_.push_back(std::move(shortcut)); + + return s; } MessageId QuickReplyManager::get_input_reply_to_message_id(const Shortcut *s, MessageId reply_to_message_id) { @@ -1703,6 +1721,9 @@ void QuickReplyManager::load_quick_reply_shortcuts() { shortcuts_.are_inited_ = true; for (const auto &shortcut : shortcuts_.shortcuts_) { + if (shortcut->shortcut_id_.get() >= next_local_shortcut_id_) { + next_local_shortcut_id_ = shortcut->shortcut_id_.get() + 1; + } for (const auto &message : shortcut->messages_) { change_message_files({shortcut->shortcut_id_, message->message_id}, message.get(), {}); diff --git a/td/telegram/QuickReplyManager.h b/td/telegram/QuickReplyManager.h index 6199b59bf..4e6ba9171 100644 --- a/td/telegram/QuickReplyManager.h +++ b/td/telegram/QuickReplyManager.h @@ -231,7 +231,7 @@ class QuickReplyManager final : public Actor { vector>::iterator get_message_it(Shortcut *s, MessageId message_id); - Status check_new_shortcut_name(const string &name, int32 new_message_count); + Result create_new_local_shortcut(const string &name, int32 new_message_count); static MessageId get_input_reply_to_message_id(const Shortcut *s, MessageId reply_to_message_id); @@ -303,6 +303,8 @@ class QuickReplyManager final : public Actor { Shortcuts shortcuts_; + int32 next_local_shortcut_id_ = QuickReplyShortcutId::MAX_SERVER_SHORTCUT_ID + 1; + FlatHashSet deleted_shortcut_ids_; FlatHashMap>, QuickReplyShortcutIdHash> get_shortcut_messages_queries_;