1 |
|
%%%------------------------------------------------------------------- |
2 |
|
%%% @copyright (C) 2019 Erlang Solutions Ltd. |
3 |
|
%%% @end |
4 |
|
%%%------------------------------------------------------------------- |
5 |
|
%%% @doc |
6 |
|
%%% RDBMS backend for mod_event_pusher_push. |
7 |
|
%%% @end |
8 |
|
%%%------------------------------------------------------------------- |
9 |
|
-module(mod_event_pusher_push_rdbms). |
10 |
|
-behavior(mod_event_pusher_push_backend). |
11 |
|
|
12 |
|
%%-------------------------------------------------------------------- |
13 |
|
%% Exports |
14 |
|
%%-------------------------------------------------------------------- |
15 |
|
|
16 |
|
-export([init/2]). |
17 |
|
-export([enable/5, |
18 |
|
disable/2, |
19 |
|
disable/4, |
20 |
|
get_publish_services/2]). |
21 |
|
|
22 |
|
%%-------------------------------------------------------------------- |
23 |
|
%% Backend callbacks |
24 |
|
%%-------------------------------------------------------------------- |
25 |
|
|
26 |
|
-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
27 |
|
init(_HostType, _Opts) -> |
28 |
24 |
prepare_queries(). |
29 |
|
|
30 |
|
-spec enable(HostType, User, PubSub, Node, Form) -> Result when |
31 |
|
HostType :: mongooseim:host_type(), |
32 |
|
User :: jid:jid(), |
33 |
|
PubSub :: jid:jid(), |
34 |
|
Node :: mod_event_pusher_push:pubsub_node(), |
35 |
|
Form :: mod_event_pusher_push:form(), |
36 |
|
Result :: ok | {error, term()}. |
37 |
|
enable(HostType, User, PubSub, Node, Form) -> |
38 |
106 |
ExtUser = jid:to_bare_binary(User), |
39 |
106 |
ExtPubSub = jid:to_binary(PubSub), |
40 |
106 |
ExtForms = encode_form(Form), |
41 |
106 |
execute_delete(HostType, ExtUser, Node, ExtPubSub), |
42 |
106 |
CreatedAt = os:system_time(microsecond), |
43 |
106 |
case execute_insert(HostType, ExtUser, Node, ExtPubSub, ExtForms, CreatedAt) of |
44 |
106 |
{updated, 1} -> ok; |
45 |
:-( |
Other -> Other |
46 |
|
end. |
47 |
|
|
48 |
|
-spec disable(mongooseim:host_type(), User :: jid:jid()) -> ok. |
49 |
|
disable(HostType, User) -> |
50 |
2 |
ExtUser = jid:to_bare_binary(User), |
51 |
2 |
execute_delete(HostType, ExtUser), |
52 |
2 |
ok. |
53 |
|
|
54 |
|
-spec disable(mongooseim:host_type(), User :: jid:jid(), PubSub :: jid:jid(), |
55 |
|
Node :: mod_event_pusher_push:pubsub_node() | undefined) -> ok. |
56 |
|
disable(HostType, User, PubSub, undefined) -> |
57 |
2 |
ExtUser = jid:to_bare_binary(User), |
58 |
2 |
ExtPubSub = jid:to_binary(PubSub), |
59 |
2 |
execute_delete(HostType, ExtUser, ExtPubSub), |
60 |
2 |
ok; |
61 |
|
disable(HostType, User, PubSub, Node) -> |
62 |
9 |
ExtUser = jid:to_bare_binary(User), |
63 |
9 |
ExtPubSub = jid:to_binary(PubSub), |
64 |
9 |
execute_delete(HostType, ExtUser, Node, ExtPubSub), |
65 |
9 |
ok. |
66 |
|
|
67 |
|
-spec get_publish_services(mongooseim:host_type(), User :: jid:jid()) -> |
68 |
|
{ok, [{PubSub :: jid:jid(), |
69 |
|
Node :: mod_event_pusher_push:pubsub_node(), |
70 |
|
Form :: mod_event_pusher_push:form()}]}. |
71 |
|
get_publish_services(HostType, User) -> |
72 |
305 |
ExtUser = jid:to_bare_binary(User), |
73 |
305 |
{selected, Rows} = execute_select(HostType, ExtUser), |
74 |
305 |
{ok, decode_rows(Rows)}. |
75 |
|
|
76 |
|
decode_rows(Rows) -> |
77 |
305 |
[decode_row(Row) || Row <- Rows]. |
78 |
|
|
79 |
|
decode_row({NodeID, PubSubBin, FormJSON}) -> |
80 |
198 |
{jid:from_binary(PubSubBin), |
81 |
|
NodeID, |
82 |
|
decode_form(FormJSON)}. |
83 |
|
|
84 |
|
encode_form(Form) -> |
85 |
106 |
jiffy:encode(Form). |
86 |
|
|
87 |
|
decode_form(FormJSON) -> |
88 |
198 |
jiffy:decode(FormJSON, [return_maps]). |
89 |
|
|
90 |
|
%% Prepared queries |
91 |
|
|
92 |
|
-spec prepare_queries() -> ok. |
93 |
|
prepare_queries() -> |
94 |
24 |
mongoose_rdbms:prepare(event_pusher_push_insert, event_pusher_push_subscription, |
95 |
|
[owner_jid, node, pubsub_jid, form, created_at], |
96 |
|
<<"INSERT INTO event_pusher_push_subscription VALUES (?, ?, ?, ?, ?)">>), |
97 |
24 |
mongoose_rdbms:prepare(event_pusher_push_select, event_pusher_push_subscription, |
98 |
|
[owner_jid], |
99 |
|
<<"SELECT node, pubsub_jid, form FROM event_pusher_push_subscription " |
100 |
|
"WHERE owner_jid = ?">>), |
101 |
24 |
mongoose_rdbms:prepare(event_pusher_push_delete, event_pusher_push_subscription, |
102 |
|
[owner_jid], |
103 |
|
<<"DELETE FROM event_pusher_push_subscription " |
104 |
|
"WHERE owner_jid = ?">>), |
105 |
24 |
mongoose_rdbms:prepare(event_pusher_push_delete_pubsub_jid, event_pusher_push_subscription, |
106 |
|
[owner_jid, pubsub_jid], |
107 |
|
<<"DELETE FROM event_pusher_push_subscription " |
108 |
|
"WHERE owner_jid = ? AND pubsub_jid = ?">>), |
109 |
24 |
mongoose_rdbms:prepare(event_pusher_push_delete_node, event_pusher_push_subscription, |
110 |
|
[owner_jid, node, pubsub_jid], |
111 |
|
<<"DELETE FROM event_pusher_push_subscription " |
112 |
|
"WHERE owner_jid = ? AND node = ? AND pubsub_jid = ?">>), |
113 |
24 |
ok. |
114 |
|
|
115 |
|
-spec execute_insert(mongooseim:host_type(), jid:literal_jid(), mod_event_pusher_push:pubsub_node(), |
116 |
|
jid:literal_jid(), iodata(), non_neg_integer()) -> |
117 |
|
mongoose_rdbms:query_result(). |
118 |
|
execute_insert(HostType, OwnerJid, Node, PubSubJid, Form, CreatedAt) -> |
119 |
106 |
mongoose_rdbms:execute_successfully(HostType, event_pusher_push_insert, |
120 |
|
[OwnerJid, Node, PubSubJid, Form, CreatedAt]). |
121 |
|
|
122 |
|
-spec execute_select(mongooseim:host_type(), jid:literal_jid()) -> mongoose_rdbms:query_result(). |
123 |
|
execute_select(HostType, OwnerJid) -> |
124 |
305 |
mongoose_rdbms:execute_successfully(HostType, event_pusher_push_select, [OwnerJid]). |
125 |
|
|
126 |
|
-spec execute_delete(mongooseim:host_type(), jid:literal_jid()) -> mongoose_rdbms:query_result(). |
127 |
|
execute_delete(HostType, OwnerJid) -> |
128 |
2 |
mongoose_rdbms:execute_successfully(HostType, event_pusher_push_delete, [OwnerJid]). |
129 |
|
|
130 |
|
-spec execute_delete(mongooseim:host_type(), jid:literal_jid(), jid:literal_jid()) -> |
131 |
|
mongoose_rdbms:query_result(). |
132 |
|
execute_delete(HostType, OwnerJid, PubSubJid) -> |
133 |
2 |
mongoose_rdbms:execute_successfully(HostType, event_pusher_push_delete_pubsub_jid, |
134 |
|
[OwnerJid, PubSubJid]). |
135 |
|
|
136 |
|
-spec execute_delete(mongooseim:host_type(), jid:literal_jid(), mod_event_pusher_push:pubsub_node(), |
137 |
|
jid:literal_jid()) -> |
138 |
|
mongoose_rdbms:query_result(). |
139 |
|
execute_delete(HostType, OwnerJid, Node, PubSubJid) -> |
140 |
115 |
mongoose_rdbms:execute_successfully(HostType, event_pusher_push_delete_node, |
141 |
|
[OwnerJid, Node, PubSubJid]). |