Add transactional versions of BlogManager methods

This commit is contained in:
Torsten Grote
2023-07-13 13:01:30 -03:00
parent c47253fc5f
commit 8f735d176e
2 changed files with 43 additions and 15 deletions

View File

@@ -74,6 +74,13 @@ public interface BlogManager {
@Nullable String comment, BlogPostHeader parentHeader)
throws DbException;
/**
* Adds a comment to an existing blog post or reblogs it.
*/
void addLocalComment(Transaction txn, LocalAuthor author,
GroupId groupId, @Nullable String comment,
BlogPostHeader parentHeader) throws DbException;
/**
* Returns the blog with the given ID.
*/
@@ -99,6 +106,11 @@ public interface BlogManager {
*/
Collection<Blog> getBlogs() throws DbException;
/**
* Returns all blogs to which the user subscribes.
*/
Collection<Blog> getBlogs(Transaction txn) throws DbException;
/**
* Returns the group IDs of all blogs to which the user subscribes.
*/
@@ -136,6 +148,11 @@ public interface BlogManager {
*/
void setReadFlag(MessageId m, boolean read) throws DbException;
/**
* Marks a blog post as read or unread.
*/
void setReadFlag(Transaction txn, MessageId m, boolean read) throws DbException;
/**
* Registers a hook to be called whenever a blog is removed.
*/

View File

@@ -257,12 +257,19 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
public void addLocalComment(LocalAuthor author, GroupId groupId,
@Nullable String comment, BlogPostHeader parentHeader)
throws DbException {
db.transaction(false, txn -> {
addLocalComment(txn, author, groupId, comment, parentHeader);
});
}
@Override
public void addLocalComment(Transaction txn, LocalAuthor author,
GroupId groupId, @Nullable String comment,
BlogPostHeader parentHeader) throws DbException {
MessageType type = parentHeader.getType();
if (type != POST && type != COMMENT)
throw new IllegalArgumentException("Comment on unknown type!");
Transaction txn = db.startTransaction(false);
try {
// Wrap post that we are commenting on
MessageId parentOriginalId =
@@ -291,13 +298,10 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
message.getId(), meta);
BlogPostAddedEvent event = new BlogPostAddedEvent(groupId, h, true);
txn.attach(event);
db.commitTransaction(txn);
} catch (FormatException e) {
throw new DbException(e);
} catch (GeneralSecurityException e) {
throw new IllegalArgumentException("Invalid key of author", e);
} finally {
db.endTransaction(txn);
}
}
@@ -430,18 +434,17 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
@Override
public Collection<Blog> getBlogs() throws DbException {
return db.transactionWithResult(true, this::getBlogs);
}
@Override
public Collection<Blog> getBlogs(Transaction txn) throws DbException {
try {
List<Blog> blogs = new ArrayList<>();
Collection<Group> groups;
Transaction txn = db.startTransaction(true);
try {
groups = db.getGroups(txn, CLIENT_ID, MAJOR_VERSION);
for (Group g : groups) {
blogs.add(blogFactory.parseBlog(g));
}
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
Collection<Group> groups =
db.getGroups(txn, CLIENT_ID, MAJOR_VERSION);
for (Group g : groups) {
blogs.add(blogFactory.parseBlog(g));
}
return blogs;
} catch (FormatException e) {
@@ -556,10 +559,18 @@ class BlogManagerImpl extends BdfIncomingMessageHook implements BlogManager,
@Override
public void setReadFlag(MessageId m, boolean read) throws DbException {
db.transaction(true, txn -> {
setReadFlag(txn, m, read);
});
}
@Override
public void setReadFlag(Transaction txn, MessageId m, boolean read)
throws DbException {
try {
BdfDictionary meta = new BdfDictionary();
meta.put(KEY_READ, read);
clientHelper.mergeMessageMetadata(m, meta);
clientHelper.mergeMessageMetadata(txn, m, meta);
} catch (FormatException e) {
throw new RuntimeException(e);
}