• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

processone / ejabberd / 1296

19 Jan 2026 11:25AM UTC coverage: 33.562% (+0.09%) from 33.468%
1296

push

github

badlop
mod_conversejs: Cosmetic change: sort paths alphabetically

0 of 4 new or added lines in 1 file covered. (0.0%)

11245 existing lines in 174 files now uncovered.

15580 of 46421 relevant lines covered (33.56%)

1074.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.43
/src/mod_privacy_mnesia.erl
1
%%%-------------------------------------------------------------------
2
%%% File    : mod_privacy_mnesia.erl
3
%%% Author  : Evgeny Khramtsov <ekhramtsov@process-one.net>
4
%%% Created : 14 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
5
%%%
6
%%%
7
%%% ejabberd, Copyright (C) 2002-2026   ProcessOne
8
%%%
9
%%% This program is free software; you can redistribute it and/or
10
%%% modify it under the terms of the GNU General Public License as
11
%%% published by the Free Software Foundation; either version 2 of the
12
%%% License, or (at your option) any later version.
13
%%%
14
%%% This program is distributed in the hope that it will be useful,
15
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
16
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
%%% General Public License for more details.
18
%%%
19
%%% You should have received a copy of the GNU General Public License along
20
%%% with this program; if not, write to the Free Software Foundation, Inc.,
21
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
%%%
23
%%%----------------------------------------------------------------------
24

25
-module(mod_privacy_mnesia).
26

27
-behaviour(mod_privacy).
28

29
%% API
30
-export([init/2, set_default/3, unset_default/2, set_lists/1,
31
         set_list/4, get_lists/2, get_list/3, remove_lists/2,
32
         remove_list/3, use_cache/1, import/1]).
33
-export([need_transform/1, transform/1]).
34

35
-include_lib("xmpp/include/xmpp.hrl").
36
-include("mod_privacy.hrl").
37
-include("logger.hrl").
38

39
%%%===================================================================
40
%%% API
41
%%%===================================================================
42
init(_Host, _Opts) ->
UNCOV
43
    ejabberd_mnesia:create(?MODULE, privacy,
2✔
44
                           [{disc_only_copies, [node()]},
45
                            {attributes, record_info(fields, privacy)}]).
46

47
use_cache(Host) ->
UNCOV
48
    case mnesia:table_info(privacy, storage_type) of
26,116✔
49
        disc_only_copies ->
UNCOV
50
            mod_privacy_opt:use_cache(Host);
26,116✔
51
        _ ->
52
            false
×
53
    end.
54

55
unset_default(LUser, LServer) ->
UNCOV
56
    F = fun () ->
2✔
UNCOV
57
                case mnesia:read({privacy, {LUser, LServer}}) of
2✔
58
                    [] -> ok;
