diff --git a/briar-android-tests/src/test/java/org/briarproject/ForumSharingIntegrationTest.java b/briar-android-tests/src/test/java/org/briarproject/ForumSharingIntegrationTest.java index 86ae6ed6001e15cf719fefae04e505a532697648..444f2f4c8df18691ff30d72a89bde26499a4d5e2 100644 --- a/briar-android-tests/src/test/java/org/briarproject/ForumSharingIntegrationTest.java +++ b/briar-android-tests/src/test/java/org/briarproject/ForumSharingIntegrationTest.java @@ -183,7 +183,7 @@ public class ForumSharingIntegrationTest extends BriarTestCase { assertTrue(listener0.responseReceived); // forum was added successfully - assertEquals(0, forumSharingManager0.getAvailable().size()); + assertEquals(0, forumSharingManager0.getInvited().size()); assertEquals(1, forumManager1.getForums().size()); // invitee has one invitation message from sharer @@ -233,10 +233,10 @@ public class ForumSharingIntegrationTest extends BriarTestCase { assertTrue(listener0.responseReceived); // forum was not added - assertEquals(0, forumSharingManager0.getAvailable().size()); + assertEquals(0, forumSharingManager0.getInvited().size()); assertEquals(0, forumManager1.getForums().size()); // forum is no longer available to invitee who declined - assertEquals(0, forumSharingManager1.getAvailable().size()); + assertEquals(0, forumSharingManager1.getInvited().size()); // invitee has one invitation message from sharer List<ForumInvitationMessage> list = @@ -283,7 +283,7 @@ public class ForumSharingIntegrationTest extends BriarTestCase { assertTrue(listener0.responseReceived); // forum was added successfully - assertEquals(0, forumSharingManager0.getAvailable().size()); + assertEquals(0, forumSharingManager0.getInvited().size()); assertEquals(1, forumManager1.getForums().size()); assertTrue(forumManager1.getForums().contains(forum0)); @@ -303,7 +303,7 @@ public class ForumSharingIntegrationTest extends BriarTestCase { syncToSharer(); // forum is gone - assertEquals(0, forumSharingManager0.getAvailable().size()); + assertEquals(0, forumSharingManager0.getInvited().size()); assertEquals(0, forumManager1.getForums().size()); // sharer no longer shares forum with invitee @@ -343,7 +343,7 @@ public class ForumSharingIntegrationTest extends BriarTestCase { assertTrue(listener0.responseReceived); // forum was added successfully - assertEquals(0, forumSharingManager0.getAvailable().size()); + assertEquals(0, forumSharingManager0.getInvited().size()); assertEquals(1, forumManager1.getForums().size()); assertTrue(forumManager1.getForums().contains(forum0)); @@ -394,8 +394,9 @@ public class ForumSharingIntegrationTest extends BriarTestCase { // sharer un-subscribes from forum forumManager0.removeForum(forum0); - // from here on expect the response to fail with a DbException - thrown.expect(DbException.class); + // from here on expect the response to fail with an AssertionError, + // because there is in fact no invited forum available anymore + thrown.expect(AssertionError.class); // sync first request message and leave message syncToInvitee(); @@ -403,7 +404,7 @@ public class ForumSharingIntegrationTest extends BriarTestCase { assertTrue(listener1.requestReceived); // invitee has no forums available - assertEquals(0, forumSharingManager1.getAvailable().size()); + assertEquals(0, forumSharingManager1.getInvited().size()); } finally { stopLifecycles(); } @@ -709,10 +710,11 @@ public class ForumSharingIntegrationTest extends BriarTestCase { deliverMessage(sync2, contactId2, sync1, contactId1, "Sharer2 to Invitee"); - // make sure we have only one forum available - Collection<Forum> forums = - forumSharingManager1.getAvailable(); + // make sure we now have two invitations to the same forum available + Collection<Forum> forums = forumSharingManager1.getInvited(); assertEquals(1, forums.size()); + assertEquals(2, + forumSharingManager1.getSharedBy(forum0.getId()).size()); // make sure both sharers actually share the forum Collection<Contact> contacts = @@ -946,6 +948,8 @@ public class ForumSharingIntegrationTest extends BriarTestCase { if (!answer) return; Forum f = event.getForum(); try { + eventWaiter.assertEquals(1, + forumSharingManager1.getInvited().size()); Contact c = contactManager1.getContact(event.getContactId()); forumSharingManager1.respondToInvitation(f, c, accept); diff --git a/briar-android/src/org/briarproject/android/forum/AvailableForumsActivity.java b/briar-android/src/org/briarproject/android/forum/AvailableForumsActivity.java index f7034475cfe2b89f043f887a0a3f11030d8a714f..c44fbcb010a1e641e0af9b84ea8c9b20bda41675 100644 --- a/briar-android/src/org/briarproject/android/forum/AvailableForumsActivity.java +++ b/briar-android/src/org/briarproject/android/forum/AvailableForumsActivity.java @@ -82,7 +82,7 @@ public class AvailableForumsActivity extends BriarActivity try { Collection<ForumContacts> available = new ArrayList<>(); long now = System.currentTimeMillis(); - for (Forum f : forumSharingManager.getAvailable()) { + for (Forum f : forumSharingManager.getInvited()) { try { Collection<Contact> c = forumSharingManager.getSharedBy(f.getId()); diff --git a/briar-android/src/org/briarproject/android/forum/ForumListFragment.java b/briar-android/src/org/briarproject/android/forum/ForumListFragment.java index d8c5c81bde9058b91ddcda96eb0df4837ea51298..161e4bb80518d38d56aa137c747c5d067837aa10 100644 --- a/briar-android/src/org/briarproject/android/forum/ForumListFragment.java +++ b/briar-android/src/org/briarproject/android/forum/ForumListFragment.java @@ -188,7 +188,7 @@ public class ForumListFragment extends BaseEventFragment implements try { long now = System.currentTimeMillis(); int available = - forumSharingManager.getAvailable().size(); + forumSharingManager.getInvited().size(); long duration = System.currentTimeMillis() - now; if (LOG.isLoggable(INFO)) LOG.info("Loading available took " + duration + " ms"); diff --git a/briar-api/src/org/briarproject/api/blogs/BlogSharingManager.java b/briar-api/src/org/briarproject/api/blogs/BlogSharingManager.java index ec9f23e49df4910fc2bdc5935fd98b4e9eed4cbd..6fed69e19faf24ba63cb3c82e9aef514833d2bf6 100644 --- a/briar-api/src/org/briarproject/api/blogs/BlogSharingManager.java +++ b/briar-api/src/org/briarproject/api/blogs/BlogSharingManager.java @@ -38,9 +38,9 @@ public interface BlogSharingManager ContactId contactId) throws DbException; /** - * Returns all blogs to which the user could subscribe. + * Returns all blogs to which the user has been invited. */ - Collection<Blog> getAvailable() throws DbException; + Collection<Blog> getInvited() throws DbException; /** * Returns all contacts who are sharing the given blog with us. diff --git a/briar-api/src/org/briarproject/api/forum/ForumSharingManager.java b/briar-api/src/org/briarproject/api/forum/ForumSharingManager.java index 55da0615f9134ee0d6a1c89f62a0d53fcef7321c..365cf18e753cb28964d11e0ae4018abb5e334d0f 100644 --- a/briar-api/src/org/briarproject/api/forum/ForumSharingManager.java +++ b/briar-api/src/org/briarproject/api/forum/ForumSharingManager.java @@ -34,8 +34,8 @@ public interface ForumSharingManager extends SharingManager<Forum, ForumInvitati Collection<ForumInvitationMessage> getInvitationMessages( ContactId contactId) throws DbException; - /** Returns all forums to which the user could subscribe. */ - Collection<Forum> getAvailable() throws DbException; + /** Returns all forums to which the user has been invited. */ + Collection<Forum> getInvited() throws DbException; /** Returns all contacts who are sharing the given forum with us. */ Collection<Contact> getSharedBy(GroupId g) throws DbException; diff --git a/briar-api/src/org/briarproject/api/sharing/SharingConstants.java b/briar-api/src/org/briarproject/api/sharing/SharingConstants.java index 952bfd6602b2c3956c287c17bad66abe1cb30fe0..43d689729be66acab0d2733c36d35c88fcb79ad4 100644 --- a/briar-api/src/org/briarproject/api/sharing/SharingConstants.java +++ b/briar-api/src/org/briarproject/api/sharing/SharingConstants.java @@ -25,7 +25,6 @@ public interface SharingConstants { int SHARE_MSG_TYPE_DECLINE = 3; int SHARE_MSG_TYPE_LEAVE = 4; int SHARE_MSG_TYPE_ABORT = 5; - String TASK = "task"; int TASK_ADD_SHAREABLE_TO_LIST_SHARED_WITH_US = 0; int TASK_REMOVE_SHAREABLE_FROM_LIST_SHARED_WITH_US = 1; int TASK_ADD_SHARED_SHAREABLE = 2; diff --git a/briar-api/src/org/briarproject/api/sharing/SharingManager.java b/briar-api/src/org/briarproject/api/sharing/SharingManager.java index 8c5db480b7058bf63d4730a8f6d9c406c119fd49..100ab5dc48d13097e252b3cdb653afc3ebc5cc8f 100644 --- a/briar-api/src/org/briarproject/api/sharing/SharingManager.java +++ b/briar-api/src/org/briarproject/api/sharing/SharingManager.java @@ -3,8 +3,6 @@ package org.briarproject.api.sharing; import org.briarproject.api.contact.Contact; import org.briarproject.api.contact.ContactId; import org.briarproject.api.db.DbException; -import org.briarproject.api.forum.Forum; -import org.briarproject.api.forum.ForumInvitationMessage; import org.briarproject.api.sync.ClientId; import org.briarproject.api.sync.GroupId; @@ -16,7 +14,7 @@ public interface SharingManager<S extends Shareable, IM extends InvitationMessag ClientId getClientId(); /** - * Sends an invitation to share the given group with the given contact + * Sends an invitation to share the given shareable with the given contact * and sends an optional message along with it. */ void sendInvitation(GroupId groupId, ContactId contactId, @@ -35,8 +33,8 @@ public interface SharingManager<S extends Shareable, IM extends InvitationMessag Collection<IM> getInvitationMessages( ContactId contactId) throws DbException; - /** Returns all groups to which the user could subscribe. */ - Collection<S> getAvailable() throws DbException; + /** Returns all shareables to which the user has been invited. */ + Collection<S> getInvited() throws DbException; /** Returns all contacts who are sharing the given group with us. */ Collection<Contact> getSharedBy(GroupId g) throws DbException; diff --git a/briar-core/src/org/briarproject/sharing/BlogSharingManagerImpl.java b/briar-core/src/org/briarproject/sharing/BlogSharingManagerImpl.java index a25520fac0f49ffdcea490d227340bff041181cb..a8bd64b2aa57b19417fa58db52026ef883c3d830 100644 --- a/briar-core/src/org/briarproject/sharing/BlogSharingManagerImpl.java +++ b/briar-core/src/org/briarproject/sharing/BlogSharingManagerImpl.java @@ -80,11 +80,6 @@ class BlogSharingManagerImpl extends return CLIENT_ID; } - @Override - protected ClientId getShareableClientId() { - return blogManager.getClientId(); - } - @Override protected BlogInvitationMessage createInvitationMessage(MessageId id, BlogInvitation msg, ContactId contactId, boolean available, diff --git a/briar-core/src/org/briarproject/sharing/ForumSharingManagerImpl.java b/briar-core/src/org/briarproject/sharing/ForumSharingManagerImpl.java index eb06dff4ca6ade66985921d35bce2169bb64c56c..9cee6346369bf7605cf760c162e1deb28dc413ee 100644 --- a/briar-core/src/org/briarproject/sharing/ForumSharingManagerImpl.java +++ b/briar-core/src/org/briarproject/sharing/ForumSharingManagerImpl.java @@ -78,11 +78,6 @@ class ForumSharingManagerImpl extends return CLIENT_ID; } - @Override - protected ClientId getShareableClientId() { - return forumManager.getClientId(); - } - @Override protected ForumInvitationMessage createInvitationMessage(MessageId id, ForumInvitation msg, ContactId contactId, boolean available, diff --git a/briar-core/src/org/briarproject/sharing/SharingManagerImpl.java b/briar-core/src/org/briarproject/sharing/SharingManagerImpl.java index e4929fb7948d7a9d492782b15bd35223875c6896..9df89ceb6d5895323b8f0d9538ea7a749ea57d00 100644 --- a/briar-core/src/org/briarproject/sharing/SharingManagerImpl.java +++ b/briar-core/src/org/briarproject/sharing/SharingManagerImpl.java @@ -81,6 +81,7 @@ import static org.briarproject.api.sharing.SharingConstants.TO_BE_SHARED_BY_US; import static org.briarproject.api.sharing.SharingConstants.TYPE; import static org.briarproject.api.sharing.SharingMessage.BaseMessage; import static org.briarproject.api.sharing.SharingMessage.Invitation; +import static org.briarproject.sharing.InviteeSessionState.State.AWAIT_LOCAL_RESPONSE; abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM extends InvitationMessage, IS extends InviteeSessionState, SS extends SharerSessionState, IR extends InvitationReceivedEvent, IRR extends InvitationResponseReceivedEvent> extends BdfIncomingMessageHook @@ -116,8 +117,6 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM public abstract ClientId getClientId(); - protected abstract ClientId getShareableClientId(); - protected abstract IM createInvitationMessage(MessageId id, I msg, ContactId contactId, boolean available, long time, boolean local, boolean sent, boolean seen, boolean read); @@ -331,7 +330,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM new BdfEntry(TYPE, SHARE_MSG_TYPE_INVITATION) ); - Transaction txn = db.startTransaction(false); + Transaction txn = db.startTransaction(true); try { Contact contact = db.getContact(txn, contactId); Group group = getContactGroup(contact); @@ -356,7 +355,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM if (!(s instanceof InviteeSessionState)) continue; available = ((InviteeSessionState) s).getState() == - InviteeSessionState.State.AWAIT_LOCAL_RESPONSE; + AWAIT_LOCAL_RESPONSE; } IM im = createInvitationMessage(m.getKey(), msg, contactId, available, time, local, status.isSent(), @@ -377,32 +376,52 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM } @Override - public Collection<S> getAvailable() throws DbException { + public Collection<S> getInvited() throws DbException { + Transaction txn = db.startTransaction(true); try { - Set<S> available = new HashSet<S>(); - Transaction txn = db.startTransaction(true); + Set<S> invited = new HashSet<S>(); + Collection<Contact> contacts = db.getContacts(txn); + for (Contact contact : contacts) { + invited.addAll(getInvited(txn, contact)); + } + txn.setComplete(); + return Collections.unmodifiableCollection(invited); + } catch (FormatException e) { + throw new DbException(e); + } finally { + db.endTransaction(txn); + } + } + + private Collection<S> getInvited(Transaction txn, Contact contact) + throws DbException, FormatException { + + // query for all external invitations + BdfDictionary query = BdfDictionary.of( + new BdfEntry(TYPE, SHARE_MSG_TYPE_INVITATION), + new BdfEntry(LOCAL, false) + ); + Group group = getContactGroup(contact); + + Set<S> invited = new HashSet<S>(); + Map<MessageId, BdfDictionary> map = clientHelper + .getMessageMetadataAsDictionary(txn, group.getId(), query); + for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) { + BdfDictionary d = m.getValue(); try { - // Get any shareables we subscribe to - Set<Group> subscribed = new HashSet<Group>(db.getGroups(txn, - getShareableClientId())); - // Get all shareables shared by contacts - for (Contact c : db.getContacts(txn)) { - Group g = getContactGroup(c); - List<S> shareables = - getShareableList(txn, g.getId(), SHARED_WITH_US); - for (S f : shareables) { - if (!subscribed.contains(f.getGroup())) - available.add(f); - } + I msg = getIFactory().build(group.getId(), d); + IS iss = (IS) getSessionState(txn, msg.getSessionId(), true); + // get and add the shareable if the invitation is unanswered + if (iss.getState().equals(AWAIT_LOCAL_RESPONSE)) { + S s = getSFactory().parse(iss); + invited.add(s); } - txn.setComplete(); - } finally { - db.endTransaction(txn); + } catch (FormatException e) { + if (LOG.isLoggable(WARNING)) + LOG.log(WARNING, e.toString(), e); } - return Collections.unmodifiableSet(available); - } catch (IOException e) { - throw new DbException(e); } + return invited; } @Override @@ -473,7 +492,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM } } - protected void removingShareable(Transaction txn, S f) throws DbException { + void removingShareable(Transaction txn, S f) throws DbException { try { for (Contact c : db.getContacts(txn)) { GroupId g = getContactGroup(c).getId(); @@ -642,9 +661,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM new BdfEntry(IS_SHARER, false), new BdfEntry(CONTACT_ID, c.getId().getInt()), new BdfEntry(SHAREABLE_ID, f.getId()), - new BdfEntry(STATE, - InviteeSessionState.State.AWAIT_LOCAL_RESPONSE - .getValue()) + new BdfEntry(STATE, AWAIT_LOCAL_RESPONSE.getValue()) ); Map<MessageId, BdfDictionary> map = clientHelper