Skip to content

Commit 2704c19

Browse files
committed
Fixes #281
1 parent 036f837 commit 2704c19

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

src/main/java/org/buddycloud/channelserver/db/jdbc/JDBCNodeStore.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,29 @@ public ResultSet<NodeAffiliation> getAffiliationChanges(JID user, Date startDate
401401

402402
@Override
403403
public ResultSet<NodeMembership> getUserMemberships(JID jid) throws NodeStoreException {
404-
return getUserMemberships(jid, false);
404+
PreparedStatement stmt = null;
405+
try {
406+
stmt = conn.prepareStatement(dialect.selectUserMemberships());
407+
stmt.setString(1, jid.toBareJID());
408+
java.sql.ResultSet rs = stmt.executeQuery();
409+
410+
ArrayList<NodeMembership> result = new ArrayList<NodeMembership>();
411+
while (rs.next()) {
412+
JID inviter = null;
413+
if (null != rs.getString(6)) {
414+
inviter = new JID(rs.getString(6));
415+
}
416+
NodeMembershipImpl membership =
417+
new NodeMembershipImpl(rs.getString(1), new JID(rs.getString(2)), new JID(rs.getString(3)),
418+
Subscriptions.valueOf(rs.getString(4)), Affiliations.valueOf(rs.getString(5)), inviter, rs.getTimestamp(7));
419+
result.add(membership);
420+
}
421+
return new ResultSetImpl<NodeMembership>(result);
422+
} catch (SQLException e) {
423+
throw new NodeStoreException(e);
424+
} finally {
425+
close(stmt); // Will implicitly close the resultset if required
426+
}
405427
}
406428

407429
@Override
@@ -473,7 +495,7 @@ private static String getCollectionStatement(int collectionLength) {
473495
public ResultSet<NodeMembership> getUserMemberships(JID jid, boolean ephemeral) throws NodeStoreException {
474496
PreparedStatement stmt = null;
475497
try {
476-
String sql = dialect.selectUserMemberships();
498+
String sql = dialect.selectUserMembershipsFilteredByEphemeral();
477499
String replace = "IS NULL OR \"node_config\".\"value\" != 'true'";
478500
if (ephemeral) {
479501
replace = "= 'true'";
@@ -1886,6 +1908,8 @@ public interface NodeStoreSQLDialect {
18861908

18871909
String selectUserMemberships();
18881910

1911+
String selectUserMembershipsFilteredByEphemeral();
1912+
18891913
String selectUserMembershipsWithConfiguration();
18901914

18911915
String selectMembership();

src/main/java/org/buddycloud/channelserver/db/jdbc/dialect/Sql92NodeStoreDialect.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public class Sql92NodeStoreDialect implements NodeStoreSQLDialect {
263263
+ "ON \"subscriptions\".\"node\" = \"affiliations\".\"node\" AND \"affiliations\".\"user\" = \"subscriptions\".\"user\" " + "WHERE "
264264
+ "(\"subscriptions\".\"user\" = ? AND \"subscriptions\".\"node\" = ?) " + "ORDER BY \"updated\" DESC; ";
265265

266-
private static final String SELECT_USER_MEMBERSHIPS = "" + "SELECT " + "CASE WHEN \"subscriptions\".\"node\" != '' "
266+
private static final String SELECT_USER_MEMBERSHIPS_FILTERED_BY_EPHEMERAL = "" + "SELECT " + "CASE WHEN \"subscriptions\".\"node\" != '' "
267267
+ "THEN \"subscriptions\".\"node\" " + "ELSE \"affiliations\".\"node\" " + "END AS \"node\"," + "CASE WHEN \"subscriptions\".\"user\" != '' "
268268
+ "THEN \"subscriptions\".\"user\" " + "ELSE \"affiliations\".\"user\" " + "END AS \"user\", " + "CASE "
269269
+ "WHEN \"subscriptions\".\"listener\" != '' THEN \"subscriptions\".\"listener\" "
@@ -281,6 +281,22 @@ public class Sql92NodeStoreDialect implements NodeStoreSQLDialect {
281281
+ "AND (\"node_config\".\"value\" %equals%)"
282282
+ "ORDER BY \"updated\" DESC; ";
283283

284+
private static final String SELECT_USER_MEMBERSHIPS = "" + "SELECT " + "CASE WHEN \"subscriptions\".\"node\" != '' "
285+
+ "THEN \"subscriptions\".\"node\" " + "ELSE \"affiliations\".\"node\" " + "END AS \"node\"," + "CASE WHEN \"subscriptions\".\"user\" != '' "
286+
+ "THEN \"subscriptions\".\"user\" " + "ELSE \"affiliations\".\"user\" " + "END AS \"user\", " + "CASE "
287+
+ "WHEN \"subscriptions\".\"listener\" != '' THEN \"subscriptions\".\"listener\" "
288+
+ "WHEN \"subscriptions\".\"user\" != '' THEN \"subscriptions\".\"user\" " + "ELSE \"affiliations\".\"user\" " + "END AS \"listener\", "
289+
+ "CASE WHEN \"subscriptions\".\"subscription\" != '' " + "THEN \"subscriptions\".\"subscription\" " + "ELSE 'none' "
290+
+ "END AS \"subscription\", " + "CASE WHEN \"affiliations\".\"affiliation\" != '' " + "THEN \"affiliations\".\"affiliation\" "
291+
+ "ELSE 'none' " + "END AS \"affiliation\", " + "\"subscriptions\".\"invited_by\" AS \"invited_by\","
292+
+ "CASE WHEN \"affiliations\".\"updated\" > \"subscriptions\".\"updated\" " + "THEN \"affiliations\".\"updated\" "
293+
+ "ELSE \"subscriptions\".\"updated\" " + "END AS \"updated\" " + "FROM \"subscriptions\" "
294+
295+
+ "LEFT JOIN \"affiliations\" "
296+
+ "ON \"subscriptions\".\"node\" = \"affiliations\".\"node\" AND \"affiliations\".\"user\" = \"subscriptions\".\"user\" " + "WHERE "
297+
+ "(\"subscriptions\".\"user\" = ?) "
298+
+ "ORDER BY \"updated\" DESC; ";
299+
284300
private static final String SELECT_USER_MEMBERSHIPS_WITH_CONFIGURATION = "SELECT " +
285301
"CASE WHEN \"subscriptions\".\"node\" != '' THEN " +
286302
"\"subscriptions\".\"node\" " +
@@ -692,6 +708,11 @@ public String selectMembership() {
692708
return SELECT_NODE_MEMBERSHIP;
693709
}
694710

711+
@Override
712+
public String selectUserMembershipsFilteredByEphemeral() {
713+
return SELECT_USER_MEMBERSHIPS_FILTERED_BY_EPHEMERAL;
714+
}
715+
695716
@Override
696717
public String selectUserMemberships() {
697718
return SELECT_USER_MEMBERSHIPS;

src/test/java/org/buddycloud/channelserver/db/jdbc/JDBCNodeStoreRecentItemsTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ public void testGetRecentItemsNoSubscriptions() throws Exception {
5454
TEST_SERVER1_USER1_JID, new Date(), -1, -1, null, null, false);
5555
assertEquals(false, items.hasNext());
5656
}
57+
58+
@Test
59+
public void testGetRecentItemsFromEphemeralNode() throws Exception {
60+
61+
Date since = new Date();
62+
dbTester.loadData("node_1");
63+
store.setNodeConfValue(TEST_SERVER1_NODE1_ID, "buddycloud#ephemeral", "true");
64+
65+
NodeItem nodeItem1 = new NodeItemImpl(TEST_SERVER1_NODE1_ID, "123", new Date(), "payload");
66+
store.addNodeItem(nodeItem1);
67+
68+
Thread.sleep(20);
69+
70+
CloseableIterator<NodeItem> items = store.getRecentItems(TEST_SERVER1_USER1_JID, since, -1, -1, null, null, false);
71+
72+
// 2 -> 1 on purpose results are most recent first!
73+
assertSameNodeItem(items.next(), nodeItem1);
74+
assertEquals(false, items.hasNext());
75+
}
5776

5877
@Test
5978
public void testGetRecentItemsCanBePaged() throws Exception {

0 commit comments

Comments
 (0)