Use ConversationManager for timestamps and unread counts

This commit is contained in:
str4d
2016-06-02 04:06:56 +00:00
parent c333052396
commit 7a3bd86522
30 changed files with 411 additions and 293 deletions

View File

@@ -0,0 +1,127 @@
package org.briarproject.clients;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.ReadableMessageManager;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataParser;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import static org.briarproject.api.clients.ReadableMessageConstants.LOCAL;
import static org.briarproject.api.clients.ReadableMessageConstants.READ;
import static org.briarproject.api.clients.ReadableMessageConstants.TIMESTAMP;
import static org.briarproject.api.clients.ReadableMessageConstants.UNREAD;
public abstract class ReadableMessageManagerImpl
extends BdfIncomingMessageHook implements ReadableMessageManager {
protected final DatabaseComponent db;
protected ReadableMessageManagerImpl(ClientHelper clientHelper,
DatabaseComponent db, MetadataParser metadataParser) {
super(clientHelper, metadataParser);
this.db = db;
}
protected abstract Group getContactGroup(Contact c);
protected abstract boolean incomingReadableMessage(Transaction txn,
Message m, BdfList body, BdfDictionary meta)
throws DbException, FormatException;
@Override
protected void incomingMessage(Transaction txn, Message m, BdfList body,
BdfDictionary meta) throws DbException, FormatException {
// Check if we accept this message
if (incomingReadableMessage(txn, m, body, meta)) {
// Update the group timestamp and unread count
GroupId groupId = m.getGroupId();
long timestamp = meta.getLong(TIMESTAMP);
boolean local = meta.getBoolean(LOCAL);
boolean read = meta.getBoolean(READ);
updateGroupMetadata(txn, groupId, timestamp, local, read, read);
}
}
@Override
public long getTimestamp(ContactId c) throws DbException {
BdfDictionary meta;
Transaction txn = db.startTransaction(true);
try {
GroupId g = getContactGroup(db.getContact(txn, c)).getId();
meta = clientHelper.getGroupMetadataAsDictionary(txn, g);
txn.setComplete();
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
return meta.getLong(TIMESTAMP, -1L);
}
@Override
public int getUnreadCount(ContactId c) throws DbException {
BdfDictionary meta;
Transaction txn = db.startTransaction(true);
try {
GroupId g = getContactGroup(db.getContact(txn, c)).getId();
meta = clientHelper.getGroupMetadataAsDictionary(txn, g);
txn.setComplete();
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
return meta.getLong(UNREAD, 0L).intValue();
}
@Override
public void setReadFlag(ContactId c, MessageId m, boolean local,
boolean read) throws DbException {
Transaction txn = db.startTransaction(false);
try {
boolean wasRead =
clientHelper.getMessageMetadataAsDictionary(txn, m)
.getBoolean(READ);
BdfDictionary meta = new BdfDictionary();
meta.put(READ, read);
clientHelper.mergeMessageMetadata(txn, m, meta);
GroupId g = getContactGroup(db.getContact(txn, c)).getId();
updateGroupMetadata(txn, g, -1, local, wasRead, read);
txn.setComplete();
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
}
private void updateGroupMetadata(Transaction txn, GroupId groupId,
long timestamp, boolean local, boolean wasRead, boolean read)
throws DbException, FormatException {
BdfDictionary groupMeta =
clientHelper.getGroupMetadataAsDictionary(txn, groupId);
long groupTimestamp = groupMeta.getLong(TIMESTAMP, -1L);
int unread = groupMeta.getLong(UNREAD, 0L).intValue();
BdfDictionary d = new BdfDictionary();
if (timestamp > groupTimestamp) {
d.put(TIMESTAMP, timestamp);
}
if (!local && (wasRead != read)) {
d.put(UNREAD, read ? (unread > 0 ? unread - 1 : 0) : unread + 1);
}
clientHelper.mergeGroupMetadata(txn, groupId, d);
}
}

View File

@@ -7,6 +7,7 @@ import org.briarproject.api.conversation.ConversationForumInvitationItem;
import org.briarproject.api.conversation.ConversationIntroductionRequestItem;
import org.briarproject.api.conversation.ConversationIntroductionResponseItem;
import org.briarproject.api.conversation.ConversationItem;
import org.briarproject.api.conversation.ConversationItem.OutgoingItem;
import org.briarproject.api.conversation.ConversationItem.Partial;
import org.briarproject.api.conversation.ConversationManager;
import org.briarproject.api.conversation.ConversationMessageItem;
@@ -73,6 +74,12 @@ public class ConversationManagerImpl implements ConversationManager {
return CLIENT_ID;
}
@Override
public boolean isWrappedClient(ClientId clientId) {
return clientId.equals(introductionManager.getClientId()) ||
clientId.equals(forumSharingManager.getClientId());
}
@Override
public ConversationItem addLocalMessage(PrivateMessage m, byte[] body)
throws DbException {
@@ -101,12 +108,6 @@ public class ConversationManagerImpl implements ConversationManager {
@Override
public List<ConversationItem> getMessages(ContactId c)
throws DbException {
return getMessages(c, true);
}
@Override
public List<ConversationItem> getMessages(ContactId c, boolean content)
throws DbException {
Collection<PrivateMessageHeader> headers =
messagingManager.getMessageHeaders(c);
Collection<IntroductionMessage> introductions =
@@ -116,11 +117,9 @@ public class ConversationManagerImpl implements ConversationManager {
List<ConversationItem> items = new ArrayList<ConversationItem>();
for (PrivateMessageHeader h : headers) {
ConversationItem item = ConversationMessageItemImpl.from(h);
if (content) {
byte[] body = bodyCache.get(h.getId());
if (body == null) loadMessageContent((Partial) item);
else ((Partial) item).setContent(body);
}
byte[] body = bodyCache.get(h.getId());
if (body == null) loadMessageContent((Partial) item);
else ((Partial) item).setContent(body);
items.add(item);
}
for (IntroductionMessage m : introductions) {
@@ -141,6 +140,27 @@ public class ConversationManagerImpl implements ConversationManager {
return items;
}
@Override
public long getTimestamp(ContactId c) throws DbException {
long timestamp = -1;
long t = messagingManager.getTimestamp(c);
if (t > timestamp) timestamp = t;
t = introductionManager.getTimestamp(c);
if (t > timestamp) timestamp = t;
t = forumSharingManager.getTimestamp(c);
if (t > timestamp) timestamp = t;
return timestamp;
}
@Override
public int getUnreadCount(ContactId c) throws DbException {
int unread = 0;
unread += messagingManager.getUnreadCount(c);
unread += introductionManager.getUnreadCount(c);
unread += forumSharingManager.getUnreadCount(c);
return unread;
}
@Override
public void loadMessageContent(final Partial m) {
dbExecutor.execute(new Runnable() {
@@ -185,16 +205,17 @@ public class ConversationManagerImpl implements ConversationManager {
}
@Override
public void setReadFlag(ConversationItem item, boolean read)
public void setReadFlag(ContactId c, ConversationItem item, boolean read)
throws DbException {
MessageId id = item.getId();
boolean local = item instanceof OutgoingItem;
if (item instanceof ConversationMessageItem) {
messagingManager.setReadFlag(id, read);
messagingManager.setReadFlag(c, id, local, read);
} else if (item instanceof ConversationIntroductionRequestItem ||
item instanceof ConversationIntroductionResponseItem) {
introductionManager.setReadFlag(id, read);
item instanceof ConversationIntroductionResponseItem) {
introductionManager.setReadFlag(c, id, local, read);
} else if (item instanceof ConversationForumInvitationItem) {
forumSharingManager.setReadFlag(id, read);
forumSharingManager.setReadFlag(c, id, local, read);
}
}
}

View File

@@ -22,6 +22,7 @@ import java.util.logging.Logger;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.api.clients.ReadableMessageConstants.TIMESTAMP;
import static org.briarproject.api.introduction.IntroduceeAction.LOCAL_ABORT;
import static org.briarproject.api.introduction.IntroduceeAction.LOCAL_ACCEPT;
import static org.briarproject.api.introduction.IntroduceeAction.LOCAL_DECLINE;
@@ -112,7 +113,7 @@ public class IntroduceeEngine
msg.put(E_PUBLIC_KEY, localState.getRaw(OUR_PUBLIC_KEY));
msg.put(TRANSPORT, localAction.getDictionary(TRANSPORT));
}
msg.put(MESSAGE_TIME, localAction.getLong(MESSAGE_TIME));
msg.put(TIMESTAMP, localAction.getLong(MESSAGE_TIME));
messages.add(msg);
logAction(currentState, localState, msg);
@@ -330,7 +331,7 @@ public class IntroduceeEngine
SessionId sessionId = new SessionId(localState.getRaw(SESSION_ID));
MessageId messageId = new MessageId(msg.getRaw(MESSAGE_ID));
long time = msg.getLong(MESSAGE_TIME);
long time = msg.getLong(TIMESTAMP);
String name = msg.getString(NAME);
String message = msg.getOptionalString(MSG);
boolean exists = localState.getBoolean(EXISTS);

View File

@@ -22,6 +22,7 @@ import java.util.logging.Logger;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.api.clients.ReadableMessageConstants.TIMESTAMP;
import static org.briarproject.api.introduction.IntroducerAction.LOCAL_ABORT;
import static org.briarproject.api.introduction.IntroducerAction.LOCAL_REQUEST;
import static org.briarproject.api.introduction.IntroducerAction.REMOTE_ACCEPT_1;
@@ -106,7 +107,7 @@ public class IntroducerEngine
if (localAction.containsKey(MSG)) {
msg1.put(MSG, localAction.getString(MSG));
}
msg1.put(MESSAGE_TIME, localAction.getLong(MESSAGE_TIME));
msg1.put(TIMESTAMP, localAction.getLong(MESSAGE_TIME));
messages.add(msg1);
logLocalAction(currentState, localState, msg1);
BdfDictionary msg2 = new BdfDictionary();
@@ -118,7 +119,7 @@ public class IntroducerEngine
if (localAction.containsKey(MSG)) {
msg2.put(MSG, localAction.getString(MSG));
}
msg2.put(MESSAGE_TIME, localAction.getLong(MESSAGE_TIME));
msg2.put(TIMESTAMP, localAction.getLong(MESSAGE_TIME));
messages.add(msg2);
logLocalAction(currentState, localState, msg2);
@@ -298,7 +299,7 @@ public class IntroducerEngine
SessionId sessionId = new SessionId(localState.getRaw(SESSION_ID));
MessageId messageId = new MessageId(msg.getRaw(MESSAGE_ID));
long time = msg.getLong(MESSAGE_TIME);
long time = msg.getLong(TIMESTAMP);
String name = getOtherContact(localState, msg);
boolean accept = msg.getBoolean(ACCEPT);

View File

@@ -29,7 +29,7 @@ import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus;
import org.briarproject.clients.BdfIncomingMessageHook;
import org.briarproject.clients.ReadableMessageManagerImpl;
import org.briarproject.util.StringUtils;
import java.io.IOException;
@@ -42,6 +42,8 @@ import java.util.logging.Logger;
import javax.inject.Inject;
import static java.util.logging.Level.WARNING;
import static org.briarproject.api.clients.ReadableMessageConstants.READ;
import static org.briarproject.api.clients.ReadableMessageConstants.TIMESTAMP;
import static org.briarproject.api.introduction.IntroduceeProtocolState.FINISHED;
import static org.briarproject.api.introduction.IntroductionConstants.ACCEPT;
import static org.briarproject.api.introduction.IntroductionConstants.ANSWERED;
@@ -56,11 +58,9 @@ import static org.briarproject.api.introduction.IntroductionConstants.EXISTS;
import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID;
import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID_1;
import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID_2;
import static org.briarproject.api.introduction.IntroductionConstants.MESSAGE_TIME;
import static org.briarproject.api.introduction.IntroductionConstants.MSG;
import static org.briarproject.api.introduction.IntroductionConstants.NAME;
import static org.briarproject.api.introduction.IntroductionConstants.NOT_OUR_RESPONSE;
import static org.briarproject.api.introduction.IntroductionConstants.READ;
import static org.briarproject.api.introduction.IntroductionConstants.REMOTE_AUTHOR_ID;
import static org.briarproject.api.introduction.IntroductionConstants.REMOTE_AUTHOR_IS_US;
import static org.briarproject.api.introduction.IntroductionConstants.RESPONSE_1;
@@ -76,7 +76,7 @@ import static org.briarproject.api.introduction.IntroductionConstants.TYPE_ACK;
import static org.briarproject.api.introduction.IntroductionConstants.TYPE_REQUEST;
import static org.briarproject.api.introduction.IntroductionConstants.TYPE_RESPONSE;
class IntroductionManagerImpl extends BdfIncomingMessageHook
class IntroductionManagerImpl extends ReadableMessageManagerImpl
implements IntroductionManager, Client, AddContactHook,
RemoveContactHook {
@@ -87,7 +87,6 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
private static final Logger LOG =
Logger.getLogger(IntroductionManagerImpl.class.getName());
private final DatabaseComponent db;
private final IntroducerManager introducerManager;
private final IntroduceeManager introduceeManager;
private final IntroductionGroupFactory introductionGroupFactory;
@@ -98,8 +97,7 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
IntroduceeManager introduceeManager,
IntroductionGroupFactory introductionGroupFactory) {
super(clientHelper, metadataParser);
this.db = db;
super(clientHelper, db, metadataParser);
this.introducerManager = introducerManager;
this.introduceeManager = introduceeManager;
this.introductionGroupFactory = introductionGroupFactory;
@@ -166,8 +164,10 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
.getMessageMetadataAsDictionary(txn, gId, query);
for (Map.Entry<MessageId, BdfDictionary> entry : map.entrySet()) {
BdfDictionary d = entry.getValue();
ContactId c1 = new ContactId(d.getLong(CONTACT_ID_1).intValue());
ContactId c2 = new ContactId(d.getLong(CONTACT_ID_2).intValue());
ContactId c1 =
new ContactId(d.getLong(CONTACT_ID_1).intValue());
ContactId c2 =
new ContactId(d.getLong(CONTACT_ID_2).intValue());
if (c1.equals(c.getId()) || c2.equals(c.getId())) {
IntroducerProtocolState state = IntroducerProtocolState
@@ -198,7 +198,13 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
// remove the group (all messages will be removed with it)
// this contact won't get our abort message, but the other will
db.removeGroup(txn, introductionGroupFactory.createIntroductionGroup(c));
db.removeGroup(txn,
introductionGroupFactory.createIntroductionGroup(c));
}
@Override
protected Group getContactGroup(Contact c) {
return introductionGroupFactory.createIntroductionGroup(c);
}
/**
@@ -207,8 +213,8 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
* in the introduction protocol and which engine we need to start.
*/
@Override
protected void incomingMessage(Transaction txn, Message m, BdfList body,
BdfDictionary message) throws DbException {
protected boolean incomingReadableMessage(Transaction txn, Message m, BdfList body,
BdfDictionary message) throws DbException, FormatException {
// Get message data and type
GroupId groupId = m.getGroupId();
@@ -218,7 +224,8 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
if (type == TYPE_REQUEST) {
boolean stateExists = true;
try {
getSessionState(txn, groupId, message.getRaw(SESSION_ID), false);
getSessionState(txn, groupId, message.getRaw(SESSION_ID),
false);
} catch (FormatException e) {
stateExists = false;
}
@@ -233,7 +240,7 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
LOG.log(WARNING, e.toString(), e);
}
deleteMessage(txn, m.getId());
return;
return false;
}
try {
introduceeManager.incomingMessage(txn, state, message);
@@ -246,7 +253,8 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
}
}
// our role can be anything
else if (type == TYPE_RESPONSE || type == TYPE_ACK || type == TYPE_ABORT) {
else if (type == TYPE_RESPONSE || type == TYPE_ACK ||
type == TYPE_ABORT) {
BdfDictionary state;
try {
state = getSessionState(txn, groupId,
@@ -254,7 +262,7 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
} catch (FormatException e) {
LOG.warning("Could not find state for message, deleting...");
deleteMessage(txn, m.getId());
return;
return false;
}
long role = state.getLong(ROLE, -1L);
@@ -264,27 +272,35 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
} else if (role == ROLE_INTRODUCEE) {
introduceeManager.incomingMessage(txn, state, message);
} else {
if(LOG.isLoggable(WARNING)) {
if (LOG.isLoggable(WARNING)) {
LOG.warning("Unknown role '" + role +
"'. Deleting message...");
deleteMessage(txn, m.getId());
}
deleteMessage(txn, m.getId());
return false;
}
} catch (DbException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (role == ROLE_INTRODUCER) introducerManager.abort(txn, state);
if (role == ROLE_INTRODUCER)
introducerManager.abort(txn, state);
else introduceeManager.abort(txn, state);
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (role == ROLE_INTRODUCER) introducerManager.abort(txn, state);
if (role == ROLE_INTRODUCER)
introducerManager.abort(txn, state);
else introduceeManager.abort(txn, state);
}
} else {
// the message has been validated, so this should not happen
if(LOG.isLoggable(WARNING)) {
if (LOG.isLoggable(WARNING)) {
LOG.warning("Unknown message type '" + type + "', deleting...");
}
deleteMessage(txn, m.getId());
return false;
}
// The message is valid
return true;
}
@Override
@@ -374,14 +390,15 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
int role = state.getLong(ROLE).intValue();
boolean local;
long time = msg.getLong(MESSAGE_TIME);
long time = msg.getLong(TIMESTAMP);
boolean accepted = msg.getBoolean(ACCEPT, false);
boolean read = msg.getBoolean(READ, false);
AuthorId authorId;
String name;
if (type == TYPE_RESPONSE) {
if (role == ROLE_INTRODUCER) {
if (!concernsThisContact(contactId, messageId, state)) {
if (!concernsThisContact(contactId, messageId,
state)) {
// this response is not from contactId
continue;
}
@@ -445,7 +462,8 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
list.add(ir);
}
} catch (FormatException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
if (LOG.isLoggable(WARNING))
LOG.log(WARNING, e.toString(), e);
}
}
txn.setComplete();
@@ -457,17 +475,6 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
return list;
}
@Override
public void setReadFlag(MessageId m, boolean read) throws DbException {
try {
BdfDictionary meta = new BdfDictionary();
meta.put(READ, read);
clientHelper.mergeMessageMetadata(m, meta);
} catch (FormatException e) {
throw new RuntimeException(e);
}
}
private String getNameForIntroducer(ContactId contactId,
BdfDictionary state) throws FormatException {
@@ -475,7 +482,8 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
return state.getString(CONTACT_2);
if (contactId.getInt() == state.getLong(CONTACT_ID_2).intValue())
return state.getString(CONTACT_1);
throw new RuntimeException("Contact not part of this introduction session");
throw new RuntimeException(
"Contact not part of this introduction session");
}
private AuthorId getAuthorIdForIntroducer(ContactId contactId,
@@ -485,10 +493,12 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
return new AuthorId(state.getRaw(AUTHOR_ID_2));
if (contactId.getInt() == state.getLong(CONTACT_ID_2).intValue())
return new AuthorId(state.getRaw(AUTHOR_ID_1));
throw new RuntimeException("Contact not part of this introduction session");
throw new RuntimeException(
"Contact not part of this introduction session");
}
private boolean concernsThisContact(ContactId contactId, MessageId messageId,
private boolean concernsThisContact(ContactId contactId,
MessageId messageId,
BdfDictionary state) throws FormatException {
if (contactId.getInt() == state.getLong(CONTACT_ID_1).intValue()) {
@@ -520,7 +530,8 @@ class IntroductionManagerImpl extends BdfIncomingMessageHook
// to find state for introducee
Map<MessageId, BdfDictionary> map = clientHelper
.getMessageMetadataAsDictionary(txn,
introductionGroupFactory.createLocalGroup().getId());
introductionGroupFactory.createLocalGroup()
.getId());
for (Map.Entry<MessageId, BdfDictionary> m : map.entrySet()) {
if (Arrays.equals(m.getValue().getRaw(SESSION_ID), sessionId)) {
BdfDictionary state = m.getValue();

View File

@@ -1,9 +1,9 @@
package org.briarproject.introduction;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.BdfMessageContext;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.SessionId;
import org.briarproject.api.clients.BdfMessageContext;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder;
@@ -13,13 +13,15 @@ import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator;
import static org.briarproject.api.TransportId.MAX_TRANSPORT_ID_LENGTH;
import static org.briarproject.api.clients.ReadableMessageConstants.LOCAL;
import static org.briarproject.api.clients.ReadableMessageConstants.READ;
import static org.briarproject.api.clients.ReadableMessageConstants.TIMESTAMP;
import static org.briarproject.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.introduction.IntroductionConstants.ACCEPT;
import static org.briarproject.api.introduction.IntroductionConstants.E_PUBLIC_KEY;
import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID;
import static org.briarproject.api.introduction.IntroductionConstants.MESSAGE_ID;
import static org.briarproject.api.introduction.IntroductionConstants.MESSAGE_TIME;
import static org.briarproject.api.introduction.IntroductionConstants.MSG;
import static org.briarproject.api.introduction.IntroductionConstants.NAME;
import static org.briarproject.api.introduction.IntroductionConstants.PUBLIC_KEY;
@@ -67,7 +69,9 @@ class IntroductionValidator extends BdfMessageValidator {
d.put(SESSION_ID, id);
d.put(GROUP_ID, m.getGroupId());
d.put(MESSAGE_ID, m.getId());
d.put(MESSAGE_TIME, m.getTimestamp());
d.put(TIMESTAMP, m.getTimestamp());
d.put(LOCAL, false);
d.put(READ, false);
return new BdfMessageContext(d);
}

View File

@@ -16,10 +16,10 @@ import org.briarproject.api.system.Clock;
import javax.inject.Inject;
import static org.briarproject.api.clients.ReadableMessageConstants.TIMESTAMP;
import static org.briarproject.api.introduction.IntroductionConstants.ACCEPT;
import static org.briarproject.api.introduction.IntroductionConstants.E_PUBLIC_KEY;
import static org.briarproject.api.introduction.IntroductionConstants.GROUP_ID;
import static org.briarproject.api.introduction.IntroductionConstants.MESSAGE_TIME;
import static org.briarproject.api.introduction.IntroductionConstants.MSG;
import static org.briarproject.api.introduction.IntroductionConstants.NAME;
import static org.briarproject.api.introduction.IntroductionConstants.PUBLIC_KEY;
@@ -61,7 +61,7 @@ public class MessageSender {
Group group = db.getGroup(txn, groupId);
long timestamp = clock.currentTimeMillis();
message.put(MESSAGE_TIME, timestamp);
message.put(TIMESTAMP, timestamp);
Metadata metadata = metadataEncoder.encode(message);
messageQueueManager

View File

@@ -24,7 +24,7 @@ import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus;
import org.briarproject.clients.BdfIncomingMessageHook;
import org.briarproject.clients.ReadableMessageManagerImpl;
import org.briarproject.util.StringUtils;
import java.util.ArrayList;
@@ -33,23 +33,25 @@ import java.util.Map;
import javax.inject.Inject;
class MessagingManagerImpl extends BdfIncomingMessageHook
import static org.briarproject.api.clients.ReadableMessageConstants.LOCAL;
import static org.briarproject.api.clients.ReadableMessageConstants.READ;
import static org.briarproject.api.clients.ReadableMessageConstants.TIMESTAMP;
class MessagingManagerImpl extends ReadableMessageManagerImpl
implements MessagingManager, Client, AddContactHook, RemoveContactHook {
static final ClientId CLIENT_ID = new ClientId(StringUtils.fromHexString(
"6bcdc006c0910b0f44e40644c3b31f1a"
+ "8bf9a6d6021d40d219c86b731b903070"));
private final DatabaseComponent db;
private final PrivateGroupFactory privateGroupFactory;
@Inject
MessagingManagerImpl(DatabaseComponent db, ClientHelper clientHelper,
MetadataParser metadataParser,
PrivateGroupFactory privateGroupFactory) {
super(clientHelper, metadataParser);
super(clientHelper, db, metadataParser);
this.db = db;
this.privateGroupFactory = privateGroupFactory;
}
@@ -78,7 +80,8 @@ class MessagingManagerImpl extends BdfIncomingMessageHook
}
}
private Group getContactGroup(Contact c) {
@Override
protected Group getContactGroup(Contact c) {
return privateGroupFactory.createPrivateGroup(CLIENT_ID, c);
}
@@ -93,30 +96,33 @@ class MessagingManagerImpl extends BdfIncomingMessageHook
}
@Override
protected void incomingMessage(Transaction txn, Message m, BdfList body,
protected boolean incomingReadableMessage(Transaction txn, Message m, BdfList body,
BdfDictionary meta) throws DbException, FormatException {
// Broadcast event
GroupId groupId = m.getGroupId();
long timestamp = meta.getLong("timestamp");
long timestamp = meta.getLong(TIMESTAMP);
String contentType = meta.getString("contentType");
boolean local = meta.getBoolean("local");
boolean read = meta.getBoolean("read");
boolean local = meta.getBoolean(LOCAL);
boolean read = meta.getBoolean(READ);
PrivateMessageHeader header = new PrivateMessageHeader(
m.getId(), timestamp, contentType, local, read, false, false);
PrivateMessageReceivedEvent event = new PrivateMessageReceivedEvent(
header, groupId);
txn.attach(event);
return true;
}
@Override
public void addLocalMessage(PrivateMessage m) throws DbException {
try {
BdfDictionary meta = new BdfDictionary();
meta.put("timestamp", m.getMessage().getTimestamp());
meta.put(TIMESTAMP, m.getMessage().getTimestamp());
if (m.getParent() != null) meta.put("parent", m.getParent());
meta.put("contentType", m.getContentType());
meta.put("local", true);
meta.put("read", true);
meta.put(LOCAL, true);
meta.put(READ, true);
clientHelper.addLocalMessage(m.getMessage(), CLIENT_ID, meta, true);
} catch (FormatException e) {
throw new RuntimeException(e);
@@ -169,10 +175,10 @@ class MessagingManagerImpl extends BdfIncomingMessageHook
BdfDictionary meta = metadata.get(id);
if (meta == null) continue;
try {
long timestamp = meta.getLong("timestamp");
long timestamp = meta.getLong(TIMESTAMP);
String contentType = meta.getString("contentType");
boolean local = meta.getBoolean("local");
boolean read = meta.getBoolean("read");
boolean local = meta.getBoolean(LOCAL);
boolean read = meta.getBoolean(READ);
headers.add(new PrivateMessageHeader(id, timestamp, contentType,
local, read, s.isSent(), s.isSeen()));
} catch (FormatException e) {
@@ -192,15 +198,4 @@ class MessagingManagerImpl extends BdfIncomingMessageHook
throw new DbException(e);
}
}
@Override
public void setReadFlag(MessageId m, boolean read) throws DbException {
try {
BdfDictionary meta = new BdfDictionary();
meta.put("read", read);
clientHelper.mergeMessageMetadata(m, meta);
} catch (FormatException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -12,6 +12,9 @@ import org.briarproject.api.sync.Message;
import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator;
import static org.briarproject.api.clients.ReadableMessageConstants.LOCAL;
import static org.briarproject.api.clients.ReadableMessageConstants.READ;
import static org.briarproject.api.clients.ReadableMessageConstants.TIMESTAMP;
import static org.briarproject.api.messaging.MessagingConstants.MAX_CONTENT_TYPE_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH;
@@ -38,11 +41,11 @@ class PrivateMessageValidator extends BdfMessageValidator {
checkLength(privateMessageBody, 0, MAX_PRIVATE_MESSAGE_BODY_LENGTH);
// Return the metadata
BdfDictionary meta = new BdfDictionary();
meta.put("timestamp", m.getTimestamp());
meta.put(TIMESTAMP, m.getTimestamp());
if (parentId != null) meta.put("parent", parentId);
meta.put("contentType", contentType);
meta.put("local", false);
meta.put("read", false);
meta.put(LOCAL, false);
meta.put(READ, false);
return new BdfMessageContext(meta);
}
}

View File

@@ -16,21 +16,21 @@ import javax.inject.Inject;
import static org.briarproject.api.blogs.BlogConstants.BLOG_AUTHOR_NAME;
import static org.briarproject.api.blogs.BlogConstants.BLOG_DESC;
import static org.briarproject.api.blogs.BlogConstants.BLOG_TITLE;
import static org.briarproject.api.blogs.BlogConstants.BLOG_PUBLIC_KEY;
import static org.briarproject.api.blogs.BlogConstants.BLOG_TITLE;
import static org.briarproject.api.blogs.BlogConstants.MAX_BLOG_DESC_LENGTH;
import static org.briarproject.api.blogs.BlogConstants.MAX_BLOG_TITLE_LENGTH;
import static org.briarproject.api.clients.ReadableMessageConstants.LOCAL;
import static org.briarproject.api.clients.ReadableMessageConstants.TIMESTAMP;
import static org.briarproject.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
import static org.briarproject.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
import static org.briarproject.api.sharing.SharingConstants.INVITATION_MSG;
import static org.briarproject.api.sharing.SharingConstants.LOCAL;
import static org.briarproject.api.sharing.SharingConstants.SESSION_ID;
import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_ABORT;
import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_ACCEPT;
import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_DECLINE;
import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_INVITATION;
import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_LEAVE;
import static org.briarproject.api.sharing.SharingConstants.TIME;
import static org.briarproject.api.sharing.SharingConstants.TYPE;
import static org.briarproject.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
@@ -92,7 +92,7 @@ class BlogSharingValidator extends BdfMessageValidator {
d.put(TYPE, type);
d.put(SESSION_ID, id);
d.put(LOCAL, false);
d.put(TIME, m.getTimestamp());
d.put(TIMESTAMP, m.getTimestamp());
return new BdfMessageContext(d);
}
}

View File

@@ -14,19 +14,20 @@ import org.briarproject.clients.BdfMessageValidator;
import javax.inject.Inject;
import static org.briarproject.api.clients.ReadableMessageConstants.LOCAL;
import static org.briarproject.api.clients.ReadableMessageConstants.READ;
import static org.briarproject.api.clients.ReadableMessageConstants.TIMESTAMP;
import static org.briarproject.api.forum.ForumConstants.FORUM_NAME;
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT;
import static org.briarproject.api.forum.ForumConstants.FORUM_SALT_LENGTH;
import static org.briarproject.api.forum.ForumConstants.MAX_FORUM_NAME_LENGTH;
import static org.briarproject.api.sharing.SharingConstants.INVITATION_MSG;
import static org.briarproject.api.sharing.SharingConstants.LOCAL;
import static org.briarproject.api.sharing.SharingConstants.SESSION_ID;
import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_ABORT;
import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_ACCEPT;
import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_DECLINE;
import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_INVITATION;
import static org.briarproject.api.sharing.SharingConstants.SHARE_MSG_TYPE_LEAVE;
import static org.briarproject.api.sharing.SharingConstants.TIME;
import static org.briarproject.api.sharing.SharingConstants.TYPE;
import static org.briarproject.api.sync.SyncConstants.MAX_MESSAGE_BODY_LENGTH;
@@ -77,7 +78,8 @@ class ForumSharingValidator extends BdfMessageValidator {
d.put(TYPE, type);
d.put(SESSION_ID, id);
d.put(LOCAL, false);
d.put(TIME, m.getTimestamp());
d.put(TIMESTAMP, m.getTimestamp());
d.put(READ, false);
return new BdfMessageContext(d);
}
}

View File

@@ -35,7 +35,7 @@ import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.api.sync.MessageStatus;
import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfIncomingMessageHook;
import org.briarproject.clients.ReadableMessageManagerImpl;
import org.briarproject.util.StringUtils;
import java.io.IOException;
@@ -53,10 +53,11 @@ import java.util.logging.Logger;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING;
import static org.briarproject.api.clients.ProtocolEngine.StateUpdate;
import static org.briarproject.api.clients.ReadableMessageConstants.LOCAL;
import static org.briarproject.api.clients.ReadableMessageConstants.READ;
import static org.briarproject.api.clients.ReadableMessageConstants.TIMESTAMP;
import static org.briarproject.api.sharing.SharingConstants.CONTACT_ID;
import static org.briarproject.api.sharing.SharingConstants.IS_SHARER;
import static org.briarproject.api.sharing.SharingConstants.LOCAL;
import static org.briarproject.api.sharing.SharingConstants.READ;
import static org.briarproject.api.sharing.SharingConstants.SESSION_ID;
import static org.briarproject.api.sharing.SharingConstants.SHAREABLE_ID;
import static org.briarproject.api.sharing.SharingConstants.SHARED_BY_US;
@@ -76,7 +77,6 @@ import static org.briarproject.api.sharing.SharingConstants.TASK_REMOVE_SHAREABL
import static org.briarproject.api.sharing.SharingConstants.TASK_SHARE_SHAREABLE;
import static org.briarproject.api.sharing.SharingConstants.TASK_UNSHARE_SHAREABLE_SHARED_BY_US;
import static org.briarproject.api.sharing.SharingConstants.TASK_UNSHARE_SHAREABLE_SHARED_WITH_US;
import static org.briarproject.api.sharing.SharingConstants.TIME;
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;
@@ -84,14 +84,13 @@ 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
extends ReadableMessageManagerImpl
implements SharingManager<S, IM>, Client, AddContactHook,
RemoveContactHook {
private static final Logger LOG =
Logger.getLogger(SharingManagerImpl.class.getName());
private final DatabaseComponent db;
private final MessageQueueManager messageQueueManager;
private final MetadataEncoder metadataEncoder;
private final SecureRandom random;
@@ -105,8 +104,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
SecureRandom random, PrivateGroupFactory privateGroupFactory,
Clock clock) {
super(clientHelper, metadataParser);
this.db = db;
super(clientHelper, db, metadataParser);
this.messageQueueManager = messageQueueManager;
this.metadataEncoder = metadataEncoder;
this.random = random;
@@ -186,8 +184,8 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
}
@Override
protected void incomingMessage(Transaction txn, Message m, BdfList body,
BdfDictionary d) throws DbException, FormatException {
protected boolean incomingReadableMessage(Transaction txn, Message m,
BdfList body, BdfDictionary d) throws DbException, FormatException {
BaseMessage msg = BaseMessage.from(getIFactory(), m.getGroupId(), d);
SessionId sessionId = msg.getSessionId();
@@ -223,6 +221,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
} catch (FormatException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
deleteMessage(txn, m.getId());
return false;
}
} else if (msg.getType() == SHARE_MSG_TYPE_ACCEPT ||
msg.getType() == SHARE_MSG_TYPE_DECLINE) {
@@ -257,6 +256,9 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
// message has passed validator, so that should never happen
throw new RuntimeException("Illegal Sharing Message");
}
// The message is valid
return true;
}
@Override
@@ -344,7 +346,7 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
I msg = getIFactory().build(group.getId(), d);
MessageStatus status =
db.getMessageStatus(txn, contactId, m.getKey());
long time = d.getLong(TIME);
long time = d.getLong(TIMESTAMP);
boolean local = d.getBoolean(LOCAL);
boolean read = d.getBoolean(READ, false);
boolean available = false;
@@ -492,17 +494,6 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
}
}
@Override
public void setReadFlag(MessageId m, boolean read) throws DbException {
try {
BdfDictionary meta = new BdfDictionary();
meta.put(READ, read);
clientHelper.mergeMessageMetadata(m, meta);
} catch (FormatException e) {
throw new RuntimeException(e);
}
}
void removingShareable(Transaction txn, S f) throws DbException {
try {
for (Contact c : db.getContacts(txn)) {
@@ -851,14 +842,14 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
// add message itself as metadata
BdfDictionary d = m.toBdfDictionary();
d.put(LOCAL, true);
d.put(TIME, timestamp);
d.put(TIMESTAMP, timestamp);
Metadata meta = metadataEncoder.encode(d);
messageQueueManager
.sendMessage(txn, group, timestamp, body, meta);
}
private Group getContactGroup(Contact c) {
protected Group getContactGroup(Contact c) {
return privateGroupFactory.createPrivateGroup(getClientId(), c);
}
@@ -933,8 +924,8 @@ abstract class SharingManagerImpl<S extends Shareable, I extends Invitation, IM
}
private void storeShareableList(Transaction txn, GroupId groupId,
String key,
List<S> shareables) throws DbException, FormatException {
String key, List<S> shareables)
throws DbException, FormatException {
BdfList list = encodeShareableList(shareables);
BdfDictionary metadata = BdfDictionary.of(