From b9bac8b6a54da1d83ee03f933b8779aaff398742 Mon Sep 17 00:00:00 2001 From: ialokim Date: Wed, 23 Nov 2022 18:31:49 +0100 Subject: [PATCH] add transactional versions to functions related to forums --- .../briar/api/forum/ForumManager.java | 10 ++++++ .../briar/api/sharing/SharingManager.java | 31 ++++++++++++++++ .../briar/forum/ForumManagerImpl.java | 19 +++++++--- .../briar/sharing/SharingManagerImpl.java | 36 ++++++++++++++----- 4 files changed, 82 insertions(+), 14 deletions(-) diff --git a/briar-api/src/main/java/org/briarproject/briar/api/forum/ForumManager.java b/briar-api/src/main/java/org/briarproject/briar/api/forum/ForumManager.java index 1b0cd636c..0ce923474 100644 --- a/briar-api/src/main/java/org/briarproject/briar/api/forum/ForumManager.java +++ b/briar-api/src/main/java/org/briarproject/briar/api/forum/ForumManager.java @@ -48,6 +48,11 @@ public interface ForumManager { */ void removeForum(Forum f) throws DbException; + /** + * Unsubscribes from a forum. + */ + void removeForum(Transaction txn, Forum f) throws DbException; + /** * Creates a local forum post. */ @@ -127,6 +132,11 @@ public interface ForumManager { */ void setReadFlag(GroupId g, MessageId m, boolean read) 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; + interface RemoveForumHook { /** * Called when a forum is being removed. diff --git a/briar-api/src/main/java/org/briarproject/briar/api/sharing/SharingManager.java b/briar-api/src/main/java/org/briarproject/briar/api/sharing/SharingManager.java index 87241aacb..ae81f9aa8 100644 --- a/briar-api/src/main/java/org/briarproject/briar/api/sharing/SharingManager.java +++ b/briar-api/src/main/java/org/briarproject/briar/api/sharing/SharingManager.java @@ -24,23 +24,48 @@ public interface SharingManager void sendInvitation(GroupId shareableId, ContactId contactId, @Nullable String text) throws DbException; + /** + * Sends an invitation to share the given group with the given contact, + * including optional text. + */ + void sendInvitation(Transaction txn, GroupId shareableId, + ContactId contactId, @Nullable String text) throws DbException; + /** * Responds to a pending group invitation */ void respondToInvitation(S s, Contact c, boolean accept) throws DbException; + /** + * Responds to a pending group invitation + */ + void respondToInvitation(Transaction txn, S s, Contact c, boolean accept) + throws DbException; + /** * Responds to a pending group invitation */ void respondToInvitation(ContactId c, SessionId id, boolean accept) throws DbException; + /** + * Responds to a pending group invitation + */ + void respondToInvitation(Transaction txn, ContactId c, SessionId id, + boolean accept) throws DbException; + /** * Returns all invitations to groups. */ Collection getInvitations() throws DbException; + /** + * Returns all invitations to groups. + */ + Collection getInvitations(Transaction txn) + throws DbException; + /** * Returns all contacts with whom the given group is shared. */ @@ -57,4 +82,10 @@ public interface SharingManager */ boolean canBeShared(GroupId g, Contact c) throws DbException; + /** + * Returns true if the group not already shared and no invitation is open + */ + boolean canBeShared(Transaction txn, GroupId g, Contact c) + throws DbException; + } diff --git a/briar-core/src/main/java/org/briarproject/briar/forum/ForumManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/forum/ForumManagerImpl.java index 1bd80f4e3..f15fb9b75 100644 --- a/briar-core/src/main/java/org/briarproject/briar/forum/ForumManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/forum/ForumManagerImpl.java @@ -105,11 +105,14 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager { @Override public void removeForum(Forum f) throws DbException { - db.transaction(false, txn -> { - for (RemoveForumHook hook : removeHooks) - hook.removingForum(txn, f); - db.removeGroup(txn, f.getGroup()); - }); + db.transaction(false, txn -> removeForum(txn, f)); + } + + @Override + public void removeForum(Transaction txn, Forum f) throws DbException { + for (RemoveForumHook hook : removeHooks) + hook.removingForum(txn, f); + db.removeGroup(txn, f.getGroup()); } @Override @@ -268,6 +271,12 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager { messageTracker.setReadFlag(txn, g, m, read)); } + @Override + public void setReadFlag(Transaction txn, GroupId g, MessageId m, + boolean read) throws DbException { + messageTracker.setReadFlag(txn, g, m, read); + } + private Forum parseForum(Group g) throws FormatException { byte[] descriptor = g.getDescriptor(); // Name, salt diff --git a/briar-core/src/main/java/org/briarproject/briar/sharing/SharingManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/sharing/SharingManagerImpl.java index 22473416f..940cae1cb 100644 --- a/briar-core/src/main/java/org/briarproject/briar/sharing/SharingManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/sharing/SharingManagerImpl.java @@ -259,8 +259,14 @@ abstract class SharingManagerImpl @Override public void sendInvitation(GroupId shareableId, ContactId contactId, @Nullable String text) throws DbException { + db.transaction(false, + txn -> sendInvitation(txn, shareableId, contactId, text)); + } + + @Override + public void sendInvitation(Transaction txn, GroupId shareableId, + ContactId contactId, @Nullable String text) throws DbException { SessionId sessionId = getSessionId(shareableId); - Transaction txn = db.startTransaction(false); try { Contact contact = db.getContact(txn, contactId); if (!canBeShared(txn, shareableId, contact)) @@ -286,11 +292,8 @@ abstract class SharingManagerImpl session = engine.onInviteAction(txn, session, text); // Store the updated session storeSession(txn, storageId, session); - db.commitTransaction(txn); } catch (FormatException e) { throw new DbException(e); - } finally { - db.endTransaction(txn); } } @@ -300,6 +303,12 @@ abstract class SharingManagerImpl respondToInvitation(c.getId(), getSessionId(s.getId()), accept); } + @Override + public void respondToInvitation(Transaction txn, S s, Contact c, + boolean accept) throws DbException { + respondToInvitation(txn, c.getId(), getSessionId(s.getId()), accept); + } + @Override public void respondToInvitation(ContactId c, SessionId id, boolean accept) throws DbException { @@ -307,6 +316,12 @@ abstract class SharingManagerImpl txn -> respondToInvitation(txn, c, id, accept, false)); } + @Override + public void respondToInvitation(Transaction txn, ContactId c, SessionId id, + boolean accept) throws DbException { + respondToInvitation(txn, c, id, accept, false); + } + private void respondToInvitation(Transaction txn, ContactId c, SessionId id, boolean accept, boolean isAutoDecline) throws DbException { @@ -390,10 +405,15 @@ abstract class SharingManagerImpl @Override public Collection getInvitations() throws DbException { + return db.transactionWithResult(true, this::getInvitations); + } + + @Override + public Collection getInvitations(Transaction txn) + throws DbException { List items = new ArrayList<>(); BdfDictionary query = messageParser.getInvitesAvailableToAnswerQuery(); Map> sharers = new HashMap<>(); - Transaction txn = db.startTransaction(true); try { // get invitations from each contact for (Contact c : db.getContacts(txn)) { @@ -423,12 +443,9 @@ abstract class SharingManagerImpl new SharingInvitationItem(s, subscribed, contacts); items.add(invitation); } - db.commitTransaction(txn); return items; } catch (FormatException e) { throw new DbException(e); - } finally { - db.endTransaction(txn); } } @@ -461,7 +478,8 @@ abstract class SharingManagerImpl } } - private boolean canBeShared(Transaction txn, GroupId g, Contact c) + @Override + public boolean canBeShared(Transaction txn, GroupId g, Contact c) throws DbException { // The group can't be shared unless the contact supports the client Visibility client = clientVersioningManager.getClientVisibility(txn,