./ct_report/coverage/mod_event_pusher_push_plugin.COVER.html

1 %%%-------------------------------------------------------------------
2 %%% @author Rafal Slota
3 %%% @copyright (C) 2017 Erlang Solutions Ltd.
4 %%% This software is released under the Apache License, Version 2.0
5 %%% cited in 'LICENSE.txt'.
6 %%% @end
7 %%%-------------------------------------------------------------------
8 %%% @doc
9 %%% Plugin behaviour module for mod_event_pusher_push.
10 %%% This module defines API for some dynamic customizations.
11 %%% @end
12 %%%-------------------------------------------------------------------
13 -module(mod_event_pusher_push_plugin).
14 -author('rafal.slota@erlang-solutions.com').
15
16 %% API
17 -export([init/2,
18 should_publish/3,
19 prepare_notification/2,
20 publish_notification/4,
21 default_plugin_module/0]).
22
23 -callback should_publish(Acc :: mongoose_acc:t(),
24 Event :: mod_event_pusher:event(),
25 Services :: [mod_event_pusher_push:publish_service()]) ->
26 [mod_event_pusher_push:publish_service()].
27
28 -callback prepare_notification(Acc :: mongoose_acc:t(),
29 Event :: mod_event_pusher:event()) ->
30 push_payload() | skip.
31
32 -callback publish_notification(Acc :: mongoose_acc:t(),
33 Event :: mod_event_pusher:event(),
34 Payload :: push_payload(),
35 Services :: [mod_event_pusher_push:publish_service()]) ->
36 mongoose_acc:t().
37
38 -optional_callbacks([should_publish/3, prepare_notification/2, publish_notification/4]).
39
40 -type push_payload() :: [{binary(), binary()}].
41 -export_type([push_payload/0]).
42 %%--------------------------------------------------------------------
43 %% API
44 %%--------------------------------------------------------------------
45 -spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
46 init(_HostType, #{plugin_module := PluginModule}) ->
47
:-(
ensure_loaded(PluginModule),
48
:-(
ok.
49
50 %% @doc used for filtering push notifications. A push notification is triggered for a given
51 %% message only if this callback returns `true'.
52 -spec should_publish(mongoose_acc:t(), mod_event_pusher:event(),
53 [mod_event_pusher_push:publish_service()]) ->
54 [mod_event_pusher_push:publish_service()].
55 should_publish(Acc, Event, Services) ->
56
:-(
HostType = mongoose_acc:host_type(Acc),
57
:-(
PluginModule = plugin_module(HostType, should_publish, 3),
58
:-(
PluginModule:should_publish(Acc, Event, Services).
59
60 %% @doc a separate interface for rejecting the event publishing (e.g. when
61 %% message doesn't have a body) or creating push notification payload.
62 -spec prepare_notification(Acc :: mongoose_acc:t(),
63 Event :: mod_event_pusher:event()) -> push_payload() | skip.
64 prepare_notification(Acc, Event) ->
65
:-(
HostType = mongoose_acc:host_type(Acc),
66
:-(
PluginModule = plugin_module(HostType, prepare_notification, 2),
67
:-(
PluginModule:prepare_notification(Acc, Event).
68
69 %% @doc does the actual push. By default it pushes to the registered pubsub
70 %% nodes (or executes the internal hook in case of a publish to a virtual domain).
71 -spec publish_notification(Acc :: mongoose_acc:t(),
72 Event :: mod_event_pusher:event(), Payload :: push_payload(),
73 Services :: [mod_event_pusher_push:publish_service()]) -> mongoose_acc:t().
74 publish_notification(Acc, _Event, _Payload, []) ->
75
:-(
Acc;
76 publish_notification(Acc, Event, Payload, Services) ->
77
:-(
HostType = mongoose_acc:host_type(Acc),
78
:-(
PluginModule = plugin_module(HostType, publish_notification, 4),
79
:-(
PluginModule:publish_notification(Acc, Event, Payload, Services).
80
81 %%--------------------------------------------------------------------
82 %% Helper functions
83 %%--------------------------------------------------------------------
84 -spec plugin_module(mongooseim:host_type(), atom(), arity()) -> module().
85 plugin_module(HostType, Func, Arity) ->
86
:-(
Mod = plugin_module(HostType),
87
:-(
case erlang:function_exported(Mod, Func, Arity) of
88
:-(
true -> Mod;
89
:-(
false -> default_plugin_module()
90 end.
91
92 -spec plugin_module(mongooseim:host_type()) -> module().
93 plugin_module(HostType) ->
94
:-(
gen_mod:get_module_opt(HostType, mod_event_pusher_push, plugin_module).
95
96 default_plugin_module() ->
97 186 mod_event_pusher_push_plugin_defaults.
98
99 -spec ensure_loaded(module()) -> {module, module()}.
100 ensure_loaded(PluginModule) ->
101
:-(
{module, PluginModule} = code:ensure_loaded(PluginModule).
Line Hits Source