Add td_api::checkQuickReplyShortcutName.

This commit is contained in:
levlam 2024-03-04 14:34:47 +03:00
parent e61c4d03e5
commit 74c5ba7087
6 changed files with 67 additions and 0 deletions

View File

@ -7780,6 +7780,9 @@ editInlineMessageReplyMarkup inline_message_id:string reply_markup:ReplyMarkup =
editMessageSchedulingState chat_id:int53 message_id:int53 scheduling_state:MessageSchedulingState = Ok;
//@description Checks validness of a name for a quick reply shortcut. Can be called synchronously @name The name of the shortcut; 1-32 characters
checkQuickReplyShortcutName name:string = Ok;
//@description Loads quick reply shortcuts created by the current user. The loaded topics will be sent through updateQuickReplyShortcuts
loadQuickReplyShortcuts = Ok;

View File

@ -27,6 +27,8 @@
#include "td/utils/buffer.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/unicode.h"
#include "td/utils/utf8.h"
namespace td {
@ -375,6 +377,43 @@ void QuickReplyManager::tear_down() {
parent_.reset();
}
bool QuickReplyManager::is_shortcut_name_letter(uint32 code) {
auto category = get_unicode_simple_category(code);
if (code == '_' || code == 0x200c || code == 0xb7 || (0xd80 <= code && code <= 0xdff)) {
return true;
}
switch (category) {
case UnicodeSimpleCategory::DecimalNumber:
case UnicodeSimpleCategory::Letter:
return true;
default:
return false;
}
}
Status QuickReplyManager::check_shortcut_name(CSlice name) {
if (!check_utf8(name)) {
return Status::Error("Strings must be encoded in UTF-8");
}
int32 length = 0;
auto *ptr = name.ubegin();
while (ptr != name.uend()) {
uint32 code;
ptr = next_utf8_unsafe(ptr, &code);
if (!is_shortcut_name_letter(code)) {
return Status::Error("A letter is not allowed");
}
length++;
}
if (length == 0) {
return Status::Error("Name can't be empty");
}
if (length > 32) {
return Status::Error("Name is too long");
}
return Status::OK();
}
unique_ptr<QuickReplyManager::QuickReplyMessage> QuickReplyManager::create_message(
telegram_api::object_ptr<telegram_api::Message> message_ptr, const char *source) const {
LOG(DEBUG) << "Receive from " << source << " " << to_string(message_ptr);

View File

@ -21,6 +21,7 @@
#include "td/utils/FlatHashMap.h"
#include "td/utils/FlatHashSet.h"
#include "td/utils/Promise.h"
#include "td/utils/Status.h"
#include <utility>
@ -34,6 +35,8 @@ class QuickReplyManager final : public Actor {
public:
QuickReplyManager(Td *td, ActorShared<> parent);
static Status check_shortcut_name(CSlice name);
void get_quick_reply_shortcuts(Promise<Unit> &&promise);
void delete_quick_reply_shortcut(QuickReplyShortcutId shortcut_id, Promise<Unit> &&promise);
@ -145,6 +148,8 @@ class QuickReplyManager final : public Actor {
void tear_down() final;
static bool is_shortcut_name_letter(uint32 code);
void add_quick_reply_message_dependencies(Dependencies &dependencies, const QuickReplyMessage *m) const;
unique_ptr<QuickReplyMessage> create_message(telegram_api::object_ptr<telegram_api::Message> message_ptr,

View File

@ -2780,6 +2780,7 @@ bool Td::is_synchronous_request(const td_api::Function *function) {
case td_api::parseMarkdown::ID:
case td_api::getMarkdownText::ID:
case td_api::searchStringsByPrefix::ID:
case td_api::checkQuickReplyShortcutName::ID:
case td_api::getCountryFlagEmoji::ID:
case td_api::getFileMimeType::ID:
case td_api::getFileExtension::ID:
@ -3022,6 +3023,7 @@ td_api::object_ptr<td_api::Object> Td::static_request(td_api::object_ptr<td_api:
case td_api::parseMarkdown::ID:
case td_api::getMarkdownText::ID:
case td_api::searchStringsByPrefix::ID:
case td_api::checkQuickReplyShortcutName::ID:
case td_api::getCountryFlagEmoji::ID:
case td_api::getFileMimeType::ID:
case td_api::getFileExtension::ID:
@ -9357,6 +9359,10 @@ void Td::on_request(uint64 id, const td_api::searchStringsByPrefix &request) {
UNREACHABLE();
}
void Td::on_request(uint64 id, const td_api::checkQuickReplyShortcutName &request) {
UNREACHABLE();
}
void Td::on_request(uint64 id, const td_api::getCountryFlagEmoji &request) {
UNREACHABLE();
}
@ -9565,6 +9571,15 @@ td_api::object_ptr<td_api::Object> Td::do_static_request(td_api::searchStringsBy
return td_api::make_object<td_api::foundPositions>(total_count, std::move(result));
}
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::checkQuickReplyShortcutName &request) {
// don't check name UTF-8 correctness
auto status = QuickReplyManager::check_shortcut_name(request.name_);
if (status.is_ok()) {
return td_api::make_object<td_api::ok>();
}
return make_error(200, status.error().message());
}
td_api::object_ptr<td_api::Object> Td::do_static_request(const td_api::getCountryFlagEmoji &request) {
// don't check country code UTF-8 correctness
return td_api::make_object<td_api::text>(CountryInfoManager::get_country_flag_emoji(request.country_code_));

View File

@ -1802,6 +1802,8 @@ class Td final : public Actor {
void on_request(uint64 id, const td_api::searchStringsByPrefix &request);
void on_request(uint64 id, const td_api::checkQuickReplyShortcutName &request);
void on_request(uint64 id, const td_api::getCountryFlagEmoji &request);
void on_request(uint64 id, const td_api::getFileMimeType &request);
@ -1866,6 +1868,7 @@ class Td final : public Actor {
static td_api::object_ptr<td_api::Object> do_static_request(td_api::parseMarkdown &request);
static td_api::object_ptr<td_api::Object> do_static_request(td_api::getMarkdownText &request);
static td_api::object_ptr<td_api::Object> do_static_request(td_api::searchStringsByPrefix &request);
static td_api::object_ptr<td_api::Object> do_static_request(const td_api::checkQuickReplyShortcutName &request);
static td_api::object_ptr<td_api::Object> do_static_request(const td_api::getCountryFlagEmoji &request);
static td_api::object_ptr<td_api::Object> do_static_request(const td_api::getFileMimeType &request);
static td_api::object_ptr<td_api::Object> do_static_request(const td_api::getFileExtension &request);

View File

@ -4845,6 +4845,8 @@ class CliClient final : public Actor {
get_args(args, chat_id, message_id, date);
send_request(td_api::make_object<td_api::editMessageSchedulingState>(chat_id, message_id,
as_message_scheduling_state(date)));
} else if (op == "cqrsn") {
execute(td_api::make_object<td_api::checkQuickReplyShortcutName>(args));
} else if (op == "lqrs") {
send_request(td_api::make_object<td_api::loadQuickReplyShortcuts>());
} else if (op == "dqrs") {