Skip to content

Commit

Permalink
removing run_global/2 and run_for_host_type/3 interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysGonchar committed Apr 19, 2021
1 parent ec24b01 commit d14cbcc
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 31 deletions.
36 changes: 14 additions & 22 deletions src/ejabberd_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
add/5,
delete/4,
delete/5,
run_global/2,
run_global/3,
run_for_host_type/3,
run_for_host_type/4]).

%% gen_server callbacks
Expand Down Expand Up @@ -133,31 +131,13 @@ delete(Hooks) when is_list(Hooks) ->
delete_hook(Hook) ->
gen_server:call(ejabberd_hooks, {delete, Hook}).

%% @doc Run the calls of this hook in order, don't care about function results.
%% If a call returns stop, no more calls are performed.
-spec run_global(HookName :: atom(),
Args :: [any()]) -> ok.
run_global(HookName, Args) ->
run_fold(HookName, global, ok, Args).

-spec run_for_host_type(HookName :: atom(),
HostType :: binary(),
Args :: [any()]) -> ok.
run_for_host_type(HookName, HostType, Args) ->
%% We don't provide mongoose_acc here because we can't create a valid one.
run_fold(HookName, HostType, ok, Args).

%% @spec (HookName::atom(), Acc, Args) -> Val | stopped | NewVal
%% @doc Run the calls of this hook in order.
%% The arguments passed to the function are: [Acc | Args].
%% The result of a call is used as Acc for the next call.
%% If a call returns 'stop', no more calls are performed and 'stopped' is returned.
%% If a call returns {stop, NewAcc}, no more calls are performed and NewAcc is returned.
%% @doc run global hook, for more details see run_fold/4 documentation.
-spec run_global(HookName :: atom(), Acc :: term(), Args :: [term()]) ->
NewAcc :: term() | stopped.
run_global(HookName, Acc, Args) ->
run_fold(HookName, global, Acc, Args).

%% @doc run hook for the host type, for more details see run_fold/4 documentation.
-spec run_for_host_type(HookName :: atom(),
HostType :: binary(),
Acc :: term(),
Expand Down Expand Up @@ -261,6 +241,18 @@ code_change(_OldVsn, State, _Extra) ->
%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------

%% @doc Run the handlers of the hook in order.
%% The arguments passed to the hook handler function are: [Acc | Args].
%% The result of a call is used as Acc for the next call.
%% If a call returns 'stop', no more calls are performed and 'stopped' is returned.
%% If a call returns {stop, NewAcc}, no more calls are performed and NewAcc is returned.
%% If hook doesn't need accumulator and doesn't care about the return value. The 'ok'
%% atom can be used as initial Acc value.
%%
%% Note that every hook handler MUST return a valid Acc. if any hook handler is not
%% interested in Acc parameter (or even if Acc is not used for a hook at all), it must
%% return (pass through) an unchanged input accumulator value.
-spec run_fold(HookName :: atom(),
HostType :: global | binary(),
Acc :: term(),
Expand Down
4 changes: 2 additions & 2 deletions src/mongoose_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,14 @@ auth_failed(Server, Username) ->
Domain :: jid:lserver(),
Result :: ok.
disable_domain(HostType, Domain) ->
ejabberd_hooks:run_global(disable_domain, [HostType, Domain]).
ejabberd_hooks:run_global(disable_domain, ok, [HostType, Domain]).

-spec remove_domain(HostType, Domain) -> Result when
HostType :: binary(),
Domain :: jid:lserver(),
Result :: ok.
remove_domain(HostType, Domain) ->
ejabberd_hooks:run_global(remove_domain, [HostType, Domain]).
ejabberd_hooks:run_global(remove_domain, ok, [HostType, Domain]).

-spec node_cleanup(Node :: node()) -> Acc :: map().
node_cleanup(Node) ->
Expand Down
2 changes: 0 additions & 2 deletions test/ejabberd_c2s_SUITE_mocks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ setup() ->
meck:expect(mongoose_credentials, get, fun mcred_get/2),

meck:new(ejabberd_hooks),
meck:expect(ejabberd_hooks, run_global, fun(_, _) -> ok end),
meck:expect(ejabberd_hooks, run_for_host_type, fun(_, _, _) -> ok end),
meck:expect(ejabberd_hooks, run_global, fun hookfold/3),
meck:expect(ejabberd_hooks, run_for_host_type, fun hookfold/4),

Expand Down
8 changes: 4 additions & 4 deletions test/ejabberd_hooks_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ hooks_run_launches_nullary_fun(_) ->
given_hook_added(test_run_hook, hook_mod, fun_nullary, 1),

%% when
ejabberd_hooks:run_for_host_type(test_run_hook, ?HOST, []),
ejabberd_hooks:run_for_host_type(test_run_hook, ?HOST, ok, []),

%% then
H = meck:history(hook_mod),
Expand All @@ -99,7 +99,7 @@ hooks_run_launches_unary_fun(_) ->
given_hook_added(test_run_hook, hook_mod, fun_onearg, 1),

%% when
ejabberd_hooks:run_for_host_type(test_run_hook, ?HOST, [oneval]),
ejabberd_hooks:run_for_host_type(test_run_hook, ?HOST, ok, [oneval]),

%% then
[{_,{hook_mod,fun_onearg,[ok, oneval]}, oneval}] = meck:history(hook_mod).
Expand All @@ -113,7 +113,7 @@ hooks_run_ignores_different_arity_funs(_) ->
given_hook_added(test_run_hook, hook_mod, fun_twoarg, 1),

%% when
ejabberd_hooks:run_for_host_type(test_run_hook, ?HOST, [one, two]),
ejabberd_hooks:run_for_host_type(test_run_hook, ?HOST, ok, [one, two]),

%% then
[{_,{hook_mod, fun_twoarg, [ok, one, two]}, success2}] = meck:history(hook_mod).
Expand All @@ -127,7 +127,7 @@ hooks_run_stops_when_fun_returns_stop(_) ->
given_hook_added(test_run_hook, hook_mod, another_fun, 2),

%% when
ejabberd_hooks:run_for_host_type(test_run_hook, ?HOST, []),
ejabberd_hooks:run_for_host_type(test_run_hook, ?HOST, ok, []),

%% then
[{_,{hook_mod,a_fun,[ok]}, stop}] = meck:history(hook_mod).
Expand Down
1 change: 0 additions & 1 deletion test/ejabberd_sm_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ set_test_case_meck(MaxUserSessions) ->
meck:new(acl, []),
meck:expect(acl, match_rule, fun(_, _, _) -> MaxUserSessions end),
meck:new(ejabberd_hooks, []),
meck:expect(ejabberd_hooks, run_for_host_type, fun(_, _, _) -> ok end),
meck:expect(ejabberd_hooks, run_for_host_type, fun(_, _, Acc, _) -> Acc end).

set_test_case_meck_unique_count_crash(Backend) ->
Expand Down

0 comments on commit d14cbcc

Please sign in to comment.