25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

Move pubsub sql export to pubsub_db_sql module

This commit is contained in:
Christophe Romain 2017-08-03 15:34:01 +02:00
parent 96c0483533
commit ce0beb550c
2 changed files with 97 additions and 96 deletions

View File

@ -39,8 +39,6 @@
-protocol({xep, 163, '1.2'}).
-protocol({xep, 248, '0.2'}).
-compile([{parse_transform, ejabberd_sql_pt}]).
-include("ejabberd.hrl").
-include("logger.hrl").
-include("xmpp.hrl").
@ -91,14 +89,9 @@
%% API and gen_server callbacks
-export([start/2, stop/1, init/1,
handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3, depends/2]).
terminate/2, code_change/3, depends/2, export/1]).
-export([send_loop/1, mod_opt_type/1]).
-export([export/1]).
-include("ejabberd_sql_pt.hrl").
-define(PROCNAME, ejabberd_mod_pubsub).
-define(LOOPNAME, ejabberd_mod_pubsub_loop).
@ -3873,94 +3866,8 @@ purge_offline(Host, LJID, Node) ->
Error
end.
%% REVIEW:
%% * this code takes NODEID from Itemid2, and forgets about Nodeidx
%% * this code assumes Payload only contains one xmlelement()
%% * PUBLISHER is taken from Creation
export(_Server) ->
[{pubsub_item,
fun(_Host, #pubsub_item{itemid = {Itemid1, NODEID},
%nodeidx = _Nodeidx,
creation = {{C1, C2, C3}, Cusr},
modification = {{M1, M2, M3}, _Musr},
payload = Payload}) ->
ITEMID = ejabberd_sql:escape(Itemid1),
CREATION = ejabberd_sql:escape(list_to_binary(
string:join([string:right(integer_to_list(I),6,$0)||I<-[C1,C2,C3]],":"))),
MODIFICATION = ejabberd_sql:escape(list_to_binary(
string:join([string:right(integer_to_list(I),6,$0)||I<-[M1,M2,M3]],":"))),
PUBLISHER = ejabberd_sql:escape(jid:encode(Cusr)),
[PayloadEl] = [El || {xmlel,_,_,_} = El <- Payload],
PAYLOAD = ejabberd_sql:escape(fxml:element_to_binary(PayloadEl)),
[?SQL("delete from pubsub_item where itemid=%(ITEMID)s;"),
?SQL("insert into pubsub_item(itemid,nodeid,creation,modification,publisher,payload) \n"
" values (%(ITEMID)s, %(NODEID)d, %(CREATION)s,
%(MODIFICATION)s, %(PUBLISHER)s, %(PAYLOAD)s);")];
(_Host, _R) ->
[]
end},
%% REVIEW:
%% * From the mnesia table, the #pubsub_state.items is not used in ODBC
%% * Right now AFFILIATION is the first letter of Affiliation
%% * Right now SUBSCRIPTIONS expects only one Subscription
%% * Right now SUBSCRIPTIONS letter is the first letter of Subscription
{pubsub_state,
fun(_Host, #pubsub_state{stateid = {Jid, Stateid},
%nodeidx = Nodeidx,
items = _Items,
affiliation = Affiliation,
subscriptions = Subscriptions}) ->
STATEID = list_to_binary(integer_to_list(Stateid)),
JID = ejabberd_sql:escape(jid:encode(Jid)),
NODEID = <<"unknown">>, %% TODO: integer_to_list(Nodeidx),
AFFILIATION = list_to_binary(string:substr(atom_to_list(Affiliation),1,1)),
SUBSCRIPTIONS = list_to_binary(parse_subscriptions(Subscriptions)),
[?SQL("delete from pubsub_state where stateid=%(STATEID)s;"),
?SQL("insert into pubsub_state(stateid,jid,nodeid,affiliation,subscriptions)\n"
" values (%(STATEID)s, %(JID)s, %(NODEID)s, %(AFFILIATION)s, %(SUBSCRIPTIONS)s);")];
(_Host, _R) ->
[]
end},
%% REVIEW:
%% * Parents is not migrated to PARENTs
%% * Probably some option VALs are not correctly represented in mysql
{pubsub_node,
fun(_Host, #pubsub_node{nodeid = {Hostid, Nodeid},
id = Id,
parents = _Parents,
type = Type,
owners = Owners,
options = Options}) ->
HOST = case Hostid of
{U,S,R} -> ejabberd_sql:escape(jid:encode({U,S,R}));
_ -> ejabberd_sql:escape(Hostid)
end,
NODE = ejabberd_sql:escape(Nodeid),
PARENT = <<"">>,
IdB = integer_to_binary(Id),
TYPE = ejabberd_sql:escape(<<Type/binary, "_odbc">>),
[?SQL("delete from pubsub_node where nodeid=%(Id)d;"),
?SQL("insert into pubsub_node(host,node,nodeid,parent,type) \n"
" values (%(HOST)s, %(NODE)s, %(Id)d, %(PARENT)s, %(TYPE)s);"),
?SQL("delete from pubsub_node_option where nodeid=%(Id)d;"),
[["insert into pubsub_node_option(nodeid,name,val)\n"
" values (", IdB, ", '", atom_to_list(Name), "', '",
io_lib:format("~p", [Val]), "');\n"] || {Name,Val} <- Options],
?SQL("delete from pubsub_node_owner where nodeid=%(Id)d;"),
[["insert into pubsub_node_owner(nodeid,owner)\n"
" values (", IdB, ", '", jid:encode(Usr), "');\n"] || Usr <- Owners],"\n"];
(_Host, _R) ->
[]
end}].
parse_subscriptions([]) ->
"";
parse_subscriptions([{State, Item}]) ->
STATE = case State of
subscribed -> "s"
end,
string:join([STATE, Item],":").
export(Server) ->
pubsub_db_sql:export(Server).
mod_opt_type(access_createnode) -> fun acl:access_rules_validator/1;
mod_opt_type(db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end;

View File

@ -25,12 +25,16 @@
-module(pubsub_db_sql).
-compile([{parse_transform, ejabberd_sql_pt}]).
-author("pablo.polvorin@process-one.net").
-include("pubsub.hrl").
-include("ejabberd_sql_pt.hrl").
-export([add_subscription/1, read_subscription/1,
delete_subscription/1, update_subscription/1]).
-export([export/1]).
%% TODO: Those -spec lines produce errors in old Erlang versions.
%% They can be enabled again in ejabberd 3.0 because it uses R12B or higher.
@ -139,3 +143,93 @@ sql_to_integer(N) -> binary_to_integer(N).
sql_to_boolean(B) -> B == <<"1">>.
sql_to_timestamp(T) -> xmpp_util:decode_timestamp(T).
%% REVIEW:
%% * this code takes NODEID from Itemid2, and forgets about Nodeidx
%% * this code assumes Payload only contains one xmlelement()
%% * PUBLISHER is taken from Creation
export(_Server) ->
[{pubsub_item,
fun(_Host, #pubsub_item{itemid = {Itemid1, NODEID},
%nodeidx = _Nodeidx,
creation = {{C1, C2, C3}, Cusr},
modification = {{M1, M2, M3}, _Musr},
payload = Payload}) ->
ITEMID = ejabberd_sql:escape(Itemid1),
CREATION = ejabberd_sql:escape(list_to_binary(
string:join([string:right(integer_to_list(I),6,$0)||I<-[C1,C2,C3]],":"))),
MODIFICATION = ejabberd_sql:escape(list_to_binary(
string:join([string:right(integer_to_list(I),6,$0)||I<-[M1,M2,M3]],":"))),
PUBLISHER = ejabberd_sql:escape(jid:encode(Cusr)),
[PayloadEl] = [El || {xmlel,_,_,_} = El <- Payload],
PAYLOAD = ejabberd_sql:escape(fxml:element_to_binary(PayloadEl)),
[?SQL("delete from pubsub_item where itemid=%(ITEMID)s;"),
?SQL("insert into pubsub_item(itemid,nodeid,creation,modification,publisher,payload) \n"
" values (%(ITEMID)s, %(NODEID)d, %(CREATION)s,
%(MODIFICATION)s, %(PUBLISHER)s, %(PAYLOAD)s);")];
(_Host, _R) ->
[]
end},
%% REVIEW:
%% * From the mnesia table, the #pubsub_state.items is not used in ODBC
%% * Right now AFFILIATION is the first letter of Affiliation
%% * Right now SUBSCRIPTIONS expects only one Subscription
%% * Right now SUBSCRIPTIONS letter is the first letter of Subscription
{pubsub_state,
fun(_Host, #pubsub_state{stateid = {Jid, Stateid},
%nodeidx = Nodeidx,
items = _Items,
affiliation = Affiliation,
subscriptions = Subscriptions}) ->
STATEID = list_to_binary(integer_to_list(Stateid)),
JID = ejabberd_sql:escape(jid:encode(Jid)),
NODEID = <<"unknown">>, %% TODO: integer_to_list(Nodeidx),
AFFILIATION = list_to_binary(string:substr(atom_to_list(Affiliation),1,1)),
SUBSCRIPTIONS = list_to_binary(parse_subscriptions(Subscriptions)),
[?SQL("delete from pubsub_state where stateid=%(STATEID)s;"),
?SQL("insert into pubsub_state(stateid,jid,nodeid,affiliation,subscriptions)\n"
" values (%(STATEID)s, %(JID)s, %(NODEID)s, %(AFFILIATION)s, %(SUBSCRIPTIONS)s);")];
(_Host, _R) ->
[]
end},
%% REVIEW:
%% * Parents is not migrated to PARENTs
%% * Probably some option VALs are not correctly represented in mysql
{pubsub_node,
fun(_Host, #pubsub_node{nodeid = {Hostid, Nodeid},
id = Id,
parents = _Parents,
type = Type,
owners = Owners,
options = Options}) ->
HOST = case Hostid of
{U,S,R} -> ejabberd_sql:escape(jid:encode({U,S,R}));
_ -> ejabberd_sql:escape(Hostid)
end,
NODE = ejabberd_sql:escape(Nodeid),
PARENT = <<"">>,
IdB = integer_to_binary(Id),
TYPE = ejabberd_sql:escape(<<Type/binary, "_odbc">>),
[?SQL("delete from pubsub_node where nodeid=%(Id)d;"),
?SQL("insert into pubsub_node(host,node,nodeid,parent,type) \n"
" values (%(HOST)s, %(NODE)s, %(Id)d, %(PARENT)s, %(TYPE)s);"),
?SQL("delete from pubsub_node_option where nodeid=%(Id)d;"),
[["insert into pubsub_node_option(nodeid,name,val)\n"
" values (", IdB, ", '", atom_to_list(Name), "', '",
io_lib:format("~p", [Val]), "');\n"] || {Name,Val} <- Options],
?SQL("delete from pubsub_node_owner where nodeid=%(Id)d;"),
[["insert into pubsub_node_owner(nodeid,owner)\n"
" values (", IdB, ", '", jid:encode(Usr), "');\n"] || Usr <- Owners],"\n"];
(_Host, _R) ->
[]
end}].
parse_subscriptions([]) ->
"";
parse_subscriptions([{State, Item}]) ->
STATE = case State of
subscribed -> "s"
end,
string:join([STATE, Item],":").