diff --git a/briar-api/src/main/java/org/briarproject/briar/api/forum/ForumManager.java b/briar-api/src/main/java/org/briarproject/briar/api/forum/ForumManager.java index 7d6bd42a9..30ee18e35 100644 --- a/briar-api/src/main/java/org/briarproject/briar/api/forum/ForumManager.java +++ b/briar-api/src/main/java/org/briarproject/briar/api/forum/ForumManager.java @@ -11,6 +11,7 @@ import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.briar.api.client.MessageTracker.GroupCount; import java.util.Collection; +import java.util.List; import javax.annotation.Nullable; @@ -84,11 +85,22 @@ public interface ForumManager { */ String getPostText(MessageId m) throws DbException; + /** + * Returns the text of the forum post with the given ID. + */ + String getPostText(Transaction txn, MessageId m) throws DbException; + /** * Returns the headers of all posts in the given forum. */ Collection getPostHeaders(GroupId g) throws DbException; + /** + * Returns the headers of all posts in the given forum. + */ + List getPostHeaders(Transaction txn, GroupId g) + throws DbException; + /** * Registers a hook to be called whenever a forum is removed. */ diff --git a/briar-api/src/main/java/org/briarproject/briar/api/privategroup/PrivateGroupManager.java b/briar-api/src/main/java/org/briarproject/briar/api/privategroup/PrivateGroupManager.java index 478b03975..df10ccdf5 100644 --- a/briar-api/src/main/java/org/briarproject/briar/api/privategroup/PrivateGroupManager.java +++ b/briar-api/src/main/java/org/briarproject/briar/api/privategroup/PrivateGroupManager.java @@ -12,6 +12,7 @@ import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.briar.api.client.MessageTracker.GroupCount; import java.util.Collection; +import java.util.List; @NotNullByDefault public interface PrivateGroupManager { @@ -107,11 +108,22 @@ public interface PrivateGroupManager { */ String getMessageText(MessageId m) throws DbException; + /** + * Returns the text of the private group message with the given ID. + */ + String getMessageText(Transaction txn, MessageId m) throws DbException; + /** * Returns the headers of all messages in the given private group. */ Collection getHeaders(GroupId g) throws DbException; + /** + * Returns the headers of all messages in the given private group. + */ + List getHeaders(Transaction txn, GroupId g) + throws DbException; + /** * Returns all members of the given private group. */ diff --git a/briar-core/src/main/java/org/briarproject/briar/forum/ForumManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/forum/ForumManagerImpl.java index 4e4a63af3..673d1f3a3 100644 --- a/briar-core/src/main/java/org/briarproject/briar/forum/ForumManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/forum/ForumManagerImpl.java @@ -192,6 +192,15 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager { } } + @Override + public String getPostText(Transaction txn, MessageId m) throws DbException { + try { + return getPostText(clientHelper.getMessageAsList(txn, m)); + } catch (FormatException e) { + throw new DbException(e); + } + } + private String getPostText(BdfList body) throws FormatException { // Parent ID, author, text, signature return body.getString(2); @@ -200,33 +209,35 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager { @Override public Collection getPostHeaders(GroupId g) throws DbException { + return db.transactionWithResult(true, txn -> getPostHeaders(txn, g)); + } + + @Override + public List getPostHeaders(Transaction txn, GroupId g) + throws DbException { try { - return db.transactionWithResult(true, txn -> { - Collection headers = new ArrayList<>(); - Map metadata = - clientHelper.getMessageMetadataAsDictionary(txn, g); - // get all authors we need to get the info for - Set authors = new HashSet<>(); - for (Entry entry : - metadata.entrySet()) { - BdfList authorList = entry.getValue().getList(KEY_AUTHOR); - Author a = clientHelper.parseAndValidateAuthor(authorList); - authors.add(a.getId()); - } - // get information for all authors - Map authorInfos = new HashMap<>(); - for (AuthorId id : authors) { - authorInfos.put(id, authorManager.getAuthorInfo(txn, id)); - } - // Parse the metadata - for (Entry entry : - metadata.entrySet()) { - BdfDictionary meta = entry.getValue(); - headers.add(getForumPostHeader(txn, entry.getKey(), meta, - authorInfos)); - } - return headers; - }); + List headers = new ArrayList<>(); + Map metadata = + clientHelper.getMessageMetadataAsDictionary(txn, g); + // get all authors we need to get the info for + Set authors = new HashSet<>(); + for (Entry entry : metadata.entrySet()) { + BdfList authorList = entry.getValue().getList(KEY_AUTHOR); + Author a = clientHelper.parseAndValidateAuthor(authorList); + authors.add(a.getId()); + } + // get information for all authors + Map authorInfos = new HashMap<>(); + for (AuthorId id : authors) { + authorInfos.put(id, authorManager.getAuthorInfo(txn, id)); + } + // Parse the metadata + for (Entry entry : metadata.entrySet()) { + BdfDictionary meta = entry.getValue(); + headers.add(getForumPostHeader(txn, entry.getKey(), meta, + authorInfos)); + } + return headers; } catch (FormatException e) { throw new DbException(e); } diff --git a/briar-core/src/main/java/org/briarproject/briar/privategroup/PrivateGroupManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/privategroup/PrivateGroupManagerImpl.java index 7bf044347..bff8fb14c 100644 --- a/briar-core/src/main/java/org/briarproject/briar/privategroup/PrivateGroupManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/privategroup/PrivateGroupManagerImpl.java @@ -315,6 +315,16 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook } } + @Override + public String getMessageText(Transaction txn, MessageId m) + throws DbException { + try { + return getMessageText(clientHelper.getMessageAsList(txn, m)); + } catch (FormatException e) { + throw new DbException(e); + } + } + private String getMessageText(BdfList body) throws FormatException { // Message type (0), member (1), parent ID (2), previous message ID (3), // text (4), signature (5) @@ -324,8 +334,13 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook @Override public Collection getHeaders(GroupId g) throws DbException { - Collection headers = new ArrayList<>(); - Transaction txn = db.startTransaction(true); + return db.transactionWithResult(true, txn -> getHeaders(txn, g)); + } + + @Override + public List getHeaders(Transaction txn, GroupId g) + throws DbException { + List headers = new ArrayList<>(); try { Map metadata = clientHelper.getMessageMetadataAsDictionary(txn, g); @@ -354,12 +369,9 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook meta, authorInfos)); } } - db.commitTransaction(txn); return headers; } catch (FormatException e) { throw new DbException(e); - } finally { - db.endTransaction(txn); } }