mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-15 12:19:54 +01:00
Move post/message creation into clients
This way the forum and private group client do not need to keep track of message timestamps themselves and do not need to interact with post/message factories.
This commit is contained in:
@@ -13,19 +13,24 @@ import org.briarproject.api.forum.Forum;
|
||||
import org.briarproject.api.forum.ForumFactory;
|
||||
import org.briarproject.api.forum.ForumManager;
|
||||
import org.briarproject.api.forum.ForumPost;
|
||||
import org.briarproject.api.forum.ForumPostFactory;
|
||||
import org.briarproject.api.forum.ForumPostHeader;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.Author.Status;
|
||||
import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.sync.ClientId;
|
||||
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 org.briarproject.api.system.Clock;
|
||||
import org.briarproject.clients.BdfIncomingMessageHook;
|
||||
import org.briarproject.util.StringUtils;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -47,6 +52,7 @@ import static org.briarproject.api.forum.ForumConstants.KEY_PARENT;
|
||||
import static org.briarproject.api.forum.ForumConstants.KEY_PUBLIC_NAME;
|
||||
import static org.briarproject.api.forum.ForumConstants.KEY_TIMESTAMP;
|
||||
import static org.briarproject.api.identity.Author.Status.ANONYMOUS;
|
||||
import static org.briarproject.api.identity.Author.Status.OURSELVES;
|
||||
import static org.briarproject.clients.BdfConstants.MSG_KEY_READ;
|
||||
|
||||
class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
|
||||
@@ -57,16 +63,21 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
|
||||
|
||||
private final IdentityManager identityManager;
|
||||
private final ForumFactory forumFactory;
|
||||
private final ForumPostFactory forumPostFactory;
|
||||
private final Clock clock;
|
||||
private final List<RemoveForumHook> removeHooks;
|
||||
|
||||
@Inject
|
||||
ForumManagerImpl(DatabaseComponent db, IdentityManager identityManager,
|
||||
ClientHelper clientHelper, MetadataParser metadataParser,
|
||||
ForumFactory forumFactory) {
|
||||
ForumFactory forumFactory, ForumPostFactory forumPostFactory,
|
||||
Clock clock) {
|
||||
super(db, clientHelper, metadataParser);
|
||||
|
||||
this.identityManager = identityManager;
|
||||
this.forumFactory = forumFactory;
|
||||
this.forumPostFactory = forumPostFactory;
|
||||
this.clock = clock;
|
||||
removeHooks = new CopyOnWriteArrayList<RemoveForumHook>();
|
||||
}
|
||||
|
||||
@@ -118,7 +129,38 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLocalPost(ForumPost p) throws DbException {
|
||||
public ForumPost createLocalPost(final GroupId groupId,
|
||||
final String body, final @Nullable MessageId parentId)
|
||||
throws DbException {
|
||||
|
||||
LocalAuthor author;
|
||||
GroupCount count;
|
||||
Transaction txn = db.startTransaction(true);
|
||||
try {
|
||||
author = identityManager.getLocalAuthor(txn);
|
||||
count = getGroupCount(txn, groupId);
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
long timestamp = clock.currentTimeMillis();
|
||||
timestamp = Math.max(timestamp, count.getLatestMsgTime());
|
||||
|
||||
ForumPost p;
|
||||
try {
|
||||
p = forumPostFactory
|
||||
.createPseudonymousPost(groupId, timestamp, parentId,
|
||||
author, body);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ForumPostHeader addLocalPost(ForumPost p) throws DbException {
|
||||
Transaction txn = db.startTransaction(false);
|
||||
try {
|
||||
BdfDictionary meta = new BdfDictionary();
|
||||
@@ -142,6 +184,8 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
return new ForumPostHeader(p.getMessage().getId(), p.getParent(),
|
||||
p.getMessage().getTimestamp(), p.getAuthor(), OURSELVES, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,12 +3,13 @@ package org.briarproject.forum;
|
||||
import org.briarproject.api.FormatException;
|
||||
import org.briarproject.api.clients.ClientHelper;
|
||||
import org.briarproject.api.crypto.CryptoComponent;
|
||||
import org.briarproject.api.crypto.KeyParser;
|
||||
import org.briarproject.api.crypto.PrivateKey;
|
||||
import org.briarproject.api.crypto.Signature;
|
||||
import org.briarproject.api.data.BdfList;
|
||||
import org.briarproject.api.forum.ForumPost;
|
||||
import org.briarproject.api.forum.ForumPostFactory;
|
||||
import org.briarproject.api.identity.Author;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.sync.GroupId;
|
||||
import org.briarproject.api.sync.Message;
|
||||
import org.briarproject.api.sync.MessageId;
|
||||
@@ -49,9 +50,10 @@ class ForumPostFactoryImpl implements ForumPostFactory {
|
||||
|
||||
@Override
|
||||
public ForumPost createPseudonymousPost(GroupId groupId, long timestamp,
|
||||
MessageId parent, Author author, String contentType, byte[] body,
|
||||
PrivateKey privateKey) throws FormatException,
|
||||
GeneralSecurityException {
|
||||
MessageId parent, LocalAuthor author, String bodyStr)
|
||||
throws FormatException, GeneralSecurityException {
|
||||
String contentType = "text/plain";
|
||||
byte[] body = StringUtils.toUtf8(bodyStr);
|
||||
// Validate the arguments
|
||||
if (StringUtils.toUtf8(contentType).length > MAX_CONTENT_TYPE_LENGTH)
|
||||
throw new IllegalArgumentException();
|
||||
@@ -62,6 +64,10 @@ class ForumPostFactoryImpl implements ForumPostFactory {
|
||||
author.getPublicKey());
|
||||
BdfList signed = BdfList.of(groupId, timestamp, parent, authorList,
|
||||
contentType, body);
|
||||
// Get private key
|
||||
KeyParser keyParser = crypto.getSignatureKeyParser();
|
||||
byte[] k = author.getPrivateKey();
|
||||
PrivateKey privateKey = keyParser.parsePrivateKey(k);
|
||||
// Generate the signature
|
||||
Signature signature = crypto.getSignature();
|
||||
signature.initSign(privateKey);
|
||||
|
||||
@@ -9,7 +9,9 @@ import org.briarproject.api.db.DatabaseComponent;
|
||||
import org.briarproject.api.db.DbException;
|
||||
import org.briarproject.api.db.Transaction;
|
||||
import org.briarproject.api.identity.IdentityManager;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
import org.briarproject.api.privategroup.GroupMessage;
|
||||
import org.briarproject.api.privategroup.GroupMessageFactory;
|
||||
import org.briarproject.api.privategroup.GroupMessageHeader;
|
||||
import org.briarproject.api.privategroup.PrivateGroup;
|
||||
import org.briarproject.api.privategroup.PrivateGroupFactory;
|
||||
@@ -19,16 +21,21 @@ 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 org.briarproject.api.system.Clock;
|
||||
import org.briarproject.clients.BdfIncomingMessageHook;
|
||||
import org.briarproject.util.StringUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.briarproject.api.identity.Author.Status.OURSELVES;
|
||||
|
||||
public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
|
||||
PrivateGroupManager {
|
||||
|
||||
@@ -40,16 +47,21 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
|
||||
|
||||
private final IdentityManager identityManager;
|
||||
private final PrivateGroupFactory privateGroupFactory;
|
||||
private final GroupMessageFactory groupMessageFactory;
|
||||
private final Clock clock;
|
||||
|
||||
@Inject
|
||||
PrivateGroupManagerImpl(ClientHelper clientHelper,
|
||||
MetadataParser metadataParser, DatabaseComponent db,
|
||||
IdentityManager identityManager,
|
||||
PrivateGroupFactory privateGroupFactory) {
|
||||
PrivateGroupFactory privateGroupFactory,
|
||||
GroupMessageFactory groupMessageFactory, Clock clock) {
|
||||
super(db, clientHelper, metadataParser);
|
||||
|
||||
this.identityManager = identityManager;
|
||||
this.privateGroupFactory = privateGroupFactory;
|
||||
this.groupMessageFactory = groupMessageFactory;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -64,7 +76,25 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLocalMessage(GroupMessage m) throws DbException {
|
||||
public GroupMessage createLocalMessage(GroupId groupId, String body,
|
||||
@Nullable MessageId parentId) throws DbException {
|
||||
|
||||
long timestamp = clock.currentTimeMillis();
|
||||
LocalAuthor author = identityManager.getLocalAuthor();
|
||||
try {
|
||||
return groupMessageFactory
|
||||
.createGroupMessage(groupId, timestamp, parentId, author,
|
||||
body);
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e);
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public GroupMessageHeader addLocalMessage(GroupMessage m)
|
||||
throws DbException {
|
||||
Transaction txn = db.startTransaction(false);
|
||||
try {
|
||||
BdfDictionary meta = new BdfDictionary();
|
||||
@@ -76,6 +106,9 @@ public class PrivateGroupManagerImpl extends BdfIncomingMessageHook implements
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
return new GroupMessageHeader(m.getMessage().getGroupId(),
|
||||
m.getMessage().getId(), m.getParent(),
|
||||
m.getMessage().getTimestamp(), m.getAuthor(), OURSELVES, true);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
Reference in New Issue
Block a user