Cleanup pubsub subscriptions quering, fix pep case

This commit is contained in:
Christophe Romain 2017-11-15 11:16:15 +01:00
parent 11ee896f10
commit e706e24b92
5 changed files with 117 additions and 154 deletions

View File

@ -2333,8 +2333,6 @@ get_subscriptions(Host, Node, JID, Plugins) when is_list(Plugins) ->
case Result of
{ok, Subs} ->
Entities = lists:flatmap(fun
({_, none}) ->
[];
({#pubsub_node{nodeid = {_, SubsNode}}, Sub}) ->
case Node of
<<>> ->
@ -2344,8 +2342,6 @@ get_subscriptions(Host, Node, JID, Plugins) when is_list(Plugins) ->
_ ->
[]
end;
({_, none, _}) ->
[];
({#pubsub_node{nodeid = {_, SubsNode}}, Sub, SubId, SubJID}) ->
case Node of
<<>> ->

View File

@ -573,17 +573,10 @@ get_entity_subscriptions(Host, Owner) ->
get_node_subscriptions(Nidx) ->
{result, States} = get_states(Nidx),
Tr = fun (#pubsub_state{stateid = {J, _}, subscriptions = Subscriptions}) ->
case Subscriptions of
[_ | _] ->
lists:foldl(fun ({S, SubId}, Acc) ->
[{J, S, SubId} | Acc]
end,
[], Subscriptions);
[] ->
[];
_ ->
[{J, none}]
end
lists:foldl(fun ({S, SubId}, Acc) ->
[{J, S, SubId} | Acc]
end,
[], Subscriptions)
end,
{result, lists:flatmap(Tr, States)}.

View File

@ -53,12 +53,10 @@
path_to_node/1,
get_entity_subscriptions_for_send_last/2, get_last_items/3]).
-export([decode_jid/1, encode_jid/1,
encode_jid_like/1,
decode_affiliation/1, decode_subscriptions/1,
encode_affiliation/1, encode_subscriptions/1,
encode_host/1,
encode_host_like/1]).
-export([decode_jid/1, encode_jid/1, encode_jid_like/1,
decode_affiliation/1, decode_subscriptions/1,
encode_affiliation/1, encode_subscriptions/1,
encode_host/1, encode_host_like/1]).
init(_Host, _ServerHost, _Opts) ->
%%pubsub_subscription_sql:init(Host, ServerHost, Opts),
@ -387,25 +385,21 @@ get_entity_subscriptions(Host, Owner) ->
SubKey = jid:tolower(Owner),
GenKey = jid:remove_resource(SubKey),
H = encode_host(Host),
SJ = encode_jid(SubKey),
GJ = encode_jid(GenKey),
GJLike = <<(encode_jid_like(GenKey))/binary, "/%">>,
Query =
case SubKey of
GenKey ->
?SQL("select @(node)s, @(type)s, @(i.nodeid)d,"
" @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n "
"where i.nodeid = n.nodeid and "
"(jid=%(GJ)s or jid like %(GJLike)s escape '^')"
" and host=%(H)s");
_ ->
?SQL("select @(node)s, @(type)s, @(i.nodeid)d,"
" @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n "
"where i.nodeid = n.nodeid and"
" jid in (%(SJ)s, %(GJ)s) and host=%(H)s")
end,
Query = case SubKey of
GenKey ->
GJLike = <<(encode_jid_like(GenKey))/binary, "/%">>,
?SQL("select @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n "
"where i.nodeid = n.nodeid and "
"(jid=%(GJ)s or jid like %(GJLike)s escape '^') and host=%(H)s");
_ ->
SJ = encode_jid(SubKey),
?SQL("select @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n "
"where i.nodeid = n.nodeid and "
"jid in (%(SJ)s, %(GJ)s) and host=%(H)s")
end,
{result,
case ejabberd_sql:sql_query_t(Query) of
{selected, RItems} ->
@ -413,15 +407,10 @@ get_entity_subscriptions(Host, Owner) ->
fun({N, T, I, J, S}, Acc) ->
Node = nodetree_tree_sql:raw_to_node(Host, {N, <<"">>, T, I}),
Jid = decode_jid(J),
case decode_subscriptions(S) of
[] ->
[{Node, none, Jid} | Acc];
Subs ->
lists:foldl(
fun({Sub, SubId}, Acc2) ->
[{Node, Sub, SubId, Jid} | Acc2]
end, Acc, Subs)
end
lists:foldl(
fun({Sub, SubId}, Acc2) ->
[{Node, Sub, SubId, Jid} | Acc2]
end, Acc, decode_subscriptions(S))
end, [], RItems);
_ ->
[]
@ -438,27 +427,23 @@ get_entity_subscriptions_for_send_last(Host, Owner) ->
SubKey = jid:tolower(Owner),
GenKey = jid:remove_resource(SubKey),
H = encode_host(Host),
SJ = encode_jid(SubKey),
GJ = encode_jid(GenKey),
GJLike = <<(encode_jid_like(GenKey))/binary, "/%">>,
Query =
case SubKey of
GenKey ->
?SQL("select @(node)s, @(type)s, @(i.nodeid)d,"
" @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and name='send_last_published_item' "
"and val='on_sub_and_presence' and "
"(jid=%(GJ)s or jid like %(GJLike)s escape '^')"
" and host=%(H)s");
_ ->
?SQL("select @(node)s, @(type)s, @(i.nodeid)d,"
" @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and name='send_last_published_item' "
"and val='on_sub_and_presence' and"
" jid in (%(SJ)s, %(GJ)s) and host=%(H)s")
end,
Query = case SubKey of
GenKey ->
GJLike = <<(encode_jid_like(GenKey))/binary, "/%">>,
?SQL("select @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and "
"name='send_last_published_item' and val='on_sub_and_presence' and "
"(jid=%(GJ)s or jid like %(GJLike)s escape '^') and host=%(H)s");
_ ->
SJ = encode_jid(SubKey),
?SQL("select @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and "
"name='send_last_published_item' and val='on_sub_and_presence' and "
"jid in (%(SJ)s, %(GJ)s) and host=%(H)s")
end,
{result,
case ejabberd_sql:sql_query_t(Query) of
{selected, RItems} ->
@ -466,15 +451,10 @@ get_entity_subscriptions_for_send_last(Host, Owner) ->
fun ({N, T, I, J, S}, Acc) ->
Node = nodetree_tree_sql:raw_to_node(Host, {N, <<"">>, T, I}),
Jid = decode_jid(J),
case decode_subscriptions(S) of
[] ->
[{Node, none, Jid} | Acc];
Subs ->
lists:foldl(
fun ({Sub, SubId}, Acc2) ->
[{Node, Sub, SubId, Jid}| Acc2]
end, Acc, Subs)
end
lists:foldl(
fun ({Sub, SubId}, Acc2) ->
[{Node, Sub, SubId, Jid}| Acc2]
end, Acc, decode_subscriptions(S))
end, [], RItems);
_ ->
[]
@ -489,15 +469,10 @@ get_node_subscriptions(Nidx) ->
lists:foldl(
fun ({J, S}, Acc) ->
Jid = decode_jid(J),
case decode_subscriptions(S) of
[] ->
[{Jid, none} | Acc];
Subs ->
lists:foldl(
fun ({Sub, SubId}, Acc2) ->
[{Jid, Sub, SubId} | Acc2]
end, Acc, Subs)
end
lists:foldl(
fun ({Sub, SubId}, Acc2) ->
[{Jid, Sub, SubId} | Acc2]
end, Acc, decode_subscriptions(S))
end, [], RItems);
_ ->
[]

View File

@ -31,7 +31,6 @@
-author('christophe.romain@process-one.net').
-include("pubsub.hrl").
-include("logger.hrl").
-export([init/3, terminate/2, options/0, features/0,
create_node_permission/6, create_node/2, delete_node/1,

View File

@ -30,8 +30,10 @@
-behaviour(gen_pubsub_node).
-author('christophe.romain@process-one.net').
-compile([{parse_transform, ejabberd_sql_pt}]).
-include("pubsub.hrl").
-include("logger.hrl").
-include("ejabberd_sql_pt.hrl").
-export([init/3, terminate/2, options/0, features/0,
create_node_permission/6, create_node/2, delete_node/1,
@ -116,78 +118,76 @@ set_affiliation(Nidx, Owner, Affiliation) ->
get_entity_subscriptions(_Host, Owner) ->
SubKey = jid:tolower(Owner),
GenKey = jid:remove_resource(SubKey),
HostLike = node_flat_sql:encode_host_like(element(2, SubKey)),
SJ = ejabberd_sql:escape(node_flat_sql:encode_jid(SubKey)),
GJ = ejabberd_sql:escape(node_flat_sql:encode_jid(GenKey)),
GJLike = ejabberd_sql:escape(node_flat_sql:encode_jid_like(GenKey)),
HLike = <<"%@", (node_flat_sql:encode_host_like(element(2, SubKey)))/binary>>,
GJ = node_flat_sql:encode_jid(GenKey),
Query = case SubKey of
GenKey ->
[<<"select host, node, type, i.nodeid, jid, "
"subscriptions from pubsub_state i, pubsub_node n "
"where i.nodeid = n.nodeid and jid "
"like '">>, GJLike, <<"%' escape '^' and host like '%@">>, HostLike, <<"' escape '^';">>];
_ ->
[<<"select host, node, type, i.nodeid, jid, "
"subscriptions from pubsub_state i, pubsub_node n "
"where i.nodeid = n.nodeid and jid "
"in ('">>, SJ, <<"', '">>, GJ, <<"') and host like '%@">>, HostLike, <<"' escape '^';">>]
end,
Reply = case catch ejabberd_sql:sql_query_t(Query) of
{selected,
[<<"host">>, <<"node">>, <<"type">>, <<"nodeid">>, <<"jid">>, <<"subscriptions">>],
RItems} ->
lists:map(fun ([H, N, T, I, J, S]) ->
O = node_flat_sql:decode_jid(H),
Node = nodetree_tree_sql:raw_to_node(O, [N, <<"">>, T, I]),
{Node,
node_flat_sql:decode_subscriptions(S),
node_flat_sql:decode_jid(J)}
end,
RItems);
_ ->
[]
end,
{result, Reply}.
GenKey ->
GJLike = <<(node_flat_sql:encode_jid_like(GenKey))/binary, "/%">>,
?SQL("select @(host)s, @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n "
"where i.nodeid = n.nodeid and "
"(jid=%(GJ)s or jid like %(GJLike)s escape '^') and host like %(HLike)s escape '^'");
_ ->
SJ = node_flat_sql:encode_jid(SubKey),
?SQL("select @(host)s, @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n "
"where i.nodeid = n.nodeid and "
"jid in (%(SJ)s,%(GJ)s) and host like %(HLike)s escape '^'")
end,
{result,
case ejabberd_sql:sql_query_t(Query) of
{selected, RItems} ->
lists:foldl(
fun({H, N, T, I, J, S}, Acc) ->
O = node_flat_sql:decode_jid(H),
Node = nodetree_tree_sql:raw_to_node(O, {N, <<"">>, T, I}),
Jid = node_flat_sql:decode_jid(J),
lists:foldl(
fun({Sub, SubId}, Acc2) ->
[{Node, Sub, SubId, Jid} | Acc2]
end, Acc, node_flat_sql:decode_subscriptions(S))
end, [], RItems);
_ ->
[]
end}.
get_entity_subscriptions_for_send_last(_Host, Owner) ->
SubKey = jid:tolower(Owner),
GenKey = jid:remove_resource(SubKey),
HostLike = node_flat_sql:encode_host_like(element(2, SubKey)),
SJ = ejabberd_sql:escape(node_flat_sql:encode_jid(SubKey)),
GJ = ejabberd_sql:escape(node_flat_sql:encode_jid(GenKey)),
GJLike = ejabberd_sql:escape(node_flat_sql:encode_jid_like(GenKey)),
HLike = <<"%@", (node_flat_sql:encode_host_like(element(2, SubKey)))/binary>>,
GJ = node_flat_sql:encode_jid(GenKey),
Query = case SubKey of
GenKey ->
[<<"select host, node, type, i.nodeid, jid, "
"subscriptions from pubsub_state i, pubsub_node n, "
"pubsub_node_option o where i.nodeid = n.nodeid "
"and n.nodeid = o.nodeid and name='send_last_published_item' and "
"val='on_sub_and_presence' and jid like '">>,
GJLike, <<"%' escape '^' and host like '%@">>, HostLike, <<"' escape '^';">>];
_ ->
[<<"select host, node, type, i.nodeid, jid, "
"subscriptions from pubsub_state i, pubsub_node n, "
"pubsub_node_option o where i.nodeid = n.nodeid "
"and n.nodeid = o.nodeid and name='send_last_published_item' and "
"val='on_sub_and_presence' and jid in ",
"('">>, SJ, <<"', '">>, GJ, <<"') and host like '%@">>, HostLike, <<"' escape '^';">>]
end,
Reply = case catch ejabberd_sql:sql_query_t(Query) of
{selected,
[<<"host">>, <<"node">>, <<"type">>, <<"nodeid">>, <<"jid">>, <<"subscriptions">>],
RItems} ->
lists:map(fun ([H, N, T, I, J, S]) ->
O = node_flat_sql:decode_jid(H),
Node = nodetree_tree_sql:raw_to_node(O, [N, <<"">>, T, I]),
{Node,
node_flat_sql:decode_subscriptions(S),
node_flat_sql:decode_jid(J)}
end,
RItems);
_ ->
[]
end,
{result, Reply}.
GenKey ->
GJLike = <<(node_flat_sql:encode_jid_like(GenKey))/binary, "/%">>,
?SQL("select @(host)s, @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and "
"name='send_last_published_item' and val='on_sub_and_presence' and "
"(jid=%(GJ)s or jid like %(GJLike)s escape '^') and host like %(HLike)s escape '^'");
_ ->
SJ = node_flat_sql:encode_jid(SubKey),
?SQL("select @(host)s, @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and "
"name='send_last_published_item' and val='on_sub_and_presence' and "
"jid in (%(SJ)s,%(GJ)s) and host like %(HLike)s escape '^'")
end,
{result,
case ejabberd_sql:sql_query_t(Query) of
{selected, RItems} ->
lists:foldl(
fun ({H, N, T, I, J, S}, Acc) ->
O = node_flat_sql:decode_jid(H),
Node = nodetree_tree_sql:raw_to_node(O, {N, <<"">>, T, I}),
Jid = node_flat_sql:decode_jid(J),
lists:foldl(
fun ({Sub, SubId}, Acc2) ->
[{Node, Sub, SubId, Jid}| Acc2]
end, Acc, node_flat_sql:decode_subscriptions(S))
end, [], RItems);
_ ->
[]
end}.
get_node_subscriptions(Nidx) ->
node_flat_sql:get_node_subscriptions(Nidx).