Skip to content

Commit

Permalink
Refactored hook handlers in mod_keystore
Browse files Browse the repository at this point in the history
  • Loading branch information
pawlooss1 committed Nov 10, 2022
1 parent 145669a commit ed7f122
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
24 changes: 12 additions & 12 deletions src/mod_keystore.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-export([config_spec/0]).

%% Hook handlers
-export([get_key/2]).
-export([get_key/3]).

-export([process_key/1]).

Expand All @@ -21,8 +21,6 @@
key_name/0,
raw_key/0]).

-ignore_xref([get_key/2]).

-include("mod_keystore.hrl").
-include("mongoose.hrl").
-include("mongoose_config_spec.hrl").
Expand Down Expand Up @@ -53,18 +51,19 @@ start(HostType, Opts) ->
create_keystore_ets(),
mod_keystore_backend:init(HostType, Opts),
init_keys(HostType, Opts),
ejabberd_hooks:add(hooks(HostType)),
gen_hook:add_handlers(hooks(HostType)),
ok.

-spec stop(mongooseim:host_type()) -> ok.
stop(HostType) ->
ejabberd_hooks:delete(hooks(HostType)),
gen_hook:delete_handlers(hooks(HostType)),
clear_keystore_ets(HostType),
ok.

-spec hooks(mongooseim:host_type()) -> gen_hook:hook_list().
hooks(HostType) ->
[
{get_key, HostType, ?MODULE, get_key, 50}
{get_key, HostType, fun ?MODULE:get_key/3, #{}, 50}
].

-spec supported_features() -> [atom()].
Expand Down Expand Up @@ -105,12 +104,12 @@ process_key(#{name := Name, type := ram}) ->
%% Hook handlers
%%

-spec get_key(HandlerAcc, KeyID) -> Result when
-spec get_key(HandlerAcc, Params, Extra) -> {ok, HandlerAcc} when
HandlerAcc :: key_list(),
KeyID :: key_id(),
Result :: key_list().
get_key(HandlerAcc, KeyID) ->
try
Params :: #{key_id := key_id()},
Extra :: gen_hook:extra().
get_key(HandlerAcc, #{key_id := KeyID}, _) ->
NewAcc = try
%% This is OK, because the key is
%% EITHER stored in ETS
%% OR stored in BACKEND,
Expand All @@ -124,7 +123,8 @@ get_key(HandlerAcc, KeyID) ->
?LOG_ERROR(#{what => get_key_failed,
error => E, reason => R, stacktrace => S}),
HandlerAcc
end.
end,
{ok, NewAcc}.

%%
%% Internal functions
Expand Down
5 changes: 4 additions & 1 deletion src/mongoose_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ extend_inbox_message(MongooseAcc, InboxRes, IQ) ->
KeyName :: atom(),
Result :: mod_keystore:key_list().
get_key(HostType, KeyName) ->
run_hook_for_host_type(get_key, HostType, [], [{KeyName, HostType}]).
Params = #{key_id => {KeyName, HostType}},
Args = [{KeyName, HostType}],
ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args),
run_hook_for_host_type(get_key, HostType, [], ParamsWithLegacyArgs).

-spec packet_to_component(Acc, From, To) -> Result when
Acc :: mongoose_acc:t(),
Expand Down

0 comments on commit ed7f122

Please sign in to comment.