./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 -include("jlib.hrl").
17 -include("mongoose.hrl").
18
19 -define(DEFAULT_PLUGIN_MODULE, mod_event_pusher_push_plugin_defaults).
20
21 %% API
22 -export([init/1,
23 should_publish/4,
24 prepare_notification/3,
25 publish_notification/5]).
26
27 -callback should_publish(Acc :: mongooseim_acc:t(),
28 Event :: mod_event_pusher:event(),
29 Services :: [mod_event_pusher_push:publish_service()]) ->
30 [mod_event_pusher_push:publish_service()].
31
32 -callback prepare_notification(Acc :: mongooseim_acc:t(),
33 Event :: mod_event_pusher:event()) ->
34 push_payload() | skip.
35
36 -callback publish_notification(Acc :: mongooseim_acc:t(),
37 Event :: mod_event_pusher:event(),
38 Payload :: push_payload(),
39 Services :: [mod_event_pusher_push:publish_service()]) ->
40 mongooseim_acc:t().
41
42 -optional_callbacks([should_publish/3, prepare_notification/2, publish_notification/4]).
43
44 -type push_payload() :: mod_event_pusher_push:form().
45 -export_type([push_payload/0]).
46 %%--------------------------------------------------------------------
47 %% API
48 %%--------------------------------------------------------------------
49 -spec init(Host :: jid:server()) -> ok.
50 init(Host) ->
51 16 PluginModule = plugin_module(Host),
52 16 ensure_loaded(PluginModule),
53 16 ok.
54
55 %% @doc used for filtering push notifications. A push notification is triggered for a given
56 %% message only if this callback returns `true'.
57 -spec should_publish(Host :: jid:server(), Acc :: mongooseim_acc:t(),
58 Event :: mod_event_pusher:event(),
59 [mod_event_pusher_push:publish_service()]) -> [mod_event_pusher_push:publish_service()].
60 should_publish(Host, From, To, Packet) ->
61 146 PluginModule = plugin_module(Host, should_publish, 3),
62 146 PluginModule:should_publish(From, To, Packet).
63
64 %% @doc a separate interface for rejecting the event publishing (e.g. when
65 %% message doesn't have a body) or creating push notification payload.
66 -spec prepare_notification(Host :: jid:server(), Acc :: mongooseim_acc:t(),
67 Event :: mod_event_pusher:event()) -> push_payload() | skip.
68 prepare_notification(Host, Acc, Event) ->
69 190 PluginModule = plugin_module(Host, prepare_notification, 2),
70 190 PluginModule:prepare_notification(Acc, Event).
71
72 %% @doc does the actual push. By default it pushes to the registered pubsub
73 %% nodes (or executes the internal hook in case of a publish to a virtual domain).
74 -spec publish_notification(Host :: jid:server(), Acc :: mongooseim_acc:t(),
75 Event :: mod_event_pusher:event(), Payload :: push_payload(),
76 Services :: [mod_event_pusher_push:publish_service()]) -> mongooseim_acc:t().
77 publish_notification(_Host, Acc, _Event, _Payload, []) ->
78 70 Acc;
79 publish_notification(Host, Acc, Event, Payload, Services) ->
80 76 PluginModule = plugin_module(Host, publish_notification, 4),
81 76 PluginModule:publish_notification(Acc, Event, Payload, Services).
82
83 %%--------------------------------------------------------------------
84 %% Helper functions
85 %%--------------------------------------------------------------------
86 -spec plugin_module(Host :: jid:server(), atom(), arity()) -> Module :: atom().
87 plugin_module(Host, Func, Arity) ->
88 412 Mod = plugin_module(Host),
89 412 case erlang:function_exported(Mod, Func, Arity) of
90 368 true -> Mod;
91 44 false -> ?DEFAULT_PLUGIN_MODULE
92 end.
93
94 -spec plugin_module(Host :: jid:server()) -> Module :: atom().
95 plugin_module(Host) ->
96 428 gen_mod:get_module_opt(Host, mod_event_pusher_push, plugin_module,
97 ?DEFAULT_PLUGIN_MODULE).
98
99 -spec ensure_loaded(module()) -> {module, module()}.
100 ensure_loaded(PluginModule) ->
101 16 {module, PluginModule} = code:ensure_loaded(PluginModule).
Line Hits Source