]> granicus.if.org Git - ejabberd/commitdiff
Cleanup pubsub subscriptions quering, fix pep case
authorChristophe Romain <christophe.romain@process-one.net>
Wed, 15 Nov 2017 10:16:15 +0000 (11:16 +0100)
committerChristophe Romain <christophe.romain@process-one.net>
Wed, 15 Nov 2017 10:16:15 +0000 (11:16 +0100)
src/mod_pubsub.erl
src/node_flat.erl
src/node_flat_sql.erl
src/node_pep.erl
src/node_pep_sql.erl

index e065bdaae8ca604abdda2447b6a43fde43ccf87b..c7e433d466d620e459bb55e298630052a08a912f 100644 (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
                                <<>> ->
index 18d4f4745bebe98d48e931fbc99d15afedf1ede8..bc529e145e0eebed8436714cf3a1cf99c5228749 100644 (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)}.
 
index 8057cf2e16e6fac343c3442544fa6f4e4073e9a6..5995d9fa3a198361317333fd81a04ab99388f86f 100644 (file)
     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);
         _ ->
             []
index cc0dd41fb458b5404ae393c08d0b296e57c4faa9..08c84ce9af2a76125cc688c285a8db4b31af9659 100644 (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,
index b84c945bd22b99342b26dcedc687849e6e2d31b0..07ff24b51ee429df7c721665f0e078e64758ba82 100644 (file)
 -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).