From 2782430887fdf5975e93d9bf94ee16c217859ce8 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Fri, 8 Sep 2023 19:49:17 +0200 Subject: [PATCH] mod_push: Add 'notify_on' option If the new 'notify_on' option is set to 'messages', notifications are only triggered by actual chat messages with a body (or encrypted payload). The default behavior remains to generate a notification on any kind of stanzas. Thanks to EISST International Ltd for sponsoring this work. --- src/mod_push.erl | 41 +++++++++++++++++++++++++++++------------ src/mod_push_opt.erl | 7 +++++++ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/mod_push.erl b/src/mod_push.erl index b515adf53..7979125c5 100644 --- a/src/mod_push.erl +++ b/src/mod_push.erl @@ -135,6 +135,8 @@ depends(_Host, _Opts) -> []. -spec mod_opt_type(atom()) -> econf:validator(). +mod_opt_type(notify_on) -> + econf:enum([messages, all]); mod_opt_type(include_sender) -> econf:bool(); mod_opt_type(include_body) -> @@ -154,7 +156,8 @@ mod_opt_type(cache_life_time) -> -spec mod_options(binary()) -> [{atom(), any()}]. mod_options(Host) -> - [{include_sender, false}, + [{notify_on, all}, + {include_sender, false}, {include_body, <<"New message">>}, {db_type, ejabberd_config:default_db(Host, ?MODULE)}, {use_cache, ejabberd_option:use_cache(Host)}, @@ -175,7 +178,16 @@ mod_doc() -> "notification delivery to the user's mobile device using " "platform-dependant backend services such as FCM or APNS."), opts => - [{include_sender, + [{notify_on, + #{value => "messages | all", + desc => + ?T("If this option is set to 'messages', notifications are " + "generated only for actual chat messages with a body text " + "(or some encrypted payload). If it's set to 'all', any " + "kind of XMPP stanza will trigger a notification. If " + "unsure, it's strongly recommended to stick to 'all', " + "which is the default value.")}}, + {include_sender, #{value => "true | false", desc => ?T("If this option is set to 'true', the sender's JID " @@ -510,16 +522,21 @@ notify(LUser, LServer, Clients, Pkt, Dir) -> notify(LServer, PushLJID, Node, XData, Pkt0, Dir, HandleResponse) -> Pkt = unwrap_message(Pkt0), From = jid:make(LServer), - Summary = make_summary(LServer, Pkt, Dir), - Item = #ps_item{sub_els = [#push_notification{xdata = Summary}]}, - PubSub = #pubsub{publish = #ps_publish{node = Node, items = [Item]}, - publish_options = XData}, - IQ = #iq{type = set, - from = From, - to = jid:make(PushLJID), - id = p1_rand:get_string(), - sub_els = [PubSub]}, - ejabberd_router:route_iq(IQ, HandleResponse). + case {make_summary(LServer, Pkt, Dir), mod_push_opt:notify_on(LServer)} of + {undefined, messages} -> + ?DEBUG("Suppressing notification for stanza without payload", []), + ok; + {Summary, _NotifyOn} -> + Item = #ps_item{sub_els = [#push_notification{xdata = Summary}]}, + PubSub = #pubsub{publish = #ps_publish{node = Node, items = [Item]}, + publish_options = XData}, + IQ = #iq{type = set, + from = From, + to = jid:make(PushLJID), + id = p1_rand:get_string(), + sub_els = [PubSub]}, + ejabberd_router:route_iq(IQ, HandleResponse) + end. %%-------------------------------------------------------------------- %% Miscellaneous. diff --git a/src/mod_push_opt.erl b/src/mod_push_opt.erl index 6ab94b9c7..db6c55389 100644 --- a/src/mod_push_opt.erl +++ b/src/mod_push_opt.erl @@ -9,6 +9,7 @@ -export([db_type/1]). -export([include_body/1]). -export([include_sender/1]). +-export([notify_on/1]). -export([use_cache/1]). -spec cache_life_time(gen_mod:opts() | global | binary()) -> 'infinity' | pos_integer(). @@ -47,6 +48,12 @@ include_sender(Opts) when is_map(Opts) -> include_sender(Host) -> gen_mod:get_module_opt(Host, mod_push, include_sender). +-spec notify_on(gen_mod:opts() | global | binary()) -> 'all' | 'messages'. +notify_on(Opts) when is_map(Opts) -> + gen_mod:get_opt(notify_on, Opts); +notify_on(Host) -> + gen_mod:get_module_opt(Host, mod_push, notify_on). + -spec use_cache(gen_mod:opts() | global | binary()) -> boolean(). use_cache(Opts) when is_map(Opts) -> gen_mod:get_opt(use_cache, Opts);