Skip to content

Commit 16f8003

Browse files
author
Lloyd Watkin
committed
Add tests and fix logic around affiliations retrieval
1 parent 3ff2b35 commit 16f8003

5 files changed

Lines changed: 110 additions & 108 deletions

File tree

src/main/java/org/buddycloud/channelserver/channel/LocalDomainChecker.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ public static boolean isLocal(String domain, Properties configuration, Set<Strin
2222
}
2323
String command = configuration.getProperty(
2424
Configuration.CONFIGURATION_LOCAL_DOMAIN_CHECKER);
25-
26-
if (command == null) {
25+
if (null == command) {
2726
String serverDomain = configuration
2827
.getProperty(Configuration.CONFIGURATION_SERVER_DOMAIN);
2928
String topicDomain = configuration

src/main/java/org/buddycloud/channelserver/packetprocessor/iq/namespace/pubsub/get/AffiliationsGet.java

Lines changed: 105 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.JabberPubsub;
1010
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.PubSubElementProcessorAbstract;
1111
import org.buddycloud.channelserver.pubsub.model.NodeMembership;
12+
import org.buddycloud.channelserver.pubsub.subscription.Subscriptions;
1213
import org.buddycloud.channelserver.utils.XMLConstants;
1314
import org.buddycloud.channelserver.utils.node.item.payload.Buddycloud;
1415
import org.dom4j.Element;
@@ -19,134 +20,136 @@
1920

2021
public class AffiliationsGet extends PubSubElementProcessorAbstract {
2122

22-
private final BlockingQueue<Packet> outQueue;
23-
private final ChannelManager channelManager;
23+
private static final Logger LOGGER = Logger.getLogger(AffiliationsGet.class);
2424

25-
private IQ requestIq;
26-
private String node;
27-
private JID actorJid;
28-
private IQ result;
29-
private String firstItem;
25+
public AffiliationsGet(BlockingQueue<Packet> outQueue, ChannelManager channelManager) {
26+
this.outQueue = outQueue;
27+
this.channelManager = channelManager;
28+
}
3029

31-
private static final Logger LOGGER = Logger.getLogger(AffiliationsGet.class);
30+
@Override
31+
public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm) throws Exception {
32+
response = IQ.createResultIQ(reqIQ);
33+
request = reqIQ;
34+
actor = actorJID;
3235

36+
node = request.getChildElement().element("affiliations").attributeValue(XMLConstants.NODE_ATTR);
37+
if (!Configuration.getInstance().isLocalJID(request.getFrom())) {
38+
response.getElement().addAttribute(XMLConstants.REMOTE_SERVER_DISCOVER_ATTR,
39+
Boolean.FALSE.toString());
3340

34-
public AffiliationsGet(BlockingQueue<Packet> outQueue, ChannelManager channelManager) {
35-
this.outQueue = outQueue;
36-
this.channelManager = channelManager;
3741
}
38-
39-
@Override
40-
public void process(Element elm, JID actorJID, IQ reqIQ, Element rsm) throws Exception {
41-
result = IQ.createResultIQ(reqIQ);
42-
requestIq = reqIQ;
43-
actorJid = actorJID;
44-
45-
node = elm.attributeValue(XMLConstants.NODE_ATTR);
46-
if (!Configuration.getInstance().isLocalJID(requestIq.getFrom())) {
47-
result.getElement().addAttribute(XMLConstants.REMOTE_SERVER_DISCOVER_ATTR, Boolean.FALSE.toString());
48-
49-
}
50-
String namespace = JabberPubsub.NS_PUBSUB_OWNER;
51-
if (node == null) {
52-
namespace = JabberPubsub.NAMESPACE_URI;
53-
}
54-
55-
56-
Element pubsub = result.setChildElement(XMLConstants.PUBSUB_ELEM, namespace);
57-
Element affiliations = pubsub.addElement(XMLConstants.AFFILIATIONS_ELEM);
58-
59-
if (actorJid == null) {
60-
actorJid = requestIq.getFrom();
61-
}
62-
63-
boolean isProcessedLocally = true;
64-
if (node == null) {
65-
isProcessedLocally = getUserMemberships(affiliations);
66-
} else {
67-
isProcessedLocally = getNodeAffiliations(affiliations);
68-
}
69-
if (!isProcessedLocally) {
70-
return;
71-
}
72-
73-
outQueue.put(result);
42+
String namespace = JabberPubsub.NAMESPACE_URI;
43+
if (node == null) {
44+
namespace = JabberPubsub.NS_PUBSUB_OWNER;
7445
}
7546

76-
private boolean getNodeAffiliations(Element affiliations) throws NodeStoreException, InterruptedException {
77-
if (!Configuration.getInstance().isLocalNode(node) && (!channelManager.isCachedNode(node))) {
7847

79-
makeRemoteRequest(node.split("/")[2]);
80-
return false;
81-
}
82-
ResultSet<NodeMembership> nodeMemberships;
83-
nodeMemberships = channelManager.getNodeMemberships(node);
48+
Element pubsub = response.setChildElement(XMLConstants.PUBSUB_ELEM, namespace);
49+
Element affiliations = pubsub.addElement(XMLConstants.AFFILIATIONS_ELEM);
8450

85-
if ((!nodeMemberships.isEmpty()) && (!Configuration.getInstance().isLocalNode(node))) {
86-
makeRemoteRequest(node.split("/")[2]);
87-
return false;
88-
}
51+
if (actor == null) {
52+
actor = request.getFrom();
53+
}
8954

55+
boolean isProcessedLocally = true;
56+
if (node == null) {
57+
isProcessedLocally = getUserMemberships(affiliations);
58+
} else {
59+
isProcessedLocally = getNodeAffiliations(affiliations);
60+
}
61+
if (!isProcessedLocally) {
62+
return;
63+
}
9064

91-
for (NodeMembership nodeMembership : nodeMemberships) {
65+
outQueue.put(response);
66+
}
9267

93-
if (actorJid.toBareJID().equals(nodeMembership.getUser().toBareJID())) {
94-
if (null == firstItem) {
95-
firstItem = nodeMembership.getUser().toString();
96-
}
68+
private boolean getNodeAffiliations(Element affiliations) throws NodeStoreException,
69+
InterruptedException {
9770

98-
affiliations.addElement(XMLConstants.AFFILIATION_ELEM).addAttribute(XMLConstants.NODE_ATTR, nodeMembership.getNodeId())
99-
.addAttribute(XMLConstants.AFFILIATION_ELEM, nodeMembership.getAffiliation().toString())
100-
.addAttribute(XMLConstants.JID_ATTR, nodeMembership.getUser().toString());
101-
}
102-
}
71+
if (!Configuration.getInstance().isLocalNode(node) && (!channelManager.isCachedNode(node))) {
10372

104-
return true;
73+
makeRemoteRequest(node.split("/")[2]);
74+
return false;
10575
}
76+
ResultSet<NodeMembership> nodeMemberships;
77+
nodeMemberships = channelManager.getNodeMemberships(node);
10678

107-
private boolean isOwnerModerator() throws NodeStoreException {
108-
return channelManager.getNodeMembership(node, actorJid).getAffiliation().canAuthorize();
79+
if ((!nodeMemberships.isEmpty()) && (!Configuration.getInstance().isLocalNode(node))) {
80+
makeRemoteRequest(node.split("/")[2]);
81+
return false;
10982
}
11083

111-
private boolean getUserMemberships(Element affiliations) throws NodeStoreException, InterruptedException {
11284

113-
if (!Configuration.getInstance().isLocalJID(actorJid) && (!channelManager.isCachedJID(requestIq.getFrom()))) {
114-
makeRemoteRequest(actorJid.getDomain());
115-
return false;
116-
}
85+
for (NodeMembership nodeMembership : nodeMemberships) {
86+
if (!nodeMembership.getSubscription().equals(Subscriptions.subscribed)
87+
&& !actor.toBareJID().equals(nodeMembership.getUser().toBareJID()) && !isOwnerModerator()) {
88+
continue;
89+
}
90+
if (nodeMembership.getSubscription().equals(Subscriptions.none)) {
91+
continue;
92+
}
93+
Element affiliation = affiliations.addElement(XMLConstants.AFFILIATION_ELEM);
94+
affiliation.addAttribute(XMLConstants.NODE_ATTR, nodeMembership.getNodeId());
95+
affiliation.addAttribute(XMLConstants.AFFILIATION_ELEM, nodeMembership.getAffiliation()
96+
.toString());
97+
affiliation.addAttribute(XMLConstants.JID_ATTR, nodeMembership.getUser().toString());
98+
if ((actor.toBareJID().equals(nodeMembership.getUser().toBareJID()) || isOwnerModerator())
99+
&& (null != nodeMembership.getInvitedBy())) {
100+
affiliation.addAttribute(XMLConstants.INVITED_BY_ATTR, nodeMembership.getInvitedBy()
101+
.toBareJID());
102+
}
103+
}
117104

118-
ResultSet<NodeMembership> memberships = channelManager.getUserMemberships(actorJid);
105+
return true;
106+
}
119107

120-
for (NodeMembership membership : memberships) {
108+
private boolean isOwnerModerator() throws NodeStoreException {
109+
return channelManager.getNodeMembership(node, actor).getAffiliation().canAuthorize();
110+
}
121111

122-
if (actorJid.toBareJID().equals(membership.getUser().toBareJID())) {
123-
LOGGER.trace("Adding affiliation for " + membership.getUser() + " affiliation " + membership.getAffiliation() + " (no node provided)");
112+
private boolean getUserMemberships(Element affiliations) throws NodeStoreException,
113+
InterruptedException {
124114

125-
if (null == firstItem) {
126-
firstItem = membership.getNodeId();
127-
}
115+
if (!channelManager.isCachedJID(request.getFrom())
116+
&& !Configuration.getInstance().isLocalJID(actor)) {
117+
makeRemoteRequest(actor.getDomain());
118+
return false;
119+
}
128120

129-
affiliations.addElement(XMLConstants.AFFILIATION_ELEM).addAttribute(XMLConstants.NODE_ATTR, membership.getNodeId())
130-
.addAttribute(XMLConstants.AFFILIATION_ELEM, membership.getAffiliation().toString())
131-
.addAttribute(XMLConstants.JID_ATTR, membership.getUser().toBareJID());
132-
}
121+
ResultSet<NodeMembership> memberships = channelManager.getUserMemberships(actor);
133122

134-
}
135-
return true;
136-
}
123+
for (NodeMembership membership : memberships) {
137124

138-
private void makeRemoteRequest(String node) throws InterruptedException {
139-
LOGGER.info("Going federated for <affiliations />");
140-
requestIq.setTo(new JID(node).getDomain());
141-
if (null == requestIq.getElement().element("pubsub").element("actor")) {
142-
Element actor = requestIq.getElement().element("pubsub").addElement("actor", Buddycloud.NS);
143-
actor.addText(requestIq.getFrom().toBareJID());
144-
}
145-
outQueue.put(requestIq);
146-
}
125+
if (membership.getSubscription().equals(Subscriptions.none)) {
126+
continue;
127+
}
128+
Element affiliation = affiliations.addElement(XMLConstants.AFFILIATION_ELEM);
129+
affiliation.addAttribute(XMLConstants.NODE_ATTR, membership.getNodeId());
130+
affiliation.addAttribute(XMLConstants.AFFILIATION_ELEM, membership.getAffiliation().toString());
131+
affiliation.addAttribute(XMLConstants.JID_ATTR, membership.getUser().toBareJID());
132+
133+
if (membership.getSubscription().equals(Subscriptions.invited) && (null != membership.getInvitedBy())) {
134+
affiliation.addAttribute(XMLConstants.INVITED_BY_ATTR, membership.getInvitedBy().toBareJID());
135+
}
147136

148-
@Override
149-
public boolean accept(Element elm) {
150-
return XMLConstants.AFFILIATIONS_ELEM.equals(elm.getName());
151137
}
138+
return true;
139+
}
140+
141+
private void makeRemoteRequest(String node) throws InterruptedException {
142+
LOGGER.info("Going federated for <affiliations />");
143+
request.setTo(new JID(node).getDomain());
144+
if (null == request.getElement().element("pubsub").element("actor")) {
145+
Element actor = request.getElement().element("pubsub").addElement("actor", Buddycloud.NS);
146+
actor.addText(request.getFrom().toBareJID());
147+
}
148+
outQueue.put(request);
149+
}
150+
151+
@Override
152+
public boolean accept(Element elm) {
153+
return XMLConstants.AFFILIATIONS_ELEM.equals(elm.getName());
154+
}
152155
}

src/main/java/org/buddycloud/channelserver/packetprocessor/iq/namespace/pubsub/get/SubscriptionsGet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ private boolean getNodeMemberships(Element subscriptions) throws NodeStoreExcept
9999

100100
// Only add the invited by attribute if the user has the necessary priviledges
101101
if (null != ns.getInvitedBy() && isUserPriviledged(ns.getUser())) {
102-
subscription.addAttribute(XMLConstants.INVITED_BY_ELEM, ns.getInvitedBy().toBareJID());
102+
subscription.addAttribute(XMLConstants.INVITED_BY_ATTR, ns.getInvitedBy().toBareJID());
103103
}
104104

105105
}
@@ -148,7 +148,7 @@ private boolean getUserMemberships(Element subscriptions) throws NodeStoreExcept
148148
.addAttribute(XMLConstants.SUBSCRIPTION_ELEM, ns.getSubscription().toString())
149149
.addAttribute(XMLConstants.JID_ATTR, ns.getUser().toBareJID());
150150
if (null != ns.getInvitedBy() && isOwnerModerator()) {
151-
subscription.addAttribute(XMLConstants.INVITED_BY_ELEM, ns.getInvitedBy().toBareJID());
151+
subscription.addAttribute(XMLConstants.INVITED_BY_ATTR, ns.getInvitedBy().toBareJID());
152152

153153
}
154154
}

src/main/java/org/buddycloud/channelserver/utils/XMLConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class XMLConstants {
2222

2323
public static final String PUBSUB_ELEM = "pubsub";
2424

25-
public static final String INVITED_BY_ELEM = "invited-by";
25+
public static final String INVITED_BY_ATTR = "invited-by";
2626

2727
public static final String SET_ELEM = "set";
2828

src/test/resources/stanzas/iq/pubsub/affiliations/request-with-node.stanza

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
to="channels.shakespeare.lit"
44
id="subscriptions1">
55
<pubsub xmlns="http://jabber.org/protocol/pubsub">
6-
<affiliations node="/users/pamela@denmark.lit/posts" />
6+
<affiliations node="/user/pamela@shakespeare.lit/posts" />
77
</pubsub>
88
</iq>

0 commit comments

Comments
 (0)