Skip to content

Commit

Permalink
gco 1
Browse files Browse the repository at this point in the history
  • Loading branch information
lattenwald committed Nov 10, 2022
1 parent 82c29a9 commit ec69dff
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/tdlib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
-export([register_handler/2, config/2, send/2, execute/2, method/2, send_sync/2, send_sync/3]).
-export([phone_number/2, auth_code/2, auth_code/3, auth_code/4, auth_password/2]).

-export([get_handlers/1, get_auth_state/1, get_config/1]).
-export([get_handlers/1, get_auth_state/1, get_config/1, get_version/1]).

-export([remove_extra/2]).

-define(RECEIVE_TIMEOUT, 5.0).
-define(SEND_SYNC_TIMEOUT, 5000).

-record(state, {
tdlib_version = null,
tdlib_commit = null,
tdlib = null,
handlers,
auth_state = null,
Expand Down Expand Up @@ -224,6 +226,14 @@ get_config(Pid) ->
remove_extra(Pid, Index) ->
gen_server:cast(Pid, {remove_extra, Index}).

%%====================================================================
%% @doc Get tdlib version.
%%
%% @param Pid tdlib gen_server pid
%%====================================================================
get_version(Pid) ->
gen_server:call(Pid, get_version).

%%====================================================================
%% callbacks
%%====================================================================
Expand Down Expand Up @@ -287,7 +297,17 @@ handle_info({received, {ok, Msg}}, State = #state{extra = Extra, handlers = Hand
end,

self() ! poll,
{noreply, State#state{extra = NewExtra}};

NewState = case Data of
#{<<"@type">> := <<"updateOption">>,
<<"name">> := OptionName,
<<"value">> := #{<<"value">> := OptionValue}} ->
update_option(State, OptionName, OptionValue);
_ ->
State
end,

{noreply, NewState#state{extra = NewExtra}};
handle_info(_Msg, State) ->
{noreply, State}.

Expand Down Expand Up @@ -355,6 +375,8 @@ handle_call(get_auth_state, _From, State = #state{auth_state = AuthState}) ->
{reply, AuthState, State};
handle_call(get_config, _From, State = #state{config = Config}) ->
{reply, Config, State};
handle_call(get_version, _From, State = #state{tdlib_version = Version, tdlib_commit = Commit}) ->
{reply, #{version => Version, commit => Commit}, State};
handle_call(_Msg, _From, State) ->
{reply, ok, State}.

Expand Down Expand Up @@ -451,3 +473,11 @@ set_auth_state(Pid, AuthStateType) ->
%% @private
send_config(Pid) ->
gen_server:cast(Pid, send_config).

%% @private
update_option(State, <<"version">>, Version) ->
State#state{tdlib_version = Version};
update_option(State, <<"commit_hash">>, Commit) ->
State#state{tdlib_commit = Commit};
update_option(State, _, _) ->
State.

0 comments on commit ec69dff

Please sign in to comment.