From 0704304ec502f869d91fc249a73ac6f2e0a9b2d2 Mon Sep 17 00:00:00 2001 From: Alexander Q Date: Tue, 15 Nov 2022 19:14:45 +0300 Subject: [PATCH] updated for tdlib 1.8.8 (probably 1.6.0 and newer); fixed docs --- doc/tdlib.md | 60 +++++++---------------------------------- doc/tdlib_nif.md | 20 +------------- src/tdlib.erl | 69 +++++++++++++++++++++++++++++++++++------------- 3 files changed, 61 insertions(+), 88 deletions(-) diff --git a/doc/tdlib.md b/doc/tdlib.md index 543668b..39ec2e9 100644 --- a/doc/tdlib.md +++ b/doc/tdlib.md @@ -11,7 +11,7 @@ __Behaviours:__ [`gen_server`](gen_server.md). ## Function Index ## -
auth_code/2Send authentication code.
auth_code/3Send authentication code for unregistered user.
auth_code/4Send authentication code for unregistered user.
auth_password/2Send password.
config/2Send tdlib configuration.
execute/2Execute synchronous tdlib request.
get_auth_state/1Get current auth state of tdlib.
get_config/1Get tdlib configuration.
get_handlers/1Get list of current handlers.
phone_number/2Send phone number.
register_handler/2Add handler to tdlib instance.
send/2Send tdlib request.
send_sync/2Send tdlib request and block until response is received.
send_sync/3Send tdlib request and block until response is received.
set_log_file_path/0Set log logging behaviour to default.
set_log_file_path/1Set log file path.
set_log_max_file_size/1Set max log file size.
set_log_verbosity_level/1Set tdlib log verbosity level.
start_link/0Start new tdlib instance.
start_link/2Start new tdlib instance, register it and send config when ready.
+
auth_code/2Send authentication code.
auth_code/3Send authentication code for unregistered user.
auth_code/4Send authentication code for unregistered user.
auth_password/2Send password.
config/2Send tdlib configuration.
execute/2Execute synchronous tdlib request.
get_auth_state/1Get current auth state of tdlib.
get_config/1Get tdlib configuration.
get_handlers/1Get list of current handlers.
get_version/1Get tdlib version.
phone_number/2Send phone number.
register_handler/2Add handler to tdlib instance.
send/2Send tdlib request.
send_sync/2Send tdlib request and block until response is received.
send_sync/3Send tdlib request and block until response is received.
start_link/0Start new tdlib instance.
start_link/2Start new tdlib instance, register it and send config when ready.
@@ -90,6 +90,14 @@ Get tdlib configuration. Get list of current handlers. + + +### get_version/1 ### + +`get_version(Pid) -> any()` + +Get tdlib version. + ### phone_number/2 ### @@ -135,56 +143,6 @@ Send tdlib request and block until response is received. Inside it's a `gen_server:call`, so expect `{timeout, _}` error if you need to handle this. - - -### set_log_file_path/0 ### - -`set_log_file_path() -> any()` - -Set log logging behaviour to default. - -By default TDLib writes logs to stderr or an OS specific log. Use this -method restore such behaviour. - -__See also:__ [set_log_file_path/1](#set_log_file_path-1). - - - -### set_log_file_path/1 ### - -`set_log_file_path(Path) -> any()` - -Set log file path. - -Sets the path to the file where the internal TDLib log will be written. -By default TDLib writes logs to stderr or an OS specific log. Use this -method to write the log to a file instead. - -__See also:__ [set_log_file_path/0](#set_log_file_path-0). - - - -### set_log_max_file_size/1 ### - -`set_log_max_file_size(Size) -> any()` - -Set max log file size. - -Sets maximum size of the file to where the internal TDLib log is -written before the file will be auto-rotated. Unused if log is not -written to a file. Defaults to 10 MB. - - - -### set_log_verbosity_level/1 ### - -`set_log_verbosity_level(Level) -> any()` - -Set tdlib log verbosity level. - -Sets the verbosity level of the internal logging of TDLib. By default -the TDLib uses a log verbosity level of 5. - ### start_link/0 ### diff --git a/doc/tdlib_nif.md b/doc/tdlib_nif.md index 34c7418..205ee50 100644 --- a/doc/tdlib_nif.md +++ b/doc/tdlib_nif.md @@ -9,7 +9,7 @@ ## Function Index ## -
execute/2
new/0
recv/2
send/2
set_log_file_path/1
set_log_max_file_size/1
set_log_verbosity_level/1
+
execute/2
new/0
recv/2
send/2
@@ -40,21 +40,3 @@ `send(X1, X2) -> any()` - - -### set_log_file_path/1 ### - -`set_log_file_path(X1) -> any()` - - - -### set_log_max_file_size/1 ### - -`set_log_max_file_size(X1) -> any()` - - - -### set_log_verbosity_level/1 ### - -`set_log_verbosity_level(X1) -> any()` - diff --git a/src/tdlib.erl b/src/tdlib.erl index 0f2947f..c6c1ca9 100644 --- a/src/tdlib.erl +++ b/src/tdlib.erl @@ -67,22 +67,8 @@ register_handler(Pid, Handler) -> %%==================================================================== config(Pid, Cfg) -> try - case lists:keyfind(api_id, 1, Cfg) of - false -> throw({missing_param, api_id}); - _ -> ok - end, - - case lists:keyfind(api_hash, 1, Cfg) of - false -> throw({missing_param, api_hash}); - _ -> ok - end, - - case lists:keyfind(database_directory, 1, Cfg) of - false -> throw({missing_param, database_directory}); - _ -> ok - end, - - gen_server:call(Pid, {config, Cfg}) + Config = create_config(Cfg), + gen_server:call(Pid, {config, Config}) catch _:Err = {missing_param, _} -> {error, Err} @@ -346,7 +332,7 @@ handle_call( Cfg ), - Request = method(setTdlibParameters, [{parameters, Config}]), + Request = method(setTdlibParameters, Config), send(self(), Request), @@ -436,7 +422,9 @@ terminate(_, _) -> ok. %%==================================================================== %% @private -method(Type, Params) -> +method(Type, Params) when is_map(Params) -> + Params#{'@type' => Type}; +method(Type, Params) when is_list(Params) -> [{'@type', Type} | Params]. %% @private @@ -481,3 +469,48 @@ update_option(State, <<"commit_hash">>, Commit) -> State#state{tdlib_commit = Commit}; update_option(State, _, _) -> State. + +%% @private +create_config(null) -> + null; +create_config(Cfg) when is_map(Cfg) -> + create_config(maps:to_list(Cfg)); +create_config(Cfg) -> + DefaultConfig = [ + {<<"application_version">>, <<"Unknown">>}, + {<<"device_model">>, <<"Unknown">>}, + {<<"enable_storage_optimizer">>, true}, + {<<"files_directory">>, null}, + {<<"ignore_file_names">>, true}, + {<<"system_language_code">>, <<"en">>}, + {<<"system_version">>, null}, + {<<"use_chat_info_database">>, true}, + {<<"use_file_database">>, true}, + {<<"use_message_database">>, true}, + {<<"use_secret_chats">>, false}, + {<<"use_test_dc">>, false} + ], + + Config = lists:foldl( + fun({Key, Val}, Acc) -> + Key_ = + case Key of + _ when is_atom(Key) -> atom_to_binary(Key, utf8); + _ when is_list(Key) -> list_to_binary(Key); + _ when is_binary(Key) -> Key + end, + lists:keystore(Key_, 1, Acc, {Key_, Val}) + end, + DefaultConfig, + Cfg + ), + + lists:foreach( + fun(Name) -> + case lists:keyfind(Name, 1, Config) of + false -> throw({missing_param, api_id}); + _ -> ok + end + end, [<<"api_id">>, <<"api_hash">>, <<"database_directory">>]), + + Config.