Skip to content

Commit f5baa9e

Browse files
committed
Merge pull request #253 from surevine/fix-affiliations
Add tests and fix logic around affiliations retrieval
2 parents d43b5c0 + da7292c commit f5baa9e

6 files changed

Lines changed: 529 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: 104 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,135 @@
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.AFFILIATION_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 =
56+
(node == null) ? getUserMemberships(affiliations) : getNodeAffiliations(affiliations);
57+
if (!isProcessedLocally) {
58+
return;
59+
}
9060

91-
for (NodeMembership nodeMembership : nodeMemberships) {
61+
outQueue.put(response);
62+
}
9263

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

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-
}
67+
if (!Configuration.getInstance().isLocalNode(node) && (!channelManager.isCachedNode(node))) {
68+
69+
makeRemoteRequest(node.split("/")[2]);
70+
return false;
71+
}
72+
ResultSet<NodeMembership> nodeMemberships;
73+
nodeMemberships = channelManager.getNodeMemberships(node);
10374

104-
return true;
75+
if ((nodeMemberships.isEmpty()) && (!Configuration.getInstance().isLocalNode(node))) {
76+
makeRemoteRequest(node.split("/")[2]);
77+
return false;
10578
}
10679

107-
private boolean isOwnerModerator() throws NodeStoreException {
108-
return channelManager.getNodeMembership(node, actorJid).getAffiliation().canAuthorize();
80+
81+
for (NodeMembership nodeMembership : nodeMemberships) {
82+
if (!nodeMembership.getSubscription().equals(Subscriptions.subscribed)
83+
&& !actor.toBareJID().equals(nodeMembership.getUser().toBareJID()) && !isOwnerModerator()) {
84+
continue;
85+
}
86+
if (nodeMembership.getSubscription().equals(Subscriptions.none)) {
87+
continue;
88+
}
89+
Element affiliation = affiliations.addElement(XMLConstants.AFFILIATION_ELEM);
90+
affiliation.addAttribute(XMLConstants.NODE_ATTR, nodeMembership.getNodeId());
91+
affiliation.addAttribute(XMLConstants.AFFILIATION_ELEM, nodeMembership.getAffiliation()
92+
.toString());
93+
affiliation.addAttribute(XMLConstants.JID_ATTR, nodeMembership.getUser().toString());
94+
if ((actor.toBareJID().equals(nodeMembership.getUser().toBareJID()) || isOwnerModerator())
95+
&& (null != nodeMembership.getInvitedBy())) {
96+
affiliation.addAttribute(XMLConstants.INVITED_BY_ATTR, nodeMembership.getInvitedBy()
97+
.toBareJID());
98+
}
10999
}
110100

111-
private boolean getUserMemberships(Element affiliations) throws NodeStoreException, InterruptedException {
101+
return true;
102+
}
112103

113-
if (!Configuration.getInstance().isLocalJID(actorJid) && (!channelManager.isCachedJID(requestIq.getFrom()))) {
114-
makeRemoteRequest(actorJid.getDomain());
115-
return false;
116-
}
104+
private boolean isOwnerModerator() throws NodeStoreException {
105+
return channelManager.getNodeMembership(node, actor).getAffiliation().canAuthorize();
106+
}
117107

118-
ResultSet<NodeMembership> memberships = channelManager.getUserMemberships(actorJid);
108+
private boolean getUserMemberships(Element affiliations) throws NodeStoreException,
109+
InterruptedException {
119110

120-
for (NodeMembership membership : memberships) {
111+
if (!channelManager.isCachedJID(request.getFrom())
112+
&& !Configuration.getInstance().isLocalJID(actor)) {
113+
makeRemoteRequest(actor.getDomain());
114+
return false;
115+
}
121116

122-
if (actorJid.toBareJID().equals(membership.getUser().toBareJID())) {
123-
LOGGER.trace("Adding affiliation for " + membership.getUser() + " affiliation " + membership.getAffiliation() + " (no node provided)");
117+
ResultSet<NodeMembership> memberships = channelManager.getUserMemberships(actor);
124118

125-
if (null == firstItem) {
126-
firstItem = membership.getNodeId();
127-
}
119+
for (NodeMembership membership : memberships) {
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+
if (membership.getSubscription().equals(Subscriptions.none)) {
122+
continue;
123+
}
124+
Element affiliation = affiliations.addElement(XMLConstants.AFFILIATION_ELEM);
125+
affiliation.addAttribute(XMLConstants.NODE_ATTR, membership.getNodeId());
126+
affiliation.addAttribute(XMLConstants.AFFILIATION_ELEM, membership.getAffiliation()
127+
.toString());
128+
affiliation.addAttribute(XMLConstants.JID_ATTR, membership.getUser().toBareJID());
133129

134-
}
135-
return true;
136-
}
130+
if (membership.getSubscription().equals(Subscriptions.invited)
131+
&& (null != membership.getInvitedBy())) {
132+
affiliation.addAttribute(XMLConstants.INVITED_BY_ATTR, membership.getInvitedBy()
133+
.toBareJID());
134+
}
137135

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

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

0 commit comments

Comments
 (0)