From d14cbcc34026f0c85d500ea4dbaaeb11ab805546 Mon Sep 17 00:00:00 2001 From: DenysGonchar Date: Mon, 19 Apr 2021 02:28:14 +0200 Subject: [PATCH] removing run_global/2 and run_for_host_type/3 interfaces --- src/ejabberd_hooks.erl | 36 ++++++++++++------------------- src/mongoose_hooks.erl | 4 ++-- test/ejabberd_c2s_SUITE_mocks.erl | 2 -- test/ejabberd_hooks_SUITE.erl | 8 +++---- test/ejabberd_sm_SUITE.erl | 1 - 5 files changed, 20 insertions(+), 31 deletions(-) diff --git a/src/ejabberd_hooks.erl b/src/ejabberd_hooks.erl index bfd31a50f1..f331438c3a 100644 --- a/src/ejabberd_hooks.erl +++ b/src/ejabberd_hooks.erl @@ -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 @@ -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(), @@ -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(), diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 5260c34baf..5d50561f98 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -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) -> diff --git a/test/ejabberd_c2s_SUITE_mocks.erl b/test/ejabberd_c2s_SUITE_mocks.erl index 3da5d127ed..e84c3561c9 100644 --- a/test/ejabberd_c2s_SUITE_mocks.erl +++ b/test/ejabberd_c2s_SUITE_mocks.erl @@ -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), diff --git a/test/ejabberd_hooks_SUITE.erl b/test/ejabberd_hooks_SUITE.erl index 133ac0cca9..65264462fa 100644 --- a/test/ejabberd_hooks_SUITE.erl +++ b/test/ejabberd_hooks_SUITE.erl @@ -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), @@ -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). @@ -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). @@ -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). diff --git a/test/ejabberd_sm_SUITE.erl b/test/ejabberd_sm_SUITE.erl index c0e053655a..2f2d43802b 100644 --- a/test/ejabberd_sm_SUITE.erl +++ b/test/ejabberd_sm_SUITE.erl @@ -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) ->