Skip to content

Commit

Permalink
Refactored hook handlers in ejabberd_s2s module
Browse files Browse the repository at this point in the history
  • Loading branch information
pawlooss1 committed Sep 16, 2022
1 parent 563764e commit 723d58b
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/ejabberd_s2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
]).

%% Hooks callbacks
-export([node_cleanup/2]).
-export([node_cleanup/3]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
Expand All @@ -59,7 +59,7 @@
-export([get_info_s2s_connections/1]).

-ignore_xref([dirty_get_connections/0, get_info_s2s_connections/1, have_connection/1,
incoming_s2s_number/0, node_cleanup/2, outgoing_s2s_number/0, start_link/0]).
incoming_s2s_number/0, outgoing_s2s_number/0, start_link/0]).

-include("mongoose.hrl").
-include("jlib.hrl").
Expand Down Expand Up @@ -163,7 +163,8 @@ dirty_get_connections() ->
%% Hooks callbacks
%%====================================================================

node_cleanup(Acc, Node) ->
-spec node_cleanup(map(), map(), map()) -> {ok, map()}.
node_cleanup(Acc, #{node := Node}, _) ->
F = fun() ->
Es = mnesia:select(
s2s,
Expand All @@ -175,7 +176,7 @@ node_cleanup(Acc, Node) ->
end, Es)
end,
Res = mnesia:async_dirty(F),
maps:put(?MODULE, Res, Acc).
{ok, maps:put(?MODULE, Res, Acc)}.

-spec key(mongooseim:host_type(), {jid:lserver(), jid:lserver()}, binary()) ->
binary().
Expand Down Expand Up @@ -205,7 +206,7 @@ init([]) ->
mnesia:add_table_copy(s2s_shared, node(), ram_copies),
{atomic, ok} = set_shared_secret(),
ejabberd_commands:register_commands(commands()),
ejabberd_hooks:add(node_cleanup, global, ?MODULE, node_cleanup, 50),
gen_hook:add_handlers(hooks()),
{ok, #state{}}.

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -250,7 +251,7 @@ handle_info(Msg, State) ->
%% The return value is ignored.
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
ejabberd_hooks:delete(node_cleanup, global, ?MODULE, node_cleanup, 50),
gen_hook:delete_handlers(hooks()),
ejabberd_commands:unregister_commands(commands()),
ok.

Expand All @@ -264,6 +265,9 @@ code_change(_OldVsn, State, _Extra) ->
%%--------------------------------------------------------------------
%%% Internal functions
%%--------------------------------------------------------------------
-spec hooks() -> [gen_hook:hook_tuple()].
hooks() ->
[{node_cleanup, global, fun ?MODULE:node_cleanup/3, #{}, 50}].

-spec do_route(From :: jid:jid(),
To :: jid:jid(),
Expand Down

0 comments on commit 723d58b

Please sign in to comment.