From 2729285977e5538b252d92b203556263b7c17b13 Mon Sep 17 00:00:00 2001 From: Christophe Romain Date: Fri, 17 Jul 2009 19:58:42 +0000 Subject: [PATCH] Fix updating pending subscriptions (EJAB-980) SVN Revision: 2367 --- src/mod_pubsub/mod_pubsub.erl | 26 ++++---------------------- src/mod_pubsub/node_hometree.erl | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/mod_pubsub/mod_pubsub.erl b/src/mod_pubsub/mod_pubsub.erl index a1ec03e62..e97bf69b9 100644 --- a/src/mod_pubsub/mod_pubsub.erl +++ b/src/mod_pubsub/mod_pubsub.erl @@ -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)}, diff --git a/src/mod_pubsub/node_hometree.erl b/src/mod_pubsub/node_hometree.erl index cdad0699f..eda7fef7f 100644 --- a/src/mod_pubsub/node_hometree.erl +++ b/src/mod_pubsub/node_hometree.erl @@ -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),