Add td_api::setQuickReplyShortcutName.

This commit is contained in:
levlam 2024-03-04 15:00:00 +03:00
parent 74c5ba7087
commit f03bdc7e50
6 changed files with 98 additions and 6 deletions

View File

@ -7786,6 +7786,9 @@ checkQuickReplyShortcutName name:string = Ok;
//@description Loads quick reply shortcuts created by the current user. The loaded topics will be sent through updateQuickReplyShortcuts
loadQuickReplyShortcuts = Ok;
//@description Changes name of a quick reply shortcut @shortcut_id Unique identifier of the quick reply shortcut @name New name for the shortcut. Use checkQuickReplyShortcutName to check its validness
setQuickReplyShortcutName shortcut_id:int32 name:string = Ok;
//@description Deletes a quick reply shortcut @shortcut_id Unique identifier of the quick reply shortcut
deleteQuickReplyShortcut shortcut_id:int32 = Ok;

View File

@ -41,7 +41,7 @@ class GetQuickRepliesQuery final : public Td::ResultHandler {
}
void send(int64 hash) {
send_query(G()->net_query_creator().create(telegram_api::messages_getQuickReplies(hash), {{"me"}}));
send_query(G()->net_query_creator().create(telegram_api::messages_getQuickReplies(hash), {{"quick_reply"}}));
}
void on_result(BufferSlice packet) final {
@ -60,6 +60,32 @@ class GetQuickRepliesQuery final : public Td::ResultHandler {
}
};
class EditQuickReplyShortcutQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
public:
explicit EditQuickReplyShortcutQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
}
void send(QuickReplyShortcutId shortcut_id, const string &name) {
send_query(G()->net_query_creator().create(telegram_api::messages_editQuickReplyShortcut(shortcut_id.get(), name),
{{"quick_reply"}}));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_editQuickReplyShortcut>(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));
}
};
class DeleteQuickReplyShortcutQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
@ -68,8 +94,8 @@ class DeleteQuickReplyShortcutQuery final : public Td::ResultHandler {
}
void send(QuickReplyShortcutId shortcut_id) {
send_query(
G()->net_query_creator().create(telegram_api::messages_deleteQuickReplyShortcut(shortcut_id.get()), {{"me"}}));
send_query(G()->net_query_creator().create(telegram_api::messages_deleteQuickReplyShortcut(shortcut_id.get()),
{{"quick_reply"}}));
}
void on_result(BufferSlice packet) final {
@ -98,7 +124,7 @@ class ReorderQuickRepliesQuery final : public Td::ResultHandler {
send_query(
G()->net_query_creator().create(telegram_api::messages_reorderQuickReplies(
QuickReplyShortcutId::get_input_quick_reply_shortcut_ids(shortcut_ids)),
{{"me"}}));
{{"quick_reply"}}));
}
void on_result(BufferSlice packet) final {
@ -132,7 +158,7 @@ class GetQuickReplyMessagesQuery final : public Td::ResultHandler {
send_query(G()->net_query_creator().create(
telegram_api::messages_getQuickReplyMessages(flags, shortcut_id.get(),
MessageId::get_server_message_ids(message_ids), hash),
{{"me"}}));
{{"quick_reply"}}));
}
void on_result(BufferSlice packet) final {
@ -163,7 +189,7 @@ class DeleteQuickReplyMessagesQuery final : public Td::ResultHandler {
shortcut_id_ = shortcut_id;
send_query(G()->net_query_creator().create(telegram_api::messages_deleteQuickReplyMessages(
shortcut_id.get(), MessageId::get_server_message_ids(message_ids)),
{{"me"}}));
{{"quick_reply"}}));
}
void on_result(BufferSlice packet) final {
@ -833,6 +859,47 @@ int64 QuickReplyManager::get_shortcuts_hash() const {
return get_vector_hash(numbers);
}
void QuickReplyManager::set_quick_reply_shortcut_name(QuickReplyShortcutId shortcut_id, const string &name,
Promise<Unit> &&promise) {
const auto *shortcut = get_shortcut(shortcut_id);
if (shortcut == nullptr) {
return promise.set_error(Status::Error(400, "Shortcut not found"));
}
if (check_shortcut_name(name).is_error()) {
return promise.set_error(Status::Error(400, "Shortcut name is invalid"));
}
if (!shortcut_id.is_server()) {
return promise.set_error(Status::Error(400, "Shortcut isn't created yet"));
}
auto query_promise = PromiseCreator::lambda(
[actor_id = actor_id(this), shortcut_id, name, promise = std::move(promise)](Result<Unit> &&result) mutable {
if (result.is_ok()) {
send_closure(actor_id, &QuickReplyManager::on_set_quick_reply_shortcut_name, shortcut_id, name,
std::move(promise));
}
});
set_quick_reply_shortcut_name_on_server(shortcut_id, name, std::move(query_promise));
}
void QuickReplyManager::set_quick_reply_shortcut_name_on_server(QuickReplyShortcutId shortcut_id, const string &name,
Promise<Unit> &&promise) {
CHECK(shortcut_id.is_server());
td_->create_handler<EditQuickReplyShortcutQuery>(std::move(promise))->send(shortcut_id, name);
}
void QuickReplyManager::on_set_quick_reply_shortcut_name(QuickReplyShortcutId shortcut_id, const string &name,
Promise<Unit> &&promise) {
auto *shortcut = get_shortcut(shortcut_id);
if (shortcut == nullptr || shortcut->name_ == name) {
return promise.set_value(Unit());
}
shortcut->name_ = name;
send_update_quick_reply_shortcut(shortcut, "on_set_quick_reply_shortcut_name");
save_quick_reply_shortcuts();
promise.set_value(Unit());
}
void QuickReplyManager::delete_quick_reply_shortcut(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise) {
auto it = get_shortcut_it(shortcut_id);
if (it == shortcuts_.shortcuts_.end()) {

View File

@ -21,6 +21,7 @@
#include "td/utils/FlatHashMap.h"
#include "td/utils/FlatHashSet.h"
#include "td/utils/Promise.h"
#include "td/utils/Slice.h"
#include "td/utils/Status.h"
#include <utility>
@ -39,6 +40,8 @@ class QuickReplyManager final : public Actor {
void get_quick_reply_shortcuts(Promise<Unit> &&promise);
void set_quick_reply_shortcut_name(QuickReplyShortcutId shortcut_id, const string &name, Promise<Unit> &&promise);
void delete_quick_reply_shortcut(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise);
void reorder_quick_reply_shortcuts(const vector<QuickReplyShortcutId> &shortcut_ids, Promise<Unit> &&promise);
@ -183,6 +186,8 @@ class QuickReplyManager final : public Actor {
void on_load_quick_reply_fail(Status error);
void on_set_quick_reply_shortcut_name(QuickReplyShortcutId shortcut_id, const string &name, Promise<Unit> &&promise);
int64 get_shortcuts_hash() const;
void on_reload_quick_reply_messages(QuickReplyShortcutId shortcut_id,
@ -250,6 +255,9 @@ class QuickReplyManager final : public Actor {
void send_update_quick_reply_shortcut_messages(const Shortcut *s, const char *source);
void set_quick_reply_shortcut_name_on_server(QuickReplyShortcutId shortcut_id, const string &name,
Promise<Unit> &&promise);
void delete_quick_reply_shortcut_from_server(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise);
void reorder_quick_reply_shortcuts_on_server(vector<QuickReplyShortcutId> shortcut_ids, Promise<Unit> &&promise);

View File

@ -5769,6 +5769,13 @@ 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::setQuickReplyShortcutName &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();
quick_reply_manager_->set_quick_reply_shortcut_name(QuickReplyShortcutId(request.shortcut_id_), request.name_,
std::move(promise));
}
void Td::on_request(uint64 id, const td_api::deleteQuickReplyShortcut &request) {
CHECK_IS_USER();
CREATE_OK_REQUEST_PROMISE();

View File

@ -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::setQuickReplyShortcutName &request);
void on_request(uint64 id, const td_api::deleteQuickReplyShortcut &request);
void on_request(uint64 id, const td_api::reorderQuickReplyShortcuts &request);

View File

@ -4853,6 +4853,11 @@ class CliClient final : public Actor {
ShortcutId shortcut_id;
get_args(args, shortcut_id);
send_request(td_api::make_object<td_api::deleteQuickReplyShortcut>(shortcut_id));
} else if (op == "sqrsn") {
ShortcutId shortcut_id;
string name;
get_args(args, shortcut_id, name);
send_request(td_api::make_object<td_api::setQuickReplyShortcutName>(shortcut_id, name));
} else if (op == "rqrs") {
string shortcut_ids;
get_args(args, shortcut_ids);