From 0b5b4b8daba1b1b94d7856567e895fdccc8f5f7a Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 5 Jan 2024 13:42:46 +0300 Subject: [PATCH] Add synchronous td_api::getCountryFlagEmoji. --- td/generate/scheme/td_api.tl | 7 +++++-- td/telegram/CountryInfoManager.cpp | 26 ++++++++++++++++++++++++++ td/telegram/CountryInfoManager.h | 2 ++ td/telegram/Td.cpp | 11 +++++++++++ td/telegram/Td.h | 3 +++ td/telegram/cli.cpp | 2 ++ 6 files changed, 49 insertions(+), 2 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 2fa496ec1..e4ddee99b 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -3978,7 +3978,7 @@ availableReaction type:ReactionType needs_premium:Bool = AvailableReaction; //@allow_custom_emoji True, if any custom emoji reaction can be added by Telegram Premium subscribers availableReactions top_reactions:vector recent_reactions:vector popular_reactions:vector allow_custom_emoji:Bool = AvailableReactions; -//@description Contains information about a emoji reaction +//@description Contains information about an emoji reaction //@emoji Text representation of the reaction //@title Reaction title //@is_active True, if the reaction can be added to new messages and enabled in chats @@ -7520,7 +7520,7 @@ setPinnedForumTopics chat_id:int53 message_thread_ids:vector = Ok; deleteForumTopic chat_id:int53 message_thread_id:int53 = Ok; -//@description Returns information about a emoji reaction. Returns a 404 error if the reaction is not found @emoji Text representation of the reaction +//@description Returns information about an emoji reaction. Returns a 404 error if the reaction is not found @emoji Text representation of the reaction getEmojiReaction emoji:string = EmojiReaction; //@description Returns TGS stickers with generic animations for custom emoji reactions @@ -7587,6 +7587,9 @@ parseMarkdown text:formattedText = FormattedText; //@description Replaces text entities with Markdown formatting in a human-friendly format. Entities that can't be represented in Markdown unambiguously are kept as is. Can be called synchronously @text The text getMarkdownText text:formattedText = FormattedText; +//@description Returns an emoji for the given country. Returns an empty string on failure. Can be called synchronously @country_code A two-letter ISO 3166-1 alpha-2 country code as received from getCountries +getCountryFlagEmoji country_code:string = Text; + //@description Returns the MIME type of a file, guessed by its extension. Returns an empty string on failure. Can be called synchronously @file_name The name of the file or path to the file getFileMimeType file_name:string = Text; diff --git a/td/telegram/CountryInfoManager.cpp b/td/telegram/CountryInfoManager.cpp index 457da2a7f..e68480f06 100644 --- a/td/telegram/CountryInfoManager.cpp +++ b/td/telegram/CountryInfoManager.cpp @@ -577,6 +577,32 @@ const CountryInfoManager::CountryList *CountryInfoManager::get_country_list(Coun return country; } +string CountryInfoManager::get_country_flag_emoji(const string &country_code) { + if (country_code.size() != 2 || !is_alpha(country_code[0]) || !is_alpha(country_code[1])) { + return string(); + } + char first = to_upper(country_code[0]); + char second = to_upper(country_code[1]); + if (first == 'Y' && second == 'L') { + return string(); + } + if (first == 'F' && second == 'T') { + return "\xF0\x9F\x8F\xB4\xE2\x80\x8D\xE2\x98\xA0\xEF\xB8\x8F"; // pirate flag + } + if (first == 'X' && second == 'G') { + return "\xF0\x9F\x9B\xB0"; // satellite + } + if (first == 'X' && second == 'V') { + return "\xF0\x9F\x8C\x8D"; // globe showing Europe-Africa + } + string result; + result.reserve(8); + append_utf8_character(result, 0x1F1A5 + first); + append_utf8_character(result, 0x1F1A5 + second); + CHECK(result.size() == 8); + return result; +} + int32 CountryInfoManager::manager_count_ = 0; std::mutex CountryInfoManager::country_mutex_; FlatHashMap> CountryInfoManager::countries_; diff --git a/td/telegram/CountryInfoManager.h b/td/telegram/CountryInfoManager.h index ba46372c4..001c898f1 100644 --- a/td/telegram/CountryInfoManager.h +++ b/td/telegram/CountryInfoManager.h @@ -37,6 +37,8 @@ class CountryInfoManager final : public Actor { static td_api::object_ptr get_phone_number_info_sync(const string &language_code, string phone_number_prefix); + static string get_country_flag_emoji(const string &country_code); + void on_update_fragment_prefixes(); CountryInfoManager(const CountryInfoManager &) = delete; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 3511fa772..8e487dc54 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -2740,6 +2740,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::getCountryFlagEmoji::ID: case td_api::getFileMimeType::ID: case td_api::getFileExtension::ID: case td_api::cleanFileName::ID: @@ -2980,6 +2981,7 @@ td_api::object_ptr Td::static_request(td_api::object_ptr Td::do_static_request(td_api::searchStringsBy return td_api::make_object(total_count, std::move(result)); } +td_api::object_ptr Td::do_static_request(const td_api::getCountryFlagEmoji &request) { + // don't check country code UTF-8 correctness + return td_api::make_object(CountryInfoManager::get_country_flag_emoji(request.country_code_)); +} + td_api::object_ptr Td::do_static_request(const td_api::getFileMimeType &request) { // don't check file name UTF-8 correctness return make_tl_object(MimeType::from_extension(PathView(request.file_name_).extension())); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index e738bc336..b91532046 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1731,6 +1731,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::searchStringsByPrefix &request); + void on_request(uint64 id, const td_api::getCountryFlagEmoji &request); + void on_request(uint64 id, const td_api::getFileMimeType &request); void on_request(uint64 id, const td_api::getFileExtension &request); @@ -1793,6 +1795,7 @@ class Td final : public Actor { static td_api::object_ptr do_static_request(td_api::parseMarkdown &request); static td_api::object_ptr do_static_request(td_api::getMarkdownText &request); static td_api::object_ptr do_static_request(td_api::searchStringsByPrefix &request); + static td_api::object_ptr do_static_request(const td_api::getCountryFlagEmoji &request); static td_api::object_ptr do_static_request(const td_api::getFileMimeType &request); static td_api::object_ptr do_static_request(const td_api::getFileExtension &request); static td_api::object_ptr do_static_request(const td_api::cleanFileName &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 94cc193df..795cb70a3 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -4133,6 +4133,8 @@ class CliClient final : public Actor { get_args(args, strings, query, limit, return_none_for_empty_query); execute(td_api::make_object(autosplit_str(strings), query, as_limit(limit), return_none_for_empty_query)); + } else if (op == "gcfe") { + execute(td_api::make_object(trim(args))); } else if (op == "gfmt") { execute(td_api::make_object(trim(args))); } else if (op == "gfe") {