Merge branch 'transactions-forum' into 'master'

Add transactional versions to functions related to forums

See merge request briar/briar!1743
This commit is contained in:
akwizgran
2022-11-23 17:47:59 +00:00
4 changed files with 82 additions and 14 deletions

View File

@@ -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.

View File

@@ -24,23 +24,48 @@ public interface SharingManager<S extends Shareable>
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<SharingInvitationItem> getInvitations() throws DbException;
/**
* Returns all invitations to groups.
*/
Collection<SharingInvitationItem> getInvitations(Transaction txn)
throws DbException;
/**
* Returns all contacts with whom the given group is shared.
*/
@@ -57,4 +82,10 @@ public interface SharingManager<S extends Shareable>
*/
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;
}

View File

@@ -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

View File

@@ -259,8 +259,14 @@ abstract class SharingManagerImpl<S extends Shareable>
@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<S extends Shareable>
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<S extends Shareable>
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<S extends Shareable>
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<S extends Shareable>
@Override
public Collection<SharingInvitationItem> getInvitations()
throws DbException {
return db.transactionWithResult(true, this::getInvitations);
}
@Override
public Collection<SharingInvitationItem> getInvitations(Transaction txn)
throws DbException {
List<SharingInvitationItem> items = new ArrayList<>();
BdfDictionary query = messageParser.getInvitesAvailableToAnswerQuery();
Map<S, Collection<Contact>> 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<S extends Shareable>
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<S extends Shareable>
}
}
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,