2016-04-14 13:16:32 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_privacy_riak.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-14 13:16:32 +02:00
|
|
|
%%% Created : 14 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-04-14 13:16:32 +02:00
|
|
|
-module(mod_privacy_riak).
|
|
|
|
|
|
|
|
-behaviour(mod_privacy).
|
|
|
|
|
|
|
|
%% API
|
2017-05-20 21:36:32 +02:00
|
|
|
-export([init/2, set_default/3, unset_default/2, set_lists/1,
|
|
|
|
set_list/4, get_lists/2, get_list/3, remove_lists/2,
|
|
|
|
remove_list/3, import/1]).
|
2016-04-14 13:16:32 +02:00
|
|
|
|
|
|
|
-export([privacy_schema/0]).
|
|
|
|
|
2016-07-26 10:29:17 +02:00
|
|
|
-include("xmpp.hrl").
|
2016-04-14 13:16:32 +02:00
|
|
|
-include("mod_privacy.hrl").
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
unset_default(LUser, LServer) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
case ejabberd_riak:get(privacy, privacy_schema(), {LUser, LServer}) of
|
2017-05-20 21:36:32 +02:00
|
|
|
{ok, R} ->
|
|
|
|
ejabberd_riak:put(R#privacy{default = none}, privacy_schema());
|
|
|
|
{error, notfound} ->
|
|
|
|
ok;
|
|
|
|
Err ->
|
|
|
|
Err
|
2016-04-14 13:16:32 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
set_default(LUser, LServer, Name) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
case ejabberd_riak:get(privacy, privacy_schema(), {LUser, LServer}) of
|
2017-05-20 21:36:32 +02:00
|
|
|
{ok, #privacy{lists = Lists} = P} ->
|
|
|
|
case lists:keymember(Name, 1, Lists) of
|
|
|
|
true ->
|
|
|
|
ejabberd_riak:put(P#privacy{default = Name,
|
|
|
|
lists = Lists},
|
|
|
|
privacy_schema());
|
|
|
|
false ->
|
|
|
|
{error, notfound}
|
|
|
|
end;
|
|
|
|
Err ->
|
|
|
|
Err
|
2016-04-14 13:16:32 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
remove_list(LUser, LServer, Name) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
case ejabberd_riak:get(privacy, privacy_schema(), {LUser, LServer}) of
|
2017-05-20 21:36:32 +02:00
|
|
|
{ok, #privacy{default = Default, lists = Lists} = P} ->
|
|
|
|
if Name == Default ->
|
|
|
|
{error, conflict};
|
|
|
|
true ->
|
|
|
|
NewLists = lists:keydelete(Name, 1, Lists),
|
|
|
|
ejabberd_riak:put(P#privacy{lists = NewLists},
|
|
|
|
privacy_schema())
|
|
|
|
end;
|
|
|
|
Err ->
|
|
|
|
Err
|
2016-04-14 13:16:32 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
set_lists(Privacy) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
ejabberd_riak:put(Privacy, privacy_schema()).
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
set_list(LUser, LServer, Name, List) ->
|
|
|
|
case ejabberd_riak:get(privacy, privacy_schema(), {LUser, LServer}) of
|
|
|
|
{ok, #privacy{lists = Lists} = P} ->
|
|
|
|
NewLists1 = lists:keydelete(Name, 1, Lists),
|
|
|
|
NewLists = [{Name, List} | NewLists1],
|
|
|
|
ejabberd_riak:put(P#privacy{lists = NewLists}, privacy_schema());
|
|
|
|
{error, notfound} ->
|
|
|
|
NewLists = [{Name, List}],
|
|
|
|
ejabberd_riak:put(#privacy{us = {LUser, LServer},
|
|
|
|
lists = NewLists},
|
|
|
|
privacy_schema());
|
|
|
|
Err ->
|
|
|
|
Err
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_list(LUser, LServer, Name) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
case ejabberd_riak:get(privacy, privacy_schema(), {LUser, LServer}) of
|
2017-05-20 21:36:32 +02:00
|
|
|
{ok, #privacy{default = Default, lists = Lists}} when Name == default ->
|
|
|
|
case lists:keyfind(Default, 1, Lists) of
|
|
|
|
{_, List} -> {ok, {Default, List}};
|
|
|
|
false -> error
|
|
|
|
end;
|
|
|
|
{ok, #privacy{lists = Lists}} ->
|
|
|
|
case lists:keyfind(Name, 1, Lists) of
|
|
|
|
{_, List} -> {ok, {Name, List}};
|
|
|
|
false -> error
|
|
|
|
end;
|
|
|
|
{error, notfound} ->
|
|
|
|
error;
|
|
|
|
Err ->
|
|
|
|
Err
|
2016-04-14 13:16:32 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
get_lists(LUser, LServer) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
case ejabberd_riak:get(privacy, privacy_schema(), {LUser, LServer}) of
|
|
|
|
{ok, #privacy{} = P} ->
|
|
|
|
{ok, P};
|
2017-05-20 21:36:32 +02:00
|
|
|
{error, notfound} ->
|
|
|
|
error;
|
|
|
|
Err ->
|
|
|
|
Err
|
2016-04-14 13:16:32 +02:00
|
|
|
end.
|
|
|
|
|
2017-05-20 21:36:32 +02:00
|
|
|
remove_lists(LUser, LServer) ->
|
|
|
|
ejabberd_riak:delete(privacy, {LUser, LServer}).
|
2016-04-14 13:16:32 +02:00
|
|
|
|
2016-11-22 16:01:08 +01:00
|
|
|
import(#privacy{} = P) ->
|
2016-04-14 13:16:32 +02:00
|
|
|
ejabberd_riak:put(P, privacy_schema()).
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
privacy_schema() ->
|
|
|
|
{record_info(fields, privacy), #privacy{}}.
|