Skip to content

Commit e36d711

Browse files
committed
Added auto-approve to channel auto-subscribe (for private channels)
1 parent 341b530 commit e36d711

5 files changed

Lines changed: 85 additions & 23 deletions

File tree

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,39 @@
22

33
import org.buddycloud.channelserver.db.NodeStore;
44
import org.buddycloud.channelserver.db.exception.NodeStoreException;
5-
import org.buddycloud.channelserver.pubsub.model.NodeAffiliation;
5+
import org.buddycloud.channelserver.pubsub.affiliation.Affiliations;
66
import org.xmpp.packet.JID;
7-
import org.xmpp.resultsetmanagement.ResultSet;
87

98
public interface ChannelManager extends NodeStore {
10-
9+
1110
/**
1211
* Creates a channel.
13-
* @param channelJID the JID of the channel.
14-
* @throws NodeStoreException
12+
*
13+
* @param channelJID
14+
* the JID of the channel.
15+
* @throws NodeStoreException
1516
*/
1617
void createPersonalChannel(JID ownerJID) throws NodeStoreException;
17-
18+
1819
/**
1920
* Determines whether the node id given refers to a local node.
20-
* @param nodeId the node id
21-
* @return <code>true</code> if the node appears to be local, <code>false</code> otherwise.
22-
* @throws NodeStoreException
21+
*
22+
* @param nodeId
23+
* the node id
24+
* @return <code>true</code> if the node appears to be local,
25+
* <code>false</code> otherwise.
26+
* @throws NodeStoreException
2327
*/
2428
boolean isLocalNode(String nodeId) throws NodeStoreException;
25-
29+
2630
/**
2731
* Determines whether the jid refers to a local user.
28-
* @param jid the user's jid
29-
* @return <code>true</code> if the jid appears to be local, <code>false</code> otherwise.
30-
* @throws NodeStoreException
32+
*
33+
* @param jid
34+
* the user's jid
35+
* @return <code>true</code> if the jid appears to be local,
36+
* <code>false</code> otherwise.
37+
* @throws NodeStoreException
3138
*/
3239
boolean isLocalJID(JID jid) throws NodeStoreException;
3340

@@ -37,4 +44,12 @@ public interface ChannelManager extends NodeStore {
3744
* @throws NodeStoreException
3845
*/
3946
void deleteRemoteData() throws NodeStoreException;
47+
48+
/**
49+
* Gets the default affiliation for a node
50+
* @return
51+
*
52+
* @throws NodeStoreException
53+
*/
54+
Affiliations getDefaultNodeAffiliation(String nodeId) throws NodeStoreException;
4055
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,21 @@ public CloseableIterator<NodeItem> getFirehose(int limit,
413413
public int getFirehoseItemCount(boolean isAdmin) throws NodeStoreException {
414414
return nodeStore.getFirehoseItemCount(isAdmin);
415415
}
416+
417+
@Override
418+
public Affiliations getDefaultNodeAffiliation(String nodeId)
419+
throws NodeStoreException {
420+
String affiliationString = getNodeConfValue(nodeId, Conf.DEFAULT_AFFILIATION);
421+
422+
if(affiliationString != null) {
423+
try {
424+
return Affiliations.valueOf(affiliationString);
425+
}
426+
catch(IllegalArgumentException e) {
427+
logger.error("Invalid default affiliation stored for node " + nodeId + ": " + affiliationString, e);
428+
}
429+
}
430+
431+
return Affiliations.member;
432+
}
416433
}

src/main/java/org/buddycloud/channelserver/packetprocessor/iq/namespace/register/RegisterSet.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public void process(IQ reqIQ) throws Exception {
7878

7979
// TODO: We should really be returning an error as per spec shouldn't we?
8080
// It should be up to the client to ignore the error, not the server.
81+
@SuppressWarnings("unused")
8182
private void userAlreadyRegistered() throws InterruptedException {
8283
// User is already registered.
8384
IQ reply = IQ.createResultIQ(request);
@@ -158,9 +159,13 @@ private void autosubscribeToChannels(final JID from) {
158159
channelManager.getNodeConfValue(channelNodeId,
159160
Conf.ACCESS_MODEL))) {
160161
channelManager
161-
.addUserSubscription(new NodeSubscriptionImpl(Conf
162-
.getPostChannelNodename(channel), from,
162+
.addUserSubscription(new NodeSubscriptionImpl(
163+
channelNodeId, from,
163164
Subscriptions.subscribed));
165+
166+
channelManager.setUserAffiliation(channelNodeId, from,
167+
channelManager
168+
.getDefaultNodeAffiliation(channelNodeId));
164169
}
165170
} catch (InterruptedException e) {
166171
LOGGER.error("Could not auto-subscribe " + from + " to "

src/test/java/org/buddycloud/channelserver/channel/ChannelManagerImplTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.buddycloud.channelserver.channel;
22

3+
import static org.junit.Assert.assertEquals;
34
import static org.junit.Assert.assertFalse;
45
import static org.junit.Assert.assertTrue;
56
import static org.mockito.Mockito.never;
@@ -10,6 +11,7 @@
1011

1112
import org.buddycloud.channelserver.Configuration;
1213
import org.buddycloud.channelserver.db.NodeStore;
14+
import org.buddycloud.channelserver.pubsub.affiliation.Affiliations;
1315
import org.junit.After;
1416
import org.junit.Before;
1517
import org.junit.Ignore;
@@ -150,4 +152,22 @@ public void testDeleteRemoteDoesNotPurgeLocalNodes() throws Exception {
150152
verify(nodeStore).getNodeList();
151153
verify(nodeStore, Mockito.times(2)).purgeNodeItems(Mockito.anyString());
152154
}
155+
156+
@Test
157+
public void testGetNodeDefaultAffiliationForNodeWithConf() throws Exception {
158+
when(nodeStore.getNodeConfValue(user1, Conf.DEFAULT_AFFILIATION)).thenReturn("moderator");
159+
160+
Affiliations affiliation = channelManager.getDefaultNodeAffiliation(user1);
161+
162+
assertEquals("Incorrect default affiliation", Affiliations.moderator, affiliation);
163+
}
164+
165+
166+
@Test
167+
public void testGetNodeDefaultAffiliationForNodeWithoutConf() throws Exception {
168+
Affiliations affiliation = channelManager.getDefaultNodeAffiliation(user1);
169+
170+
// If nothing is specified, the default affiliation is "member"
171+
assertEquals("Incorrect default affiliation", Affiliations.member, affiliation);
172+
}
153173
}

src/test/java/org/buddycloud/channelserver/packetprocessor/iq/namespace/register/RegisterSetTest.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.buddycloud.channelserver.packetHandler.iq.IQTestHandler;
1919
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.JabberPubsub;
2020
import org.buddycloud.channelserver.pubsub.accessmodel.AccessModels;
21+
import org.buddycloud.channelserver.pubsub.affiliation.Affiliations;
2122
import org.buddycloud.channelserver.pubsub.model.impl.NodeSubscriptionImpl;
2223
import org.buddycloud.channelserver.pubsub.subscription.Subscriptions;
2324
import org.dom4j.Element;
@@ -171,13 +172,10 @@ public void testRegisterNewUserAutoApprovesOnlyLocalPrivateChannels()
171172

172173
final JID localPrivateChannel = new JID("channel1@server1");
173174
final String localPrivateChannelNode = "/user/channel1@server1/posts";
174-
175+
175176
final JID localOpenChannel = new JID("channel2@server1");
176-
final String localOpenChannelNode = "/user/channel2@server1/posts";
177-
178177
final JID remoteChannel = new JID("channel1@server2");
179-
final String remoteChannelNode = "/user/channel1@server2/posts";
180-
178+
181179
when(channelManagerMock.nodeExists(anyString())).thenReturn(false);
182180

183181
when(configuration.getAutosubscribeChannels()).thenReturn(
@@ -201,6 +199,11 @@ public void testRegisterNewUserAutoApprovesOnlyLocalPrivateChannels()
201199
Conf.ACCESS_MODEL)).thenReturn(
202200
AccessModels.authorize.toString());
203201

202+
when(
203+
channelManagerMock
204+
.getDefaultNodeAffiliation(localPrivateChannelNode))
205+
.thenReturn(Affiliations.moderator);
206+
204207
when(channelManagerMock.isLocalJID(localPrivateChannel)).thenReturn(
205208
true);
206209
when(channelManagerMock.isLocalJID(localOpenChannel)).thenReturn(true);
@@ -213,12 +216,13 @@ public void testRegisterNewUserAutoApprovesOnlyLocalPrivateChannels()
213216
verify(channelManagerMock).addUserSubscription(
214217
new NodeSubscriptionImpl(localPrivateChannelNode,
215218
REGISTER_REQUEST_FROM, Subscriptions.subscribed));
219+
verify(channelManagerMock).setUserAffiliation(localPrivateChannelNode,
220+
REGISTER_REQUEST_FROM, Affiliations.moderator);
216221
}
217222

218223
@SuppressWarnings("serial")
219224
@Test
220-
public void testRegisterNewUserDoesntAutoApprove()
221-
throws Exception {
225+
public void testRegisterNewUserDoesntAutoApprove() throws Exception {
222226
IQ request = readStanzaAsIq(REGISTER_REQUEST_STANZA);
223227

224228
final JID localPrivateChannel = new JID("channel1@server1");
@@ -250,7 +254,8 @@ public void testRegisterNewUserDoesntAutoApprove()
250254

251255
registerSet.process(request);
252256

253-
// Check that a subscription has been added, but only for
257+
// Check that a subscription and affiliation has been added, but only
258+
// for
254259
// localPrivateChannel.
255260
verify(channelManagerMock, never()).addUserSubscription(
256261
new NodeSubscriptionImpl(localPrivateChannelNode,

0 commit comments

Comments
 (0)