Skip to content

Commit

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

%% Hooks and IQ handlers
-export([push_notifications/4]).
-export([push_notifications/3]).

-export([http_notification/5]).

-export([config_metrics/1]).

-ignore_xref([http_notification/5, push_notifications/4]).
-ignore_xref([http_notification/5]).

%%--------------------------------------------------------------------
%% Module callbacks
Expand All @@ -42,7 +42,7 @@ start(Host, Opts) ->
?LOG_INFO(#{what => push_service_starting, server => Host}),
start_pool(Host, Opts),
%% Hooks
ejabberd_hooks:add(push_notifications, Host, ?MODULE, push_notifications, 10),
gen_hook:add_handlers(hooks(Host)),
ok.

-spec start_pool(mongooseim:host_type(), gen_mod:module_opts()) -> term().
Expand All @@ -56,7 +56,7 @@ pool_opts(#{max_http_connections := MaxHTTPConnections}) ->

-spec stop(Host :: jid:server()) -> ok.
stop(Host) ->
ejabberd_hooks:delete(push_notifications, Host, ?MODULE, push_notifications, 10),
gen_hook:delete_handlers(hooks(Host)),
mongoose_wpool:stop(generic, Host, mongoosepush_service),
ok.

Expand All @@ -79,12 +79,18 @@ config_spec() ->
%% Hooks
%%--------------------------------------------------------------------

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

%% Hook 'push_notifications'
-spec push_notifications(AccIn :: ok | mongoose_acc:t(), Host :: jid:server(),
Notifications :: [#{binary() => binary()}],
Options :: #{binary() => binary()}) ->
ok | {error, Reason :: term()}.
push_notifications(AccIn, Host, Notifications, Options = #{<<"device_id">> := DeviceId}) ->
-spec push_notifications(Acc, Params, Extra) -> {ok, ok | {error, Reason :: term()}} when
Acc :: ok | mongoose_acc:t(),
Params :: #{notification_forms := [#{atom() => binary()}], options := #{binary() => binary()}},
Extra :: gen_hook:extra().
push_notifications(AccIn,
#{notification_forms := Notifications, options := Options = #{<<"device_id">> := DeviceId}},
#{host_type := Host}) ->
?LOG_DEBUG(#{what => push_notifications, notifications => Notifications,
opts => Options, acc => AccIn}),

Expand All @@ -97,7 +103,7 @@ push_notifications(AccIn, Host, Notifications, Options = #{<<"device_id">> := De
Payload = jiffy:encode(JSON),
call(Host, ?MODULE, http_notification, [Host, post, Path, ReqHeaders, Payload])
end,
send_push_notifications(Notifications, Fun, ok).
{ok, send_push_notifications(Notifications, Fun, ok)}.

send_push_notifications([], _, Result) ->
Result;
Expand Down
3 changes: 1 addition & 2 deletions src/mongoose_hooks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,7 @@ presence_probe_hook(HostType, Acc, From, To, Pid) ->
Options :: #{atom() => binary()},
Result :: ok | {error, any()}.
push_notifications(HostType, Acc, NotificationForms, Options) ->
Params = #{host_type => HostType, options => Options,
notification_forms => NotificationForms},
Params = #{options => Options, notification_forms => NotificationForms},
Args = [HostType, NotificationForms, Options],
ParamsWithLegacyArgs = ejabberd_hooks:add_args(Params, Args),
run_hook_for_host_type(push_notifications, HostType, Acc, ParamsWithLegacyArgs).
Expand Down

0 comments on commit 166834d

Please sign in to comment.