Allow to add forum/group posts in transaction

This commit is contained in:
Torsten Grote
2021-01-27 15:06:55 -03:00
parent 4a0327a62b
commit 998c435b13
4 changed files with 61 additions and 47 deletions

View File

@@ -60,6 +60,12 @@ public interface ForumManager {
*/ */
ForumPostHeader addLocalPost(ForumPost p) throws DbException; ForumPostHeader addLocalPost(ForumPost p) throws DbException;
/**
* Stores a local forum post.
*/
ForumPostHeader addLocalPost(Transaction txn, ForumPost p)
throws DbException;
/** /**
* Returns the forum with the given ID. * Returns the forum with the given ID.
*/ */

View File

@@ -82,6 +82,12 @@ public interface PrivateGroupManager {
*/ */
GroupMessageHeader addLocalMessage(GroupMessage p) throws DbException; GroupMessageHeader addLocalMessage(GroupMessage p) throws DbException;
/**
* Stores and sends a local private group message.
*/
GroupMessageHeader addLocalMessage(Transaction txn, GroupMessage p)
throws DbException;
/** /**
* Returns the private group with the given ID. * Returns the private group with the given ID.
*/ */

View File

@@ -126,17 +126,13 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
@Override @Override
public ForumPostHeader addLocalPost(ForumPost p) throws DbException { public ForumPostHeader addLocalPost(ForumPost p) throws DbException {
return db.transactionWithResult(false, txn -> { return db.transactionWithResult(false, txn -> addLocalPost(txn, p));
try {
return addLocalPost(txn, p);
} catch (FormatException e) {
throw new AssertionError(e);
}
});
} }
private ForumPostHeader addLocalPost(Transaction txn, ForumPost p) @Override
throws DbException, FormatException { public ForumPostHeader addLocalPost(Transaction txn, ForumPost p)
throws DbException {
try {
BdfDictionary meta = new BdfDictionary(); BdfDictionary meta = new BdfDictionary();
meta.put(KEY_TIMESTAMP, p.getMessage().getTimestamp()); meta.put(KEY_TIMESTAMP, p.getMessage().getTimestamp());
if (p.getParent() != null) meta.put(KEY_PARENT, p.getParent()); if (p.getParent() != null) meta.put(KEY_PARENT, p.getParent());
@@ -144,11 +140,16 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
meta.put(KEY_AUTHOR, clientHelper.toList(a)); meta.put(KEY_AUTHOR, clientHelper.toList(a));
meta.put(KEY_LOCAL, true); meta.put(KEY_LOCAL, true);
meta.put(MSG_KEY_READ, true); meta.put(MSG_KEY_READ, true);
clientHelper.addLocalMessage(txn, p.getMessage(), meta, true, false); clientHelper
.addLocalMessage(txn, p.getMessage(), meta, true, false);
messageTracker.trackOutgoingMessage(txn, p.getMessage()); messageTracker.trackOutgoingMessage(txn, p.getMessage());
AuthorInfo authorInfo = authorManager.getMyAuthorInfo(txn); AuthorInfo authorInfo = authorManager.getMyAuthorInfo(txn);
return new ForumPostHeader(p.getMessage().getId(), p.getParent(), return new ForumPostHeader(p.getMessage().getId(), p.getParent(),
p.getMessage().getTimestamp(), p.getAuthor(), authorInfo, true); p.getMessage().getTimestamp(), p.getAuthor(), authorInfo,
true);
} catch (FormatException e) {
throw new AssertionError(e);
}
} }
@Override @Override

View File

@@ -210,17 +210,13 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
@Override @Override
public GroupMessageHeader addLocalMessage(GroupMessage m) public GroupMessageHeader addLocalMessage(GroupMessage m)
throws DbException { throws DbException {
return db.transactionWithResult(false, txn -> { return db.transactionWithResult(false, txn -> addLocalMessage(txn, m));
try {
return addLocalMessage(txn, m);
} catch (FormatException e) {
throw new DbException(e);
}
});
} }
private GroupMessageHeader addLocalMessage(Transaction txn, GroupMessage m) @Override
throws DbException, FormatException { public GroupMessageHeader addLocalMessage(Transaction txn, GroupMessage m)
throws DbException {
try {
// store message and metadata // store message and metadata
BdfDictionary meta = new BdfDictionary(); BdfDictionary meta = new BdfDictionary();
meta.put(KEY_TYPE, POST.getInt()); meta.put(KEY_TYPE, POST.getInt());
@@ -228,7 +224,8 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
meta.put(KEY_PARENT_MSG_ID, m.getParent()); meta.put(KEY_PARENT_MSG_ID, m.getParent());
addMessageMetadata(meta, m); addMessageMetadata(meta, m);
GroupId g = m.getMessage().getGroupId(); GroupId g = m.getMessage().getGroupId();
clientHelper.addLocalMessage(txn, m.getMessage(), meta, true, false); clientHelper
.addLocalMessage(txn, m.getMessage(), meta, true, false);
// track message // track message
setPreviousMsgId(txn, g, m.getMessage().getId()); setPreviousMsgId(txn, g, m.getMessage().getId());
messageTracker.trackOutgoingMessage(txn, m.getMessage()); messageTracker.trackOutgoingMessage(txn, m.getMessage());
@@ -237,7 +234,11 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
AuthorInfo authorInfo = authorManager.getMyAuthorInfo(txn); AuthorInfo authorInfo = authorManager.getMyAuthorInfo(txn);
return new GroupMessageHeader(m.getMessage().getGroupId(), return new GroupMessageHeader(m.getMessage().getGroupId(),
m.getMessage().getId(), m.getParent(), m.getMessage().getId(), m.getParent(),
m.getMessage().getTimestamp(), m.getMember(), authorInfo, true); m.getMessage().getTimestamp(), m.getMember(), authorInfo,
true);
} catch (FormatException e) {
throw new DbException(e);
}
} }
private void addMessageMetadata(BdfDictionary meta, GroupMessage m) { private void addMessageMetadata(BdfDictionary meta, GroupMessage m) {