1 |
|
%%%---------------------------------------------------------------------- |
2 |
|
%%% File : mod_roster_mnesia.erl |
3 |
|
%%% Author : MichaĆ Piotrowski <michal.piotrowski@erlang-solutions.com> |
4 |
|
%%% Purpose : mod_roster mnesia backend |
5 |
|
%%% |
6 |
|
%%% |
7 |
|
%%% ejabberd, Copyright (C) 2002-2014 ProcessOne |
8 |
|
%%% MongooseIM, Copyright (C) 2015 Erlang Solutions Ltd. |
9 |
|
%%% |
10 |
|
%%%---------------------------------------------------------------------- |
11 |
|
-module(mod_roster_mnesia). |
12 |
|
|
13 |
|
-include("mod_roster.hrl"). |
14 |
|
-include("jlib.hrl"). |
15 |
|
|
16 |
|
-behaviour(mod_roster_backend). |
17 |
|
|
18 |
|
%% API |
19 |
|
-export([init/2, |
20 |
|
transaction/2, |
21 |
|
read_roster_version/3, |
22 |
|
write_roster_version/5, |
23 |
|
get_roster/3, |
24 |
|
get_roster_entry/6, |
25 |
|
get_subscription_lists/3, |
26 |
|
roster_subscribe_t/2, |
27 |
|
update_roster_t/2, |
28 |
|
del_roster_t/4, |
29 |
|
remove_user_t/3]). |
30 |
|
|
31 |
|
-spec init(mongooseim:host_type(), gen_mod:module_opts()) -> ok. |
32 |
|
init(_HostType, _Opts) -> |
33 |
436 |
mongoose_mnesia:create_table(roster, |
34 |
|
[{disc_copies, [node()]}, |
35 |
|
{attributes, record_info(fields, roster)}]), |
36 |
436 |
mongoose_mnesia:create_table(roster_version, |
37 |
|
[{disc_copies, [node()]}, |
38 |
|
{attributes, record_info(fields, roster_version)}]), |
39 |
436 |
mnesia:add_table_index(roster, us), |
40 |
436 |
mnesia:add_table_index(roster_version, us), |
41 |
436 |
ok. |
42 |
|
|
43 |
|
-spec transaction(mongooseim:host_type(), fun(() -> any())) -> |
44 |
|
{aborted, any()} | {atomic, any()}. |
45 |
|
transaction(_HostType, F) -> |
46 |
3584 |
mnesia:transaction(F). |
47 |
|
|
48 |
|
-spec read_roster_version(mongooseim:host_type(), jid:luser(), jid:lserver()) -> |
49 |
|
binary() | error. |
50 |
|
read_roster_version(_HostType, LUser, LServer) -> |
51 |
4 |
US = {LUser, LServer}, |
52 |
4 |
case mnesia:dirty_read(roster_version, US) of |
53 |
3 |
[#roster_version{version = V}] -> V; |
54 |
1 |
[] -> error |
55 |
|
end. |
56 |
|
|
57 |
|
-spec write_roster_version(mongooseim:host_type(), jid:luser(), jid:lserver(), |
58 |
|
mod_roster:transaction_state(), mod_roster:version()) -> ok. |
59 |
|
write_roster_version(_HostType, LUser, LServer, TransactionState, Ver) -> |
60 |
2 |
write(#roster_version{us = {LUser, LServer}, version = Ver}, TransactionState). |
61 |
|
|
62 |
|
-spec get_roster(mongooseim:host_type(), jid:luser(), jid:lserver()) -> [mod_roster:roster()]. |
63 |
|
get_roster(_HostType, LUser, LServer) -> |
64 |
2998 |
US = {LUser, LServer}, |
65 |
2998 |
case catch mnesia:dirty_index_read(roster, US, |
66 |
|
#roster.us) |
67 |
|
of |
68 |
2998 |
Items when is_list(Items)-> Items; |
69 |
:-( |
_ -> [] |
70 |
|
end. |
71 |
|
|
72 |
|
-spec get_roster_entry(mongooseim:host_type(), jid:luser(), jid:lserver(), mod_roster:contact(), |
73 |
|
mod_roster:transaction_state(), mod_roster:entry_format()) -> |
74 |
|
mod_roster:roster() | does_not_exist. |
75 |
|
get_roster_entry(_HostType, LUser, LServer, LJID, TransactionState, _Format) -> |
76 |
916 |
LowerJID = jid:to_lower(LJID), |
77 |
916 |
case read({roster, {LUser, LServer, LowerJID}}, TransactionState) of |
78 |
|
[] -> |
79 |
277 |
does_not_exist; |
80 |
|
[I] -> |
81 |
639 |
I#roster{jid = LJID, xs = []} |
82 |
|
end. |
83 |
|
|
84 |
|
-spec get_subscription_lists(mongoose_acc:t(), jid:luser(), jid:lserver()) -> [mod_roster:roster()]. |
85 |
|
get_subscription_lists(_, LUser, LServer) -> |
86 |
2950 |
US = {LUser, LServer}, |
87 |
2950 |
case catch mnesia:dirty_index_read(roster, US, #roster.us) of |
88 |
2950 |
Items when is_list(Items) -> Items; |
89 |
:-( |
_ -> [] |
90 |
|
end. |
91 |
|
|
92 |
|
-spec roster_subscribe_t(mongooseim:host_type(), mod_roster:roster()) -> ok. |
93 |
|
roster_subscribe_t(_HostType, Item) -> |
94 |
603 |
mnesia:write(Item). |
95 |
|
|
96 |
|
-spec update_roster_t(mongooseim:host_type(), mod_roster:roster()) -> ok. |
97 |
|
update_roster_t(_HostType, Item) -> |
98 |
120 |
mnesia:write(Item). |
99 |
|
|
100 |
|
-spec del_roster_t(mongooseim:host_type(), jid:luser(), jid:lserver(), mod_roster:contact()) -> ok. |
101 |
|
del_roster_t(_HostType, LUser, LServer, LJID) -> |
102 |
27 |
mnesia:delete({roster, {LUser, LServer, LJID}}). |
103 |
|
|
104 |
|
-spec remove_user_t(mongooseim:host_type(), jid:luser(), jid:lserver()) -> ok. |
105 |
|
remove_user_t(_HostType, LUser, LServer) -> |
106 |
2847 |
US = {LUser, LServer}, |
107 |
2847 |
lists:foreach(fun (R) -> mnesia:delete_object(R) end, |
108 |
|
mnesia:index_read(roster, US, #roster.us)), |
109 |
2819 |
ok. |
110 |
|
|
111 |
|
%% Helpers |
112 |
|
|
113 |
1 |
write(Record, in_transaction) -> mnesia:write(Record); |
114 |
1 |
write(Record, no_transaction) -> mnesia:dirty_write(Record). |
115 |
|
|
116 |
774 |
read(TabKey, in_transaction) -> mnesia:read(TabKey); |
117 |
142 |
read(TabKey, no_transaction) -> mnesia:dirty_read(TabKey). |