2016-04-14 09:58:32 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_roster_riak.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-14 09:58:32 +02:00
|
|
|
%%% Created : 14 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 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 09:58:32 +02:00
|
|
|
|
2016-12-27 10:44:07 +01:00
|
|
|
-module(mod_roster_riak).
|
2016-04-14 09:58:32 +02:00
|
|
|
|
|
|
|
-behaviour(mod_roster).
|
|
|
|
|
|
|
|
%% API
|
|
|
|
-export([init/2, read_roster_version/2, write_roster_version/4,
|
2017-05-17 13:47:35 +02:00
|
|
|
get_roster/2, get_roster_item/3, create_roster/1,
|
|
|
|
roster_subscribe/4, remove_user/2, update_roster/4,
|
|
|
|
del_roster/3, read_subscription_and_groups/3, transaction/2,
|
|
|
|
import/3]).
|
2016-04-14 09:58:32 +02:00
|
|
|
|
|
|
|
-include("mod_roster.hrl").
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
|
|
|
read_roster_version(LUser, LServer) ->
|
|
|
|
case ejabberd_riak:get(roster_version, roster_version_schema(),
|
|
|
|
{LUser, LServer}) of
|
2017-05-17 13:47:35 +02:00
|
|
|
{ok, #roster_version{version = V}} -> {ok, V};
|
2016-04-14 09:58:32 +02:00
|
|
|
_Err -> error
|
|
|
|
end.
|
|
|
|
|
|
|
|
write_roster_version(LUser, LServer, _InTransaction, Ver) ->
|
|
|
|
US = {LUser, LServer},
|
|
|
|
ejabberd_riak:put(#roster_version{us = US, version = Ver},
|
|
|
|
roster_version_schema()).
|
|
|
|
|
|
|
|
get_roster(LUser, LServer) ->
|
|
|
|
case ejabberd_riak:get_by_index(roster, roster_schema(),
|
|
|
|
<<"us">>, {LUser, LServer}) of
|
2017-05-17 13:47:35 +02:00
|
|
|
{ok, Items} -> {ok, Items};
|
|
|
|
_Err -> error
|
2016-04-14 09:58:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
roster_subscribe(LUser, LServer, _LJID, Item) ->
|
|
|
|
ejabberd_riak:put(Item, roster_schema(),
|
|
|
|
[{'2i', [{<<"us">>, {LUser, LServer}}]}]).
|
|
|
|
|
|
|
|
transaction(_LServer, F) ->
|
|
|
|
{atomic, F()}.
|
|
|
|
|
2017-05-17 13:47:35 +02:00
|
|
|
get_roster_item(LUser, LServer, LJID) ->
|
2016-04-14 09:58:32 +02:00
|
|
|
case ejabberd_riak:get(roster, roster_schema(), {LUser, LServer, LJID}) of
|
|
|
|
{ok, I} ->
|
2017-05-17 13:47:35 +02:00
|
|
|
{ok, I};
|
|
|
|
{error, _} ->
|
|
|
|
error
|
2016-04-14 09:58:32 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
remove_user(LUser, LServer) ->
|
2017-05-17 13:47:35 +02:00
|
|
|
ejabberd_riak:delete_by_index(roster, <<"us">>, {LUser, LServer}).
|
2016-04-14 09:58:32 +02:00
|
|
|
|
|
|
|
update_roster(LUser, LServer, _LJID, Item) ->
|
|
|
|
ejabberd_riak:put(Item, roster_schema(),
|
|
|
|
[{'2i', [{<<"us">>, {LUser, LServer}}]}]).
|
|
|
|
|
|
|
|
del_roster(LUser, LServer, LJID) ->
|
|
|
|
ejabberd_riak:delete(roster, {LUser, LServer, LJID}).
|
|
|
|
|
|
|
|
read_subscription_and_groups(LUser, LServer, LJID) ->
|
|
|
|
case ejabberd_riak:get(roster, roster_schema(), {LUser, LServer, LJID}) of
|
2017-05-17 13:47:35 +02:00
|
|
|
{ok, #roster{subscription = Subscription,
|
2018-01-26 13:02:06 +01:00
|
|
|
ask = Ask,
|
2017-05-17 13:47:35 +02:00
|
|
|
groups = Groups}} ->
|
2018-01-26 13:02:06 +01:00
|
|
|
{ok, {Subscription, Ask, Groups}};
|
2017-05-17 13:47:35 +02:00
|
|
|
_ ->
|
|
|
|
error
|
2016-04-14 09:58:32 +02:00
|
|
|
end.
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
create_roster(#roster{us = {LUser, LServer}} = RItem) ->
|
|
|
|
ejabberd_riak:put(
|
|
|
|
RItem, roster_schema(),
|
|
|
|
[{'2i', [{<<"us">>, {LUser, LServer}}]}]).
|
|
|
|
|
|
|
|
import(_LServer, <<"rosterusers">>, RosterItem) ->
|
|
|
|
{LUser, LServer} = RosterItem#roster.us,
|
|
|
|
ejabberd_riak:put(RosterItem, roster_schema(),
|
2016-04-14 09:58:32 +02:00
|
|
|
[{'2i', [{<<"us">>, {LUser, LServer}}]}]);
|
2016-11-22 14:48:01 +01:00
|
|
|
import(LServer, <<"roster_version">>, [LUser, Ver]) ->
|
|
|
|
RV = #roster_version{us = {LUser, LServer}, version = Ver},
|
2016-04-14 09:58:32 +02:00
|
|
|
ejabberd_riak:put(RV, roster_version_schema()).
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
roster_schema() ->
|
|
|
|
{record_info(fields, roster), #roster{}}.
|
|
|
|
|
|
|
|
roster_version_schema() ->
|
|
|
|
{record_info(fields, roster_version), #roster_version{}}.
|