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};
{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)},
{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),