]> granicus.if.org Git - ejabberd/commitdiff
Fix updating pending subscriptions (EJAB-980)
authorChristophe Romain <christophe.romain@process-one.net>
Fri, 17 Jul 2009 19:58:42 +0000 (19:58 +0000)
committerChristophe Romain <christophe.romain@process-one.net>
Fri, 17 Jul 2009 19:58:42 +0000 (19:58 +0000)
SVN Revision: 2367

src/mod_pubsub/mod_pubsub.erl
src/mod_pubsub/node_hometree.erl

index a1ec03e62163ff8bf98a0e18bec2d4207f265800..e97bf69b96b0db0cc2a3a300ce692d5f85a7556d 100644 (file)
@@ -1416,18 +1416,11 @@ update_auth(Host, Node, Type, NodeId, Subscriber,
     case Subscription of
        [{pending, SubID}] -> %% TODO does not work if several pending
            NewSubscription = case Allow of
-                                 true  ->
-                                     node_call(Type, set_subscriptions,
-                                               [NodeId, Subscriber,
-                                                replace_subscription({subscribed, SubID},
-                                                                     Subscriptions)]),
-                                     {subscribed, SubID};
-                                 false ->
-                                     node_call(Type, unsubscribe_node,
-                                               [NodeId, Subscriber, Subscriber,
-                                                SubID]),
-                                     none
+                                 true  -> subscribed;
+                                 false -> none
                              end,
+            node_call(Type, set_subscriptions,
+                      [NodeId, Subscriber, NewSubscription, SubID]),
            send_authorization_approval(Host, Subscriber, Node,
                                        NewSubscription),
            {result, ok};
@@ -1435,17 +1428,6 @@ update_auth(Host, Node, Type, NodeId, Subscriber,
            {error, ?ERR_UNEXPECTED_REQUEST}
     end.
 
-replace_subscription(NewSub, Subs) ->
-    lists:foldl(fun(S, A) -> replace_subscription_helper(NewSub, S, A) end,
-               [], Subs).
-
-replace_subscription_helper({none, SubID}, {_, SubID}, Acc) ->
-    Acc;
-replace_subscription_helper({NewSub, SubID}, {_, SubID}, Acc) ->
-    [{NewSub, SubID} | Acc];
-replace_subscription_helper(_, OldSub, Acc) ->
-    [OldSub | Acc].
-
 -define(XFIELD(Type, Label, Var, Val),
        {xmlelement, "field", [{"type", Type},
                               {"label", translate:translate(Lang, Label)},
index cdad0699f912264a69208580b1b9a697214a88e2..eda7fef7f2a684be351c0cdbbd04b8dfbc0202c4 100644 (file)
@@ -703,8 +703,31 @@ set_subscriptions(NodeId, Owner, none, SubId) ->
             {error, ?ERR_EXTENDED(?ERR_BAD_REQUEST, "subid-required")};
         _ ->
             unsub_with_subid(NodeId, SubId, SubState)
+    end;
+set_subscriptions(NodeId, Owner, Subscription, SubId) ->
+    SubKey = jlib:jid_tolower(Owner),
+    SubState = get_state(NodeId, SubKey),
+    case {SubId, SubState#pubsub_state.subscriptions} of
+        {_, []} ->
+            {error, ?ERR_ITEM_NOT_FOUND};
+        {"", [{_, SID}]} ->
+            replace_subscription({Subscription, SID}, SubState);
+        {"", [_|_]} ->
+            {error, ?ERR_EXTENDED(?ERR_BAD_REQUEST, "subid-required")};
+         _ ->
+            replace_subscription({Subscription, SubId}, SubState)
     end.
 
+replace_subscription(NewSub, SubState) ->
+    NewSubs = replace_subscription(NewSub,
+                                   SubState#pubsub_state.subscriptions, []),
+    set_state(SubState#pubsub_state{subscriptions = NewSubs}).
+
+replace_subscription(_, [], Acc) ->
+    Acc;
+replace_subscription({Sub, SubID}, [{_, SubID} | T], Acc) ->
+    replace_subscription({Sub, SubID}, T, [{Sub, SubID} | Acc]).
+
 unsub_with_subid(NodeId, SubId, SubState) ->
     pubsub_subscription:unsubscribe_node(SubState#pubsub_state.stateid,
                                          NodeId, SubId),