mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 05:09:53 +01:00
Use ClientHelper in ForumManagerImpl.
This commit is contained in:
@@ -3,15 +3,12 @@ package org.briarproject.forum;
|
|||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
|
||||||
import org.briarproject.api.FormatException;
|
import org.briarproject.api.FormatException;
|
||||||
|
import org.briarproject.api.clients.ClientHelper;
|
||||||
import org.briarproject.api.contact.Contact;
|
import org.briarproject.api.contact.Contact;
|
||||||
import org.briarproject.api.data.BdfDictionary;
|
import org.briarproject.api.data.BdfDictionary;
|
||||||
import org.briarproject.api.data.BdfReader;
|
import org.briarproject.api.data.BdfList;
|
||||||
import org.briarproject.api.data.BdfReaderFactory;
|
|
||||||
import org.briarproject.api.data.MetadataEncoder;
|
|
||||||
import org.briarproject.api.data.MetadataParser;
|
|
||||||
import org.briarproject.api.db.DatabaseComponent;
|
import org.briarproject.api.db.DatabaseComponent;
|
||||||
import org.briarproject.api.db.DbException;
|
import org.briarproject.api.db.DbException;
|
||||||
import org.briarproject.api.db.Metadata;
|
|
||||||
import org.briarproject.api.db.Transaction;
|
import org.briarproject.api.db.Transaction;
|
||||||
import org.briarproject.api.forum.Forum;
|
import org.briarproject.api.forum.Forum;
|
||||||
import org.briarproject.api.forum.ForumManager;
|
import org.briarproject.api.forum.ForumManager;
|
||||||
@@ -26,8 +23,6 @@ import org.briarproject.api.sync.GroupId;
|
|||||||
import org.briarproject.api.sync.MessageId;
|
import org.briarproject.api.sync.MessageId;
|
||||||
import org.briarproject.util.StringUtils;
|
import org.briarproject.util.StringUtils;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -36,16 +31,10 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import static java.util.logging.Level.WARNING;
|
|
||||||
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.forum.ForumConstants.MAX_FORUM_POST_BODY_LENGTH;
|
|
||||||
import static org.briarproject.api.identity.Author.Status.ANONYMOUS;
|
import static org.briarproject.api.identity.Author.Status.ANONYMOUS;
|
||||||
import static org.briarproject.api.identity.Author.Status.UNKNOWN;
|
import static org.briarproject.api.identity.Author.Status.UNKNOWN;
|
||||||
import static org.briarproject.api.identity.Author.Status.VERIFIED;
|
import static org.briarproject.api.identity.Author.Status.VERIFIED;
|
||||||
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
|
|
||||||
|
|
||||||
class ForumManagerImpl implements ForumManager {
|
class ForumManagerImpl implements ForumManager {
|
||||||
|
|
||||||
@@ -53,21 +42,13 @@ class ForumManagerImpl implements ForumManager {
|
|||||||
"859a7be50dca035b64bd6902fb797097"
|
"859a7be50dca035b64bd6902fb797097"
|
||||||
+ "795af837abbf8c16d750b3c2ccc186ea"));
|
+ "795af837abbf8c16d750b3c2ccc186ea"));
|
||||||
|
|
||||||
private static final Logger LOG =
|
|
||||||
Logger.getLogger(ForumManagerImpl.class.getName());
|
|
||||||
|
|
||||||
private final DatabaseComponent db;
|
private final DatabaseComponent db;
|
||||||
private final BdfReaderFactory bdfReaderFactory;
|
private final ClientHelper clientHelper;
|
||||||
private final MetadataEncoder metadataEncoder;
|
|
||||||
private final MetadataParser metadataParser;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ForumManagerImpl(DatabaseComponent db, BdfReaderFactory bdfReaderFactory,
|
ForumManagerImpl(DatabaseComponent db, ClientHelper clientHelper) {
|
||||||
MetadataEncoder metadataEncoder, MetadataParser metadataParser) {
|
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.bdfReaderFactory = bdfReaderFactory;
|
this.clientHelper = clientHelper;
|
||||||
this.metadataEncoder = metadataEncoder;
|
|
||||||
this.metadataParser = metadataParser;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -78,29 +59,22 @@ class ForumManagerImpl implements ForumManager {
|
|||||||
@Override
|
@Override
|
||||||
public void addLocalPost(ForumPost p) throws DbException {
|
public void addLocalPost(ForumPost p) throws DbException {
|
||||||
try {
|
try {
|
||||||
BdfDictionary d = new BdfDictionary();
|
BdfDictionary meta = new BdfDictionary();
|
||||||
d.put("timestamp", p.getMessage().getTimestamp());
|
meta.put("timestamp", p.getMessage().getTimestamp());
|
||||||
if (p.getParent() != null)
|
if (p.getParent() != null)
|
||||||
d.put("parent", p.getParent().getBytes());
|
meta.put("parent", p.getParent().getBytes());
|
||||||
if (p.getAuthor() != null) {
|
if (p.getAuthor() != null) {
|
||||||
Author a = p.getAuthor();
|
Author a = p.getAuthor();
|
||||||
BdfDictionary d1 = new BdfDictionary();
|
BdfDictionary author = new BdfDictionary();
|
||||||
d1.put("id", a.getId().getBytes());
|
author.put("id", a.getId().getBytes());
|
||||||
d1.put("name", a.getName());
|
author.put("name", a.getName());
|
||||||
d1.put("publicKey", a.getPublicKey());
|
author.put("publicKey", a.getPublicKey());
|
||||||
d.put("author", d1);
|
meta.put("author", author);
|
||||||
}
|
|
||||||
d.put("contentType", p.getContentType());
|
|
||||||
d.put("local", true);
|
|
||||||
d.put("read", true);
|
|
||||||
Metadata meta = metadataEncoder.encode(d);
|
|
||||||
Transaction txn = db.startTransaction();
|
|
||||||
try {
|
|
||||||
db.addLocalMessage(txn, p.getMessage(), CLIENT_ID, meta, true);
|
|
||||||
txn.setComplete();
|
|
||||||
} finally {
|
|
||||||
db.endTransaction(txn);
|
|
||||||
}
|
}
|
||||||
|
meta.put("contentType", p.getContentType());
|
||||||
|
meta.put("local", true);
|
||||||
|
meta.put("read", true);
|
||||||
|
clientHelper.addLocalMessage(p.getMessage(), CLIENT_ID, meta, true);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
@@ -145,34 +119,11 @@ class ForumManagerImpl implements ForumManager {
|
|||||||
@Override
|
@Override
|
||||||
public byte[] getPostBody(MessageId m) throws DbException {
|
public byte[] getPostBody(MessageId m) throws DbException {
|
||||||
try {
|
try {
|
||||||
byte[] raw;
|
// Parent ID, author, content type, forum post body, signature
|
||||||
Transaction txn = db.startTransaction();
|
BdfList message = clientHelper.getMessageAsList(m);
|
||||||
try {
|
return message.getRaw(3);
|
||||||
raw = db.getRawMessage(txn, m);
|
|
||||||
txn.setComplete();
|
|
||||||
} finally {
|
|
||||||
db.endTransaction(txn);
|
|
||||||
}
|
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(raw,
|
|
||||||
MESSAGE_HEADER_LENGTH, raw.length - MESSAGE_HEADER_LENGTH);
|
|
||||||
BdfReader r = bdfReaderFactory.createReader(in);
|
|
||||||
r.readListStart();
|
|
||||||
if (r.hasRaw()) r.skipRaw(); // Parent ID
|
|
||||||
else r.skipNull(); // No parent
|
|
||||||
if (r.hasList()) r.skipList(); // Author
|
|
||||||
else r.skipNull(); // No author
|
|
||||||
r.skipString(); // Content type
|
|
||||||
byte[] postBody = r.readRaw(MAX_FORUM_POST_BODY_LENGTH);
|
|
||||||
if (r.hasRaw()) r.skipRaw(); // Signature
|
|
||||||
else r.skipNull();
|
|
||||||
r.readListEnd();
|
|
||||||
if (!r.eof()) throw new FormatException();
|
|
||||||
return postBody;
|
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
} catch (IOException e) {
|
|
||||||
// Shouldn't happen with ByteArrayInputStream
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +132,7 @@ class ForumManagerImpl implements ForumManager {
|
|||||||
throws DbException {
|
throws DbException {
|
||||||
Set<AuthorId> localAuthorIds = new HashSet<AuthorId>();
|
Set<AuthorId> localAuthorIds = new HashSet<AuthorId>();
|
||||||
Set<AuthorId> contactAuthorIds = new HashSet<AuthorId>();
|
Set<AuthorId> contactAuthorIds = new HashSet<AuthorId>();
|
||||||
Map<MessageId, Metadata> metadata;
|
Map<MessageId, BdfDictionary> metadata;
|
||||||
Transaction txn = db.startTransaction();
|
Transaction txn = db.startTransaction();
|
||||||
try {
|
try {
|
||||||
// Load the IDs of the user's identities
|
// Load the IDs of the user's identities
|
||||||
@@ -191,20 +142,22 @@ class ForumManagerImpl implements ForumManager {
|
|||||||
for (Contact c : db.getContacts(txn))
|
for (Contact c : db.getContacts(txn))
|
||||||
contactAuthorIds.add(c.getAuthor().getId());
|
contactAuthorIds.add(c.getAuthor().getId());
|
||||||
// Load the metadata
|
// Load the metadata
|
||||||
metadata = db.getMessageMetadata(txn, g);
|
metadata = clientHelper.getMessageMetadataAsDictionary(txn, g);
|
||||||
txn.setComplete();
|
txn.setComplete();
|
||||||
|
} catch (FormatException e) {
|
||||||
|
throw new DbException(e);
|
||||||
} finally {
|
} finally {
|
||||||
db.endTransaction(txn);
|
db.endTransaction(txn);
|
||||||
}
|
}
|
||||||
// Parse the metadata
|
// Parse the metadata
|
||||||
Collection<ForumPostHeader> headers = new ArrayList<ForumPostHeader>();
|
Collection<ForumPostHeader> headers = new ArrayList<ForumPostHeader>();
|
||||||
for (Entry<MessageId, Metadata> e : metadata.entrySet()) {
|
for (Entry<MessageId, BdfDictionary> entry : metadata.entrySet()) {
|
||||||
try {
|
try {
|
||||||
BdfDictionary d = metadataParser.parse(e.getValue());
|
BdfDictionary meta = entry.getValue();
|
||||||
long timestamp = d.getLong("timestamp");
|
long timestamp = meta.getLong("timestamp");
|
||||||
Author author = null;
|
Author author = null;
|
||||||
Author.Status authorStatus = ANONYMOUS;
|
Author.Status authorStatus = ANONYMOUS;
|
||||||
BdfDictionary d1 = d.getDictionary("author", null);
|
BdfDictionary d1 = meta.getDictionary("author", null);
|
||||||
if (d1 != null) {
|
if (d1 != null) {
|
||||||
AuthorId authorId = new AuthorId(d1.getRaw("id"));
|
AuthorId authorId = new AuthorId(d1.getRaw("id"));
|
||||||
String name = d1.getString("name");
|
String name = d1.getString("name");
|
||||||
@@ -216,13 +169,12 @@ class ForumManagerImpl implements ForumManager {
|
|||||||
authorStatus = VERIFIED;
|
authorStatus = VERIFIED;
|
||||||
else authorStatus = UNKNOWN;
|
else authorStatus = UNKNOWN;
|
||||||
}
|
}
|
||||||
String contentType = d.getString("contentType");
|
String contentType = meta.getString("contentType");
|
||||||
boolean read = d.getBoolean("read");
|
boolean read = meta.getBoolean("read");
|
||||||
headers.add(new ForumPostHeader(e.getKey(), timestamp, author,
|
headers.add(new ForumPostHeader(entry.getKey(), timestamp,
|
||||||
authorStatus, contentType, read));
|
author, authorStatus, contentType, read));
|
||||||
} catch (FormatException ex) {
|
} catch (FormatException e) {
|
||||||
if (LOG.isLoggable(WARNING))
|
throw new DbException(e);
|
||||||
LOG.log(WARNING, ex.toString(), ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return headers;
|
return headers;
|
||||||
@@ -231,36 +183,18 @@ class ForumManagerImpl implements ForumManager {
|
|||||||
@Override
|
@Override
|
||||||
public void setReadFlag(MessageId m, boolean read) throws DbException {
|
public void setReadFlag(MessageId m, boolean read) throws DbException {
|
||||||
try {
|
try {
|
||||||
BdfDictionary d = new BdfDictionary();
|
BdfDictionary meta = new BdfDictionary();
|
||||||
d.put("read", read);
|
meta.put("read", read);
|
||||||
Metadata meta = metadataEncoder.encode(d);
|
clientHelper.mergeMessageMetadata(m, meta);
|
||||||
Transaction txn = db.startTransaction();
|
|
||||||
try {
|
|
||||||
db.mergeMessageMetadata(txn, m, meta);
|
|
||||||
txn.setComplete();
|
|
||||||
} finally {
|
|
||||||
db.endTransaction(txn);
|
|
||||||
}
|
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Forum parseForum(Group g) throws FormatException {
|
private Forum parseForum(Group g) throws FormatException {
|
||||||
ByteArrayInputStream in = new ByteArrayInputStream(g.getDescriptor());
|
byte[] descriptor = g.getDescriptor();
|
||||||
BdfReader r = bdfReaderFactory.createReader(in);
|
// Name, salt
|
||||||
try {
|
BdfList forum = clientHelper.toList(descriptor, 0, descriptor.length);
|
||||||
r.readListStart();
|
return new Forum(g, forum.getString(0), forum.getRaw(1));
|
||||||
String name = r.readString(MAX_FORUM_NAME_LENGTH);
|
|
||||||
byte[] salt = r.readRaw(FORUM_SALT_LENGTH);
|
|
||||||
r.readListEnd();
|
|
||||||
if (!r.eof()) throw new FormatException();
|
|
||||||
return new Forum(g, name, salt);
|
|
||||||
} catch (FormatException e) {
|
|
||||||
throw e;
|
|
||||||
} catch (IOException e) {
|
|
||||||
// Shouldn't happen with ByteArrayInputStream
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user