From 166834dddbff6ecdfe4178732af4313fe5f505d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20D=C5=82ugosz?= Date: Mon, 21 Nov 2022 10:26:00 +0100 Subject: [PATCH] Refactored hook handlers in mod_push_service_mongoosepush --- src/mod_push_service_mongoosepush.erl | 26 ++++++++++++++++---------- src/mongoose_hooks.erl | 3 +-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/mod_push_service_mongoosepush.erl b/src/mod_push_service_mongoosepush.erl index 5f423f6e832..ae0f057c91f 100644 --- a/src/mod_push_service_mongoosepush.erl +++ b/src/mod_push_service_mongoosepush.erl @@ -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 @@ -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(). @@ -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. @@ -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}), @@ -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; diff --git a/src/mongoose_hooks.erl b/src/mongoose_hooks.erl index 7582c88696f..810891372da 100644 --- a/src/mongoose_hooks.erl +++ b/src/mongoose_hooks.erl @@ -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).