From 54b97609358629c3a80aae42ac667eed42949453 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 16 Mar 2021 00:52:37 +0300 Subject: [PATCH] Improve getExternalLink methods. --- td/generate/scheme/td_api.tl | 8 ++++++-- td/telegram/ConfigManager.cpp | 17 +++++++++-------- td/telegram/ConfigManager.h | 2 +- td/telegram/Td.cpp | 18 +++++++++--------- td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 7 +++++-- 6 files changed, 32 insertions(+), 22 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index f72b2efb2..7f969af75 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4228,8 +4228,12 @@ viewMessages chat_id:int53 message_thread_id:int53 message_ids:vector for //@description Informs TDLib that the message content has been opened (e.g., the user has opened a photo, video, document, location or venue, or has listened to an audio file or voice note message). An updateMessageContentOpened update will be generated if something has changed @chat_id Chat identifier of the message @message_id Identifier of the message with the opened content openMessageContent chat_id:int53 message_id:int53 = Ok; -//@description Returns an HTTP URL to open when user clicks on a given HTTP link. This method can be used to automatically login user on a Telegram site @link The HTTP link -getExternalLink link:string = HttpUrl; +//@description Returns information about an action to be done when the current user clicks an HTTP link. This method can be used to automatically authorize the current user on a website @link The HTTP link +getExternalLinkInfo link:string = LoginUrlInfo; + +//@description Returns an HTTP URL which can be used to automatically authorize the current user on a website after clicking an HTTP link. Use the method getExternalLinkInfo to find whether a prior user confirmation is needed +//@link The HTTP link @allow_write_access True, if the current user allowed the bot, returned in getExternalLinkInfo, to send them messages +getExternalLink link:string allow_write_access:Bool = HttpUrl; //@description Marks all mentions in a chat as read @chat_id Chat identifier diff --git a/td/telegram/ConfigManager.cpp b/td/telegram/ConfigManager.cpp index 09bdd1e81..f583ac9cc 100644 --- a/td/telegram/ConfigManager.cpp +++ b/td/telegram/ConfigManager.cpp @@ -967,33 +967,34 @@ void ConfigManager::get_app_config(Promise } } -void ConfigManager::get_external_link(string &&link, Promise &&promise) { +void ConfigManager::get_external_link_info(string &&link, Promise> &&promise) { + auto default_result = td_api::make_object(link, false); if (G()->close_flag()) { - return promise.set_value(std::move(link)); + return promise.set_value(std::move(default_result)); } auto r_url = parse_url(link); if (r_url.is_error()) { - return promise.set_value(std::move(link)); + return promise.set_value(std::move(default_result)); } if (!td::contains(autologin_domains_, r_url.ok().host_)) { - return promise.set_value(std::move(link)); + return promise.set_value(std::move(default_result)); } if (autologin_update_time_ < Time::now() - 10000) { auto query_promise = PromiseCreator::lambda([link = std::move(link), promise = std::move(promise)]( Result> &&result) mutable { if (result.is_error()) { - return promise.set_value(std::move(link)); + return promise.set_value(td_api::make_object(link, false)); } - send_closure(G()->config_manager(), &ConfigManager::get_external_link, std::move(link), std::move(promise)); + send_closure(G()->config_manager(), &ConfigManager::get_external_link_info, std::move(link), std::move(promise)); }); return get_app_config(std::move(query_promise)); } if (autologin_token_.empty()) { - return promise.set_value(std::move(link)); + return promise.set_value(std::move(default_result)); } auto url = r_url.move_as_ok(); @@ -1018,7 +1019,7 @@ void ConfigManager::get_external_link(string &&link, Promise &&promise) url.query_ = PSTRING() << path << parameters << added_parameter << hash; - promise.set_value(url.get_url()); + promise.set_value(td_api::make_object(url.get_url(), false)); } void ConfigManager::get_content_settings(Promise &&promise) { diff --git a/td/telegram/ConfigManager.h b/td/telegram/ConfigManager.h index ecc3569c6..39d110496 100644 --- a/td/telegram/ConfigManager.h +++ b/td/telegram/ConfigManager.h @@ -93,7 +93,7 @@ class ConfigManager : public NetQueryCallback { void get_app_config(Promise> &&promise); - void get_external_link(string &&link, Promise &&promise); + void get_external_link_info(string &&link, Promise> &&promise); void get_content_settings(Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 3baa18099..01a7ea4bb 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5472,19 +5472,19 @@ void Td::on_request(uint64 id, const td_api::openMessageContent &request) { id, messages_manager_->open_message_content({DialogId(request.chat_id_), MessageId(request.message_id_)})); } +void Td::on_request(uint64 id, td_api::getExternalLinkInfo &request) { + CHECK_IS_USER(); + CLEAN_INPUT_STRING(request.link_); + CREATE_REQUEST_PROMISE(); + send_closure_later(G()->config_manager(), &ConfigManager::get_external_link_info, std::move(request.link_), + std::move(promise)); +} + void Td::on_request(uint64 id, td_api::getExternalLink &request) { CHECK_IS_USER(); CLEAN_INPUT_STRING(request.link_); CREATE_REQUEST_PROMISE(); - auto query_promise = [promise = std::move(promise)](Result &&result) mutable { - if (result.is_error()) { - promise.set_error(result.move_as_error()); - } else { - promise.set_value(td_api::make_object(result.ok())); - } - }; - send_closure_later(G()->config_manager(), &ConfigManager::get_external_link, std::move(request.link_), - std::move(query_promise)); + promise.set_value(td_api::make_object(request.link_)); } void Td::on_request(uint64 id, const td_api::getChatHistory &request) { diff --git a/td/telegram/Td.h b/td/telegram/Td.h index adcc33dda..47d4b4516 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -577,6 +577,8 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, const td_api::openMessageContent &request); + void on_request(uint64 id, td_api::getExternalLinkInfo &request); + void on_request(uint64 id, td_api::getExternalLink &request); void on_request(uint64 id, const td_api::getChatHistory &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 3a355b1f3..9609df64d 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3923,9 +3923,12 @@ class CliClient final : public Actor { string message_id; get_args(args, chat_id, message_id); send_request(td_api::make_object(as_chat_id(chat_id), as_message_id(message_id))); - } else if (op == "gel") { + } else if (op == "geli") { string link = args; - send_request(td_api::make_object(link)); + send_request(td_api::make_object(link)); + } else if (op == "gel" || op == "gelw") { + string link = args; + send_request(td_api::make_object(link, op == "gelw")); } else if (op == "racm") { string chat_id = args; send_request(td_api::make_object(as_chat_id(chat_id)));