From f6a2ecdded78373cbde0dbac8ce5d97adb08193a Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 7 Sep 2022 22:19:08 +0300 Subject: [PATCH] Inline tdlibParameters in setTdlibParameters. --- example/cpp/td_example.cpp | 23 +++++----- example/csharp/TdExample.cs | 22 +++++----- .../org/drinkless/tdlib/example/Example.java | 22 +++++----- example/python/tdjson_example.py | 20 ++++----- example/swift/src/main.swift | 20 ++++----- example/uwp/app/MainPage.xaml.cs | 20 ++++----- example/web/tdweb/src/worker.js | 12 +++--- td/generate/scheme/td_api.tl | 42 +++++++++---------- td/telegram/Td.cpp | 5 ++- td/telegram/Td.h | 2 +- td/telegram/cli.cpp | 32 +++++++------- test/online.cpp | 25 ++++++----- test/tdclient.cpp | 25 ++++++----- 13 files changed, 131 insertions(+), 139 deletions(-) diff --git a/example/cpp/td_example.cpp b/example/cpp/td_example.cpp index a78d4c53c..4ba34490f 100644 --- a/example/cpp/td_example.cpp +++ b/example/cpp/td_example.cpp @@ -290,18 +290,17 @@ class TdExample { create_authentication_query_handler()); }, [this](td_api::authorizationStateWaitTdlibParameters &) { - auto parameters = td_api::make_object(); - parameters->database_directory_ = "tdlib"; - parameters->use_message_database_ = true; - parameters->use_secret_chats_ = true; - parameters->api_id_ = 94575; - parameters->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2"; - parameters->system_language_code_ = "en"; - parameters->device_model_ = "Desktop"; - parameters->application_version_ = "1.0"; - parameters->enable_storage_optimizer_ = true; - send_query(td_api::make_object(std::move(parameters)), - create_authentication_query_handler()); + auto request = td_api::make_object(); + request->database_directory_ = "tdlib"; + request->use_message_database_ = true; + request->use_secret_chats_ = true; + request->api_id_ = 94575; + request->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2"; + request->system_language_code_ = "en"; + request->device_model_ = "Desktop"; + request->application_version_ = "1.0"; + request->enable_storage_optimizer_ = true; + send_query(std::move(request), create_authentication_query_handler()); })); } diff --git a/example/csharp/TdExample.cs b/example/csharp/TdExample.cs index 3c57a398c..469656572 100644 --- a/example/csharp/TdExample.cs +++ b/example/csharp/TdExample.cs @@ -67,18 +67,18 @@ namespace TdExample } if (_authorizationState is TdApi.AuthorizationStateWaitTdlibParameters) { - TdApi.TdlibParameters parameters = new TdApi.TdlibParameters(); - parameters.DatabaseDirectory = "tdlib"; - parameters.UseMessageDatabase = true; - parameters.UseSecretChats = true; - parameters.ApiId = 94575; - parameters.ApiHash = "a3406de8d171bb422bb6ddf3bbd800e2"; - parameters.SystemLanguageCode = "en"; - parameters.DeviceModel = "Desktop"; - parameters.ApplicationVersion = "1.0"; - parameters.EnableStorageOptimizer = true; + TdApi.SetTdlibParameters request = new TdApi.SetTdlibParameters(); + request.DatabaseDirectory = "tdlib"; + request.UseMessageDatabase = true; + request.UseSecretChats = true; + request.ApiId = 94575; + request.ApiHash = "a3406de8d171bb422bb6ddf3bbd800e2"; + request.SystemLanguageCode = "en"; + request.DeviceModel = "Desktop"; + request.ApplicationVersion = "1.0"; + request.EnableStorageOptimizer = true; - _client.Send(new TdApi.SetTdlibParameters(parameters), new AuthorizationRequestHandler()); + _client.Send(request, new AuthorizationRequestHandler()); } else if (_authorizationState is TdApi.AuthorizationStateWaitPhoneNumber) { diff --git a/example/java/org/drinkless/tdlib/example/Example.java b/example/java/org/drinkless/tdlib/example/Example.java index 133585904..7bda83960 100644 --- a/example/java/org/drinkless/tdlib/example/Example.java +++ b/example/java/org/drinkless/tdlib/example/Example.java @@ -101,18 +101,18 @@ public final class Example { } switch (Example.authorizationState.getConstructor()) { case TdApi.AuthorizationStateWaitTdlibParameters.CONSTRUCTOR: - TdApi.TdlibParameters parameters = new TdApi.TdlibParameters(); - parameters.databaseDirectory = "tdlib"; - parameters.useMessageDatabase = true; - parameters.useSecretChats = true; - parameters.apiId = 94575; - parameters.apiHash = "a3406de8d171bb422bb6ddf3bbd800e2"; - parameters.systemLanguageCode = "en"; - parameters.deviceModel = "Desktop"; - parameters.applicationVersion = "1.0"; - parameters.enableStorageOptimizer = true; + TdApi.SetTdlibParameters request = new TdApi.SetTdlibParameters(); + request.databaseDirectory = "tdlib"; + request.useMessageDatabase = true; + request.useSecretChats = true; + request.apiId = 94575; + request.apiHash = "a3406de8d171bb422bb6ddf3bbd800e2"; + request.systemLanguageCode = "en"; + request.deviceModel = "Desktop"; + request.applicationVersion = "1.0"; + request.enableStorageOptimizer = true; - client.send(new TdApi.SetTdlibParameters(parameters), new AuthorizationRequestHandler()); + client.send(parameters, new AuthorizationRequestHandler()); break; case TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR: { String phoneNumber = promptString("Please enter phone number: "); diff --git a/example/python/tdjson_example.py b/example/python/tdjson_example.py index d3e4516e5..986e4a37a 100644 --- a/example/python/tdjson_example.py +++ b/example/python/tdjson_example.py @@ -94,16 +94,16 @@ while True: # you MUST obtain your own api_id and api_hash at https://my.telegram.org # and use them in the setTdlibParameters call if auth_state['@type'] == 'authorizationStateWaitTdlibParameters': - td_send({'@type': 'setTdlibParameters', 'parameters': { - 'database_directory': 'tdlib', - 'use_message_database': True, - 'use_secret_chats': True, - 'api_id': 94575, - 'api_hash': 'a3406de8d171bb422bb6ddf3bbd800e2', - 'system_language_code': 'en', - 'device_model': 'Desktop', - 'application_version': '1.0', - 'enable_storage_optimizer': True}}) + td_send({'@type': 'setTdlibParameters', + 'database_directory': 'tdlib', + 'use_message_database': True, + 'use_secret_chats': True, + 'api_id': 94575, + 'api_hash': 'a3406de8d171bb422bb6ddf3bbd800e2', + 'system_language_code': 'en', + 'device_model': 'Desktop', + 'application_version': '1.0', + 'enable_storage_optimizer': True}) # enter phone number to log in if auth_state['@type'] == 'authorizationStateWaitPhoneNumber': diff --git a/example/swift/src/main.swift b/example/swift/src/main.swift index 9fd9926e3..bbee661f7 100644 --- a/example/swift/src/main.swift +++ b/example/swift/src/main.swift @@ -107,17 +107,15 @@ func updateAuthorizationState(authorizationState: Dictionary) { case "authorizationStateWaitTdlibParameters": client.queryAsync(query:[ "@type":"setTdlibParameters", - "parameters":[ - "database_directory":"tdlib", - "use_message_database":true, - "use_secret_chats":true, - "api_id":94575, - "api_hash":"a3406de8d171bb422bb6ddf3bbd800e2", - "system_language_code":"en", - "device_model":"Desktop", - "application_version":"1.0", - "enable_storage_optimizer":true - ] + "database_directory":"tdlib", + "use_message_database":true, + "use_secret_chats":true, + "api_id":94575, + "api_hash":"a3406de8d171bb422bb6ddf3bbd800e2", + "system_language_code":"en", + "device_model":"Desktop", + "application_version":"1.0", + "enable_storage_optimizer":true ]); case "authorizationStateWaitPhoneNumber": diff --git a/example/uwp/app/MainPage.xaml.cs b/example/uwp/app/MainPage.xaml.cs index d1c39c4e3..9be3f6944 100644 --- a/example/uwp/app/MainPage.xaml.cs +++ b/example/uwp/app/MainPage.xaml.cs @@ -36,16 +36,16 @@ namespace TdApp }); _client = Td.Client.Create(_handler); - var parameters = new TdApi.TdlibParameters(); - parameters.DatabaseDirectory = Windows.Storage.ApplicationData.Current.LocalFolder.Path; - parameters.UseSecretChats = true; - parameters.UseMessageDatabase = true; - parameters.ApiId = 94575; - parameters.ApiHash = "a3406de8d171bb422bb6ddf3bbd800e2"; - parameters.SystemLanguageCode = "en"; - parameters.DeviceModel = "Desktop"; - parameters.ApplicationVersion = "1.0.0"; - _client.Send(new TdApi.SetTdlibParameters(parameters), null); + var request = new TdApi.SetTdlibParameters(); + request.DatabaseDirectory = Windows.Storage.ApplicationData.Current.LocalFolder.Path; + request.UseSecretChats = true; + request.UseMessageDatabase = true; + request.ApiId = 94575; + request.ApiHash = "a3406de8d171bb422bb6ddf3bbd800e2"; + request.SystemLanguageCode = "en"; + request.DeviceModel = "Desktop"; + request.ApplicationVersion = "1.0.0"; + _client.Send(request, null); } public void Print(String str) diff --git a/example/web/tdweb/src/worker.js b/example/web/tdweb/src/worker.js index abc087c44..dff184512 100644 --- a/example/web/tdweb/src/worker.js +++ b/example/web/tdweb/src/worker.js @@ -734,14 +734,14 @@ class TdClient { prepareQuery(query) { if (query['@type'] === 'setTdlibParameters') { - query.parameters.database_directory = this.tdfs.dbFileSystem.root; - query.parameters.files_directory = this.tdfs.inboundFileSystem.root; + query.database_directory = this.tdfs.dbFileSystem.root; + query.files_directory = this.tdfs.inboundFileSystem.root; const useDb = this.useDatabase; - query.parameters.use_file_database = useDb; - query.parameters.use_chat_info_database = useDb; - query.parameters.use_message_database = useDb; - query.parameters.use_secret_chats = useDb; + query.use_file_database = useDb; + query.use_chat_info_database = useDb; + query.use_message_database = useDb; + query.use_secret_chats = useDb; } if (query['@type'] === 'getLanguagePackString') { query.language_pack_database_path = diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 7fed1929d..727ed25d3 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -22,26 +22,6 @@ error code:int32 message:string = Error; ok = Ok; -//@description Contains parameters for TDLib initialization -//@use_test_dc If set to true, the Telegram test environment will be used instead of the production environment -//@database_directory The path to the directory for the persistent database; if empty, the current working directory will be used -//@files_directory The path to the directory for storing files; if empty, database_directory will be used -//@database_encryption_key Encryption key for the database -//@use_file_database If set to true, information about downloaded and uploaded files will be saved between application restarts -//@use_chat_info_database If set to true, the library will maintain a cache of users, basic groups, supergroups, channels and secret chats. Implies use_file_database -//@use_message_database If set to true, the library will maintain a cache of chats and messages. Implies use_chat_info_database -//@use_secret_chats If set to true, support for secret chats will be enabled -//@api_id Application identifier for Telegram API access, which can be obtained at https://my.telegram.org -//@api_hash Application identifier hash for Telegram API access, which can be obtained at https://my.telegram.org -//@system_language_code IETF language tag of the user's operating system language; must be non-empty -//@device_model Model of the device the application is being run on; must be non-empty -//@system_version Version of the operating system the application is being run on. If empty, the version is automatically detected by TDLib -//@application_version Application version; must be non-empty -//@enable_storage_optimizer If set to true, old files will automatically be deleted -//@ignore_file_names If set to true, original file names will be ignored. Otherwise, downloaded files will be saved under names as close as possible to the original name -tdlibParameters use_test_dc:Bool database_directory:string files_directory:string database_encryption_key:bytes use_file_database:Bool use_chat_info_database:Bool use_message_database:Bool use_secret_chats:Bool api_id:int32 api_hash:string system_language_code:string device_model:string system_version:string application_version:string enable_storage_optimizer:Bool ignore_file_names:Bool = TdlibParameters; - - //@class AuthenticationCodeType @description Provides information about the method by which an authentication code is delivered to the user //@description An authentication code is delivered via a private Telegram message, which can be viewed from another active session @length Length of the code @@ -96,7 +76,7 @@ termsOfService text:formattedText min_user_age:int32 show_popup:Bool = TermsOfSe //@class AuthorizationState @description Represents the current authorization state of the TDLib client -//@description TDLib needs TdlibParameters for initialization +//@description Initializetion parameters are needed. Call `setTdlibParameters` to provide them authorizationStateWaitTdlibParameters = AuthorizationState; //@description TDLib needs the user's phone number to authorize. Call `setAuthenticationPhoneNumber` to provide the phone number, or use `requestQrCodeAuthentication`, or `checkAuthenticationBotToken` for other authentication options @@ -4624,8 +4604,24 @@ testVectorStringObject value:vector = TestVectorStringObject; getAuthorizationState = AuthorizationState; -//@description Sets the parameters for TDLib initialization. Works only when the current authorization state is authorizationStateWaitTdlibParameters @parameters Parameters for TDLib initialization -setTdlibParameters parameters:tdlibParameters = Ok; +//@description Sets the parameters for TDLib initialization. Works only when the current authorization state is authorizationStateWaitTdlibParameters +//@use_test_dc Pass true to use Telegram test environment instead of the production environment +//@database_directory The path to the directory for the persistent database; if empty, the current working directory will be used +//@files_directory The path to the directory for storing files; if empty, database_directory will be used +//@database_encryption_key Encryption key for the database +//@use_file_database Pass true to keep information about downloaded and uploaded files between application restarts +//@use_chat_info_database Pass true to keep cache of users, basic groups, supergroups, channels and secret chats between restarts. Implies use_file_database +//@use_message_database Pass true to keep cache of chats and messages between restarts. Implies use_chat_info_database +//@use_secret_chats Pass true to enable support for secret chats +//@api_id Application identifier for Telegram API access, which can be obtained at https://my.telegram.org +//@api_hash Application identifier hash for Telegram API access, which can be obtained at https://my.telegram.org +//@system_language_code IETF language tag of the user's operating system language; must be non-empty +//@device_model Model of the device the application is being run on; must be non-empty +//@system_version Version of the operating system the application is being run on. If empty, the version is automatically detected by TDLib +//@application_version Application version; must be non-empty +//@enable_storage_optimizer Pass true to automatically delete old files in background +//@ignore_file_names Pass true to ignore original file names for downloaded files. Otherwise, downloaded files are saved under names as close as possible to the original name +setTdlibParameters use_test_dc:Bool database_directory:string files_directory:string database_encryption_key:bytes use_file_database:Bool use_chat_info_database:Bool use_message_database:Bool use_secret_chats:Bool api_id:int32 api_hash:string system_language_code:string device_model:string system_version:string application_version:string enable_storage_optimizer:Bool ignore_file_names:Bool = Ok; //@description Sets the phone number of the user and sends an authentication code to the user. Works only when the current authorization state is authorizationStateWaitPhoneNumber, //-or if there is no pending authentication query and the current authorization state is authorizationStateWaitCode, authorizationStateWaitRegistration, or authorizationStateWaitPassword diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index a0be1bde1..7fe11571d 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -2982,7 +2982,8 @@ void Td::run_request(uint64 id, tl_object_ptr function) { case State::WaitParameters: { switch (function_id) { case td_api::setTdlibParameters::ID: { - auto status = set_parameters(std::move(move_tl_object_as(function)->parameters_)); + auto parameters = move_tl_object_as(function); + auto status = set_parameters(std::move(parameters)); if (status.is_error()) { return send_closure(actor_id(this), &Td::send_error, id, std::move(status)); } @@ -4096,7 +4097,7 @@ Status Td::fix_parameters(TdParameters ¶meters) { return Status::OK(); } -Status Td::set_parameters(td_api::object_ptr parameters) { +Status Td::set_parameters(td_api::object_ptr parameters) { VLOG(td_init) << "Begin to set TDLib parameters"; if (parameters == nullptr) { VLOG(td_init) << "Empty parameters"; diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 9a8d3cefb..915a29ec6 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1481,7 +1481,7 @@ class Td final : public Actor { static Status fix_parameters(TdParameters ¶meters) TD_WARN_UNUSED_RESULT; - Status set_parameters(td_api::object_ptr parameters) TD_WARN_UNUSED_RESULT; + Status set_parameters(td_api::object_ptr parameters) TD_WARN_UNUSED_RESULT; static td_api::object_ptr make_error(int32 code, CSlice error) { return td_api::make_object(code, error.str()); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index b450d5fbf..a1a7a5839 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -400,19 +400,19 @@ class CliClient final : public Actor { authorization_state_ = std::move(state); switch (authorization_state_->get_id()) { case td_api::authorizationStateWaitTdlibParameters::ID: { - auto parameters = td_api::make_object(); - parameters->use_test_dc_ = use_test_dc_; - parameters->use_message_database_ = true; - parameters->use_chat_info_database_ = true; - parameters->use_secret_chats_ = true; - parameters->api_id_ = api_id_; - parameters->api_hash_ = api_hash_; - parameters->system_language_code_ = "en"; - parameters->device_model_ = "Desktop"; - parameters->application_version_ = "1.0"; + auto request = td_api::make_object(); + request->use_test_dc_ = use_test_dc_; + request->use_message_database_ = true; + request->use_chat_info_database_ = true; + request->use_secret_chats_ = true; + request->api_id_ = api_id_; + request->api_hash_ = api_hash_; + request->system_language_code_ = "en"; + request->device_model_ = "Desktop"; + request->application_version_ = "1.0"; send_request( td_api::make_object("use_pfs", td_api::make_object(true))); - send_request(td_api::make_object(std::move(parameters))); + send_request(std::move(request)); break; } case td_api::authorizationStateReady::ID: @@ -1162,11 +1162,11 @@ class CliClient final : public Actor { td_api::make_object())); send_request(td_api::make_object(0)); - auto bad_parameters = td_api::make_object(); - bad_parameters->database_directory_ = "/.."; - bad_parameters->api_id_ = api_id_; - bad_parameters->api_hash_ = api_hash_; - send_request(td_api::make_object(std::move(bad_parameters))); + auto bad_request = td_api::make_object(); + bad_request->database_directory_ = "/.."; + bad_request->api_id_ = api_id_; + bad_request->api_hash_ = api_hash_; + send_request(std::move(bad_request)); } } diff --git a/test/online.cpp b/test/online.cpp index d4339f558..f472c6ae8 100644 --- a/test/online.cpp +++ b/test/online.cpp @@ -235,19 +235,18 @@ class InitTask : public Task { stop(); break; case td::td_api::authorizationStateWaitTdlibParameters::ID: { - auto parameters = td::td_api::make_object(); - parameters->use_test_dc_ = true; - parameters->database_directory_ = options_.name + TD_DIR_SLASH; - parameters->use_message_database_ = true; - parameters->use_secret_chats_ = true; - parameters->api_id_ = options_.api_id; - parameters->api_hash_ = options_.api_hash; - parameters->system_language_code_ = "en"; - parameters->device_model_ = "Desktop"; - parameters->application_version_ = "tdclient-test"; - parameters->ignore_file_names_ = false; - parameters->enable_storage_optimizer_ = true; - send(td::td_api::make_object(std::move(parameters))); + auto request = td::td_api::make_object(); + request->use_test_dc_ = true; + request->database_directory_ = options_.name + TD_DIR_SLASH; + request->use_message_database_ = true; + request->use_secret_chats_ = true; + request->api_id_ = options_.api_id; + request->api_hash_ = options_.api_hash; + request->system_language_code_ = "en"; + request->device_model_ = "Desktop"; + request->application_version_ = "tdclient-test"; + request->enable_storage_optimizer_ = true; + send(std::move(request)); break; } default: diff --git a/test/tdclient.cpp b/test/tdclient.cpp index fe2207b78..862cb7903 100644 --- a/test/tdclient.cpp +++ b/test/tdclient.cpp @@ -221,19 +221,18 @@ class DoAuthentication final : public TestClinetTask { function = td::make_tl_object(name_, ""); break; case td::td_api::authorizationStateWaitTdlibParameters::ID: { - auto parameters = td::td_api::make_object(); - parameters->use_test_dc_ = true; - parameters->database_directory_ = name_ + TD_DIR_SLASH; - parameters->use_message_database_ = true; - parameters->use_secret_chats_ = true; - parameters->api_id_ = 94575; - parameters->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2"; - parameters->system_language_code_ = "en"; - parameters->device_model_ = "Desktop"; - parameters->application_version_ = "tdclient-test"; - parameters->ignore_file_names_ = false; - parameters->enable_storage_optimizer_ = true; - function = td::td_api::make_object(std::move(parameters)); + auto request = td::td_api::make_object(); + request->use_test_dc_ = true; + request->database_directory_ = name_ + TD_DIR_SLASH; + request->use_message_database_ = true; + request->use_secret_chats_ = true; + request->api_id_ = 94575; + request->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2"; + request->system_language_code_ = "en"; + request->device_model_ = "Desktop"; + request->application_version_ = "tdclient-test"; + request->enable_storage_optimizer_ = true; + function = std::move(request); break; } case td::td_api::authorizationStateReady::ID: