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;
/**
* Stores a local forum post.
*/
ForumPostHeader addLocalPost(Transaction txn, ForumPost p)
throws DbException;
/**
* Returns the forum with the given ID.
*/

View File

@@ -82,6 +82,12 @@ public interface PrivateGroupManager {
*/
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.
*/

View File

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

View File

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