error ->
{error, ?ERR_BAD_REQUEST};
_ ->
- Action = fun(#pubsub_node{type = Type, owners = Owners}) ->
- case lists:member(Owner, Owners) of
- true ->
- lists:foreach(
- fun({JID, Affiliation}) ->
- node_call(
- Type, set_affiliation,
- [Host, Node, JID, Affiliation])
- end, Entities),
- {result, []};
- _ ->
- {error, ?ERR_FORBIDDEN}
- end
+ Action = fun(#pubsub_node{type = Type, owners = Owners}=N) ->
+ case lists:member(Owner, Owners) of
+ true ->
+ lists:foreach(
+ fun({JID, Affiliation}) ->
+ node_call(Type, set_affiliation, [Host, Node, JID, Affiliation]),
+ case Affiliation of
+ owner ->
+ NewOwner = jlib:jid_tolower(jlib:jid_remove_resource(JID)),
+ NewOwners = [NewOwner|Owners],
+ tree_call(Host, set_node, [N#pubsub_node{owners = NewOwners}]);
+ none ->
+ OldOwner = jlib:jid_tolower(jlib:jid_remove_resource(JID)),
+ case lists:member(OldOwner, Owners) of
+ true ->
+ NewOwners = Owners--[OldOwner],
+ tree_call(Host, set_node, [N#pubsub_node{owners = NewOwners}]);
+ _ ->
+ ok
+ end;
+ _ ->
+ ok
+ end
+ end, Entities),
+ {result, []};
+ _ ->
+ {error, ?ERR_FORBIDDEN}
+ end
end,
transaction(Host, Node, Action, sync_dirty)
end.
unsubscribe_node(Host, Node, Sender, Subscriber, _SubId) ->
SubKey = jlib:jid_tolower(Subscriber),
GenKey = jlib:jid_remove_resource(SubKey),
- Authorized = (jlib:jid_tolower(jlib:jid_remove_resource(Sender)) == GenKey),
+ SenderKey = jlib:jid_tolower(jlib:jid_remove_resource(Sender)),
GenState = get_state(Host, Node, GenKey),
SubState = case SubKey of
GenKey -> GenState;
_ -> get_state(Host, Node, SubKey)
end,
- Affiliation = GenState#pubsub_state.affiliation,
- Subscription = SubState#pubsub_state.subscription,
+ Authorized = case SenderKey of
+ GenKey ->
+ true;
+ _ ->
+ SenderState = get_state(Host, Node, SenderKey),
+ SenderState#pubsub_state.affiliation == owner
+ end,
if
%% Entity did not specify SubID
%%SubID == "", ?? ->
%%InvalidSubID ->
%% {error, ?ERR_EXTENDED(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
%% Requesting entity is not a subscriber
- Subscription == none ->
+ SubState#pubsub_state.subscription == none ->
{error, ?ERR_EXTENDED(?ERR_UNEXPECTED_REQUEST, "not-subscribed")};
%% Requesting entity is prohibited from unsubscribing entity
- (not Authorized) and (Affiliation =/= owner) ->
+ not Authorized ->
{error, ?ERR_FORBIDDEN};
%% Was just subscriber, remove the record
SubState#pubsub_state.affiliation == none ->
SubKey = jlib:jid_tolower(Owner),
GenKey = jlib:jid_remove_resource(SubKey),
GenState = get_state(Host, Node, GenKey),
- set_state(GenState#pubsub_state{affiliation = Affiliation}),
+ case {Affiliation, GenState#pubsub_state.subscription} of
+ {none, none} ->
+ del_state(GenState#pubsub_state.stateid);
+ _ ->
+ set_state(GenState#pubsub_state{affiliation = Affiliation})
+ end,
ok.
%% @spec (Host, Owner) -> [{Node,Subscription}]
set_subscription(Host, Node, Owner, Subscription) ->
SubKey = jlib:jid_tolower(Owner),
SubState = get_state(Host, Node, SubKey),
- set_state(SubState#pubsub_state{subscription = Subscription}),
+ case {Subscription, SubState#pubsub_state.affiliation} of
+ {none, none} ->
+ del_state(SubState#pubsub_state.stateid);
+ _ ->
+ set_state(SubState#pubsub_state{subscription = Subscription})
+ end,
ok.
%% @spec (Host, Node) -> [States] | []
%%InvalidSubID ->
%% Entity is subscribed but specifies an invalid subscription ID
%{error, ?ERR_EXTENDED(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
- Affiliation == outcast ->
+ GenState#pubsub_state.affiliation == outcast ->
%% Requesting entity is blocked
{error, ?ERR_FORBIDDEN};
(AccessModel == presence) and (not PresenceSubscription) ->
%%InvalidSubID ->
%% Entity is subscribed but specifies an invalid subscription ID
%{error, ?ERR_EXTENDED(?ERR_NOT_ACCEPTABLE, "invalid-subid")};
- Affiliation == outcast ->
+ GenState#pubsub_state.affiliation == outcast ->
%% Requesting entity is blocked
{error, ?ERR_FORBIDDEN};
(AccessModel == presence) and (not PresenceSubscription) ->