From f750280845d050d79d356cdc0acde5fab13254c3 Mon Sep 17 00:00:00 2001 From: str4d Date: Wed, 22 Jun 2016 04:18:14 +0000 Subject: [PATCH] Pass MessageId inside *InvitationReceivedEvent so we get the right one --- .../api/blogs/BlogSharingManager.java | 8 ++ .../event/BlogInvitationReceivedEvent.java | 6 +- .../event/ForumInvitationReceivedEvent.java | 7 +- .../api/event/InvitationReceivedEvent.java | 10 ++- .../api/forum/ForumSharingManager.java | 7 ++ .../api/sharing/SharingManager.java | 26 +++---- .../conversation/ConversationManagerImpl.java | 18 ++--- .../sharing/BlogSharingManagerImpl.java | 36 +++++---- .../sharing/ForumSharingManagerImpl.java | 36 +++++---- .../sharing/InvitationMessageFactory.java | 12 +++ .../briarproject/sharing/InviteeEngine.java | 36 +++++---- .../sharing/SharingManagerImpl.java | 75 +++++++++++++------ 12 files changed, 177 insertions(+), 100 deletions(-) create mode 100644 briar-core/src/org/briarproject/sharing/InvitationMessageFactory.java diff --git a/briar-api/src/org/briarproject/api/blogs/BlogSharingManager.java b/briar-api/src/org/briarproject/api/blogs/BlogSharingManager.java index 6fed69e19..1611fe482 100644 --- a/briar-api/src/org/briarproject/api/blogs/BlogSharingManager.java +++ b/briar-api/src/org/briarproject/api/blogs/BlogSharingManager.java @@ -6,6 +6,7 @@ import org.briarproject.api.db.DbException; import org.briarproject.api.sharing.SharingManager; import org.briarproject.api.sync.ClientId; import org.briarproject.api.sync.GroupId; +import org.briarproject.api.sync.MessageId; import java.util.Collection; @@ -37,6 +38,13 @@ public interface BlogSharingManager Collection getInvitationMessages( ContactId contactId) throws DbException; + /** + * Returns a specific blog sharing message sent by the Contact + * identified by contactId. + */ + BlogInvitationMessage getInvitationMessage(ContactId contactId, + MessageId messageId) throws DbException; + /** * Returns all blogs to which the user has been invited. */ diff --git a/briar-api/src/org/briarproject/api/event/BlogInvitationReceivedEvent.java b/briar-api/src/org/briarproject/api/event/BlogInvitationReceivedEvent.java index 12d8d4cc4..8b5590f75 100644 --- a/briar-api/src/org/briarproject/api/event/BlogInvitationReceivedEvent.java +++ b/briar-api/src/org/briarproject/api/event/BlogInvitationReceivedEvent.java @@ -2,13 +2,15 @@ package org.briarproject.api.event; import org.briarproject.api.blogs.Blog; import org.briarproject.api.contact.ContactId; +import org.briarproject.api.sync.MessageId; public class BlogInvitationReceivedEvent extends InvitationReceivedEvent { private final Blog blog; - public BlogInvitationReceivedEvent(Blog blog, ContactId contactId) { - super(contactId); + public BlogInvitationReceivedEvent(ContactId contactId, MessageId messageId, + Blog blog) { + super(contactId, messageId); this.blog = blog; } diff --git a/briar-api/src/org/briarproject/api/event/ForumInvitationReceivedEvent.java b/briar-api/src/org/briarproject/api/event/ForumInvitationReceivedEvent.java index 63aa3bce1..e539113a4 100644 --- a/briar-api/src/org/briarproject/api/event/ForumInvitationReceivedEvent.java +++ b/briar-api/src/org/briarproject/api/event/ForumInvitationReceivedEvent.java @@ -2,14 +2,15 @@ package org.briarproject.api.event; import org.briarproject.api.contact.ContactId; import org.briarproject.api.forum.Forum; -import org.briarproject.api.introduction.IntroductionRequest; +import org.briarproject.api.sync.MessageId; public class ForumInvitationReceivedEvent extends InvitationReceivedEvent { private final Forum forum; - public ForumInvitationReceivedEvent(Forum forum, ContactId contactId) { - super(contactId); + public ForumInvitationReceivedEvent(ContactId contactId, + MessageId messageId, Forum forum) { + super(contactId, messageId); this.forum = forum; } diff --git a/briar-api/src/org/briarproject/api/event/InvitationReceivedEvent.java b/briar-api/src/org/briarproject/api/event/InvitationReceivedEvent.java index 4b1b6df2d..9adc0072c 100644 --- a/briar-api/src/org/briarproject/api/event/InvitationReceivedEvent.java +++ b/briar-api/src/org/briarproject/api/event/InvitationReceivedEvent.java @@ -1,17 +1,23 @@ package org.briarproject.api.event; import org.briarproject.api.contact.ContactId; -import org.briarproject.api.forum.Forum; +import org.briarproject.api.sync.MessageId; public abstract class InvitationReceivedEvent extends Event { private final ContactId contactId; + private final MessageId messageId; - public InvitationReceivedEvent(ContactId contactId) { + public InvitationReceivedEvent(ContactId contactId, MessageId messageId) { this.contactId = contactId; + this.messageId = messageId; } public ContactId getContactId() { return contactId; } + + public MessageId getMessageId() { + return messageId; + } } diff --git a/briar-api/src/org/briarproject/api/forum/ForumSharingManager.java b/briar-api/src/org/briarproject/api/forum/ForumSharingManager.java index 7b842b574..78b081192 100644 --- a/briar-api/src/org/briarproject/api/forum/ForumSharingManager.java +++ b/briar-api/src/org/briarproject/api/forum/ForumSharingManager.java @@ -35,6 +35,13 @@ public interface ForumSharingManager extends SharingManager getInvitationMessages( ContactId contactId) throws DbException; + /** + * Returns a specific forum sharing message sent by the Contact + * identified by contactId. + */ + ForumInvitationMessage getInvitationMessage(ContactId contactId, + MessageId messageId) throws DbException; + /** Returns all forums to which the user has been invited. */ Collection getInvited() throws DbException; diff --git a/briar-api/src/org/briarproject/api/sharing/SharingManager.java b/briar-api/src/org/briarproject/api/sharing/SharingManager.java index 230a980bd..42eb423b6 100644 --- a/briar-api/src/org/briarproject/api/sharing/SharingManager.java +++ b/briar-api/src/org/briarproject/api/sharing/SharingManager.java @@ -1,5 +1,6 @@ package org.briarproject.api.sharing; +import org.briarproject.api.clients.ReadableMessageManager; import org.briarproject.api.contact.Contact; import org.briarproject.api.contact.ContactId; import org.briarproject.api.db.DbException; @@ -9,7 +10,8 @@ import org.briarproject.api.sync.MessageId; import java.util.Collection; -public interface SharingManager { +public interface SharingManager + extends ReadableMessageManager { /** Returns the unique ID of the group sharing client. */ ClientId getClientId(); @@ -34,6 +36,13 @@ public interface SharingManager getInvitationMessages( ContactId contactId) throws DbException; + /** + * Returns a specific group sharing message sent by the Contact + * identified by contactId. + */ + IM getInvitationMessage(ContactId contactId, MessageId messageId) + throws DbException; + /** Returns all shareables to which the user has been invited. */ Collection getInvited() throws DbException; @@ -46,19 +55,4 @@ public interface SharingManager msgs = forumSharingManager - .getInvitationMessages(event.getContactId()); - for (ForumInvitationMessage i : msgs) { - if (i.getForumName().equals(event.getForum().getName())) { - ConversationItem item = - ConversationForumInvitationItemImpl.from(i); - eventBus.broadcast( - new ConversationItemReceivedEvent(item, - event.getContactId())); - } - } + ForumInvitationMessage fim = forumSharingManager + .getInvitationMessage(event.getContactId(), + event.getMessageId()); + ConversationItem item = + ConversationForumInvitationItemImpl.from(fim); + eventBus.broadcast(new ConversationItemReceivedEvent(item, + event.getContactId())); } catch (DbException dbe) { if (LOG.isLoggable(WARNING)) LOG.log(WARNING, dbe.toString(), dbe); diff --git a/briar-core/src/org/briarproject/sharing/BlogSharingManagerImpl.java b/briar-core/src/org/briarproject/sharing/BlogSharingManagerImpl.java index a8bd64b2a..9caa1fc77 100644 --- a/briar-core/src/org/briarproject/sharing/BlogSharingManagerImpl.java +++ b/briar-core/src/org/briarproject/sharing/BlogSharingManagerImpl.java @@ -51,6 +51,7 @@ class BlogSharingManagerImpl extends private final SFactory sFactory; private final IFactory iFactory; + private final IMFactory imFactory; private final ISFactory isFactory; private final SSFactory ssFactory; private final IRFactory irFactory; @@ -69,6 +70,7 @@ class BlogSharingManagerImpl extends sFactory = new SFactory(authorFactory, blogFactory, blogManager); iFactory = new IFactory(); + imFactory = new IMFactory(); isFactory = new ISFactory(); ssFactory = new SSFactory(); irFactory = new IRFactory(sFactory); @@ -80,16 +82,6 @@ class BlogSharingManagerImpl extends return CLIENT_ID; } - @Override - protected BlogInvitationMessage createInvitationMessage(MessageId id, - BlogInvitation msg, ContactId contactId, boolean available, - long time, boolean local, boolean sent, boolean seen, - boolean read) { - return new BlogInvitationMessage(id, msg.getSessionId(), contactId, - msg.getBlogTitle(), msg.getMessage(), available, time, local, - sent, seen, read); - } - @Override protected ShareableFactory getSFactory() { return sFactory; @@ -100,6 +92,11 @@ class BlogSharingManagerImpl extends return iFactory; } + @Override + protected InvitationMessageFactory getIMFactory() { + return imFactory; + } + @Override protected InviteeSessionStateFactory getISFactory() { return isFactory; @@ -207,6 +204,20 @@ class BlogSharingManagerImpl extends } } + static class IMFactory implements + InvitationMessageFactory { + @Override + public BlogInvitationMessage build(MessageId id, + BlogInvitation msg, ContactId contactId, + boolean available, long time, boolean local, boolean sent, + boolean seen, boolean read) { + return new BlogInvitationMessage(id, msg.getSessionId(), contactId, + msg.getBlogTitle(), msg.getMessage(), available, time, + local, + sent, seen, read); + } + } + static class ISFactory implements InviteeSessionStateFactory { @Override @@ -275,9 +286,8 @@ class BlogSharingManagerImpl extends @Override public BlogInvitationReceivedEvent build( BlogInviteeSessionState localState) { - Blog blog = sFactory.parse(localState); - ContactId contactId = localState.getContactId(); - return new BlogInvitationReceivedEvent(blog, contactId); + return new BlogInvitationReceivedEvent(localState.getContactId(), + localState.getStorageId(), sFactory.parse(localState)); } } diff --git a/briar-core/src/org/briarproject/sharing/ForumSharingManagerImpl.java b/briar-core/src/org/briarproject/sharing/ForumSharingManagerImpl.java index 9cee63463..a6a8d6c4b 100644 --- a/briar-core/src/org/briarproject/sharing/ForumSharingManagerImpl.java +++ b/briar-core/src/org/briarproject/sharing/ForumSharingManagerImpl.java @@ -46,6 +46,7 @@ class ForumSharingManagerImpl extends private final SFactory sFactory; private final IFactory iFactory; + private final IMFactory imFactory; private final ISFactory isFactory; private final SSFactory ssFactory; private final IRFactory irFactory; @@ -67,6 +68,7 @@ class ForumSharingManagerImpl extends sFactory = new SFactory(forumFactory, forumManager); iFactory = new IFactory(); + imFactory = new IMFactory(); isFactory = new ISFactory(); ssFactory = new SSFactory(); irFactory = new IRFactory(sFactory); @@ -78,16 +80,6 @@ class ForumSharingManagerImpl extends return CLIENT_ID; } - @Override - protected ForumInvitationMessage createInvitationMessage(MessageId id, - ForumInvitation msg, ContactId contactId, boolean available, - long time, boolean local, boolean sent, boolean seen, - boolean read) { - return new ForumInvitationMessage(id, msg.getSessionId(), contactId, - msg.getForumName(), msg.getMessage(), available, time, local, - sent, seen, read); - } - @Override protected ShareableFactory getSFactory() { return sFactory; @@ -98,6 +90,11 @@ class ForumSharingManagerImpl extends return iFactory; } + @Override + protected InvitationMessageFactory getIMFactory() { + return imFactory; + } + @Override protected InviteeSessionStateFactory getISFactory() { return isFactory; @@ -185,6 +182,20 @@ class ForumSharingManagerImpl extends } } + static class IMFactory implements + InvitationMessageFactory { + @Override + public ForumInvitationMessage build(MessageId id, + ForumInvitation msg, ContactId contactId, + boolean available, long time, boolean local, boolean sent, + boolean seen, boolean read) { + return new ForumInvitationMessage(id, msg.getSessionId(), contactId, + msg.getForumName(), msg.getMessage(), available, time, + local, + sent, seen, read); + } + } + static class ISFactory implements InviteeSessionStateFactory { @Override @@ -245,9 +256,8 @@ class ForumSharingManagerImpl extends @Override public ForumInvitationReceivedEvent build( ForumInviteeSessionState localState) { - Forum forum = sFactory.parse(localState); - ContactId contactId = localState.getContactId(); - return new ForumInvitationReceivedEvent(forum, contactId); + return new ForumInvitationReceivedEvent(localState.getContactId(), + localState.getStorageId(), sFactory.parse(localState)); } } diff --git a/briar-core/src/org/briarproject/sharing/InvitationMessageFactory.java b/briar-core/src/org/briarproject/sharing/InvitationMessageFactory.java new file mode 100644 index 000000000..33d835ef0 --- /dev/null +++ b/briar-core/src/org/briarproject/sharing/InvitationMessageFactory.java @@ -0,0 +1,12 @@ +package org.briarproject.sharing; + +import org.briarproject.api.contact.ContactId; +import org.briarproject.api.sharing.InvitationMessage; +import org.briarproject.api.sharing.SharingMessage; +import org.briarproject.api.sync.MessageId; + +public interface InvitationMessageFactory { + + IM build(MessageId id, I msg, ContactId contactId, boolean available, + long time, boolean local, boolean sent, boolean seen, boolean read); +} diff --git a/briar-core/src/org/briarproject/sharing/InviteeEngine.java b/briar-core/src/org/briarproject/sharing/InviteeEngine.java index e786d9a74..82e92b671 100644 --- a/briar-core/src/org/briarproject/sharing/InviteeEngine.java +++ b/briar-core/src/org/briarproject/sharing/InviteeEngine.java @@ -29,9 +29,11 @@ public class InviteeEngine invitationReceivedEventFactory; + private final InvitationReceivedEventFactory + invitationReceivedEventFactory; - InviteeEngine(InvitationReceivedEventFactory invitationReceivedEventFactory) { + InviteeEngine( + InvitationReceivedEventFactory invitationReceivedEventFactory) { this.invitationReceivedEventFactory = invitationReceivedEventFactory; } @@ -44,7 +46,8 @@ public class InviteeEngine messages; List events = Collections.emptyList(); - if (action == InviteeSessionState.Action.LOCAL_ACCEPT || action == InviteeSessionState.Action.LOCAL_DECLINE) { + if (action == InviteeSessionState.Action.LOCAL_ACCEPT || + action == InviteeSessionState.Action.LOCAL_DECLINE) { BaseMessage msg; if (action == InviteeSessionState.Action.LOCAL_ACCEPT) { localState.setTask(TASK_ADD_SHARED_SHAREABLE); @@ -72,14 +76,12 @@ public class InviteeEngine(false, @@ -95,7 +97,8 @@ public class InviteeEngine(deleteMsg, @@ -162,7 +165,8 @@ public class InviteeEngine getSFactory(); protected abstract InvitationFactory getIFactory(); + protected abstract InvitationMessageFactory getIMFactory(); + protected abstract InviteeSessionStateFactory getISFactory(); protected abstract SharerSessionStateFactory getSSFactory(); @@ -343,26 +341,10 @@ abstract class SharingManagerImpl m : map.entrySet()) { BdfDictionary d = m.getValue(); try { - I msg = getIFactory().build(group.getId(), d); - MessageStatus status = - db.getMessageStatus(txn, contactId, m.getKey()); - long time = d.getLong(TIMESTAMP); - boolean local = d.getBoolean(LOCAL); - boolean read = d.getBoolean(READ, false); - boolean available = false; - if (!local) { - // figure out whether the shareable is still available - SharingSessionState s = - getSessionState(txn, msg.getSessionId(), true); - if (!(s instanceof InviteeSessionState)) - continue; - available = ((InviteeSessionState) s).getState() == - AWAIT_LOCAL_RESPONSE; - } - IM im = createInvitationMessage(m.getKey(), msg, contactId, - available, time, local, status.isSent(), - status.isSeen(), read); - list.add(im); + IM im = getInvitationMessage(txn, group.getId(), contactId, + m.getKey(), d); + if (im != null) + list.add(im); } catch (FormatException e) { if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e); @@ -377,6 +359,50 @@ abstract class SharingManagerImpl getInvited() throws DbException { Transaction txn = db.startTransaction(true); @@ -849,6 +875,7 @@ abstract class SharingManagerImpl