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