./ct_report/coverage/mod_event_pusher_push_mnesia.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 %%% 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 18 mongoose_mnesia:create_table(push_subscription,
47 [{disc_copies, [node()]}, {type, bag},
48 {attributes, record_info(fields, push_subscription)}]),
49 18 ok.
50
51 -spec enable(mongooseim:host_type(), UserJID :: jid:jid(), PubsubJID :: jid:jid(),
52 Node :: mod_event_pusher_push:pubsub_node(), Form :: mod_event_pusher_push:form()) ->
53 ok | {error, Reason :: term()}.
54 enable(HostType, User, PubSub, Node, Forms) ->
55 86 disable(HostType, User, PubSub, Node),
56 86 write(make_record(User, PubSub, Node, Forms)).
57
58
59 -spec disable(mongooseim:host_type(), UserJID :: jid:jid()) -> ok | {error, Reason :: term()}.
60 disable(_HostType, User) ->
61 2 delete(key(User)).
62
63 -spec disable(mongooseim:host_type(), UserJID :: jid:jid(), PubsubJID :: jid:jid(),
64 Node :: mod_event_pusher_push:pubsub_node() | undefined) ->
65 ok | {error, Reason :: term()}.
66 disable(_HostType, User, PubsubJID, Node) ->
67 97 Result =
68 exec(
69 fun() ->
70 97 Filtered =
71 11 [Record ||
72 #push_subscription{pubsub_jid = RecPubsubJID,
73 97 pubsub_node = RecNode} = Record <- read(key(User)),
74 18 RecPubsubJID == PubsubJID,
75 18 Node == undefined orelse RecNode == Node],
76
77 97 [mnesia:delete_object(Record) || Record <- Filtered]
78 end),
79
80 97 case Result of
81 {error, _} = E ->
82
:-(
E;
83 _ ->
84 97 ok
85 end.
86
87
88 -spec get_publish_services(mongooseim:host_type(), User :: jid:jid()) ->
89 {ok, [{PubSub :: jid:jid(),
90 Node :: mod_event_pusher_push:pubsub_node(),
91 Form :: mod_event_pusher_push:form()}]} |
92 {error, Reason :: term()}.
93 get_publish_services(_HostType, User) ->
94 182 case safe_read(key(User)) of
95 {ok, Records} ->
96 182 {ok, [{PubsubJID, Node, Forms} ||
97 #push_subscription{pubsub_jid = PubsubJID,
98 pubsub_node = Node,
99 182 form = Forms} <- Records]};
100 {error, _} = E ->
101
:-(
E
102 end.
103
104 %%--------------------------------------------------------------------
105 %% Helper functions
106 %%--------------------------------------------------------------------
107
108 -spec read(key()) -> [sub_record()] | no_return().
109 read(Key) ->
110 279 F = fun() -> mnesia:read({push_subscription, Key}) end,
111 279 mnesia:async_dirty(F).
112
113 -spec safe_read(key()) -> {ok, [sub_record()]} | {error, Reason :: term()}.
114 safe_read(Key) ->
115 182 try read(Key) of
116 Records ->
117 182 {ok, Records}
118 catch
119 _:Reason ->
120
:-(
{error, Reason}
121 end.
122
123 -spec write(sub_record()) -> ok | {error, Reason :: term()}.
124 write(Record) ->
125 86 F = fun() -> mnesia:write(Record) end,
126 86 exec(F).
127
128 -spec delete(key()) -> ok | {error, Reason :: term()}.
129 delete(Key) ->
130 2 F = fun() -> mnesia:delete({push_subscription, Key}) end,
131 2 exec(F).
132
133 -spec exec(fun(() -> any())) -> Result :: any().
134 exec(F) ->
135 185 mnesia:sync_dirty(F).
136
137 -spec make_record(UserJID :: jid:jid(), PubsubJID :: jid:jid(),
138 Node :: mod_event_pusher_push:pubsub_node(),
139 Form :: mod_event_pusher_push:form()) -> sub_record().
140 make_record(UserJID, PubsubJID, Node, Form) ->
141 86 #push_subscription{
142 user_jid = key(UserJID),
143 pubsub_jid = PubsubJID,
144 pubsub_node = Node,
145 form = Form
146 }.
147
148 -spec key(jid:jid()) -> key().
149 key(JID) ->
150 367 jid:to_lus(JID).
Line Hits Source