Pass MessageId inside *InvitationReceivedEvent so we get the right one

This commit is contained in:
str4d
2016-06-22 04:18:14 +00:00
parent 1e8784efe9
commit f750280845
12 changed files with 177 additions and 100 deletions

View File

@@ -261,17 +261,13 @@ public class ConversationManagerImpl implements ConversationManager,
ForumInvitationReceivedEvent event =
(ForumInvitationReceivedEvent) e;
try {
Collection<ForumInvitationMessage> 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);

View File

@@ -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<Blog, BlogInvitation, BlogInviteeSessionState, BlogSharerSessionState> getSFactory() {
return sFactory;
@@ -100,6 +92,11 @@ class BlogSharingManagerImpl extends
return iFactory;
}
@Override
protected InvitationMessageFactory<BlogInvitation, BlogInvitationMessage> getIMFactory() {
return imFactory;
}
@Override
protected InviteeSessionStateFactory<Blog, BlogInviteeSessionState> getISFactory() {
return isFactory;
@@ -207,6 +204,20 @@ class BlogSharingManagerImpl extends
}
}
static class IMFactory implements
InvitationMessageFactory<BlogInvitation, BlogInvitationMessage> {
@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<Blog, BlogInviteeSessionState> {
@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));
}
}

View File

@@ -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<Forum, ForumInvitation, ForumInviteeSessionState, ForumSharerSessionState> getSFactory() {
return sFactory;
@@ -98,6 +90,11 @@ class ForumSharingManagerImpl extends
return iFactory;
}
@Override
protected InvitationMessageFactory<ForumInvitation, ForumInvitationMessage> getIMFactory() {
return imFactory;
}
@Override
protected InviteeSessionStateFactory<Forum, ForumInviteeSessionState> getISFactory() {
return isFactory;
@@ -185,6 +182,20 @@ class ForumSharingManagerImpl extends
}
}
static class IMFactory implements
InvitationMessageFactory<ForumInvitation, ForumInvitationMessage> {
@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<Forum, ForumInviteeSessionState> {
@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));
}
}

View File

@@ -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<I extends SharingMessage.Invitation, IM extends InvitationMessage> {
IM build(MessageId id, I msg, ContactId contactId, boolean available,
long time, boolean local, boolean sent, boolean seen, boolean read);
}

View File

@@ -29,9 +29,11 @@ public class InviteeEngine<IS extends InviteeSessionState, IR extends Invitation
private static final Logger LOG =
Logger.getLogger(InviteeEngine.class.getName());
private final InvitationReceivedEventFactory<IS, IR> invitationReceivedEventFactory;
private final InvitationReceivedEventFactory<IS, IR>
invitationReceivedEventFactory;
InviteeEngine(InvitationReceivedEventFactory<IS, IR> invitationReceivedEventFactory) {
InviteeEngine(
InvitationReceivedEventFactory<IS, IR> invitationReceivedEventFactory) {
this.invitationReceivedEventFactory = invitationReceivedEventFactory;
}
@@ -44,7 +46,8 @@ public class InviteeEngine<IS extends InviteeSessionState, IR extends Invitation
InviteeSessionState.State nextState = currentState.next(action);
localState.setState(nextState);
if (action == InviteeSessionState.Action.LOCAL_ABORT && currentState != InviteeSessionState.State.ERROR) {
if (action == InviteeSessionState.Action.LOCAL_ABORT &&
currentState != InviteeSessionState.State.ERROR) {
return abortSession(currentState, localState);
}
@@ -58,7 +61,8 @@ public class InviteeEngine<IS extends InviteeSessionState, IR extends Invitation
List<BaseMessage> messages;
List<Event> 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<IS extends InviteeSessionState, IR extends Invitation
}
messages = Collections.singletonList(msg);
logLocalAction(currentState, localState, msg);
}
else if (action == InviteeSessionState.Action.LOCAL_LEAVE) {
} else if (action == InviteeSessionState.Action.LOCAL_LEAVE) {
BaseMessage msg = new SimpleMessage(SHARE_MSG_TYPE_LEAVE,
localState.getGroupId(), localState.getSessionId());
messages = Collections.singletonList(msg);
logLocalAction(currentState, localState, msg);
}
else {
} else {
throw new IllegalArgumentException("Unknown Local Action");
}
return new StateUpdate<IS, BaseMessage>(false,
@@ -95,7 +97,8 @@ public class InviteeEngine<IS extends InviteeSessionState, IR extends Invitation
try {
InviteeSessionState.State currentState = localState.getState();
InviteeSessionState.Action action = InviteeSessionState.Action.getRemote(msg.getType());
InviteeSessionState.Action action =
InviteeSessionState.Action.getRemote(msg.getType());
InviteeSessionState.State nextState = currentState.next(action);
localState.setState(nextState);
@@ -118,25 +121,25 @@ public class InviteeEngine<IS extends InviteeSessionState, IR extends Invitation
deleteMsg = true;
}
// the sharer left the forum she had shared with us
else if (action == InviteeSessionState.Action.REMOTE_LEAVE && currentState == InviteeSessionState.State.FINISHED) {
else if (action == InviteeSessionState.Action.REMOTE_LEAVE &&
currentState == InviteeSessionState.State.FINISHED) {
localState.setTask(TASK_UNSHARE_SHAREABLE_SHARED_WITH_US);
}
else if (currentState == InviteeSessionState.State.FINISHED) {
} else if (currentState == InviteeSessionState.State.FINISHED) {
// ignore and delete messages coming in while in that state
// note that LEAVE is possible, but was handled above
deleteMsg = true;
}
// the sharer left the forum before we couldn't even respond
else if (action == InviteeSessionState.Action.REMOTE_LEAVE) {
localState.setTask(TASK_REMOVE_SHAREABLE_FROM_LIST_SHARED_WITH_US);
localState.setTask(
TASK_REMOVE_SHAREABLE_FROM_LIST_SHARED_WITH_US);
}
// we have just received our invitation
else if (action == InviteeSessionState.Action.REMOTE_INVITATION) {
localState.setTask(TASK_ADD_SHAREABLE_TO_LIST_SHARED_WITH_US);
Event event = invitationReceivedEventFactory.build(localState);
events = Collections.singletonList(event);
}
else {
} else {
throw new IllegalArgumentException("Bad state");
}
return new StateUpdate<IS, BaseMessage>(deleteMsg,
@@ -162,7 +165,8 @@ public class InviteeEngine<IS extends InviteeSessionState, IR extends Invitation
);
}
private void logMessageReceived(InviteeSessionState.State currentState, InviteeSessionState.State nextState,
private void logMessageReceived(InviteeSessionState.State currentState,
InviteeSessionState.State nextState,
long type, BaseMessage msg) {
if (!LOG.isLoggable(INFO)) return;

View File

@@ -115,14 +115,12 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
public abstract ClientId getClientId();
protected abstract IM createInvitationMessage(MessageId id, I msg,
ContactId contactId, boolean available, long time, boolean local,
boolean sent, boolean seen, boolean read);
protected abstract ShareableFactory<S, I, IS, SS> getSFactory();
protected abstract InvitationFactory<I, SS> getIFactory();
protected abstract InvitationMessageFactory<I, IM> getIMFactory();
protected abstract InviteeSessionStateFactory<S, IS> getISFactory();
protected abstract SharerSessionStateFactory<S, SS> getSSFactory();
@@ -343,26 +341,10 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
for (Map.Entry<MessageId, BdfDictionary> 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<S extends Shareable, I extends Invitation, IM
}
}
@Override
public IM getInvitationMessage(ContactId contactId, MessageId messageId)
throws DbException {
Transaction txn = db.startTransaction(false);
try {
Contact contact = db.getContact(txn, contactId);
Group group = getContactGroup(contact);
BdfDictionary d =
clientHelper.getMessageMetadataAsDictionary(txn, messageId);
IM im = getInvitationMessage(txn, group.getId(), contactId,
messageId, d);
txn.setComplete();
return im;
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
}
private IM getInvitationMessage(Transaction txn, GroupId groupId,
ContactId contactId, MessageId id, BdfDictionary d)
throws DbException, FormatException {
I msg = getIFactory().build(groupId, d);
MessageStatus status =
db.getMessageStatus(txn, contactId, id);
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))
return null;
available = ((InviteeSessionState) s).getState() ==
AWAIT_LOCAL_RESPONSE;
}
return getIMFactory().build(id, msg, contactId, available, time, local,
status.isSent(), status.isSeen(), read);
}
@Override
public Collection<S> getInvited() throws DbException {
Transaction txn = db.startTransaction(true);
@@ -849,6 +875,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
.sendMessage(txn, group, timestamp, body, meta);
}
@Override
protected Group getContactGroup(Contact c) {
return privateGroupFactory.createPrivateGroup(getClientId(), c);
}