Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored hook handlers in mod_push_service_mongoosepush #3866

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the strangest accumulator, this hook probably needs some rework, but that's out of the scope of this PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, totally agree

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