./ct_report/coverage/mod_private_riak.COVER.html

1 %%==============================================================================
2 %% Copyright 2015 Erlang Solutions Ltd.
3 %%
4 %% Licensed under the Apache License, Version 2.0 (the "License");
5 %% you may not use this file except in compliance with the License.
6 %% You may obtain a copy of the License at
7 %%
8 %% http://www.apache.org/licenses/LICENSE-2.0
9 %%
10 %% Unless required by applicable law or agreed to in writing, software
11 %% distributed under the License is distributed on an "AS IS" BASIS,
12 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 %% See the License for the specific language governing permissions and
14 %% limitations under the License.
15 %%==============================================================================
16 -module(mod_private_riak).
17
18 -behaviour(mod_private_backend).
19
20 %% API
21 -export([init/2,
22 multi_set_data/4,
23 multi_get_data/4,
24 get_all_nss/3,
25 remove_user/3,
26 remove_domain/2]).
27
28 -include("jlib.hrl").
29
30 -spec init(mongooseim:host_type(), list()) -> ok.
31 init(_HostType, _Opts) ->
32
:-(
ok.
33
34 -spec multi_set_data(mongooseim:host_type(),
35 jid:luser(), jid:lserver(), [{binary(), exml:element()}]) ->
36 ok | {error, term()}.
37 multi_set_data(_HostType, LUser, LServer, NS2XML) ->
38
:-(
R = [set_private_data(LUser, LServer, NS, XML) || {NS, XML} <- NS2XML],
39 %% check if something returned with error msg
40
:-(
case lists:keyfind(error, 1, R) of
41
:-(
{error, Reason} -> {error, Reason};
42
:-(
false -> ok
43 end.
44
45 -spec multi_get_data(mongooseim:host_type(),
46 jid:luser(), jid:lserver(), [{binary(), term()}]) -> [any()].
47 multi_get_data(_HostType, LUser, LServer, NS2Def) ->
48
:-(
[get_private_data(LUser, LServer, NS, Default) || {NS, Default} <- NS2Def].
49
50 -spec remove_user(mongooseim:host_type(), jid:luser(), jid:lserver()) -> ok.
51 remove_user(HostType, LUser, LServer) ->
52
:-(
Bucket = bucket_type(LServer),
53
:-(
[mongoose_riak:delete(Bucket, key(LUser, NS)) || NS <- get_all_nss(HostType, LUser, LServer)],
54
:-(
ok.
55
56 %% There is no optimized way to remove a domain.
57 %% We expect, that domain removal process would call remove_user instead
58 %% for each user.
59 remove_domain(_HostType, _LServer) ->
60
:-(
ok.
61
62 set_private_data(LUser, LServer, NS, XML) ->
63
:-(
Obj = riakc_obj:new(bucket_type(LServer), key(LUser, NS), exml:to_binary(XML)),
64
:-(
mongoose_riak:put(Obj).
65
66 get_all_nss(_HostType, LUser, LServer) ->
67
:-(
{ok, KeysWithUsername} = mongoose_riak:list_keys(bucket_type(LServer)),
68
:-(
lists:foldl(
69 fun(Key, Acc) ->
70
:-(
case binary:split(Key, <<"/">>) of
71
:-(
[LUser, ResultKey] -> [ResultKey | Acc];
72
:-(
_ -> Acc
73 end
74 end,
75 [], KeysWithUsername
76 ).
77
78 get_private_data(LUser, LServer, NS, Default) ->
79
:-(
case mongoose_riak:get(bucket_type(LServer), key(LUser, NS)) of
80 {ok, Obj} ->
81
:-(
Value = riakc_obj:get_value(Obj),
82
:-(
{ok, #xmlel{} = DecodedXML} = exml:parse(Value),
83
:-(
DecodedXML;
84 _ ->
85
:-(
Default
86 end.
87
88 bucket_type(LServer) ->
89
:-(
{gen_mod:get_module_opt(LServer, mod_private, bucket_type, <<"private">>), LServer}.
90
91 key(LUser, NS) ->
92
:-(
<<LUser/binary, "/", NS/binary>>.
Line Hits Source