Added a method to get a list of available but not subscribed groups.

This commit is contained in:
akwizgran
2013-04-13 23:33:21 +01:00
parent 03af1d359d
commit 1a8dbd1dbb
5 changed files with 82 additions and 0 deletions

View File

@@ -162,6 +162,12 @@ public interface DatabaseComponent {
Collection<TransportUpdate> generateTransportUpdates(ContactId c,
long maxLatency) throws DbException;
/**
* Returns any groups that contacts have made visible but to which the user
* does not subscribe.
*/
Collection<Group> getAvailableGroups() throws DbException;
/** Returns the configuration for the given transport. */
TransportConfig getConfig(TransportId t) throws DbException;

View File

@@ -219,6 +219,12 @@ interface Database<T> {
boolean containsVisibleSubscription(T txn, ContactId c, GroupId g)
throws DbException;
/**
* Returns any groups that contacts have made visible but to which the user
* does not subscribe.
*/
Collection<Group> getAvailableGroups(T txn) throws DbException;
/**
* Returns the configuration for the given transport.
* <p>

View File

@@ -883,6 +883,23 @@ DatabaseCleaner.Callback {
}
}
public Collection<Group> getAvailableGroups() throws DbException {
subscriptionLock.readLock().lock();
try {
T txn = db.startTransaction();
try {
Collection<Group> groups = db.getAvailableGroups(txn);
db.commitTransaction(txn);
return groups;
} catch(DbException e) {
db.abortTransaction(txn);
throw e;
}
} finally {
subscriptionLock.readLock().unlock();
}
}
public TransportConfig getConfig(TransportId t) throws DbException {
transportLock.readLock().lock();
try {

View File

@@ -1146,6 +1146,36 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public Collection<Group> getAvailableGroups(Connection txn)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT cg.groupId, cg.name, cg.publicKey"
+ " FROM contactGroups AS cg"
+ " LEFT OUTER JOIN groups AS g"
+ " ON cg.groupId = g.groupId"
+ " WHERE g.groupId IS NULL"
+ " GROUP BY cg.groupId";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
List<Group> groups = new ArrayList<Group>();
while(rs.next()) {
GroupId id = new GroupId(rs.getBytes(1));
String name = rs.getString(2);
byte[] publicKey = rs.getBytes(3);
groups.add(new Group(id, name, publicKey));
}
rs.close();
ps.close();
return Collections.unmodifiableList(groups);
} catch(SQLException e) {
tryToClose(rs);
tryToClose(ps);
throw new DbException(e);
}
}
public TransportConfig getConfig(Connection txn, TransportId t)
throws DbException {
PreparedStatement ps = null;

View File

@@ -1817,6 +1817,29 @@ public class H2DatabaseTest extends BriarTestCase {
db.close();
}
@Test
public void testGetAvailableGroups() throws Exception {
Database<Connection> db = open(false);
Connection txn = db.startTransaction();
// Add a contact who subscribes to a group
db.addLocalAuthor(txn, localAuthor);
assertEquals(contactId, db.addContact(txn, author, localAuthorId));
db.setSubscriptions(txn, contactId, Arrays.asList(group), 1);
// The group should be available
assertEquals(Collections.emptyList(), db.getSubscriptions(txn));
assertEquals(Arrays.asList(group), db.getAvailableGroups(txn));
// Subscribe to the group - it should no longer be available
db.addSubscription(txn, group);
assertEquals(Arrays.asList(group), db.getSubscriptions(txn));
assertEquals(Collections.emptyList(), db.getAvailableGroups(txn));
db.commitTransaction(txn);
db.close();
}
@Test
public void testExceptionHandling() throws Exception {
Database<Connection> db = open(false);