×
UNCOV
59
                    [R] -> mnesia:write(R#privacy{default = none})
2✔
60
                end
61
        end,
UNCOV
62
    transaction(F).
2✔
63

64
set_default(LUser, LServer, Name) ->
UNCOV
65
    F = fun () ->
18✔
UNCOV
66
                case mnesia:read({privacy, {LUser, LServer}}) of
18✔
67
                    [] ->
UNCOV
68
                        {error, notfound};
2✔
69
                    [#privacy{lists = Lists} = P] ->
UNCOV
70
                        case lists:keymember(Name, 1, Lists) of
16✔
71
                            true ->
UNCOV
72
                                mnesia:write(P#privacy{default = Name,
16✔
73
                                                       lists = Lists});
74
                            false ->
75
                                {error, notfound}
×
76
                        end
77
                end
78
        end,
UNCOV
79
    transaction(F).
18✔
80

81
remove_list(LUser, LServer, Name) ->
UNCOV
82
    F = fun () ->
6✔
UNCOV
83
                case mnesia:read({privacy, {LUser, LServer}}) of
6✔
84
                    [] ->
UNCOV
85
                        {error, notfound};
2✔
86
                    [#privacy{default = Default, lists = Lists} = P] ->
UNCOV
87
                        if Name == Default ->
4✔
UNCOV
88
                                {error, conflict};
2✔
89
                           true ->
UNCOV
90
                                NewLists = lists:keydelete(Name, 1, Lists),
2✔
UNCOV
91
                                mnesia:write(P#privacy{lists = NewLists})
2✔
92
                        end
93
                end
94
        end,
UNCOV
95
    transaction(F).
6✔
96

97
set_lists(Privacy) ->
98
    mnesia:dirty_write(Privacy).
×
99

100
set_list(LUser, LServer, Name, List) ->
UNCOV
101
    F = fun () ->
154✔
UNCOV
102
                case mnesia:wread({privacy, {LUser, LServer}}) of
154✔
103
                    [] ->
UNCOV
104
                        NewLists = [{Name, List}],
48✔
UNCOV
105
                        mnesia:write(#privacy{us = {LUser, LServer},
48✔
106
                                              lists = NewLists});
107
                    [#privacy{lists = Lists} = P] ->
UNCOV
108
                        NewLists1 = lists:keydelete(Name, 1, Lists),
106✔
UNCOV
109
                        NewLists = [{Name, List} | NewLists1],
106✔
UNCOV
110
                        mnesia:write(P#privacy{lists = NewLists})
106✔
111
                end
112
        end,
UNCOV
113
    transaction(F).
154✔
114

115
get_list(LUser, LServer, Name) ->
UNCOV
116
    case mnesia:dirty_read(privacy, {LUser, LServer}) of
258✔
117
        [#privacy{default = Default, lists = Lists}] when Name == default ->
UNCOV
118
            case lists:keyfind(Default, 1, Lists) of
70✔
UNCOV
119
                {_, List} -> {ok, {Default, List}};
22✔
UNCOV
120
                false -> error
48✔
121
            end;
122
        [#privacy{lists = Lists}] ->
UNCOV
123
            case lists:keyfind(Name, 1, Lists) of
130✔
UNCOV
124
                {_, List} -> {ok, {Name, List}};
130✔
125
                false -> error
×
126
            end;
127
        [] ->
UNCOV
128
            error
58✔
129
    end.
130

131
get_lists(LUser, LServer) ->
UNCOV
132
    case mnesia:dirty_read(privacy, {LUser, LServer}) of
58✔
133
        [#privacy{} = P] ->
UNCOV
134
            {ok, P};
50✔
135
        _ ->
UNCOV
136
            error
8✔
137
    end.
138

139
remove_lists(LUser, LServer) ->
UNCOV
140
    F = fun () -> mnesia:delete({privacy, {LUser, LServer}}) end,
80✔
UNCOV
141
    transaction(F).
80✔
142

143
import(#privacy{} = P) ->
144
    mnesia:dirty_write(P).
×
145

146
need_transform({privacy, {U, S}, _, _}) when is_list(U) orelse is_list(S) ->
147
    ?INFO_MSG("Mnesia table 'privacy' will be converted to binary", []),
×
148
    true;
×
149
need_transform(_) ->
150
    false.
×
151

152
transform(#privacy{us = {U, S}, default = Def, lists = Lists} = R) ->
153
    NewLists = lists:map(
×
154
                 fun({Name, Ls}) ->
155
                         NewLs = lists:map(
×
156
                                   fun(#listitem{value = Val} = L) ->
157
                                           NewVal = case Val of
×
158
                                                        {LU, LS, LR} ->
159
                                                            {iolist_to_binary(LU),
×
160
                                                             iolist_to_binary(LS),
161
                                                             iolist_to_binary(LR)};
162
                                                        none -> none;
×
163
                                                        both -> both;
×
164
                                                        from -> from;
×
165
                                                        to -> to;
×
166
                                                        _ -> iolist_to_binary(Val)
×
167
                                                    end,
168
                                           L#listitem{value = NewVal}
×
169
                                   end, Ls),
170
                         {iolist_to_binary(Name), NewLs}
×
171
                 end, Lists),
172
    NewDef = case Def of
×
173
                 none -> none;
×
174
                 _ -> iolist_to_binary(Def)
×
175
             end,
176
    NewUS = {iolist_to_binary(U), iolist_to_binary(S)},
×
177
    R#privacy{us = NewUS, default = NewDef, lists = NewLists}.
×
178

179
%%%===================================================================
180
%%% Internal functions
181
%%%===================================================================
182
transaction(F) ->
UNCOV
183
    case mnesia:transaction(F) of
260✔
184
        {atomic, Result} ->
UNCOV
185
            Result;
260✔
186
        {aborted, Reason} ->
187
            ?ERROR_MSG("Mnesia transaction failed: ~p", [Reason]),
×
188
            {error, db_failure}
×
189
    end.
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc