mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 19:59:05 +01:00
Unify all private message responses in one PrivateResponse class
This also adds `Shareable`s to invitation response which is a precondition for #561
This commit is contained in:
@@ -20,6 +20,7 @@ import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.briar.api.client.MessageTracker;
|
||||
import org.briarproject.briar.api.client.SessionId;
|
||||
import org.briarproject.briar.api.introduction.Introduction;
|
||||
import org.briarproject.briar.api.introduction.IntroductionResponse;
|
||||
import org.briarproject.briar.api.introduction.event.IntroductionResponseReceivedEvent;
|
||||
|
||||
@@ -147,12 +148,13 @@ abstract class AbstractProtocolEngine<S extends Session>
|
||||
void broadcastIntroductionResponseReceivedEvent(Transaction txn, Session s,
|
||||
AuthorId sender, Author otherAuthor, AbstractIntroductionMessage m)
|
||||
throws DbException {
|
||||
Introduction introduction = new Introduction(otherAuthor, s.getRole());
|
||||
AuthorId localAuthorId = identityManager.getLocalAuthor(txn).getId();
|
||||
Contact c = contactManager.getContact(txn, sender, localAuthorId);
|
||||
IntroductionResponse response =
|
||||
new IntroductionResponse(s.getSessionId(), m.getMessageId(),
|
||||
m.getGroupId(), s.getRole(), m.getTimestamp(), false,
|
||||
false, false, false, otherAuthor.getName(),
|
||||
new IntroductionResponse(m.getMessageId(), m.getGroupId(),
|
||||
m.getTimestamp(), false, false, false, false,
|
||||
s.getSessionId(), introduction,
|
||||
m instanceof AcceptMessage);
|
||||
IntroductionResponseReceivedEvent e =
|
||||
new IntroductionResponseReceivedEvent(c.getId(), response);
|
||||
|
||||
@@ -509,9 +509,10 @@ class IntroductionManagerImpl extends ConversationClientImpl
|
||||
sessionId = session.getSessionId();
|
||||
author = session.getRemote().author;
|
||||
} else throw new AssertionError();
|
||||
return new IntroductionResponse(sessionId, m, contactGroupId,
|
||||
role, meta.getTimestamp(), meta.isLocal(), status.isSent(),
|
||||
status.isSeen(), meta.isRead(), author.getName(), accept);
|
||||
Introduction introduction = new Introduction(author, role);
|
||||
return new IntroductionResponse(m, contactGroupId, meta.getTimestamp(),
|
||||
meta.isLocal(), status.isSent(), status.isSeen(), meta.isRead(),
|
||||
sessionId, introduction, accept);
|
||||
}
|
||||
|
||||
private void removeSessionWithIntroducer(Transaction txn,
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.briarproject.briar.api.client.MessageTracker;
|
||||
import org.briarproject.briar.api.client.ProtocolStateException;
|
||||
import org.briarproject.briar.api.client.SessionId;
|
||||
import org.briarproject.briar.api.privategroup.GroupMessageFactory;
|
||||
import org.briarproject.briar.api.privategroup.PrivateGroup;
|
||||
import org.briarproject.briar.api.privategroup.PrivateGroupFactory;
|
||||
import org.briarproject.briar.api.privategroup.PrivateGroupManager;
|
||||
import org.briarproject.briar.api.privategroup.event.GroupInvitationResponseReceivedEvent;
|
||||
@@ -193,8 +194,10 @@ class CreatorProtocolEngine extends AbstractProtocolEngine<CreatorSession> {
|
||||
setPrivateGroupVisibility(txn, s, SHARED);
|
||||
// Broadcast an event
|
||||
ContactId contactId = getContactId(txn, m.getContactGroupId());
|
||||
PrivateGroup privateGroup =
|
||||
privateGroupManager.getPrivateGroup(txn, m.getPrivateGroupId());
|
||||
txn.attach(new GroupInvitationResponseReceivedEvent(contactId,
|
||||
createInvitationResponse(m, contactId, true)));
|
||||
createInvitationResponse(m, privateGroup, true)));
|
||||
// Move to the JOINED state
|
||||
return new CreatorSession(s.getContactGroupId(), s.getPrivateGroupId(),
|
||||
sent.getId(), m.getId(), sent.getTimestamp(),
|
||||
@@ -215,8 +218,10 @@ class CreatorProtocolEngine extends AbstractProtocolEngine<CreatorSession> {
|
||||
m.getTimestamp(), false);
|
||||
// Broadcast an event
|
||||
ContactId contactId = getContactId(txn, m.getContactGroupId());
|
||||
PrivateGroup privateGroup =
|
||||
privateGroupManager.getPrivateGroup(txn, m.getPrivateGroupId());
|
||||
txn.attach(new GroupInvitationResponseReceivedEvent(contactId,
|
||||
createInvitationResponse(m, contactId, false)));
|
||||
createInvitationResponse(m, privateGroup, false)));
|
||||
// Move to the START state
|
||||
return new CreatorSession(s.getContactGroupId(), s.getPrivateGroupId(),
|
||||
s.getLastLocalMessageId(), m.getId(), s.getLocalTimestamp(),
|
||||
@@ -254,10 +259,11 @@ class CreatorProtocolEngine extends AbstractProtocolEngine<CreatorSession> {
|
||||
}
|
||||
|
||||
private GroupInvitationResponse createInvitationResponse(
|
||||
GroupInvitationMessage m, ContactId c, boolean accept) {
|
||||
GroupInvitationMessage m, PrivateGroup privateGroup,
|
||||
boolean accept) {
|
||||
SessionId sessionId = new SessionId(m.getPrivateGroupId().getBytes());
|
||||
return new GroupInvitationResponse(m.getId(), m.getContactGroupId(),
|
||||
m.getTimestamp(), false, false, true, false, sessionId,
|
||||
m.getPrivateGroupId(), accept);
|
||||
privateGroup, accept);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -353,7 +353,7 @@ class GroupInvitationManagerImpl extends ConversationClientImpl
|
||||
|
||||
private <S extends Session> S handleAction(Transaction txn,
|
||||
LocalAction type, S session, ProtocolEngine<S> engine)
|
||||
throws DbException, FormatException {
|
||||
throws DbException {
|
||||
if (type == LocalAction.INVITE) {
|
||||
throw new IllegalArgumentException();
|
||||
} else if (type == LocalAction.JOIN) {
|
||||
@@ -392,23 +392,36 @@ class GroupInvitationManagerImpl extends ConversationClientImpl
|
||||
.getMessageMetadataAsDictionary(txn, contactGroupId, query);
|
||||
List<PrivateMessageHeader> messages =
|
||||
new ArrayList<>(results.size());
|
||||
// get invite message first and remember private groups
|
||||
Map<SessionId, PrivateGroup> privateGroups = new HashMap<>();
|
||||
for (Entry<MessageId, BdfDictionary> e : results.entrySet()) {
|
||||
MessageId m = e.getKey();
|
||||
MessageMetadata meta =
|
||||
messageParser.parseMetadata(e.getValue());
|
||||
MessageStatus status = db.getMessageStatus(txn, c, m);
|
||||
MessageType type = meta.getMessageType();
|
||||
if (type == INVITE) {
|
||||
messages.add(parseInvitationRequest(txn, c, contactGroupId,
|
||||
m, meta, status));
|
||||
} else if (type == JOIN) {
|
||||
messages.add(
|
||||
parseInvitationResponse(c, contactGroupId, m, meta,
|
||||
status, true));
|
||||
if (type != INVITE) continue;
|
||||
MessageStatus status = db.getMessageStatus(txn, c, m);
|
||||
GroupInvitationRequest invite = parseInvitationRequest(txn,
|
||||
contactGroupId, m, meta, status);
|
||||
messages.add(invite);
|
||||
privateGroups.put(invite.getSessionId(), invite.getObject());
|
||||
}
|
||||
for (Entry<MessageId, BdfDictionary> e : results.entrySet()) {
|
||||
MessageId m = e.getKey();
|
||||
MessageMetadata meta =
|
||||
messageParser.parseMetadata(e.getValue());
|
||||
MessageType type = meta.getMessageType();
|
||||
if (type == INVITE) continue;
|
||||
MessageStatus status = db.getMessageStatus(txn, c, m);
|
||||
SessionId sessionId = getSessionId(meta.getPrivateGroupId());
|
||||
PrivateGroup privateGroup = privateGroups.get(sessionId);
|
||||
if (privateGroup == null) throw new AssertionError();
|
||||
if (type == JOIN) {
|
||||
messages.add(parseInvitationResponse(contactGroupId, m,
|
||||
meta, status, sessionId, privateGroup, true));
|
||||
} else if (type == LEAVE) {
|
||||
messages.add(
|
||||
parseInvitationResponse(c, contactGroupId, m, meta,
|
||||
status, false));
|
||||
messages.add(parseInvitationResponse(contactGroupId, m,
|
||||
meta, status, sessionId, privateGroup, false));
|
||||
}
|
||||
}
|
||||
return messages;
|
||||
@@ -418,9 +431,8 @@ class GroupInvitationManagerImpl extends ConversationClientImpl
|
||||
}
|
||||
|
||||
private GroupInvitationRequest parseInvitationRequest(Transaction txn,
|
||||
ContactId c, GroupId contactGroupId, MessageId m,
|
||||
MessageMetadata meta, MessageStatus status)
|
||||
throws DbException, FormatException {
|
||||
GroupId contactGroupId, MessageId m, MessageMetadata meta,
|
||||
MessageStatus status) throws DbException, FormatException {
|
||||
SessionId sessionId = getSessionId(meta.getPrivateGroupId());
|
||||
// Look up the invite message to get the details of the private group
|
||||
InviteMessage invite = messageParser.getInviteMessage(txn, m);
|
||||
@@ -436,15 +448,14 @@ class GroupInvitationManagerImpl extends ConversationClientImpl
|
||||
invite.getMessage(), meta.isAvailableToAnswer(), canBeOpened);
|
||||
}
|
||||
|
||||
private GroupInvitationResponse parseInvitationResponse(ContactId c,
|
||||
private GroupInvitationResponse parseInvitationResponse(
|
||||
GroupId contactGroupId, MessageId m, MessageMetadata meta,
|
||||
MessageStatus status, boolean accept)
|
||||
throws DbException, FormatException {
|
||||
SessionId sessionId = getSessionId(meta.getPrivateGroupId());
|
||||
MessageStatus status, SessionId sessionId,
|
||||
PrivateGroup privateGroup, boolean accept) {
|
||||
return new GroupInvitationResponse(m, contactGroupId,
|
||||
meta.getTimestamp(), meta.isLocal(), status.isSent(),
|
||||
status.isSeen(), meta.isRead(), sessionId,
|
||||
meta.getPrivateGroupId(), accept);
|
||||
status.isSeen(), meta.isRead(), sessionId, privateGroup,
|
||||
accept);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -30,10 +30,10 @@ public class BlogInvitationFactoryImpl
|
||||
@Override
|
||||
public BlogInvitationResponse createInvitationResponse(MessageId id,
|
||||
GroupId contactGroupId, long time, boolean local, boolean sent,
|
||||
boolean seen, boolean read, GroupId shareableId, boolean accept) {
|
||||
SessionId sessionId = new SessionId(shareableId.getBytes());
|
||||
return new BlogInvitationResponse(id, contactGroupId, time, local,
|
||||
sent, seen, read, sessionId, shareableId, accept);
|
||||
boolean seen, boolean read, Blog blog, boolean accept) {
|
||||
SessionId sessionId = new SessionId(blog.getId().getBytes());
|
||||
return new BlogInvitationResponse(id, contactGroupId, time, local, sent,
|
||||
seen, read, sessionId, blog, accept);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ class BlogMessageParserImpl extends MessageParserImpl<Blog> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Blog createShareable(BdfList descriptor) throws FormatException {
|
||||
public Blog createShareable(BdfList descriptor) throws FormatException {
|
||||
// Author, RSS
|
||||
BdfList authorList = descriptor.getList(0);
|
||||
boolean rssFeed = descriptor.getBoolean(1);
|
||||
|
||||
@@ -59,21 +59,21 @@ class BlogProtocolEngineImpl extends ProtocolEngineImpl<Blog> {
|
||||
|
||||
@Override
|
||||
Event getInvitationResponseReceivedEvent(AcceptMessage m,
|
||||
ContactId contactId) {
|
||||
ContactId contactId, Blog shareable) {
|
||||
BlogInvitationResponse response = invitationFactory
|
||||
.createInvitationResponse(m.getId(), m.getContactGroupId(),
|
||||
m.getTimestamp(), false, false, true, false,
|
||||
m.getShareableId(), true);
|
||||
shareable, true);
|
||||
return new BlogInvitationResponseReceivedEvent(contactId, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
Event getInvitationResponseReceivedEvent(DeclineMessage m,
|
||||
ContactId contactId) {
|
||||
ContactId contactId, Blog shareable) {
|
||||
BlogInvitationResponse response = invitationFactory
|
||||
.createInvitationResponse(m.getId(), m.getContactGroupId(),
|
||||
m.getTimestamp(), false, false, true, false,
|
||||
m.getShareableId(), true);
|
||||
shareable, true);
|
||||
return new BlogInvitationResponseReceivedEvent(contactId, response);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,11 +30,10 @@ public class ForumInvitationFactoryImpl
|
||||
@Override
|
||||
public ForumInvitationResponse createInvitationResponse(MessageId id,
|
||||
GroupId contactGroupId, long time, boolean local, boolean sent,
|
||||
boolean seen, boolean read, GroupId shareableId,
|
||||
boolean accept) {
|
||||
SessionId sessionId = new SessionId(shareableId.getBytes());
|
||||
boolean seen, boolean read, Forum forum, boolean accept) {
|
||||
SessionId sessionId = new SessionId(forum.getId().getBytes());
|
||||
return new ForumInvitationResponse(id, contactGroupId, time, local,
|
||||
sent, seen, read, sessionId, shareableId, accept);
|
||||
sent, seen, read, sessionId, forum, accept);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ class ForumMessageParserImpl extends MessageParserImpl<Forum> {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Forum createShareable(BdfList descriptor) throws FormatException {
|
||||
public Forum createShareable(BdfList descriptor) throws FormatException {
|
||||
// Name, salt
|
||||
String name = descriptor.getString(0);
|
||||
byte[] salt = descriptor.getRaw(1);
|
||||
|
||||
@@ -61,21 +61,21 @@ class ForumProtocolEngineImpl extends ProtocolEngineImpl<Forum> {
|
||||
|
||||
@Override
|
||||
Event getInvitationResponseReceivedEvent(AcceptMessage m,
|
||||
ContactId contactId) {
|
||||
ContactId contactId, Forum shareable) {
|
||||
ForumInvitationResponse response = invitationFactory
|
||||
.createInvitationResponse(m.getId(), m.getContactGroupId(),
|
||||
m.getTimestamp(), false, false, true, false,
|
||||
m.getShareableId(), true);
|
||||
shareable, true);
|
||||
return new ForumInvitationResponseReceivedEvent(contactId, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
Event getInvitationResponseReceivedEvent(DeclineMessage m,
|
||||
ContactId contactId) {
|
||||
ContactId contactId, Forum shareable) {
|
||||
ForumInvitationResponse response = invitationFactory
|
||||
.createInvitationResponse(m.getId(), m.getContactGroupId(),
|
||||
m.getTimestamp(), false, false, true, false,
|
||||
m.getShareableId(), true);
|
||||
shareable, true);
|
||||
return new ForumInvitationResponseReceivedEvent(contactId, response);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,10 +4,10 @@ import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.api.messaging.PrivateRequest;
|
||||
import org.briarproject.briar.api.sharing.InvitationResponse;
|
||||
import org.briarproject.briar.api.messaging.PrivateResponse;
|
||||
import org.briarproject.briar.api.sharing.Shareable;
|
||||
|
||||
public interface InvitationFactory<S extends Shareable, I extends InvitationResponse> {
|
||||
public interface InvitationFactory<S extends Shareable, I extends PrivateResponse<S>> {
|
||||
|
||||
PrivateRequest<S> createInvitationRequest(boolean local, boolean sent,
|
||||
boolean seen, boolean read, InviteMessage<S> m, ContactId c,
|
||||
@@ -15,7 +15,6 @@ public interface InvitationFactory<S extends Shareable, I extends InvitationResp
|
||||
|
||||
I createInvitationResponse(MessageId id,
|
||||
GroupId contactGroupId, long time, boolean local, boolean sent,
|
||||
boolean seen, boolean read, GroupId shareableId,
|
||||
boolean accept);
|
||||
boolean seen, boolean read, S shareable, boolean accept);
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@ interface MessageParser<S extends Shareable> {
|
||||
|
||||
MessageMetadata parseMetadata(BdfDictionary meta) throws FormatException;
|
||||
|
||||
S createShareable(BdfList descriptor) throws FormatException;
|
||||
|
||||
InviteMessage<S> getInviteMessage(Transaction txn, MessageId m)
|
||||
throws DbException, FormatException;
|
||||
|
||||
|
||||
@@ -94,9 +94,6 @@ abstract class MessageParserImpl<S extends Shareable>
|
||||
m.getGroupId(), shareable, message, m.getTimestamp());
|
||||
}
|
||||
|
||||
protected abstract S createShareable(BdfList descriptor)
|
||||
throws FormatException;
|
||||
|
||||
@Override
|
||||
public AcceptMessage parseAcceptMessage(Message m, BdfList body)
|
||||
throws FormatException {
|
||||
|
||||
@@ -113,13 +113,7 @@ abstract class ProtocolEngineImpl<S extends Shareable>
|
||||
|
||||
private Message sendInviteMessage(Transaction txn, Session s,
|
||||
@Nullable String message, long timestamp) throws DbException {
|
||||
Group g = db.getGroup(txn, s.getShareableId());
|
||||
BdfList descriptor;
|
||||
try {
|
||||
descriptor = clientHelper.toList(g.getDescriptor());
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e); // Invalid group descriptor
|
||||
}
|
||||
BdfList descriptor = getDescriptor(txn, s.getShareableId());
|
||||
long localTimestamp = Math.max(timestamp, getLocalTimestamp(s));
|
||||
Message m = messageEncoder.encodeInviteMessage(s.getContactGroupId(),
|
||||
localTimestamp, s.getLastLocalMessageId(), descriptor, message);
|
||||
@@ -373,7 +367,8 @@ abstract class ProtocolEngineImpl<S extends Shareable>
|
||||
m.getTimestamp(), false);
|
||||
// Broadcast an event
|
||||
ContactId contactId = getContactId(txn, m.getContactGroupId());
|
||||
txn.attach(getInvitationResponseReceivedEvent(m, contactId));
|
||||
S shareable = getShareable(txn, s.getShareableId());
|
||||
txn.attach(getInvitationResponseReceivedEvent(m, contactId, shareable));
|
||||
// Move to the next state
|
||||
return new Session(nextState, s.getContactGroupId(), s.getShareableId(),
|
||||
s.getLastLocalMessageId(), m.getId(), s.getLocalTimestamp(),
|
||||
@@ -391,7 +386,7 @@ abstract class ProtocolEngineImpl<S extends Shareable>
|
||||
}
|
||||
|
||||
abstract Event getInvitationResponseReceivedEvent(AcceptMessage m,
|
||||
ContactId contactId);
|
||||
ContactId contactId, S shareable);
|
||||
|
||||
@Override
|
||||
public Session onDeclineMessage(Transaction txn, Session s,
|
||||
@@ -431,7 +426,8 @@ abstract class ProtocolEngineImpl<S extends Shareable>
|
||||
}
|
||||
// Broadcast an event
|
||||
ContactId contactId = getContactId(txn, m.getContactGroupId());
|
||||
txn.attach(getInvitationResponseReceivedEvent(m, contactId));
|
||||
S shareable = getShareable(txn, s.getShareableId());
|
||||
txn.attach(getInvitationResponseReceivedEvent(m, contactId, shareable));
|
||||
// Move to the next state
|
||||
return new Session(START, s.getContactGroupId(), s.getShareableId(),
|
||||
s.getLastLocalMessageId(), m.getId(), s.getLocalTimestamp(),
|
||||
@@ -439,7 +435,7 @@ abstract class ProtocolEngineImpl<S extends Shareable>
|
||||
}
|
||||
|
||||
abstract Event getInvitationResponseReceivedEvent(DeclineMessage m,
|
||||
ContactId contactId);
|
||||
ContactId contactId, S shareable);
|
||||
|
||||
@Override
|
||||
public Session onLeaveMessage(Transaction txn, Session s,
|
||||
@@ -530,6 +526,26 @@ abstract class ProtocolEngineImpl<S extends Shareable>
|
||||
sent.getId(), null, 0, 0);
|
||||
}
|
||||
|
||||
private S getShareable(Transaction txn, GroupId groupId)
|
||||
throws DbException {
|
||||
BdfList descriptor = getDescriptor(txn, groupId);
|
||||
try {
|
||||
return messageParser.createShareable(descriptor);
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private BdfList getDescriptor(Transaction txn, GroupId groupId)
|
||||
throws DbException {
|
||||
Group g = db.getGroup(txn, groupId);
|
||||
try {
|
||||
return clientHelper.toList(g.getDescriptor());
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e); // Invalid group descriptor
|
||||
}
|
||||
}
|
||||
|
||||
private void markInvitesUnavailableToAnswer(Transaction txn, Session s)
|
||||
throws DbException, FormatException {
|
||||
GroupId shareableId = s.getShareableId();
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.briarproject.briar.api.client.MessageTracker;
|
||||
import org.briarproject.briar.api.client.SessionId;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
import org.briarproject.briar.api.messaging.PrivateRequest;
|
||||
import org.briarproject.briar.api.sharing.InvitationResponse;
|
||||
import org.briarproject.briar.api.messaging.PrivateResponse;
|
||||
import org.briarproject.briar.api.sharing.Shareable;
|
||||
import org.briarproject.briar.api.sharing.SharingInvitationItem;
|
||||
import org.briarproject.briar.api.sharing.SharingManager;
|
||||
@@ -345,23 +345,35 @@ abstract class SharingManagerImpl<S extends Shareable>
|
||||
.getMessageMetadataAsDictionary(txn, contactGroupId, query);
|
||||
List<PrivateMessageHeader> messages =
|
||||
new ArrayList<>(results.size());
|
||||
// get invite messages first and remember shareables
|
||||
Map<GroupId, S> shareables = new HashMap<>();
|
||||
for (Entry<MessageId, BdfDictionary> e : results.entrySet()) {
|
||||
MessageId m = e.getKey();
|
||||
MessageMetadata meta =
|
||||
messageParser.parseMetadata(e.getValue());
|
||||
MessageStatus status = db.getMessageStatus(txn, c, m);
|
||||
MessageType type = meta.getMessageType();
|
||||
if (type == INVITE) {
|
||||
messages.add(
|
||||
parseInvitationRequest(txn, c, m, meta, status));
|
||||
} else if (type == ACCEPT) {
|
||||
messages.add(
|
||||
parseInvitationResponse(contactGroupId, m, meta,
|
||||
status, true));
|
||||
if (type != INVITE) continue;
|
||||
MessageStatus status = db.getMessageStatus(txn, c, m);
|
||||
PrivateRequest<S> invite = parseInvitationRequest(txn, c, m,
|
||||
meta, status);
|
||||
messages.add(invite);
|
||||
shareables.put(invite.getObject().getId(), invite.getObject());
|
||||
}
|
||||
for (Entry<MessageId, BdfDictionary> e : results.entrySet()) {
|
||||
MessageId m = e.getKey();
|
||||
MessageMetadata meta =
|
||||
messageParser.parseMetadata(e.getValue());
|
||||
MessageType type = meta.getMessageType();
|
||||
if (type == INVITE) continue;
|
||||
MessageStatus status = db.getMessageStatus(txn, c, m);
|
||||
S shareable = shareables.get(meta.getShareableId());
|
||||
if (shareable == null) throw new AssertionError();
|
||||
if (type == ACCEPT) {
|
||||
messages.add(parseInvitationResponse(contactGroupId, m,
|
||||
meta, status, shareable, true));
|
||||
} else if (type == DECLINE) {
|
||||
messages.add(
|
||||
parseInvitationResponse(contactGroupId, m, meta,
|
||||
status, false));
|
||||
messages.add(parseInvitationResponse(contactGroupId, m,
|
||||
meta, status, shareable, false));
|
||||
}
|
||||
}
|
||||
return messages;
|
||||
@@ -370,7 +382,7 @@ abstract class SharingManagerImpl<S extends Shareable>
|
||||
}
|
||||
}
|
||||
|
||||
private PrivateRequest parseInvitationRequest(Transaction txn,
|
||||
private PrivateRequest<S> parseInvitationRequest(Transaction txn,
|
||||
ContactId c, MessageId m, MessageMetadata meta,
|
||||
MessageStatus status) throws DbException, FormatException {
|
||||
// Look up the invite message to get the details of the private group
|
||||
@@ -384,13 +396,12 @@ abstract class SharingManagerImpl<S extends Shareable>
|
||||
meta.isAvailableToAnswer(), canBeOpened);
|
||||
}
|
||||
|
||||
private InvitationResponse parseInvitationResponse(GroupId contactGroupId,
|
||||
private PrivateResponse<S> parseInvitationResponse(GroupId contactGroupId,
|
||||
MessageId m, MessageMetadata meta, MessageStatus status,
|
||||
boolean accept) {
|
||||
S shareable, boolean accept) {
|
||||
return invitationFactory.createInvitationResponse(m, contactGroupId,
|
||||
meta.getTimestamp(), meta.isLocal(), status.isSent(),
|
||||
status.isSeen(), meta.isRead(), meta.getShareableId(),
|
||||
accept);
|
||||
status.isSeen(), meta.isRead(), shareable, accept);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -159,7 +159,7 @@ public class IntroductionIntegrationTest
|
||||
eventWaiter.await(TIMEOUT, 1);
|
||||
assertTrue(listener0.response1Received);
|
||||
assertEquals(introducee2.getAuthor().getName(),
|
||||
listener0.getResponse().getName());
|
||||
listener0.getResponse().getObject().getName());
|
||||
assertGroupCount(messageTracker0, g1.getId(), 2, 1);
|
||||
|
||||
// sync second ACCEPT message
|
||||
@@ -167,7 +167,7 @@ public class IntroductionIntegrationTest
|
||||
eventWaiter.await(TIMEOUT, 1);
|
||||
assertTrue(listener0.response2Received);
|
||||
assertEquals(introducee1.getAuthor().getName(),
|
||||
listener0.getResponse().getName());
|
||||
listener0.getResponse().getObject().getName());
|
||||
assertGroupCount(messageTracker0, g2.getId(), 2, 1);
|
||||
|
||||
// sync forwarded ACCEPT messages to introducees
|
||||
@@ -265,7 +265,7 @@ public class IntroductionIntegrationTest
|
||||
|
||||
// assert that the name on the decline event is correct
|
||||
assertEquals(introducee2.getAuthor().getName(),
|
||||
listener0.getResponse().getName());
|
||||
listener0.getResponse().getObject().getName());
|
||||
|
||||
// sync second response
|
||||
sync2To0(1, true);
|
||||
@@ -282,7 +282,7 @@ public class IntroductionIntegrationTest
|
||||
// assert that the name on the decline event is correct
|
||||
eventWaiter.await(TIMEOUT, 1);
|
||||
assertEquals(introducee1.getAuthor().getName(),
|
||||
listener2.getResponse().getName());
|
||||
listener2.getResponse().getObject().getName());
|
||||
|
||||
// note how the introducer does not forward the second response,
|
||||
// because after the first decline the protocol finished
|
||||
@@ -351,7 +351,7 @@ public class IntroductionIntegrationTest
|
||||
// assert that the name on the decline event is correct
|
||||
eventWaiter.await(TIMEOUT, 1);
|
||||
assertEquals(contact2From0.getAuthor().getName(),
|
||||
listener1.getResponse().getName());
|
||||
listener1.getResponse().getObject().getName());
|
||||
|
||||
assertFalse(contactManager1
|
||||
.contactExists(author2.getId(), author1.getId()));
|
||||
@@ -475,7 +475,7 @@ public class IntroductionIntegrationTest
|
||||
// assert that the name on the decline event is correct
|
||||
eventWaiter.await(TIMEOUT, 1);
|
||||
assertEquals(introducee1.getAuthor().getName(),
|
||||
listener2.getResponse().getName());
|
||||
listener2.getResponse().getObject().getName());
|
||||
|
||||
// assert that introducee2 is in correct state
|
||||
introduceeSession = getIntroduceeSession(c2);
|
||||
|
||||
@@ -310,6 +310,8 @@ public class CreatorProtocolEngineTest extends AbstractProtocolEngineTest {
|
||||
oneOf(messageTracker)
|
||||
.trackMessage(txn, contactGroupId, inviteTimestamp + 1,
|
||||
false);
|
||||
oneOf(privateGroupManager).getPrivateGroup(txn, privateGroupId);
|
||||
will(returnValue(privateGroup));
|
||||
}});
|
||||
expectGetContactId();
|
||||
expectSetPrivateGroupVisibility(SHARED);
|
||||
@@ -399,6 +401,8 @@ public class CreatorProtocolEngineTest extends AbstractProtocolEngineTest {
|
||||
oneOf(messageTracker)
|
||||
.trackMessage(txn, contactGroupId, inviteTimestamp + 1,
|
||||
false);
|
||||
oneOf(privateGroupManager).getPrivateGroup(txn, privateGroupId);
|
||||
will(returnValue(privateGroup));
|
||||
}});
|
||||
expectGetContactId();
|
||||
CreatorSession newSession =
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.briarproject.briar.privategroup.invitation;
|
||||
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.sync.Group;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.test.TestDatabaseModule;
|
||||
import org.briarproject.briar.api.client.ProtocolStateException;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
@@ -12,7 +13,6 @@ import org.briarproject.briar.api.privategroup.invitation.GroupInvitationItem;
|
||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager;
|
||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationRequest;
|
||||
import org.briarproject.briar.api.privategroup.invitation.GroupInvitationResponse;
|
||||
import org.briarproject.briar.api.sharing.InvitationResponse;
|
||||
import org.briarproject.briar.test.BriarIntegrationTest;
|
||||
import org.briarproject.briar.test.BriarIntegrationTestComponent;
|
||||
import org.briarproject.briar.test.DaggerBriarIntegrationTestComponent;
|
||||
@@ -124,7 +124,8 @@ public class GroupInvitationIntegrationTest
|
||||
for (PrivateMessageHeader m : messages) {
|
||||
if (m instanceof GroupInvitationResponse) {
|
||||
foundResponse = true;
|
||||
InvitationResponse response = (GroupInvitationResponse) m;
|
||||
GroupInvitationResponse response = (GroupInvitationResponse) m;
|
||||
assertEquals(privateGroup0, response.getObject());
|
||||
assertTrue(response.isLocal());
|
||||
assertFalse(response.wasAccepted());
|
||||
}
|
||||
@@ -140,7 +141,8 @@ public class GroupInvitationIntegrationTest
|
||||
for (PrivateMessageHeader m : messages) {
|
||||
if (m instanceof GroupInvitationResponse) {
|
||||
foundResponse = true;
|
||||
InvitationResponse response = (GroupInvitationResponse) m;
|
||||
GroupInvitationResponse response = (GroupInvitationResponse) m;
|
||||
assertEquals(privateGroup0, response.getObject());
|
||||
assertFalse(response.isLocal());
|
||||
assertFalse(response.wasAccepted());
|
||||
}
|
||||
@@ -171,24 +173,27 @@ public class GroupInvitationIntegrationTest
|
||||
for (PrivateMessageHeader m : messages) {
|
||||
if (m instanceof GroupInvitationResponse) {
|
||||
foundResponse = true;
|
||||
InvitationResponse response = (GroupInvitationResponse) m;
|
||||
GroupInvitationResponse response = (GroupInvitationResponse) m;
|
||||
assertEquals(privateGroup0, response.getObject());
|
||||
assertTrue(response.wasAccepted());
|
||||
} else {
|
||||
assertTrue(((GroupInvitationRequest) m).doesExist());
|
||||
GroupInvitationRequest request = (GroupInvitationRequest) m;
|
||||
assertEquals(privateGroup0, request.getObject());
|
||||
assertTrue(request.doesExist());
|
||||
}
|
||||
}
|
||||
assertTrue(foundResponse);
|
||||
|
||||
sync1To0(1, true);
|
||||
|
||||
messages =
|
||||
groupInvitationManager0.getMessages(contactId1From0);
|
||||
messages = groupInvitationManager0.getMessages(contactId1From0);
|
||||
assertEquals(2, messages.size());
|
||||
foundResponse = false;
|
||||
for (PrivateMessageHeader m : messages) {
|
||||
if (m instanceof GroupInvitationResponse) {
|
||||
foundResponse = true;
|
||||
InvitationResponse response = (GroupInvitationResponse) m;
|
||||
GroupInvitationResponse response = (GroupInvitationResponse) m;
|
||||
assertEquals(privateGroup0, response.getObject());
|
||||
assertTrue(response.wasAccepted());
|
||||
}
|
||||
}
|
||||
@@ -432,6 +437,36 @@ public class GroupInvitationIntegrationTest
|
||||
sync1To0(1, true);
|
||||
}
|
||||
|
||||
@Test(expected = AssertionError.class)
|
||||
public void testDeleteOnlyInvitationFails() throws Exception {
|
||||
// send invitation
|
||||
sendInvitation(clock.currentTimeMillis(), null);
|
||||
sync0To1(1, true);
|
||||
|
||||
// save MessageId of invitation
|
||||
Collection<PrivateMessageHeader> messages =
|
||||
groupInvitationManager1.getMessages(contactId0From1);
|
||||
assertEquals(1, messages.size());
|
||||
MessageId inviteId = messages.iterator().next().getId();
|
||||
|
||||
// decline invitation
|
||||
groupInvitationManager1
|
||||
.respondToInvitation(contactId0From1, privateGroup0, false);
|
||||
|
||||
// we should have an invitation and a decline message
|
||||
messages = groupInvitationManager1.getMessages(contactId0From1);
|
||||
assertEquals(2, messages.size());
|
||||
|
||||
// delete only invitation
|
||||
withinTransaction(db1, txn -> {
|
||||
db1.deleteMessage(txn, inviteId);
|
||||
db1.deleteMessageMetadata(txn, inviteId);
|
||||
});
|
||||
|
||||
// This should fail
|
||||
groupInvitationManager1.getMessages(contactId0From1);
|
||||
}
|
||||
|
||||
private void sendInvitation(long timestamp, @Nullable String msg) throws
|
||||
DbException {
|
||||
byte[] signature = groupInvitationFactory.signInvitation(contact1From0,
|
||||
|
||||
@@ -682,6 +682,8 @@ public class GroupInvitationManagerImplTest extends BrambleMockTestCase {
|
||||
contactGroup.getId(), query);
|
||||
will(returnValue(results));
|
||||
// first message
|
||||
oneOf(messageParser).parseMetadata(meta2);
|
||||
will(returnValue(messageMetadata2));
|
||||
oneOf(messageParser).parseMetadata(meta);
|
||||
will(returnValue(messageMetadata1));
|
||||
oneOf(db).getMessageStatus(txn, contactId, message.getId());
|
||||
@@ -695,6 +697,8 @@ public class GroupInvitationManagerImplTest extends BrambleMockTestCase {
|
||||
// second message
|
||||
oneOf(messageParser).parseMetadata(meta2);
|
||||
will(returnValue(messageMetadata2));
|
||||
oneOf(messageParser).parseMetadata(meta);
|
||||
will(returnValue(messageMetadata1));
|
||||
oneOf(db).getMessageStatus(txn, contactId, messageId2);
|
||||
// end transaction
|
||||
oneOf(db).commitTransaction(txn);
|
||||
@@ -709,9 +713,11 @@ public class GroupInvitationManagerImplTest extends BrambleMockTestCase {
|
||||
if (m.getId().equals(message.getId())) {
|
||||
assertTrue(m instanceof GroupInvitationRequest);
|
||||
assertEquals(time1, m.getTimestamp());
|
||||
assertEquals(pg, ((GroupInvitationRequest) m).getObject());
|
||||
} else if (m.getId().equals(messageId2)) {
|
||||
assertTrue(m instanceof GroupInvitationResponse);
|
||||
assertEquals(time2, m.getTimestamp());
|
||||
assertEquals(pg, ((GroupInvitationResponse) m).getObject());
|
||||
} else {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
@@ -154,6 +154,7 @@ public class BlogSharingIntegrationTest
|
||||
for (PrivateMessageHeader m : list) {
|
||||
if (m instanceof BlogInvitationRequest) {
|
||||
BlogInvitationRequest invitation = (BlogInvitationRequest) m;
|
||||
assertEquals(blog2, invitation.getObject());
|
||||
assertFalse(invitation.wasAnswered());
|
||||
assertEquals(blog2.getAuthor().getName(),
|
||||
invitation.getName());
|
||||
@@ -161,6 +162,7 @@ public class BlogSharingIntegrationTest
|
||||
assertEquals("Hi!", invitation.getMessage());
|
||||
} else {
|
||||
BlogInvitationResponse response = (BlogInvitationResponse) m;
|
||||
assertEquals(blog2, response.getObject());
|
||||
assertTrue(response.wasAccepted());
|
||||
assertTrue(response.isLocal());
|
||||
}
|
||||
@@ -225,6 +227,7 @@ public class BlogSharingIntegrationTest
|
||||
for (PrivateMessageHeader m : list) {
|
||||
if (m instanceof BlogInvitationRequest) {
|
||||
BlogInvitationRequest invitation = (BlogInvitationRequest) m;
|
||||
assertEquals(rssBlog, invitation.getObject());
|
||||
assertFalse(invitation.wasAnswered());
|
||||
assertEquals(rssBlog.getAuthor().getName(),
|
||||
invitation.getName());
|
||||
@@ -232,6 +235,7 @@ public class BlogSharingIntegrationTest
|
||||
assertEquals("Hi!", invitation.getMessage());
|
||||
} else {
|
||||
BlogInvitationResponse response = (BlogInvitationResponse) m;
|
||||
assertEquals(rssBlog, response.getObject());
|
||||
assertTrue(response.wasAccepted());
|
||||
assertTrue(response.isLocal());
|
||||
}
|
||||
@@ -284,12 +288,14 @@ public class BlogSharingIntegrationTest
|
||||
for (PrivateMessageHeader m : list) {
|
||||
if (m instanceof BlogInvitationRequest) {
|
||||
BlogInvitationRequest invitation = (BlogInvitationRequest) m;
|
||||
assertEquals(blog2, invitation.getObject());
|
||||
assertFalse(invitation.wasAnswered());
|
||||
assertEquals(blog2.getAuthor().getName(),
|
||||
invitation.getName());
|
||||
assertEquals(null, invitation.getMessage());
|
||||
} else {
|
||||
BlogInvitationResponse response = (BlogInvitationResponse) m;
|
||||
assertEquals(blog2, response.getObject());
|
||||
assertFalse(response.wasAccepted());
|
||||
assertTrue(response.isLocal());
|
||||
}
|
||||
@@ -612,7 +618,7 @@ public class BlogSharingIntegrationTest
|
||||
}
|
||||
}
|
||||
|
||||
private void listenToEvents(boolean accept) throws DbException {
|
||||
private void listenToEvents(boolean accept) {
|
||||
listener0 = new SharerListener();
|
||||
c0.getEventBus().addListener(listener0);
|
||||
listener1 = new InviteeListener(accept);
|
||||
|
||||
@@ -137,15 +137,15 @@ public class ForumSharingIntegrationTest
|
||||
// check other things are alright with the forum message
|
||||
for (PrivateMessageHeader m : list) {
|
||||
if (m instanceof ForumInvitationRequest) {
|
||||
ForumInvitationRequest invitation =
|
||||
(ForumInvitationRequest) m;
|
||||
ForumInvitationRequest invitation = (ForumInvitationRequest) m;
|
||||
assertFalse(invitation.wasAnswered());
|
||||
assertEquals(forum0.getName(), invitation.getName());
|
||||
assertEquals(forum0, invitation.getObject());
|
||||
assertEquals("Hi!", invitation.getMessage());
|
||||
assertTrue(invitation.doesExist());
|
||||
} else {
|
||||
ForumInvitationResponse response =
|
||||
(ForumInvitationResponse) m;
|
||||
ForumInvitationResponse response = (ForumInvitationResponse) m;
|
||||
assertEquals(forum0, response.getObject());
|
||||
assertTrue(response.wasAccepted());
|
||||
assertTrue(response.isLocal());
|
||||
}
|
||||
@@ -195,12 +195,14 @@ public class ForumSharingIntegrationTest
|
||||
for (PrivateMessageHeader m : list) {
|
||||
if (m instanceof ForumInvitationRequest) {
|
||||
ForumInvitationRequest invitation = (ForumInvitationRequest) m;
|
||||
assertEquals(forum0, invitation.getObject());
|
||||
assertFalse(invitation.wasAnswered());
|
||||
assertEquals(forum0.getName(), invitation.getName());
|
||||
assertEquals(null, invitation.getMessage());
|
||||
assertFalse(invitation.doesExist());
|
||||
} else {
|
||||
ForumInvitationResponse response = (ForumInvitationResponse) m;
|
||||
assertEquals(forum0, response.getObject());
|
||||
assertFalse(response.wasAccepted());
|
||||
assertTrue(response.isLocal());
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.briarproject.bramble.api.contact.ContactManager;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.db.Transaction;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.identity.AuthorFactory;
|
||||
@@ -376,4 +377,21 @@ public abstract class BriarIntegrationTest<C extends BriarIntegrationTestCompone
|
||||
assertNotNull(contactId1From2);
|
||||
contactManager2.removeContact(contactId1From2);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
protected interface TransactionScope {
|
||||
void execute(Transaction txn) throws DbException;
|
||||
}
|
||||
|
||||
protected void withinTransaction(DatabaseComponent db, TransactionScope scope)
|
||||
throws DbException {
|
||||
Transaction txn = db.startTransaction(false);
|
||||
try {
|
||||
scope.execute(txn);
|
||||
db.commitTransaction(txn);
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user