Cleanup pubsub subscriptions quering, fix pep case
This commit is contained in:
parent
11ee896f10
commit
e706e24b92
|
@ -2333,8 +2333,6 @@ get_subscriptions(Host, Node, JID, Plugins) when is_list(Plugins) ->
|
||||||
case Result of
|
case Result of
|
||||||
{ok, Subs} ->
|
{ok, Subs} ->
|
||||||
Entities = lists:flatmap(fun
|
Entities = lists:flatmap(fun
|
||||||
({_, none}) ->
|
|
||||||
[];
|
|
||||||
({#pubsub_node{nodeid = {_, SubsNode}}, Sub}) ->
|
({#pubsub_node{nodeid = {_, SubsNode}}, Sub}) ->
|
||||||
case Node of
|
case Node of
|
||||||
<<>> ->
|
<<>> ->
|
||||||
|
@ -2344,8 +2342,6 @@ get_subscriptions(Host, Node, JID, Plugins) when is_list(Plugins) ->
|
||||||
_ ->
|
_ ->
|
||||||
[]
|
[]
|
||||||
end;
|
end;
|
||||||
({_, none, _}) ->
|
|
||||||
[];
|
|
||||||
({#pubsub_node{nodeid = {_, SubsNode}}, Sub, SubId, SubJID}) ->
|
({#pubsub_node{nodeid = {_, SubsNode}}, Sub, SubId, SubJID}) ->
|
||||||
case Node of
|
case Node of
|
||||||
<<>> ->
|
<<>> ->
|
||||||
|
|
|
@ -573,17 +573,10 @@ get_entity_subscriptions(Host, Owner) ->
|
||||||
get_node_subscriptions(Nidx) ->
|
get_node_subscriptions(Nidx) ->
|
||||||
{result, States} = get_states(Nidx),
|
{result, States} = get_states(Nidx),
|
||||||
Tr = fun (#pubsub_state{stateid = {J, _}, subscriptions = Subscriptions}) ->
|
Tr = fun (#pubsub_state{stateid = {J, _}, subscriptions = Subscriptions}) ->
|
||||||
case Subscriptions of
|
lists:foldl(fun ({S, SubId}, Acc) ->
|
||||||
[_ | _] ->
|
[{J, S, SubId} | Acc]
|
||||||
lists:foldl(fun ({S, SubId}, Acc) ->
|
end,
|
||||||
[{J, S, SubId} | Acc]
|
[], Subscriptions)
|
||||||
end,
|
|
||||||
[], Subscriptions);
|
|
||||||
[] ->
|
|
||||||
[];
|
|
||||||
_ ->
|
|
||||||
[{J, none}]
|
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
{result, lists:flatmap(Tr, States)}.
|
{result, lists:flatmap(Tr, States)}.
|
||||||
|
|
||||||
|
|
|
@ -53,12 +53,10 @@
|
||||||
path_to_node/1,
|
path_to_node/1,
|
||||||
get_entity_subscriptions_for_send_last/2, get_last_items/3]).
|
get_entity_subscriptions_for_send_last/2, get_last_items/3]).
|
||||||
|
|
||||||
-export([decode_jid/1, encode_jid/1,
|
-export([decode_jid/1, encode_jid/1, encode_jid_like/1,
|
||||||
encode_jid_like/1,
|
decode_affiliation/1, decode_subscriptions/1,
|
||||||
decode_affiliation/1, decode_subscriptions/1,
|
encode_affiliation/1, encode_subscriptions/1,
|
||||||
encode_affiliation/1, encode_subscriptions/1,
|
encode_host/1, encode_host_like/1]).
|
||||||
encode_host/1,
|
|
||||||
encode_host_like/1]).
|
|
||||||
|
|
||||||
init(_Host, _ServerHost, _Opts) ->
|
init(_Host, _ServerHost, _Opts) ->
|
||||||
%%pubsub_subscription_sql:init(Host, ServerHost, Opts),
|
%%pubsub_subscription_sql:init(Host, ServerHost, Opts),
|
||||||
|
@ -387,25 +385,21 @@ get_entity_subscriptions(Host, Owner) ->
|
||||||
SubKey = jid:tolower(Owner),
|
SubKey = jid:tolower(Owner),
|
||||||
GenKey = jid:remove_resource(SubKey),
|
GenKey = jid:remove_resource(SubKey),
|
||||||
H = encode_host(Host),
|
H = encode_host(Host),
|
||||||
SJ = encode_jid(SubKey),
|
|
||||||
GJ = encode_jid(GenKey),
|
GJ = encode_jid(GenKey),
|
||||||
GJLike = <<(encode_jid_like(GenKey))/binary, "/%">>,
|
Query = case SubKey of
|
||||||
Query =
|
GenKey ->
|
||||||
case SubKey of
|
GJLike = <<(encode_jid_like(GenKey))/binary, "/%">>,
|
||||||
GenKey ->
|
?SQL("select @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
|
||||||
?SQL("select @(node)s, @(type)s, @(i.nodeid)d,"
|
"from pubsub_state i, pubsub_node n "
|
||||||
" @(jid)s, @(subscriptions)s "
|
"where i.nodeid = n.nodeid and "
|
||||||
"from pubsub_state i, pubsub_node n "
|
"(jid=%(GJ)s or jid like %(GJLike)s escape '^') and host=%(H)s");
|
||||||
"where i.nodeid = n.nodeid and "
|
_ ->
|
||||||
"(jid=%(GJ)s or jid like %(GJLike)s escape '^')"
|
SJ = encode_jid(SubKey),
|
||||||
" and host=%(H)s");
|
?SQL("select @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
|
||||||
_ ->
|
"from pubsub_state i, pubsub_node n "
|
||||||
?SQL("select @(node)s, @(type)s, @(i.nodeid)d,"
|
"where i.nodeid = n.nodeid and "
|
||||||
" @(jid)s, @(subscriptions)s "
|
"jid in (%(SJ)s, %(GJ)s) and host=%(H)s")
|
||||||
"from pubsub_state i, pubsub_node n "
|
end,
|
||||||
"where i.nodeid = n.nodeid and"
|
|
||||||
" jid in (%(SJ)s, %(GJ)s) and host=%(H)s")
|
|
||||||
end,
|
|
||||||
{result,
|
{result,
|
||||||
case ejabberd_sql:sql_query_t(Query) of
|
case ejabberd_sql:sql_query_t(Query) of
|
||||||
{selected, RItems} ->
|
{selected, RItems} ->
|
||||||
|
@ -413,15 +407,10 @@ get_entity_subscriptions(Host, Owner) ->
|
||||||
fun({N, T, I, J, S}, Acc) ->
|
fun({N, T, I, J, S}, Acc) ->
|
||||||
Node = nodetree_tree_sql:raw_to_node(Host, {N, <<"">>, T, I}),
|
Node = nodetree_tree_sql:raw_to_node(Host, {N, <<"">>, T, I}),
|
||||||
Jid = decode_jid(J),
|
Jid = decode_jid(J),
|
||||||
case decode_subscriptions(S) of
|
lists:foldl(
|
||||||
[] ->
|
fun({Sub, SubId}, Acc2) ->
|
||||||
[{Node, none, Jid} | Acc];
|
[{Node, Sub, SubId, Jid} | Acc2]
|
||||||
Subs ->
|
end, Acc, decode_subscriptions(S))
|
||||||
lists:foldl(
|
|
||||||
fun({Sub, SubId}, Acc2) ->
|
|
||||||
[{Node, Sub, SubId, Jid} | Acc2]
|
|
||||||
end, Acc, Subs)
|
|
||||||
end
|
|
||||||
end, [], RItems);
|
end, [], RItems);
|
||||||
_ ->
|
_ ->
|
||||||
[]
|
[]
|
||||||
|
@ -438,27 +427,23 @@ get_entity_subscriptions_for_send_last(Host, Owner) ->
|
||||||
SubKey = jid:tolower(Owner),
|
SubKey = jid:tolower(Owner),
|
||||||
GenKey = jid:remove_resource(SubKey),
|
GenKey = jid:remove_resource(SubKey),
|
||||||
H = encode_host(Host),
|
H = encode_host(Host),
|
||||||
SJ = encode_jid(SubKey),
|
|
||||||
GJ = encode_jid(GenKey),
|
GJ = encode_jid(GenKey),
|
||||||
GJLike = <<(encode_jid_like(GenKey))/binary, "/%">>,
|
Query = case SubKey of
|
||||||
Query =
|
GenKey ->
|
||||||
case SubKey of
|
GJLike = <<(encode_jid_like(GenKey))/binary, "/%">>,
|
||||||
GenKey ->
|
?SQL("select @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
|
||||||
?SQL("select @(node)s, @(type)s, @(i.nodeid)d,"
|
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
|
||||||
" @(jid)s, @(subscriptions)s "
|
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and "
|
||||||
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
|
"name='send_last_published_item' and val='on_sub_and_presence' and "
|
||||||
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and name='send_last_published_item' "
|
"(jid=%(GJ)s or jid like %(GJLike)s escape '^') and host=%(H)s");
|
||||||
"and val='on_sub_and_presence' and "
|
_ ->
|
||||||
"(jid=%(GJ)s or jid like %(GJLike)s escape '^')"
|
SJ = encode_jid(SubKey),
|
||||||
" 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 "
|
||||||
?SQL("select @(node)s, @(type)s, @(i.nodeid)d,"
|
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and "
|
||||||
" @(jid)s, @(subscriptions)s "
|
"name='send_last_published_item' and val='on_sub_and_presence' and "
|
||||||
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
|
"jid in (%(SJ)s, %(GJ)s) and host=%(H)s")
|
||||||
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and name='send_last_published_item' "
|
end,
|
||||||
"and val='on_sub_and_presence' and"
|
|
||||||
" jid in (%(SJ)s, %(GJ)s) and host=%(H)s")
|
|
||||||
end,
|
|
||||||
{result,
|
{result,
|
||||||
case ejabberd_sql:sql_query_t(Query) of
|
case ejabberd_sql:sql_query_t(Query) of
|
||||||
{selected, RItems} ->
|
{selected, RItems} ->
|
||||||
|
@ -466,15 +451,10 @@ get_entity_subscriptions_for_send_last(Host, Owner) ->
|
||||||
fun ({N, T, I, J, S}, Acc) ->
|
fun ({N, T, I, J, S}, Acc) ->
|
||||||
Node = nodetree_tree_sql:raw_to_node(Host, {N, <<"">>, T, I}),
|
Node = nodetree_tree_sql:raw_to_node(Host, {N, <<"">>, T, I}),
|
||||||
Jid = decode_jid(J),
|
Jid = decode_jid(J),
|
||||||
case decode_subscriptions(S) of
|
lists:foldl(
|
||||||
[] ->
|
fun ({Sub, SubId}, Acc2) ->
|
||||||
[{Node, none, Jid} | Acc];
|
[{Node, Sub, SubId, Jid}| Acc2]
|
||||||
Subs ->
|
end, Acc, decode_subscriptions(S))
|
||||||
lists:foldl(
|
|
||||||
fun ({Sub, SubId}, Acc2) ->
|
|
||||||
[{Node, Sub, SubId, Jid}| Acc2]
|
|
||||||
end, Acc, Subs)
|
|
||||||
end
|
|
||||||
end, [], RItems);
|
end, [], RItems);
|
||||||
_ ->
|
_ ->
|
||||||
[]
|
[]
|
||||||
|
@ -489,15 +469,10 @@ get_node_subscriptions(Nidx) ->
|
||||||
lists:foldl(
|
lists:foldl(
|
||||||
fun ({J, S}, Acc) ->
|
fun ({J, S}, Acc) ->
|
||||||
Jid = decode_jid(J),
|
Jid = decode_jid(J),
|
||||||
case decode_subscriptions(S) of
|
lists:foldl(
|
||||||
[] ->
|
fun ({Sub, SubId}, Acc2) ->
|
||||||
[{Jid, none} | Acc];
|
[{Jid, Sub, SubId} | Acc2]
|
||||||
Subs ->
|
end, Acc, decode_subscriptions(S))
|
||||||
lists:foldl(
|
|
||||||
fun ({Sub, SubId}, Acc2) ->
|
|
||||||
[{Jid, Sub, SubId} | Acc2]
|
|
||||||
end, Acc, Subs)
|
|
||||||
end
|
|
||||||
end, [], RItems);
|
end, [], RItems);
|
||||||
_ ->
|
_ ->
|
||||||
[]
|
[]
|
||||||
|
|
|
@ -31,7 +31,6 @@
|
||||||
-author('christophe.romain@process-one.net').
|
-author('christophe.romain@process-one.net').
|
||||||
|
|
||||||
-include("pubsub.hrl").
|
-include("pubsub.hrl").
|
||||||
-include("logger.hrl").
|
|
||||||
|
|
||||||
-export([init/3, terminate/2, options/0, features/0,
|
-export([init/3, terminate/2, options/0, features/0,
|
||||||
create_node_permission/6, create_node/2, delete_node/1,
|
create_node_permission/6, create_node/2, delete_node/1,
|
||||||
|
|
|
@ -30,8 +30,10 @@
|
||||||
-behaviour(gen_pubsub_node).
|
-behaviour(gen_pubsub_node).
|
||||||
-author('christophe.romain@process-one.net').
|
-author('christophe.romain@process-one.net').
|
||||||
|
|
||||||
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
||||||
|
|
||||||
-include("pubsub.hrl").
|
-include("pubsub.hrl").
|
||||||
-include("logger.hrl").
|
-include("ejabberd_sql_pt.hrl").
|
||||||
|
|
||||||
-export([init/3, terminate/2, options/0, features/0,
|
-export([init/3, terminate/2, options/0, features/0,
|
||||||
create_node_permission/6, create_node/2, delete_node/1,
|
create_node_permission/6, create_node/2, delete_node/1,
|
||||||
|
@ -116,78 +118,76 @@ set_affiliation(Nidx, Owner, Affiliation) ->
|
||||||
get_entity_subscriptions(_Host, Owner) ->
|
get_entity_subscriptions(_Host, Owner) ->
|
||||||
SubKey = jid:tolower(Owner),
|
SubKey = jid:tolower(Owner),
|
||||||
GenKey = jid:remove_resource(SubKey),
|
GenKey = jid:remove_resource(SubKey),
|
||||||
HostLike = node_flat_sql:encode_host_like(element(2, SubKey)),
|
HLike = <<"%@", (node_flat_sql:encode_host_like(element(2, SubKey)))/binary>>,
|
||||||
SJ = ejabberd_sql:escape(node_flat_sql:encode_jid(SubKey)),
|
GJ = node_flat_sql:encode_jid(GenKey),
|
||||||
GJ = ejabberd_sql:escape(node_flat_sql:encode_jid(GenKey)),
|
|
||||||
GJLike = ejabberd_sql:escape(node_flat_sql:encode_jid_like(GenKey)),
|
|
||||||
Query = case SubKey of
|
Query = case SubKey of
|
||||||
GenKey ->
|
GenKey ->
|
||||||
[<<"select host, node, type, i.nodeid, jid, "
|
GJLike = <<(node_flat_sql:encode_jid_like(GenKey))/binary, "/%">>,
|
||||||
"subscriptions from pubsub_state i, pubsub_node n "
|
?SQL("select @(host)s, @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
|
||||||
"where i.nodeid = n.nodeid and jid "
|
"from pubsub_state i, pubsub_node n "
|
||||||
"like '">>, GJLike, <<"%' escape '^' and host like '%@">>, HostLike, <<"' escape '^';">>];
|
"where i.nodeid = n.nodeid and "
|
||||||
_ ->
|
"(jid=%(GJ)s or jid like %(GJLike)s escape '^') and host like %(HLike)s escape '^'");
|
||||||
[<<"select host, node, type, i.nodeid, jid, "
|
_ ->
|
||||||
"subscriptions from pubsub_state i, pubsub_node n "
|
SJ = node_flat_sql:encode_jid(SubKey),
|
||||||
"where i.nodeid = n.nodeid and jid "
|
?SQL("select @(host)s, @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
|
||||||
"in ('">>, SJ, <<"', '">>, GJ, <<"') and host like '%@">>, HostLike, <<"' escape '^';">>]
|
"from pubsub_state i, pubsub_node n "
|
||||||
end,
|
"where i.nodeid = n.nodeid and "
|
||||||
Reply = case catch ejabberd_sql:sql_query_t(Query) of
|
"jid in (%(SJ)s,%(GJ)s) and host like %(HLike)s escape '^'")
|
||||||
{selected,
|
end,
|
||||||
[<<"host">>, <<"node">>, <<"type">>, <<"nodeid">>, <<"jid">>, <<"subscriptions">>],
|
{result,
|
||||||
RItems} ->
|
case ejabberd_sql:sql_query_t(Query) of
|
||||||
lists:map(fun ([H, N, T, I, J, S]) ->
|
{selected, RItems} ->
|
||||||
O = node_flat_sql:decode_jid(H),
|
lists:foldl(
|
||||||
Node = nodetree_tree_sql:raw_to_node(O, [N, <<"">>, T, I]),
|
fun({H, N, T, I, J, S}, Acc) ->
|
||||||
{Node,
|
O = node_flat_sql:decode_jid(H),
|
||||||
node_flat_sql:decode_subscriptions(S),
|
Node = nodetree_tree_sql:raw_to_node(O, {N, <<"">>, T, I}),
|
||||||
node_flat_sql:decode_jid(J)}
|
Jid = node_flat_sql:decode_jid(J),
|
||||||
end,
|
lists:foldl(
|
||||||
RItems);
|
fun({Sub, SubId}, Acc2) ->
|
||||||
_ ->
|
[{Node, Sub, SubId, Jid} | Acc2]
|
||||||
[]
|
end, Acc, node_flat_sql:decode_subscriptions(S))
|
||||||
end,
|
end, [], RItems);
|
||||||
{result, Reply}.
|
_ ->
|
||||||
|
[]
|
||||||
|
end}.
|
||||||
|
|
||||||
get_entity_subscriptions_for_send_last(_Host, Owner) ->
|
get_entity_subscriptions_for_send_last(_Host, Owner) ->
|
||||||
SubKey = jid:tolower(Owner),
|
SubKey = jid:tolower(Owner),
|
||||||
GenKey = jid:remove_resource(SubKey),
|
GenKey = jid:remove_resource(SubKey),
|
||||||
HostLike = node_flat_sql:encode_host_like(element(2, SubKey)),
|
HLike = <<"%@", (node_flat_sql:encode_host_like(element(2, SubKey)))/binary>>,
|
||||||
SJ = ejabberd_sql:escape(node_flat_sql:encode_jid(SubKey)),
|
GJ = node_flat_sql:encode_jid(GenKey),
|
||||||
GJ = ejabberd_sql:escape(node_flat_sql:encode_jid(GenKey)),
|
|
||||||
GJLike = ejabberd_sql:escape(node_flat_sql:encode_jid_like(GenKey)),
|
|
||||||
Query = case SubKey of
|
Query = case SubKey of
|
||||||
GenKey ->
|
GenKey ->
|
||||||
[<<"select host, node, type, i.nodeid, jid, "
|
GJLike = <<(node_flat_sql:encode_jid_like(GenKey))/binary, "/%">>,
|
||||||
"subscriptions from pubsub_state i, pubsub_node n, "
|
?SQL("select @(host)s, @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
|
||||||
"pubsub_node_option o where i.nodeid = n.nodeid "
|
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
|
||||||
"and n.nodeid = o.nodeid and name='send_last_published_item' and "
|
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and "
|
||||||
"val='on_sub_and_presence' and jid like '">>,
|
"name='send_last_published_item' and val='on_sub_and_presence' and "
|
||||||
GJLike, <<"%' escape '^' and host like '%@">>, HostLike, <<"' escape '^';">>];
|
"(jid=%(GJ)s or jid like %(GJLike)s escape '^') and host like %(HLike)s escape '^'");
|
||||||
_ ->
|
_ ->
|
||||||
[<<"select host, node, type, i.nodeid, jid, "
|
SJ = node_flat_sql:encode_jid(SubKey),
|
||||||
"subscriptions from pubsub_state i, pubsub_node n, "
|
?SQL("select @(host)s, @(node)s, @(type)s, @(i.nodeid)d, @(jid)s, @(subscriptions)s "
|
||||||
"pubsub_node_option o where i.nodeid = n.nodeid "
|
"from pubsub_state i, pubsub_node n, pubsub_node_option o "
|
||||||
"and n.nodeid = o.nodeid and name='send_last_published_item' and "
|
"where i.nodeid = n.nodeid and n.nodeid = o.nodeid and "
|
||||||
"val='on_sub_and_presence' and jid in ",
|
"name='send_last_published_item' and val='on_sub_and_presence' and "
|
||||||
"('">>, SJ, <<"', '">>, GJ, <<"') and host like '%@">>, HostLike, <<"' escape '^';">>]
|
"jid in (%(SJ)s,%(GJ)s) and host like %(HLike)s escape '^'")
|
||||||
end,
|
end,
|
||||||
Reply = case catch ejabberd_sql:sql_query_t(Query) of
|
{result,
|
||||||
{selected,
|
case ejabberd_sql:sql_query_t(Query) of
|
||||||
[<<"host">>, <<"node">>, <<"type">>, <<"nodeid">>, <<"jid">>, <<"subscriptions">>],
|
{selected, RItems} ->
|
||||||
RItems} ->
|
lists:foldl(
|
||||||
lists:map(fun ([H, N, T, I, J, S]) ->
|
fun ({H, N, T, I, J, S}, Acc) ->
|
||||||
O = node_flat_sql:decode_jid(H),
|
O = node_flat_sql:decode_jid(H),
|
||||||
Node = nodetree_tree_sql:raw_to_node(O, [N, <<"">>, T, I]),
|
Node = nodetree_tree_sql:raw_to_node(O, {N, <<"">>, T, I}),
|
||||||
{Node,
|
Jid = node_flat_sql:decode_jid(J),
|
||||||
node_flat_sql:decode_subscriptions(S),
|
lists:foldl(
|
||||||
node_flat_sql:decode_jid(J)}
|
fun ({Sub, SubId}, Acc2) ->
|
||||||
end,
|
[{Node, Sub, SubId, Jid}| Acc2]
|
||||||
RItems);
|
end, Acc, node_flat_sql:decode_subscriptions(S))
|
||||||
_ ->
|
end, [], RItems);
|
||||||
[]
|
_ ->
|
||||||
end,
|
[]
|
||||||
{result, Reply}.
|
end}.
|
||||||
|
|
||||||
get_node_subscriptions(Nidx) ->
|
get_node_subscriptions(Nidx) ->
|
||||||
node_flat_sql:get_node_subscriptions(Nidx).
|
node_flat_sql:get_node_subscriptions(Nidx).
|
||||||
|
|
Loading…
Reference in New Issue