Clean mod_vcard_xupdate.erl from DB specific code

This commit is contained in:
Evgeniy Khramtsov 2016-04-13 11:06:59 +03:00
parent 7fd4808cde
commit 2d7e03f5e1
5 changed files with 216 additions and 121 deletions

View File

@ -0,0 +1,2 @@
-record(vcard_xupdate, {us = {<<>>, <<>>} :: {binary(), binary()},
hash = <<>> :: binary()}).

View File

@ -17,26 +17,22 @@
-include("ejabberd.hrl").
-include("logger.hrl").
-include("mod_vcard_xupdate.hrl").
-include("jlib.hrl").
-record(vcard_xupdate, {us = {<<>>, <<>>} :: {binary(), binary()},
hash = <<>> :: binary()}).
-callback init(binary(), gen_mod:opts()) -> any().
-callback import(binary(), #vcard_xupdate{}) -> ok | pass.
-callback add_xupdate(binary(), binary(), binary()) -> {atomic, any()}.
-callback get_xupdate(binary(), binary()) -> binary() | undefined.
-callback remove_xupdate(binary(), binary()) -> {atomic, any()}.
%%====================================================================
%% gen_mod callbacks
%%====================================================================
start(Host, Opts) ->
case gen_mod:db_type(Host, Opts) of
mnesia ->
mnesia:create_table(vcard_xupdate,
[{disc_copies, [node()]},
{attributes,
record_info(fields, vcard_xupdate)}]),
update_table();
_ -> ok
end,
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
Mod:init(Host, Opts),
ejabberd_hooks:add(c2s_update_presence, Host, ?MODULE,
update_presence, 100),
ejabberd_hooks:add(vcard_set, Host, ?MODULE, vcard_set,
@ -79,75 +75,16 @@ vcard_set(LUser, LServer, VCARD) ->
%%====================================================================
add_xupdate(LUser, LServer, Hash) ->
add_xupdate(LUser, LServer, Hash,
gen_mod:db_type(LServer, ?MODULE)).
add_xupdate(LUser, LServer, Hash, mnesia) ->
F = fun () ->
mnesia:write(#vcard_xupdate{us = {LUser, LServer},
hash = Hash})
end,
mnesia:transaction(F);
add_xupdate(LUser, LServer, Hash, riak) ->
{atomic, ejabberd_riak:put(#vcard_xupdate{us = {LUser, LServer},
hash = Hash},
vcard_xupdate_schema())};
add_xupdate(LUser, LServer, Hash, odbc) ->
Username = ejabberd_odbc:escape(LUser),
SHash = ejabberd_odbc:escape(Hash),
F = fun () ->
odbc_queries:update_t(<<"vcard_xupdate">>,
[<<"username">>, <<"hash">>],
[Username, SHash],
[<<"username='">>, Username, <<"'">>])
end,
ejabberd_odbc:sql_transaction(LServer, F).
Mod = gen_mod:db_mod(LServer, ?MODULE),
Mod:add_xupdate(LUser, LServer, Hash).
get_xupdate(LUser, LServer) ->
get_xupdate(LUser, LServer,
gen_mod:db_type(LServer, ?MODULE)).
get_xupdate(LUser, LServer, mnesia) ->
case mnesia:dirty_read(vcard_xupdate, {LUser, LServer})
of
[#vcard_xupdate{hash = Hash}] -> Hash;
_ -> undefined
end;
get_xupdate(LUser, LServer, riak) ->
case ejabberd_riak:get(vcard_xupdate, vcard_xupdate_schema(),
{LUser, LServer}) of
{ok, #vcard_xupdate{hash = Hash}} -> Hash;
_ -> undefined
end;
get_xupdate(LUser, LServer, odbc) ->
Username = ejabberd_odbc:escape(LUser),
case ejabberd_odbc:sql_query(LServer,
[<<"select hash from vcard_xupdate where "
"username='">>,
Username, <<"';">>])
of
{selected, [<<"hash">>], [[Hash]]} -> Hash;
_ -> undefined
end.
Mod = gen_mod:db_mod(LServer, ?MODULE),
Mod:get_xupdate(LUser, LServer).
remove_xupdate(LUser, LServer) ->
remove_xupdate(LUser, LServer,
gen_mod:db_type(LServer, ?MODULE)).
remove_xupdate(LUser, LServer, mnesia) ->
F = fun () ->
mnesia:delete({vcard_xupdate, {LUser, LServer}})
end,
mnesia:transaction(F);
remove_xupdate(LUser, LServer, riak) ->
{atomic, ejabberd_riak:delete(vcard_xupdate, {LUser, LServer})};
remove_xupdate(LUser, LServer, odbc) ->
Username = ejabberd_odbc:escape(LUser),
F = fun () ->
ejabberd_odbc:sql_query_t([<<"delete from vcard_xupdate where username='">>,
Username, <<"';">>])
end,
ejabberd_odbc:sql_transaction(LServer, F).
Mod = gen_mod:db_mod(LServer, ?MODULE),
Mod:remove_xupdate(LUser, LServer).
%%%----------------------------------------------------------------------
%%% Presence stanza rebuilding
@ -184,53 +121,17 @@ build_xphotoel(User, Host) ->
attrs = [{<<"xmlns">>, ?NS_VCARD_UPDATE}],
children = PhotoEl}.
vcard_xupdate_schema() ->
{record_info(fields, vcard_xupdate), #vcard_xupdate{}}.
update_table() ->
Fields = record_info(fields, vcard_xupdate),
case mnesia:table_info(vcard_xupdate, attributes) of
Fields ->
ejabberd_config:convert_table_to_binary(
vcard_xupdate, Fields, set,
fun(#vcard_xupdate{us = {U, _}}) -> U end,
fun(#vcard_xupdate{us = {U, S}, hash = Hash} = R) ->
R#vcard_xupdate{us = {iolist_to_binary(U),
iolist_to_binary(S)},
hash = iolist_to_binary(Hash)}
end);
_ ->
?INFO_MSG("Recreating vcard_xupdate table", []),
mnesia:transform_table(vcard_xupdate, ignore, Fields)
end.
export(_Server) ->
[{vcard_xupdate,
fun(Host, #vcard_xupdate{us = {LUser, LServer}, hash = Hash})
when LServer == Host ->
Username = ejabberd_odbc:escape(LUser),
SHash = ejabberd_odbc:escape(Hash),
[[<<"delete from vcard_xupdate where username='">>,
Username, <<"';">>],
[<<"insert into vcard_xupdate(username, "
"hash) values ('">>,
Username, <<"', '">>, SHash, <<"');">>]];
(_Host, _R) ->
[]
end}].
export(LServer) ->
Mod = gen_mod:db_mod(LServer, ?MODULE),
Mod:export(LServer).
import(LServer) ->
[{<<"select username, hash from vcard_xupdate;">>,
fun([LUser, Hash]) ->
#vcard_xupdate{us = {LUser, LServer}, hash = Hash}
end}].
Mod = gen_mod:db_mod(LServer, ?MODULE),
Mod:import(LServer).
import(_LServer, mnesia, #vcard_xupdate{} = R) ->
mnesia:dirty_write(R);
import(_LServer, riak, #vcard_xupdate{} = R) ->
ejabberd_riak:put(R, vcard_xupdate_schema());
import(_, _, _) ->
pass.
import(LServer, DBType, LA) ->
Mod = gen_mod:db_mod(DBType, ?MODULE),
Mod:import(LServer, LA).
mod_opt_type(db_type) -> fun gen_mod:v_db/1;
mod_opt_type(_) -> [db_type].

View File

@ -0,0 +1,69 @@
%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% @copyright (C) 2016, Evgeny Khramtsov
%%% @doc
%%%
%%% @end
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%-------------------------------------------------------------------
-module(mod_vcard_xupdate_mnesia).
-behaviour(mod_vcard_xupdate).
%% API
-export([init/2, import/2, add_xupdate/3, get_xupdate/2, remove_xupdate/2]).
-include("mod_vcard_xupdate.hrl").
-include("logger.hrl").
%%%===================================================================
%%% API
%%%===================================================================
init(_Host, _Opts) ->
mnesia:create_table(vcard_xupdate,
[{disc_copies, [node()]},
{attributes,
record_info(fields, vcard_xupdate)}]),
update_table().
add_xupdate(LUser, LServer, Hash) ->
F = fun () ->
mnesia:write(#vcard_xupdate{us = {LUser, LServer},
hash = Hash})
end,
mnesia:transaction(F).
get_xupdate(LUser, LServer) ->
case mnesia:dirty_read(vcard_xupdate, {LUser, LServer})
of
[#vcard_xupdate{hash = Hash}] -> Hash;
_ -> undefined
end.
remove_xupdate(LUser, LServer) ->
F = fun () ->
mnesia:delete({vcard_xupdate, {LUser, LServer}})
end,
mnesia:transaction(F).
import(_LServer, #vcard_xupdate{} = R) ->
mnesia:dirty_write(R).
%%%===================================================================
%%% Internal functions
%%%===================================================================
update_table() ->
Fields = record_info(fields, vcard_xupdate),
case mnesia:table_info(vcard_xupdate, attributes) of
Fields ->
ejabberd_config:convert_table_to_binary(
vcard_xupdate, Fields, set,
fun(#vcard_xupdate{us = {U, _}}) -> U end,
fun(#vcard_xupdate{us = {U, S}, hash = Hash} = R) ->
R#vcard_xupdate{us = {iolist_to_binary(U),
iolist_to_binary(S)},
hash = iolist_to_binary(Hash)}
end);
_ ->
?INFO_MSG("Recreating vcard_xupdate table", []),
mnesia:transform_table(vcard_xupdate, ignore, Fields)
end.

View File

@ -0,0 +1,44 @@
%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% @copyright (C) 2016, Evgeny Khramtsov
%%% @doc
%%%
%%% @end
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%-------------------------------------------------------------------
-module(mod_vcard_xupdate_riak).
%% API
-export([init/2, import/2, add_xupdate/3, get_xupdate/2, remove_xupdate/2]).
-include("mod_vcard_xupdate.hrl").
%%%===================================================================
%%% API
%%%===================================================================
init(_Host, _Opts) ->
ok.
add_xupdate(LUser, LServer, Hash) ->
{atomic, ejabberd_riak:put(#vcard_xupdate{us = {LUser, LServer},
hash = Hash},
vcard_xupdate_schema())}.
get_xupdate(LUser, LServer) ->
case ejabberd_riak:get(vcard_xupdate, vcard_xupdate_schema(),
{LUser, LServer}) of
{ok, #vcard_xupdate{hash = Hash}} -> Hash;
_ -> undefined
end.
remove_xupdate(LUser, LServer) ->
{atomic, ejabberd_riak:delete(vcard_xupdate, {LUser, LServer})}.
import(_LServer, #vcard_xupdate{} = R) ->
ejabberd_riak:put(R, vcard_xupdate_schema()).
%%%===================================================================
%%% Internal functions
%%%===================================================================
vcard_xupdate_schema() ->
{record_info(fields, vcard_xupdate), #vcard_xupdate{}}.

View File

@ -0,0 +1,79 @@
%%%-------------------------------------------------------------------
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
%%% @copyright (C) 2016, Evgeny Khramtsov
%%% @doc
%%%
%%% @end
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
%%%-------------------------------------------------------------------
-module(mod_vcard_xupdate_sql).
%% API
-export([init/2, import/2, add_xupdate/3, get_xupdate/2, remove_xupdate/2,
import/1, export/1]).
-include("mod_vcard_xupdate.hrl").
%%%===================================================================
%%% API
%%%===================================================================
init(_Host, _Opts) ->
ok.
add_xupdate(LUser, LServer, Hash) ->
Username = ejabberd_odbc:escape(LUser),
SHash = ejabberd_odbc:escape(Hash),
F = fun () ->
odbc_queries:update_t(<<"vcard_xupdate">>,
[<<"username">>, <<"hash">>],
[Username, SHash],
[<<"username='">>, Username, <<"'">>])
end,
ejabberd_odbc:sql_transaction(LServer, F).
get_xupdate(LUser, LServer) ->
Username = ejabberd_odbc:escape(LUser),
case ejabberd_odbc:sql_query(LServer,
[<<"select hash from vcard_xupdate where "
"username='">>,
Username, <<"';">>])
of
{selected, [<<"hash">>], [[Hash]]} -> Hash;
_ -> undefined
end.
remove_xupdate(LUser, LServer) ->
Username = ejabberd_odbc:escape(LUser),
F = fun () ->
ejabberd_odbc:sql_query_t([<<"delete from vcard_xupdate where username='">>,
Username, <<"';">>])
end,
ejabberd_odbc:sql_transaction(LServer, F).
export(_Server) ->
[{vcard_xupdate,
fun(Host, #vcard_xupdate{us = {LUser, LServer}, hash = Hash})
when LServer == Host ->
Username = ejabberd_odbc:escape(LUser),
SHash = ejabberd_odbc:escape(Hash),
[[<<"delete from vcard_xupdate where username='">>,
Username, <<"';">>],
[<<"insert into vcard_xupdate(username, "
"hash) values ('">>,
Username, <<"', '">>, SHash, <<"');">>]];
(_Host, _R) ->
[]
end}].
import(LServer) ->
[{<<"select username, hash from vcard_xupdate;">>,
fun([LUser, Hash]) ->
#vcard_xupdate{us = {LUser, LServer}, hash = Hash}
end}].
import(_LServer, _) ->
pass.
%%%===================================================================
%%% Internal functions
%%%===================================================================