mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 07:09:56 +01:00
Address bugs and comments from reblogging code review
This commit is contained in:
@@ -12,8 +12,8 @@ import static org.briarproject.api.blogs.MessageType.WRAPPED_COMMENT;
|
|||||||
|
|
||||||
public class BlogCommentHeader extends BlogPostHeader {
|
public class BlogCommentHeader extends BlogPostHeader {
|
||||||
|
|
||||||
private String comment;
|
private final String comment;
|
||||||
private BlogPostHeader parent;
|
private final BlogPostHeader parent;
|
||||||
|
|
||||||
public BlogCommentHeader(@NotNull MessageType type,
|
public BlogCommentHeader(@NotNull MessageType type,
|
||||||
@NotNull GroupId groupId, @Nullable String comment,
|
@NotNull GroupId groupId, @Nullable String comment,
|
||||||
@@ -36,7 +36,6 @@ public class BlogCommentHeader extends BlogPostHeader {
|
|||||||
return comment;
|
return comment;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
|
||||||
public BlogPostHeader getParent() {
|
public BlogPostHeader getParent() {
|
||||||
return parent;
|
return parent;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ public interface BlogConstants {
|
|||||||
String KEY_COMMENT = "comment";
|
String KEY_COMMENT = "comment";
|
||||||
String KEY_ORIGINAL_MSG_ID = "originalMessageId";
|
String KEY_ORIGINAL_MSG_ID = "originalMessageId";
|
||||||
String KEY_ORIGINAL_PARENT_MSG_ID = "originalParentMessageId";
|
String KEY_ORIGINAL_PARENT_MSG_ID = "originalParentMessageId";
|
||||||
String KEY_WRAPPED_MSG_ID = "wrappedMessageId";
|
/**
|
||||||
|
* This is the ID of either a message wrapped from a different group
|
||||||
|
* or of a message from the same group that therefore needed no wrapping.
|
||||||
|
*/
|
||||||
|
String KEY_PARENT_MSG_ID = "parentMessageId";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,20 +23,20 @@ public interface BlogPostFactory {
|
|||||||
throws FormatException, GeneralSecurityException;
|
throws FormatException, GeneralSecurityException;
|
||||||
|
|
||||||
/** Wraps a blog post */
|
/** Wraps a blog post */
|
||||||
Message createWrappedPost(GroupId groupId, byte[] descriptor,
|
Message wrapPost(GroupId groupId, byte[] descriptor,
|
||||||
long timestamp, BdfList body)
|
long timestamp, BdfList body)
|
||||||
throws FormatException;
|
throws FormatException;
|
||||||
|
|
||||||
/** Re-wraps a previously wrapped post */
|
/** Re-wraps a previously wrapped post */
|
||||||
Message createWrappedPost(GroupId groupId, BdfList body)
|
Message rewrapWrappedPost(GroupId groupId, BdfList body)
|
||||||
throws FormatException;
|
throws FormatException;
|
||||||
|
|
||||||
/** Wraps a blog comment */
|
/** Wraps a blog comment */
|
||||||
Message createWrappedComment(GroupId groupId, byte[] descriptor,
|
Message wrapComment(GroupId groupId, byte[] descriptor,
|
||||||
long timestamp, BdfList body, MessageId currentId)
|
long timestamp, BdfList body, MessageId currentId)
|
||||||
throws FormatException;
|
throws FormatException;
|
||||||
|
|
||||||
/** Re-wraps a previously wrapped comment */
|
/** Re-wraps a previously wrapped comment */
|
||||||
Message createWrappedComment(GroupId groupId, BdfList body,
|
Message rewrapWrappedComment(GroupId groupId, BdfList body,
|
||||||
MessageId currentId) throws FormatException;
|
MessageId currentId) throws FormatException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ import static org.briarproject.api.blogs.BlogConstants.KEY_READ;
|
|||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_TIMESTAMP;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_TIMESTAMP;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_TIME_RECEIVED;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_TIME_RECEIVED;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_TYPE;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_TYPE;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_WRAPPED_MSG_ID;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_PARENT_MSG_ID;
|
||||||
import static org.briarproject.api.blogs.MessageType.COMMENT;
|
import static org.briarproject.api.blogs.MessageType.COMMENT;
|
||||||
import static org.briarproject.api.blogs.MessageType.POST;
|
import static org.briarproject.api.blogs.MessageType.POST;
|
||||||
import static org.briarproject.api.blogs.MessageType.WRAPPED_COMMENT;
|
import static org.briarproject.api.blogs.MessageType.WRAPPED_COMMENT;
|
||||||
@@ -189,12 +189,12 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
BdfDictionary d = clientHelper
|
BdfDictionary d = clientHelper
|
||||||
.getMessageMetadataAsDictionary(txn, h.getParentId());
|
.getMessageMetadataAsDictionary(txn, h.getParentId());
|
||||||
byte[] original1 = d.getRaw(KEY_ORIGINAL_MSG_ID);
|
byte[] original1 = d.getRaw(KEY_ORIGINAL_MSG_ID);
|
||||||
byte[] original2 = meta.getRaw(KEY_ORIGINAL_MSG_ID);
|
byte[] original2 = meta.getRaw(KEY_ORIGINAL_PARENT_MSG_ID);
|
||||||
if (!Arrays.equals(original1, original2)) {
|
if (!Arrays.equals(original1, original2)) {
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// share dependencies recursively
|
// share dependencies recursively - TODO remove with #598
|
||||||
share(txn, h);
|
share(txn, h);
|
||||||
|
|
||||||
// broadcast event about new post or comment
|
// broadcast event about new post or comment
|
||||||
@@ -205,7 +205,7 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
// Check that the original message ID in the dependency's metadata
|
// Check that the original message ID in the dependency's metadata
|
||||||
// matches the original parent ID of the wrapped comment
|
// matches the original parent ID of the wrapped comment
|
||||||
MessageId dependencyId =
|
MessageId dependencyId =
|
||||||
new MessageId(meta.getRaw(KEY_WRAPPED_MSG_ID));
|
new MessageId(meta.getRaw(KEY_PARENT_MSG_ID));
|
||||||
BdfDictionary d = clientHelper
|
BdfDictionary d = clientHelper
|
||||||
.getMessageMetadataAsDictionary(txn, dependencyId);
|
.getMessageMetadataAsDictionary(txn, dependencyId);
|
||||||
byte[] original1 = d.getRaw(KEY_ORIGINAL_MSG_ID);
|
byte[] original1 = d.getRaw(KEY_ORIGINAL_MSG_ID);
|
||||||
@@ -305,7 +305,7 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
GroupId groupId = p.getMessage().getGroupId();
|
GroupId groupId = p.getMessage().getGroupId();
|
||||||
MessageId postId = p.getMessage().getId();
|
MessageId postId = p.getMessage().getId();
|
||||||
BlogPostHeader h =
|
BlogPostHeader h =
|
||||||
getPostHeaderFromMetadata(txn, 0, groupId, postId, meta);
|
getPostHeaderFromMetadata(txn, groupId, postId, meta);
|
||||||
BlogPostAddedEvent event =
|
BlogPostAddedEvent event =
|
||||||
new BlogPostAddedEvent(groupId, h, true);
|
new BlogPostAddedEvent(groupId, h, true);
|
||||||
txn.attach(event);
|
txn.attach(event);
|
||||||
@@ -316,37 +316,33 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addLocalComment(LocalAuthor author, GroupId groupId,
|
public void addLocalComment(LocalAuthor author, GroupId groupId,
|
||||||
@Nullable String comment, BlogPostHeader wHeader)
|
@Nullable String comment, BlogPostHeader pOriginalHeader)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
|
|
||||||
if (wHeader.getType() != POST && wHeader.getType() != COMMENT)
|
MessageType type = pOriginalHeader.getType();
|
||||||
|
if (type != POST && type != COMMENT)
|
||||||
throw new IllegalArgumentException("Comment on unknown type!");
|
throw new IllegalArgumentException("Comment on unknown type!");
|
||||||
|
|
||||||
Transaction txn = db.startTransaction(false);
|
Transaction txn = db.startTransaction(false);
|
||||||
try {
|
try {
|
||||||
// Wrap post that we are commenting on
|
// Wrap post that we are commenting on
|
||||||
MessageId wMessageId = wrapMessage(txn, 0, groupId, wHeader);
|
MessageId parentId = wrapMessage(txn, groupId, pOriginalHeader);
|
||||||
|
|
||||||
// Get ID of original message
|
// Get ID of new parent's original message.
|
||||||
MessageId originalId = wHeader.getId();
|
// Assumes that pOriginalHeader is a POST or COMMENT
|
||||||
if (wHeader.getType() != POST && wMessageId.equals(originalId)) {
|
MessageId pOriginalId = pOriginalHeader.getId();
|
||||||
// we comment on a non-post message that needs no wrapping,
|
|
||||||
// so get original message ID from it
|
|
||||||
BdfDictionary d = clientHelper
|
|
||||||
.getMessageMetadataAsDictionary(txn, wMessageId);
|
|
||||||
originalId = new MessageId(d.getRaw(KEY_ORIGINAL_MSG_ID));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create actual comment
|
// Create actual comment
|
||||||
Message message = blogPostFactory
|
Message message = blogPostFactory
|
||||||
.createBlogComment(groupId, author, comment, originalId,
|
.createBlogComment(groupId, author, comment, pOriginalId,
|
||||||
wMessageId);
|
parentId);
|
||||||
BdfDictionary meta = new BdfDictionary();
|
BdfDictionary meta = new BdfDictionary();
|
||||||
meta.put(KEY_TYPE, COMMENT.getInt());
|
meta.put(KEY_TYPE, COMMENT.getInt());
|
||||||
if (comment != null) meta.put(KEY_COMMENT, comment);
|
if (comment != null) meta.put(KEY_COMMENT, comment);
|
||||||
meta.put(KEY_TIMESTAMP, message.getTimestamp());
|
meta.put(KEY_TIMESTAMP, message.getTimestamp());
|
||||||
meta.put(KEY_ORIGINAL_MSG_ID, wHeader.getId());
|
meta.put(KEY_ORIGINAL_MSG_ID, message.getId());
|
||||||
meta.put(KEY_WRAPPED_MSG_ID, wMessageId);
|
meta.put(KEY_ORIGINAL_PARENT_MSG_ID, pOriginalId);
|
||||||
|
meta.put(KEY_PARENT_MSG_ID, parentId);
|
||||||
meta.put(KEY_AUTHOR, authorToBdfDictionary(author));
|
meta.put(KEY_AUTHOR, authorToBdfDictionary(author));
|
||||||
|
|
||||||
// Send comment
|
// Send comment
|
||||||
@@ -362,72 +358,75 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
} catch (GeneralSecurityException e) {
|
} catch (GeneralSecurityException e) {
|
||||||
throw new DbException(e);
|
throw new IllegalArgumentException("Invalid key of author", e);
|
||||||
} finally {
|
} finally {
|
||||||
//noinspection ThrowFromFinallyBlock
|
//noinspection ThrowFromFinallyBlock
|
||||||
db.endTransaction(txn);
|
db.endTransaction(txn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private MessageId wrapMessage(Transaction txn, int level, GroupId groupId,
|
private MessageId wrapMessage(Transaction txn, GroupId groupId,
|
||||||
BlogPostHeader wHeader)
|
BlogPostHeader pOriginalHeader)
|
||||||
throws DbException, FormatException {
|
throws DbException, FormatException {
|
||||||
|
|
||||||
if (groupId.equals(wHeader.getGroupId())) {
|
if (groupId.equals(pOriginalHeader.getGroupId())) {
|
||||||
// We are trying to wrap a post that is already in our group.
|
// We are trying to wrap a post that is already in our group.
|
||||||
// This is unnecessary, so just return the post's MessageId
|
// This is unnecessary, so just return the post's MessageId
|
||||||
return wHeader.getId();
|
return pOriginalHeader.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get Group and Body of Message to be wrapped
|
// Get body of message to be wrapped
|
||||||
Group wGroup = db.getGroup(txn, wHeader.getGroupId());
|
BdfList body =
|
||||||
BdfList wBody = clientHelper.getMessageAsList(txn, wHeader.getId());
|
clientHelper.getMessageAsList(txn, pOriginalHeader.getId());
|
||||||
byte[] wDescriptor = wGroup.getDescriptor();
|
long wTimestamp = pOriginalHeader.getTimestamp();
|
||||||
long wTimestamp = wHeader.getTimestamp();
|
|
||||||
Message wMessage;
|
Message wMessage;
|
||||||
|
|
||||||
BdfDictionary meta = new BdfDictionary();
|
BdfDictionary meta = new BdfDictionary();
|
||||||
MessageType type = wHeader.getType();
|
MessageType type = pOriginalHeader.getType();
|
||||||
if (type == POST) {
|
if (type == POST) {
|
||||||
|
Group wGroup = db.getGroup(txn, pOriginalHeader.getGroupId());
|
||||||
|
byte[] wDescriptor = wGroup.getDescriptor();
|
||||||
// Wrap post
|
// Wrap post
|
||||||
wMessage = blogPostFactory
|
wMessage = blogPostFactory
|
||||||
.createWrappedPost(groupId, wDescriptor, wTimestamp, wBody);
|
.wrapPost(groupId, wDescriptor, wTimestamp, body);
|
||||||
meta.put(KEY_TYPE, WRAPPED_POST.getInt());
|
meta.put(KEY_TYPE, WRAPPED_POST.getInt());
|
||||||
} else if (type == COMMENT) {
|
} else if (type == COMMENT) {
|
||||||
BlogCommentHeader wComment = (BlogCommentHeader) wHeader;
|
Group wGroup = db.getGroup(txn, pOriginalHeader.getGroupId());
|
||||||
|
byte[] wDescriptor = wGroup.getDescriptor();
|
||||||
|
BlogCommentHeader wComment = (BlogCommentHeader) pOriginalHeader;
|
||||||
MessageId wrappedId =
|
MessageId wrappedId =
|
||||||
wrapMessage(txn, ++level, groupId, wComment.getParent());
|
wrapMessage(txn, groupId, wComment.getParent());
|
||||||
// Wrap comment
|
// Wrap comment
|
||||||
wMessage = blogPostFactory
|
wMessage = blogPostFactory
|
||||||
.createWrappedComment(groupId, wDescriptor, wTimestamp,
|
.wrapComment(groupId, wDescriptor, wTimestamp,
|
||||||
wBody, wrappedId);
|
body, wrappedId);
|
||||||
meta.put(KEY_TYPE, WRAPPED_COMMENT.getInt());
|
meta.put(KEY_TYPE, WRAPPED_COMMENT.getInt());
|
||||||
if(wComment.getComment() != null)
|
if(wComment.getComment() != null)
|
||||||
meta.put(KEY_COMMENT, wComment.getComment());
|
meta.put(KEY_COMMENT, wComment.getComment());
|
||||||
meta.put(KEY_WRAPPED_MSG_ID, wrappedId);
|
meta.put(KEY_PARENT_MSG_ID, wrappedId);
|
||||||
} else if (type == WRAPPED_POST) {
|
} else if (type == WRAPPED_POST) {
|
||||||
// Re-wrap wrapped post without adding another wrapping layer
|
// Re-wrap wrapped post without adding another wrapping layer
|
||||||
wMessage = blogPostFactory.createWrappedPost(groupId, wBody);
|
wMessage = blogPostFactory.rewrapWrappedPost(groupId, body);
|
||||||
meta.put(KEY_TYPE, WRAPPED_POST.getInt());
|
meta.put(KEY_TYPE, WRAPPED_POST.getInt());
|
||||||
} else if (type == WRAPPED_COMMENT) {
|
} else if (type == WRAPPED_COMMENT) {
|
||||||
BlogCommentHeader wComment = (BlogCommentHeader) wHeader;
|
BlogCommentHeader wComment = (BlogCommentHeader) pOriginalHeader;
|
||||||
MessageId wrappedId =
|
MessageId wrappedId =
|
||||||
wrapMessage(txn, ++level, groupId, wComment.getParent());
|
wrapMessage(txn, groupId, wComment.getParent());
|
||||||
// Re-wrap wrapped comment
|
// Re-wrap wrapped comment
|
||||||
wMessage = blogPostFactory
|
wMessage = blogPostFactory
|
||||||
.createWrappedComment(groupId, wBody, wrappedId);
|
.rewrapWrappedComment(groupId, body, wrappedId);
|
||||||
meta.put(KEY_TYPE, WRAPPED_COMMENT.getInt());
|
meta.put(KEY_TYPE, WRAPPED_COMMENT.getInt());
|
||||||
if(wComment.getComment() != null)
|
if(wComment.getComment() != null)
|
||||||
meta.put(KEY_COMMENT, wComment.getComment());
|
meta.put(KEY_COMMENT, wComment.getComment());
|
||||||
meta.put(KEY_WRAPPED_MSG_ID, wrappedId);
|
meta.put(KEY_PARENT_MSG_ID, wrappedId);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"Unknown Message Type: " + type);
|
"Unknown Message Type: " + type);
|
||||||
}
|
}
|
||||||
meta.put(KEY_ORIGINAL_MSG_ID, wHeader.getId());
|
meta.put(KEY_ORIGINAL_MSG_ID, pOriginalHeader.getId());
|
||||||
meta.put(KEY_AUTHOR, authorToBdfDictionary(wHeader.getAuthor()));
|
meta.put(KEY_AUTHOR, authorToBdfDictionary(pOriginalHeader.getAuthor()));
|
||||||
meta.put(KEY_TIMESTAMP, wHeader.getTimestamp());
|
meta.put(KEY_TIMESTAMP, pOriginalHeader.getTimestamp());
|
||||||
meta.put(KEY_TIME_RECEIVED, wHeader.getTimeReceived());
|
meta.put(KEY_TIME_RECEIVED, pOriginalHeader.getTimeReceived());
|
||||||
|
|
||||||
// Send wrapped message and store metadata
|
// Send wrapped message and store metadata
|
||||||
clientHelper.addLocalMessage(txn, wMessage, CLIENT_ID, meta, true);
|
clientHelper.addLocalMessage(txn, wMessage, CLIENT_ID, meta, true);
|
||||||
@@ -553,7 +552,7 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
new BdfEntry(KEY_TYPE, COMMENT.getInt())
|
new BdfEntry(KEY_TYPE, COMMENT.getInt())
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO this could be optimized to re-use existing headers
|
// TODO this could be optimized by looking up author status once (#625)
|
||||||
|
|
||||||
Collection<BlogPostHeader> headers = new ArrayList<BlogPostHeader>();
|
Collection<BlogPostHeader> headers = new ArrayList<BlogPostHeader>();
|
||||||
Transaction txn = db.startTransaction(true);
|
Transaction txn = db.startTransaction(true);
|
||||||
@@ -605,19 +604,13 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
}
|
}
|
||||||
|
|
||||||
private BlogPostHeader getPostHeaderFromMetadata(Transaction txn,
|
private BlogPostHeader getPostHeaderFromMetadata(Transaction txn,
|
||||||
GroupId groupId, MessageId id, BdfDictionary meta)
|
|
||||||
throws DbException, FormatException {
|
|
||||||
return getPostHeaderFromMetadata(txn, 0, groupId, id, meta);
|
|
||||||
}
|
|
||||||
|
|
||||||
private BlogPostHeader getPostHeaderFromMetadata(Transaction txn, int level,
|
|
||||||
GroupId groupId, MessageId id) throws DbException, FormatException {
|
GroupId groupId, MessageId id) throws DbException, FormatException {
|
||||||
BdfDictionary meta =
|
BdfDictionary meta =
|
||||||
clientHelper.getMessageMetadataAsDictionary(txn, id);
|
clientHelper.getMessageMetadataAsDictionary(txn, id);
|
||||||
return getPostHeaderFromMetadata(txn, level, groupId, id, meta);
|
return getPostHeaderFromMetadata(txn, groupId, id, meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BlogPostHeader getPostHeaderFromMetadata(Transaction txn, int level,
|
private BlogPostHeader getPostHeaderFromMetadata(Transaction txn,
|
||||||
GroupId groupId, MessageId id, BdfDictionary meta)
|
GroupId groupId, MessageId id, BdfDictionary meta)
|
||||||
throws DbException, FormatException {
|
throws DbException, FormatException {
|
||||||
|
|
||||||
@@ -637,9 +630,9 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
|
|
||||||
if (type == COMMENT || type == WRAPPED_COMMENT) {
|
if (type == COMMENT || type == WRAPPED_COMMENT) {
|
||||||
String comment = meta.getOptionalString(KEY_COMMENT);
|
String comment = meta.getOptionalString(KEY_COMMENT);
|
||||||
MessageId parentId = new MessageId(meta.getRaw(KEY_WRAPPED_MSG_ID));
|
MessageId parentId = new MessageId(meta.getRaw(KEY_PARENT_MSG_ID));
|
||||||
BlogPostHeader parent =
|
BlogPostHeader parent =
|
||||||
getPostHeaderFromMetadata(txn, ++level, groupId, parentId);
|
getPostHeaderFromMetadata(txn, groupId, parentId);
|
||||||
return new BlogCommentHeader(type, groupId, comment, parent, id,
|
return new BlogCommentHeader(type, groupId, comment, parent, id,
|
||||||
timestamp, timeReceived, author, authorStatus, read);
|
timestamp, timeReceived, author, authorStatus, read);
|
||||||
} else {
|
} else {
|
||||||
@@ -653,6 +646,8 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
|
|||||||
return MessageType.valueOf(longType.intValue());
|
return MessageType.valueOf(longType.intValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO remove when implementing #589
|
||||||
|
@Deprecated
|
||||||
private void share(Transaction txn, BlogPostHeader h) throws DbException {
|
private void share(Transaction txn, BlogPostHeader h) throws DbException {
|
||||||
clientHelper.setMessageShared(txn, h.getId(), true);
|
clientHelper.setMessageShared(txn, h.getId(), true);
|
||||||
if (h instanceof BlogCommentHeader) {
|
if (h instanceof BlogCommentHeader) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package org.briarproject.blogs;
|
|||||||
import org.briarproject.api.FormatException;
|
import org.briarproject.api.FormatException;
|
||||||
import org.briarproject.api.blogs.BlogPost;
|
import org.briarproject.api.blogs.BlogPost;
|
||||||
import org.briarproject.api.blogs.BlogPostFactory;
|
import org.briarproject.api.blogs.BlogPostFactory;
|
||||||
|
import org.briarproject.api.blogs.MessageType;
|
||||||
import org.briarproject.api.clients.ClientHelper;
|
import org.briarproject.api.clients.ClientHelper;
|
||||||
import org.briarproject.api.data.BdfList;
|
import org.briarproject.api.data.BdfList;
|
||||||
import org.briarproject.api.identity.LocalAuthor;
|
import org.briarproject.api.identity.LocalAuthor;
|
||||||
@@ -58,28 +59,31 @@ class BlogPostFactoryImpl implements BlogPostFactory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message createBlogComment(GroupId groupId, LocalAuthor author,
|
public Message createBlogComment(GroupId groupId, LocalAuthor author,
|
||||||
@Nullable String comment, MessageId originalId, MessageId wrappedId)
|
@Nullable String comment, MessageId pOriginalId, MessageId parentId)
|
||||||
throws FormatException, GeneralSecurityException {
|
throws FormatException, GeneralSecurityException {
|
||||||
|
|
||||||
long timestamp = clock.currentTimeMillis();
|
long timestamp = clock.currentTimeMillis();
|
||||||
|
|
||||||
// Generate the signature
|
// Generate the signature
|
||||||
BdfList signed =
|
BdfList signed =
|
||||||
BdfList.of(groupId, timestamp, comment, originalId, wrappedId);
|
BdfList.of(groupId, timestamp, comment, pOriginalId, parentId);
|
||||||
byte[] sig = clientHelper.sign(signed, author.getPrivateKey());
|
byte[] sig = clientHelper.sign(signed, author.getPrivateKey());
|
||||||
|
|
||||||
// Serialise the signed message
|
// Serialise the signed message
|
||||||
BdfList message =
|
BdfList message =
|
||||||
BdfList.of(COMMENT.getInt(), comment, originalId, wrappedId,
|
BdfList.of(COMMENT.getInt(), comment, pOriginalId, parentId,
|
||||||
sig);
|
sig);
|
||||||
return clientHelper.createMessage(groupId, timestamp, message);
|
return clientHelper.createMessage(groupId, timestamp, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message createWrappedPost(GroupId groupId, byte[] descriptor,
|
public Message wrapPost(GroupId groupId, byte[] descriptor,
|
||||||
long timestamp, BdfList body)
|
long timestamp, BdfList body)
|
||||||
throws FormatException {
|
throws FormatException {
|
||||||
|
|
||||||
|
if (getType(body) != POST)
|
||||||
|
throw new IllegalArgumentException("Needs to wrap a POST");
|
||||||
|
|
||||||
// Serialise the message
|
// Serialise the message
|
||||||
String content = body.getString(1);
|
String content = body.getString(1);
|
||||||
byte[] signature = body.getRaw(2);
|
byte[] signature = body.getRaw(2);
|
||||||
@@ -91,9 +95,12 @@ class BlogPostFactoryImpl implements BlogPostFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message createWrappedPost(GroupId groupId, BdfList body)
|
public Message rewrapWrappedPost(GroupId groupId, BdfList body)
|
||||||
throws FormatException {
|
throws FormatException {
|
||||||
|
|
||||||
|
if (getType(body) != WRAPPED_POST)
|
||||||
|
throw new IllegalArgumentException("Needs to wrap a WRAPPED_POST");
|
||||||
|
|
||||||
// Serialise the message
|
// Serialise the message
|
||||||
byte[] descriptor = body.getRaw(1);
|
byte[] descriptor = body.getRaw(1);
|
||||||
long timestamp = body.getLong(2);
|
long timestamp = body.getLong(2);
|
||||||
@@ -107,38 +114,48 @@ class BlogPostFactoryImpl implements BlogPostFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message createWrappedComment(GroupId groupId, byte[] descriptor,
|
public Message wrapComment(GroupId groupId, byte[] descriptor,
|
||||||
long timestamp, BdfList body, MessageId currentId)
|
long timestamp, BdfList body, MessageId parentId)
|
||||||
throws FormatException {
|
throws FormatException {
|
||||||
|
|
||||||
|
if (getType(body) != COMMENT)
|
||||||
|
throw new IllegalArgumentException("Needs to wrap a COMMENT");
|
||||||
|
|
||||||
// Serialise the message
|
// Serialise the message
|
||||||
String comment = body.getOptionalString(1);
|
String comment = body.getOptionalString(1);
|
||||||
byte[] originalId = body.getRaw(2);
|
byte[] pOriginalId = body.getRaw(2);
|
||||||
byte[] oldId = body.getRaw(3);
|
byte[] oldParentId = body.getRaw(3);
|
||||||
byte[] signature = body.getRaw(4);
|
byte[] signature = body.getRaw(4);
|
||||||
BdfList message =
|
BdfList message =
|
||||||
BdfList.of(WRAPPED_COMMENT.getInt(), descriptor, timestamp,
|
BdfList.of(WRAPPED_COMMENT.getInt(), descriptor, timestamp,
|
||||||
comment, originalId, oldId, signature, currentId);
|
comment, pOriginalId, oldParentId, signature, parentId);
|
||||||
return clientHelper
|
return clientHelper
|
||||||
.createMessage(groupId, clock.currentTimeMillis(), message);
|
.createMessage(groupId, clock.currentTimeMillis(), message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Message createWrappedComment(GroupId groupId, BdfList body,
|
public Message rewrapWrappedComment(GroupId groupId, BdfList body,
|
||||||
MessageId currentId) throws FormatException {
|
MessageId parentId) throws FormatException {
|
||||||
|
|
||||||
|
if (getType(body) != WRAPPED_COMMENT)
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Needs to wrap a WRAPPED_COMMENT");
|
||||||
|
|
||||||
// Serialise the message
|
// Serialise the message
|
||||||
byte[] descriptor = body.getRaw(1);
|
byte[] descriptor = body.getRaw(1);
|
||||||
long timestamp = body.getLong(2);
|
long timestamp = body.getLong(2);
|
||||||
String comment = body.getOptionalString(3);
|
String comment = body.getOptionalString(3);
|
||||||
byte[] originalId = body.getRaw(4);
|
byte[] pOriginalId = body.getRaw(4);
|
||||||
byte[] oldId = body.getRaw(5);
|
byte[] oldParentId = body.getRaw(5);
|
||||||
byte[] signature = body.getRaw(6);
|
byte[] signature = body.getRaw(6);
|
||||||
BdfList message =
|
BdfList message =
|
||||||
BdfList.of(WRAPPED_COMMENT.getInt(), descriptor, timestamp,
|
BdfList.of(WRAPPED_COMMENT.getInt(), descriptor, timestamp,
|
||||||
comment, originalId, oldId, signature, currentId);
|
comment, pOriginalId, oldParentId, signature, parentId);
|
||||||
return clientHelper
|
return clientHelper
|
||||||
.createMessage(groupId, clock.currentTimeMillis(), message);
|
.createMessage(groupId, clock.currentTimeMillis(), message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private MessageType getType(BdfList body) throws FormatException {
|
||||||
|
return MessageType.valueOf(body.getLong(0).intValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ import static org.briarproject.api.blogs.BlogConstants.KEY_AUTHOR_ID;
|
|||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_AUTHOR_NAME;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_AUTHOR_NAME;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_COMMENT;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_COMMENT;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_ORIGINAL_PARENT_MSG_ID;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_ORIGINAL_PARENT_MSG_ID;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_WRAPPED_MSG_ID;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_PARENT_MSG_ID;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_ORIGINAL_MSG_ID;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_ORIGINAL_MSG_ID;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_PUBLIC_KEY;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_PUBLIC_KEY;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_READ;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_READ;
|
||||||
@@ -129,9 +129,9 @@ class BlogPostValidator extends BdfMessageValidator {
|
|||||||
|
|
||||||
// parent_original_id
|
// parent_original_id
|
||||||
// The ID of a post or comment in this group or another group
|
// The ID of a post or comment in this group or another group
|
||||||
byte[] originalIdBytes = body.getRaw(1);
|
byte[] pOriginalIdBytes = body.getRaw(1);
|
||||||
checkLength(originalIdBytes, MessageId.LENGTH);
|
checkLength(pOriginalIdBytes, MessageId.LENGTH);
|
||||||
MessageId originalId = new MessageId(originalIdBytes);
|
MessageId pOriginalId = new MessageId(pOriginalIdBytes);
|
||||||
|
|
||||||
// parent_id
|
// parent_id
|
||||||
// The ID of a post, comment, wrapped post or wrapped comment in this
|
// The ID of a post, comment, wrapped post or wrapped comment in this
|
||||||
@@ -145,7 +145,7 @@ class BlogPostValidator extends BdfMessageValidator {
|
|||||||
byte[] sig = body.getRaw(3);
|
byte[] sig = body.getRaw(3);
|
||||||
checkLength(sig, 0, MAX_SIGNATURE_LENGTH);
|
checkLength(sig, 0, MAX_SIGNATURE_LENGTH);
|
||||||
BdfList signed =
|
BdfList signed =
|
||||||
BdfList.of(g.getId(), m.getTimestamp(), comment, originalId,
|
BdfList.of(g.getId(), m.getTimestamp(), comment, pOriginalId,
|
||||||
currentId);
|
currentId);
|
||||||
Blog b = blogFactory.parseBlog(g, ""); // description doesn't matter
|
Blog b = blogFactory.parseBlog(g, ""); // description doesn't matter
|
||||||
Author a = b.getAuthor();
|
Author a = b.getAuthor();
|
||||||
@@ -154,8 +154,9 @@ class BlogPostValidator extends BdfMessageValidator {
|
|||||||
// Return the metadata and dependencies
|
// Return the metadata and dependencies
|
||||||
BdfDictionary meta = new BdfDictionary();
|
BdfDictionary meta = new BdfDictionary();
|
||||||
if (comment != null) meta.put(KEY_COMMENT, comment);
|
if (comment != null) meta.put(KEY_COMMENT, comment);
|
||||||
meta.put(KEY_ORIGINAL_MSG_ID, originalId);
|
meta.put(KEY_ORIGINAL_MSG_ID, m.getId());
|
||||||
meta.put(KEY_WRAPPED_MSG_ID, currentId);
|
meta.put(KEY_ORIGINAL_PARENT_MSG_ID, pOriginalId);
|
||||||
|
meta.put(KEY_PARENT_MSG_ID, currentId);
|
||||||
meta.put(KEY_AUTHOR, authorToBdfDictionary(a));
|
meta.put(KEY_AUTHOR, authorToBdfDictionary(a));
|
||||||
Collection<MessageId> dependencies = Collections.singleton(currentId);
|
Collection<MessageId> dependencies = Collections.singleton(currentId);
|
||||||
return new BdfMessageContext(meta, dependencies);
|
return new BdfMessageContext(meta, dependencies);
|
||||||
@@ -219,9 +220,9 @@ class BlogPostValidator extends BdfMessageValidator {
|
|||||||
|
|
||||||
// c_parent_original_id
|
// c_parent_original_id
|
||||||
// Taken from the original comment
|
// Taken from the original comment
|
||||||
byte[] originalIdBytes = body.getRaw(3);
|
byte[] pOriginalIdBytes = body.getRaw(3);
|
||||||
checkLength(originalIdBytes, MessageId.LENGTH);
|
checkLength(pOriginalIdBytes, MessageId.LENGTH);
|
||||||
MessageId originalId = new MessageId(originalIdBytes);
|
MessageId pOriginalId = new MessageId(pOriginalIdBytes);
|
||||||
|
|
||||||
// c_parent_id
|
// c_parent_id
|
||||||
// Taken from the original comment
|
// Taken from the original comment
|
||||||
@@ -238,14 +239,14 @@ class BlogPostValidator extends BdfMessageValidator {
|
|||||||
// The ID of a post, comment, wrapped post or wrapped comment in this
|
// The ID of a post, comment, wrapped post or wrapped comment in this
|
||||||
// group, which had the ID c_parent_original_id in the group
|
// group, which had the ID c_parent_original_id in the group
|
||||||
// where it was originally posted
|
// where it was originally posted
|
||||||
byte[] currentIdBytes = body.getRaw(6);
|
byte[] parentIdBytes = body.getRaw(6);
|
||||||
checkLength(currentIdBytes, MessageId.LENGTH);
|
checkLength(parentIdBytes, MessageId.LENGTH);
|
||||||
MessageId currentId = new MessageId(currentIdBytes);
|
MessageId parentId = new MessageId(parentIdBytes);
|
||||||
|
|
||||||
// Get and Validate the Wrapped Comment
|
// Get and Validate the Wrapped Comment
|
||||||
Group wGroup = groupFactory
|
Group wGroup = groupFactory
|
||||||
.createGroup(BlogManagerImpl.CLIENT_ID, descriptor);
|
.createGroup(BlogManagerImpl.CLIENT_ID, descriptor);
|
||||||
BdfList wBodyList = BdfList.of(COMMENT.getInt(), comment, originalId,
|
BdfList wBodyList = BdfList.of(COMMENT.getInt(), comment, pOriginalId,
|
||||||
oldId, signature);
|
oldId, signature);
|
||||||
byte[] wBody = clientHelper.toByteArray(wBodyList);
|
byte[] wBody = clientHelper.toByteArray(wBodyList);
|
||||||
Message wMessage =
|
Message wMessage =
|
||||||
@@ -254,11 +255,11 @@ class BlogPostValidator extends BdfMessageValidator {
|
|||||||
BdfMessageContext c = validateComment(wMessage, wGroup, wBodyList);
|
BdfMessageContext c = validateComment(wMessage, wGroup, wBodyList);
|
||||||
|
|
||||||
// Return the metadata and dependencies
|
// Return the metadata and dependencies
|
||||||
Collection<MessageId> dependencies = Collections.singleton(currentId);
|
Collection<MessageId> dependencies = Collections.singleton(parentId);
|
||||||
BdfDictionary meta = new BdfDictionary();
|
BdfDictionary meta = new BdfDictionary();
|
||||||
meta.put(KEY_ORIGINAL_MSG_ID, wMessage.getId());
|
meta.put(KEY_ORIGINAL_MSG_ID, wMessage.getId());
|
||||||
meta.put(KEY_ORIGINAL_PARENT_MSG_ID, originalId);
|
meta.put(KEY_ORIGINAL_PARENT_MSG_ID, pOriginalId);
|
||||||
meta.put(KEY_WRAPPED_MSG_ID, currentId);
|
meta.put(KEY_PARENT_MSG_ID, parentId);
|
||||||
meta.put(KEY_TIMESTAMP, wTimestamp);
|
meta.put(KEY_TIMESTAMP, wTimestamp);
|
||||||
if (comment != null) meta.put(KEY_COMMENT, comment);
|
if (comment != null) meta.put(KEY_COMMENT, comment);
|
||||||
meta.put(KEY_AUTHOR, c.getDictionary().getDictionary(KEY_AUTHOR));
|
meta.put(KEY_AUTHOR, c.getDictionary().getDictionary(KEY_AUTHOR));
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ import static org.briarproject.api.blogs.BlogConstants.KEY_AUTHOR;
|
|||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_AUTHOR_ID;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_AUTHOR_ID;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_AUTHOR_NAME;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_AUTHOR_NAME;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_COMMENT;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_COMMENT;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_WRAPPED_MSG_ID;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_PARENT_MSG_ID;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_ORIGINAL_MSG_ID;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_ORIGINAL_MSG_ID;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_PUBLIC_KEY;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_PUBLIC_KEY;
|
||||||
import static org.briarproject.api.blogs.BlogConstants.KEY_READ;
|
import static org.briarproject.api.blogs.BlogConstants.KEY_READ;
|
||||||
@@ -168,7 +168,7 @@ public class BlogPostValidatorTest extends BriarTestCase {
|
|||||||
assertEquals(comment, result.getString(KEY_COMMENT));
|
assertEquals(comment, result.getString(KEY_COMMENT));
|
||||||
assertEquals(authorDict, result.getDictionary(KEY_AUTHOR));
|
assertEquals(authorDict, result.getDictionary(KEY_AUTHOR));
|
||||||
assertEquals(originalId.getBytes(), result.getRaw(KEY_ORIGINAL_MSG_ID));
|
assertEquals(originalId.getBytes(), result.getRaw(KEY_ORIGINAL_MSG_ID));
|
||||||
assertEquals(currentId.getBytes(), result.getRaw(KEY_WRAPPED_MSG_ID));
|
assertEquals(currentId.getBytes(), result.getRaw(KEY_PARENT_MSG_ID));
|
||||||
assertFalse(result.getBoolean(KEY_READ));
|
assertFalse(result.getBoolean(KEY_READ));
|
||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
}
|
}
|
||||||
@@ -269,7 +269,7 @@ public class BlogPostValidatorTest extends BriarTestCase {
|
|||||||
assertEquals(authorDict, result.getDictionary(KEY_AUTHOR));
|
assertEquals(authorDict, result.getDictionary(KEY_AUTHOR));
|
||||||
assertEquals(
|
assertEquals(
|
||||||
message.getId().getBytes(), result.getRaw(KEY_ORIGINAL_MSG_ID));
|
message.getId().getBytes(), result.getRaw(KEY_ORIGINAL_MSG_ID));
|
||||||
assertEquals(currentId.getBytes(), result.getRaw(KEY_WRAPPED_MSG_ID));
|
assertEquals(currentId.getBytes(), result.getRaw(KEY_PARENT_MSG_ID));
|
||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user