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 |
|
%%% Mnesia backend for mod_event_pusher_push. |
10 |
|
%%% @end |
11 |
|
%%%------------------------------------------------------------------- |
12 |
|
-module(mod_event_pusher_push_mnesia). |
13 |
|
-author("Rafal Slota"). |
14 |
|
-behavior(mod_event_pusher_push_backend). |
15 |
|
|
16 |
|
%%-------------------------------------------------------------------- |
17 |
|
%% Exports |
18 |
|
%%-------------------------------------------------------------------- |
19 |
|
|
20 |
|
-export([init/2]). |
21 |
|
-export([enable/5, |
22 |
|
disable/2, |
23 |
|
disable/4, |
24 |
|
get_publish_services/2]). |
25 |
|
|
26 |
|
%%-------------------------------------------------------------------- |
27 |
|
%% Definitions |
28 |
|
%%-------------------------------------------------------------------- |
29 |
|
|
30 |
|
-record(push_subscription, { |
31 |
|
user_jid :: key() | undefined, |
32 |
|
pubsub_jid :: jid:jid(), |
33 |
|
pubsub_node :: mod_event_pusher_push:pubsub_node(), |
34 |
|
form :: mod_event_pusher_push:form() |
35 |
|
}). |
36 |
|
|
37 |
|
-type key() :: jid:simple_bare_jid(). |
38 |
|
-type sub_record() :: #push_subscription{}. |
39 |
|
|
40 |
|
%%-------------------------------------------------------------------- |
41 |
|
%% Backend callbacks |
42 |
|
%%-------------------------------------------------------------------- |
43 |
|
|
44 |
|
-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
45 |
|
init(_HostType, _Opts) -> |
46 |
17 |
mnesia:create_table(push_subscription, |
47 |
|
[{disc_copies, [node()]}, |
48 |
|
{type, bag}, |
49 |
|
{attributes, record_info(fields, push_subscription)}]), |
50 |
17 |
mnesia:add_table_copy(push_subscription, node(), disc_copies), |
51 |
17 |
ok. |
52 |
|
|
53 |
|
-spec enable(mongooseim:host_type(), UserJID :: jid:jid(), PubsubJID :: jid:jid(), |
54 |
|
Node :: mod_event_pusher_push:pubsub_node(), Form :: mod_event_pusher_push:form()) -> |
55 |
|
ok | {error, Reason :: term()}. |
56 |
|
enable(HostType, User, PubSub, Node, Forms) -> |
57 |
86 |
disable(HostType, User, PubSub, Node), |
58 |
86 |
write(make_record(User, PubSub, Node, Forms)). |
59 |
|
|
60 |
|
|
61 |
|
-spec disable(mongooseim:host_type(), UserJID :: jid:jid()) -> ok | {error, Reason :: term()}. |
62 |
|
disable(_HostType, User) -> |
63 |
2 |
delete(key(User)). |
64 |
|
|
65 |
|
-spec disable(mongooseim:host_type(), UserJID :: jid:jid(), PubsubJID :: jid:jid(), |
66 |
|
Node :: mod_event_pusher_push:pubsub_node() | undefined) -> |
67 |
|
ok | {error, Reason :: term()}. |
68 |
|
disable(_HostType, User, PubsubJID, Node) -> |
69 |
97 |
Result = |
70 |
|
exec( |
71 |
|
fun() -> |
72 |
97 |
Filtered = |
73 |
11 |
[Record || |
74 |
|
#push_subscription{pubsub_jid = RecPubsubJID, |
75 |
97 |
pubsub_node = RecNode} = Record <- read(key(User)), |
76 |
18 |
RecPubsubJID == PubsubJID, |
77 |
18 |
Node == undefined orelse RecNode == Node], |
78 |
|
|
79 |
97 |
[mnesia:delete_object(Record) || Record <- Filtered] |
80 |
|
end), |
81 |
|
|
82 |
97 |
case Result of |
83 |
|
{error, _} = E -> |
84 |
:-( |
E; |
85 |
|
_ -> |
86 |
97 |
ok |
87 |
|
end. |
88 |
|
|
89 |
|
|
90 |
|
-spec get_publish_services(mongooseim:host_type(), User :: jid:jid()) -> |
91 |
|
{ok, [{PubSub :: jid:jid(), |
92 |
|
Node :: mod_event_pusher_push:pubsub_node(), |
93 |
|
Form :: mod_event_pusher_push:form()}]} | |
94 |
|
{error, Reason :: term()}. |
95 |
|
get_publish_services(_HostType, User) -> |
96 |
183 |
case safe_read(key(User)) of |
97 |
|
{ok, Records} -> |
98 |
183 |
{ok, [{PubsubJID, Node, Forms} || |
99 |
|
#push_subscription{pubsub_jid = PubsubJID, |
100 |
|
pubsub_node = Node, |
101 |
183 |
form = Forms} <- Records]}; |
102 |
|
{error, _} = E -> |
103 |
:-( |
E |
104 |
|
end. |
105 |
|
|
106 |
|
%%-------------------------------------------------------------------- |
107 |
|
%% Helper functions |
108 |
|
%%-------------------------------------------------------------------- |
109 |
|
|
110 |
|
-spec read(key()) -> [sub_record()] | no_return(). |
111 |
|
read(Key) -> |
112 |
280 |
F = fun() -> mnesia:read({push_subscription, Key}) end, |
113 |
280 |
mnesia:async_dirty(F). |
114 |
|
|
115 |
|
-spec safe_read(key()) -> {ok, [sub_record()]} | {error, Reason :: term()}. |
116 |
|
safe_read(Key) -> |
117 |
183 |
try read(Key) of |
118 |
|
Records -> |
119 |
183 |
{ok, Records} |
120 |
|
catch |
121 |
|
_:Reason -> |
122 |
:-( |
{error, Reason} |
123 |
|
end. |
124 |
|
|
125 |
|
-spec write(sub_record()) -> ok | {error, Reason :: term()}. |
126 |
|
write(Record) -> |
127 |
86 |
F = fun() -> mnesia:write(Record) end, |
128 |
86 |
exec(F). |
129 |
|
|
130 |
|
-spec delete(key()) -> ok | {error, Reason :: term()}. |
131 |
|
delete(Key) -> |
132 |
2 |
F = fun() -> mnesia:delete({push_subscription, Key}) end, |
133 |
2 |
exec(F). |
134 |
|
|
135 |
|
-spec exec(fun(() -> any())) -> Result :: any(). |
136 |
|
exec(F) -> |
137 |
185 |
mnesia:sync_dirty(F). |
138 |
|
|
139 |
|
-spec make_record(UserJID :: jid:jid(), PubsubJID :: jid:jid(), |
140 |
|
Node :: mod_event_pusher_push:pubsub_node(), |
141 |
|
Form :: mod_event_pusher_push:form()) -> sub_record(). |
142 |
|
make_record(UserJID, PubsubJID, Node, Form) -> |
143 |
86 |
#push_subscription{ |
144 |
|
user_jid = key(UserJID), |
145 |
|
pubsub_jid = PubsubJID, |
146 |
|
pubsub_node = Node, |
147 |
|
form = Form |
148 |
|
}. |
149 |
|
|
150 |
|
-spec key(jid:jid()) -> key(). |
151 |
|
key(JID) -> |
152 |
368 |
jid:to_lus(JID). |