add transactional versions to introductionManager and privateGroupManager

This commit is contained in:
ialokim
2023-02-24 18:43:08 +01:00
parent f02bbebf6c
commit b09ea495e7
4 changed files with 81 additions and 54 deletions

View File

@@ -29,6 +29,12 @@ public interface IntroductionManager extends ConversationClient {
*/ */
boolean canIntroduce(Contact c1, Contact c2) throws DbException; boolean canIntroduce(Contact c1, Contact c2) throws DbException;
/**
* Returns true if both contacts can be introduced at this moment.
*/
boolean canIntroduce(Transaction txn, Contact c1, Contact c2)
throws DbException;
/** /**
* The current minor version of the introduction client. * The current minor version of the introduction client.
*/ */
@@ -40,6 +46,12 @@ public interface IntroductionManager extends ConversationClient {
void makeIntroduction(Contact c1, Contact c2, @Nullable String text) void makeIntroduction(Contact c1, Contact c2, @Nullable String text)
throws DbException; throws DbException;
/**
* Sends two initial introduction messages.
*/
void makeIntroduction(Transaction txn, Contact c1, Contact c2,
@Nullable String text) throws DbException;
/** /**
* Responds to an introduction. * Responds to an introduction.
*/ */

View File

@@ -52,11 +52,21 @@ public interface PrivateGroupManager {
void addPrivateGroup(Transaction txn, PrivateGroup group, void addPrivateGroup(Transaction txn, PrivateGroup group,
GroupMessage joinMsg, boolean creator) throws DbException; GroupMessage joinMsg, boolean creator) throws DbException;
/**
* Removes a dissolved private group.
*/
void removePrivateGroup(Transaction txn, GroupId g) throws DbException;
/** /**
* Removes a dissolved private group. * Removes a dissolved private group.
*/ */
void removePrivateGroup(GroupId g) throws DbException; void removePrivateGroup(GroupId g) throws DbException;
/**
* Returns the ID of the user's previous message sent to the group
*/
MessageId getPreviousMsgId(Transaction txn, GroupId g) throws DbException;
/** /**
* Returns the ID of the user's previous message sent to the group * Returns the ID of the user's previous message sent to the group
*/ */
@@ -112,7 +122,8 @@ public interface PrivateGroupManager {
/** /**
* Returns true if the given private group was created by us. * Returns true if the given private group was created by us.
*/ */
boolean isOurPrivateGroup(Transaction txn, PrivateGroup g) throws DbException; boolean isOurPrivateGroup(Transaction txn, PrivateGroup g)
throws DbException;
/** /**
* Returns the text of the private group message with the given ID. * Returns the text of the private group message with the given ID.
@@ -161,6 +172,12 @@ public interface PrivateGroupManager {
*/ */
GroupCount getGroupCount(GroupId g) throws DbException; GroupCount getGroupCount(GroupId g) throws DbException;
/**
* Marks a message as read or unread and updates the group count.
*/
void setReadFlag(Transaction txn, GroupId g, MessageId m, boolean read)
throws DbException;
/** /**
* Marks a message as read or unread and updates the group count. * Marks a message as read or unread and updates the group count.
*/ */

View File

@@ -300,36 +300,37 @@ class IntroductionManagerImpl extends ConversationClientImpl
@Override @Override
public boolean canIntroduce(Contact c1, Contact c2) throws DbException { public boolean canIntroduce(Contact c1, Contact c2) throws DbException {
Transaction txn = db.startTransaction(true); return db.transactionWithResult(true,
txn -> canIntroduce(txn, c1, c2));
}
public boolean canIntroduce(Transaction txn, Contact c1, Contact c2)
throws DbException {
try { try {
boolean can = canIntroduce(txn, c1, c2); // Look up the session, if there is one
db.commitTransaction(txn); Author introducer = identityManager.getLocalAuthor(txn);
return can; SessionId sessionId =
crypto.getSessionId(introducer, c1.getAuthor(),
c2.getAuthor());
StoredSession ss = getSession(txn, sessionId);
if (ss == null) return true;
IntroducerSession session =
sessionParser.parseIntroducerSession(ss.bdfSession);
return session.getState().isComplete();
} catch (FormatException e) { } catch (FormatException e) {
throw new DbException(e); throw new DbException(e);
} finally {
db.endTransaction(txn);
} }
} }
private boolean canIntroduce(Transaction txn, Contact c1, Contact c2) public void makeIntroduction(Contact c1, Contact c2, @Nullable String text)
throws DbException, FormatException { throws DbException {
// Look up the session, if there is one db.transaction(false,
Author introducer = identityManager.getLocalAuthor(txn); txn -> makeIntroduction(txn, c1, c2, text));
SessionId sessionId =
crypto.getSessionId(introducer, c1.getAuthor(),
c2.getAuthor());
StoredSession ss = getSession(txn, sessionId);
if (ss == null) return true;
IntroducerSession session =
sessionParser.parseIntroducerSession(ss.bdfSession);
return session.getState().isComplete();
} }
@Override @Override
public void makeIntroduction(Contact c1, Contact c2, @Nullable String text) public void makeIntroduction(Transaction txn, Contact c1, Contact c2,
throws DbException { @Nullable String text) throws DbException {
Transaction txn = db.startTransaction(false);
try { try {
// Look up the session, if there is one // Look up the session, if there is one
Author introducer = identityManager.getLocalAuthor(txn); Author introducer = identityManager.getLocalAuthor(txn);
@@ -363,11 +364,8 @@ class IntroductionManagerImpl extends ConversationClientImpl
session = introducerEngine.onRequestAction(txn, session, text); session = introducerEngine.onRequestAction(txn, session, text);
// Store the updated session // Store the updated session
storeSession(txn, storageId, session); storeSession(txn, storageId, session);
db.commitTransaction(txn);
} catch (FormatException e) { } catch (FormatException e) {
throw new DbException(e); throw new DbException(e);
} finally {
db.endTransaction(txn);
} }
} }

View File

@@ -150,40 +150,35 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
} }
@Override @Override
public void removePrivateGroup(GroupId g) throws DbException { public void removePrivateGroup(Transaction txn, GroupId g)
Transaction txn = db.startTransaction(false); throws DbException {
try { for (PrivateGroupHook hook : hooks) {
for (PrivateGroupHook hook : hooks) { hook.removingGroup(txn, g);
hook.removingGroup(txn, g);
}
Group group = db.getGroup(txn, g);
db.removeGroup(txn, group);
db.commitTransaction(txn);
} finally {
db.endTransaction(txn);
} }
Group group = db.getGroup(txn, g);
db.removeGroup(txn, group);
}
@Override
public void removePrivateGroup(GroupId g) throws DbException {
db.transaction(false, txn -> removePrivateGroup(txn, g));
} }
@Override @Override
public MessageId getPreviousMsgId(GroupId g) throws DbException { public MessageId getPreviousMsgId(GroupId g) throws DbException {
MessageId previousMsgId; return db.transactionWithResult(true,
Transaction txn = db.startTransaction(true); txn -> getPreviousMsgId(txn, g));
try {
previousMsgId = getPreviousMsgId(txn, g);
db.commitTransaction(txn);
} catch (FormatException e) {
throw new DbException(e);
} finally {
db.endTransaction(txn);
}
return previousMsgId;
} }
private MessageId getPreviousMsgId(Transaction txn, GroupId g) public MessageId getPreviousMsgId(Transaction txn, GroupId g)
throws DbException, FormatException { throws DbException {
BdfDictionary d = clientHelper.getGroupMetadataAsDictionary(txn, g); try {
byte[] previousMsgIdBytes = d.getRaw(KEY_PREVIOUS_MSG_ID); BdfDictionary d = clientHelper.getGroupMetadataAsDictionary(txn, g);
return new MessageId(previousMsgIdBytes); byte[] previousMsgIdBytes = d.getRaw(KEY_PREVIOUS_MSG_ID);
return new MessageId(previousMsgIdBytes);
} catch (FormatException e) {
throw new DbException(e);
}
} }
private void setPreviousMsgId(Transaction txn, GroupId g, private void setPreviousMsgId(Transaction txn, GroupId g,
@@ -483,11 +478,16 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
return messageTracker.getGroupCount(g); return messageTracker.getGroupCount(g);
} }
@Override
public void setReadFlag(Transaction txn, GroupId g, MessageId m,
boolean read) throws DbException {
messageTracker.setReadFlag(txn, g, m, read);
}
@Override @Override
public void setReadFlag(GroupId g, MessageId m, boolean read) public void setReadFlag(GroupId g, MessageId m, boolean read)
throws DbException { throws DbException {
db.transaction(false, txn -> db.transaction(false, txn -> setReadFlag(txn, g, m, read));
messageTracker.setReadFlag(txn, g, m, read));
} }
@Override @Override