Remove content-type and parentId from private messages

and turn them into a regular string.
This commit is contained in:
Torsten Grote
2016-10-27 13:30:34 -02:00
parent a18317e912
commit 78740a6942
18 changed files with 81 additions and 112 deletions

View File

@@ -31,7 +31,7 @@ class ForumPostFactoryImpl implements ForumPostFactory {
MessageId parent, LocalAuthor author, String body)
throws FormatException, GeneralSecurityException {
// Validate the arguments
if (StringUtils.isTooLong(body, MAX_FORUM_POST_BODY_LENGTH))
if (StringUtils.utf8IsTooLong(body, MAX_FORUM_POST_BODY_LENGTH))
throw new IllegalArgumentException();
// Serialise the data to be signed
BdfList authorList =

View File

@@ -98,12 +98,10 @@ class MessagingManagerImpl extends ConversationClientImpl
GroupId groupId = m.getGroupId();
long timestamp = meta.getLong("timestamp");
String contentType = meta.getString("contentType");
boolean local = meta.getBoolean("local");
boolean read = meta.getBoolean(MSG_KEY_READ);
PrivateMessageHeader header = new PrivateMessageHeader(
m.getId(), groupId, timestamp, contentType, local, read,
false, false);
m.getId(), groupId, timestamp, local, read, false, false);
ContactId contactId = getContactId(txn, groupId);
PrivateMessageReceivedEvent event = new PrivateMessageReceivedEvent(
header, contactId, groupId);
@@ -120,8 +118,6 @@ class MessagingManagerImpl extends ConversationClientImpl
try {
BdfDictionary meta = new BdfDictionary();
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);
clientHelper.addLocalMessage(txn, m.getMessage(), meta, true);
@@ -193,11 +189,11 @@ class MessagingManagerImpl extends ConversationClientImpl
if (meta == null) continue;
try {
long timestamp = meta.getLong("timestamp");
String contentType = meta.getString("contentType");
boolean local = meta.getBoolean("local");
boolean read = meta.getBoolean("read");
headers.add(new PrivateMessageHeader(id, g, timestamp,
contentType, local, read, s.isSent(), s.isSeen()));
headers.add(
new PrivateMessageHeader(id, g, timestamp, local, read,
s.isSent(), s.isSeen()));
} catch (FormatException e) {
throw new DbException(e);
}
@@ -206,11 +202,11 @@ class MessagingManagerImpl extends ConversationClientImpl
}
@Override
public byte[] getMessageBody(MessageId m) throws DbException {
public String getMessageBody(MessageId m) throws DbException {
try {
// Parent ID, content type, private message body
// 0: private message body
BdfList message = clientHelper.getMessageAsList(m);
return message.getRaw(2);
return message.getString(0);
} catch (FormatException e) {
throw new DbException(e);
}

View File

@@ -5,16 +5,16 @@ import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.messaging.PrivateMessage;
import org.briarproject.api.messaging.PrivateMessageFactory;
import org.briarproject.api.nullsafety.NotNullByDefault;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.briarproject.util.StringUtils;
import javax.inject.Inject;
import static org.briarproject.api.messaging.MessagingConstants.MAX_CONTENT_TYPE_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH;
import static org.briarproject.util.StringUtils.utf8IsTooLong;
@NotNullByDefault
class PrivateMessageFactoryImpl implements PrivateMessageFactory {
private final ClientHelper clientHelper;
@@ -26,16 +26,13 @@ class PrivateMessageFactoryImpl implements PrivateMessageFactory {
@Override
public PrivateMessage createPrivateMessage(GroupId groupId, long timestamp,
MessageId parent, String contentType, byte[] body)
throws FormatException {
String body) throws FormatException {
// Validate the arguments
if (StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH)
throw new IllegalArgumentException();
if (body.length > MAX_PRIVATE_MESSAGE_BODY_LENGTH)
if (utf8IsTooLong(body, MAX_PRIVATE_MESSAGE_BODY_LENGTH))
throw new IllegalArgumentException();
// Serialise the message
BdfList message = BdfList.of(parent, contentType, body);
BdfList message = BdfList.of(body);
Message m = clientHelper.createMessage(groupId, timestamp, message);
return new PrivateMessage(m, parent, contentType);
return new PrivateMessage(m);
}
}

View File

@@ -1,9 +1,8 @@
package org.briarproject.messaging;
import org.briarproject.api.FormatException;
import org.briarproject.api.UniqueId;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.BdfMessageContext;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataEncoder;
@@ -12,7 +11,6 @@ import org.briarproject.api.sync.Message;
import org.briarproject.api.system.Clock;
import org.briarproject.clients.BdfMessageValidator;
import static org.briarproject.api.messaging.MessagingConstants.MAX_CONTENT_TYPE_LENGTH;
import static org.briarproject.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_BODY_LENGTH;
import static org.briarproject.clients.BdfConstants.MSG_KEY_READ;
@@ -26,22 +24,14 @@ class PrivateMessageValidator extends BdfMessageValidator {
@Override
protected BdfMessageContext validateMessage(Message m, Group g,
BdfList body) throws FormatException {
// Parent ID, content type, private message body
checkSize(body, 3);
// Parent ID is optional
byte[] parentId = body.getOptionalRaw(0);
checkLength(parentId, UniqueId.LENGTH);
// Content type
String contentType = body.getString(1);
checkLength(contentType, 0, MAX_CONTENT_TYPE_LENGTH);
// private message body
checkSize(body, 1);
// Private message body
byte[] privateMessageBody = body.getRaw(2);
String privateMessageBody = body.getString(0);
checkLength(privateMessageBody, 0, MAX_PRIVATE_MESSAGE_BODY_LENGTH);
// Return the metadata
BdfDictionary meta = new BdfDictionary();
meta.put("timestamp", m.getTimestamp());
if (parentId != null) meta.put("parent", parentId);
meta.put("contentType", contentType);
meta.put("local", false);
meta.put(MSG_KEY_READ, false);
return new BdfMessageContext(meta);

View File

@@ -104,7 +104,7 @@ public class StringUtils {
/**
* Returns true if the string is longer than maxLength
*/
public static boolean isTooLong(String s, int maxLength) {
public static boolean utf8IsTooLong(String s, int maxLength) {
return toUtf8(s).length > maxLength;
}
}