add transactional versions to functions related to forums

This commit is contained in:
ialokim
2022-11-23 18:31:49 +01:00
parent c855967d56
commit b9bac8b6a5
4 changed files with 82 additions and 14 deletions

View File

@@ -48,6 +48,11 @@ public interface ForumManager {
*/ */
void removeForum(Forum f) throws DbException; void removeForum(Forum f) throws DbException;
/**
* Unsubscribes from a forum.
*/
void removeForum(Transaction txn, Forum f) throws DbException;
/** /**
* Creates a local forum post. * Creates a local forum post.
*/ */
@@ -127,6 +132,11 @@ public interface ForumManager {
*/ */
void setReadFlag(GroupId g, MessageId m, boolean read) throws DbException; 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 { interface RemoveForumHook {
/** /**
* Called when a forum is being removed. * 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, void sendInvitation(GroupId shareableId, ContactId contactId,
@Nullable String text) throws DbException; @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 * Responds to a pending group invitation
*/ */
void respondToInvitation(S s, Contact c, boolean accept) void respondToInvitation(S s, Contact c, boolean accept)
throws DbException; 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 * Responds to a pending group invitation
*/ */
void respondToInvitation(ContactId c, SessionId id, boolean accept) void respondToInvitation(ContactId c, SessionId id, boolean accept)
throws DbException; 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. * Returns all invitations to groups.
*/ */
Collection<SharingInvitationItem> getInvitations() throws DbException; 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. * 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; 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 @Override
public void removeForum(Forum f) throws DbException { public void removeForum(Forum f) throws DbException {
db.transaction(false, txn -> { db.transaction(false, txn -> removeForum(txn, f));
for (RemoveForumHook hook : removeHooks) }
hook.removingForum(txn, f);
db.removeGroup(txn, f.getGroup()); @Override
}); public void removeForum(Transaction txn, Forum f) throws DbException {
for (RemoveForumHook hook : removeHooks)
hook.removingForum(txn, f);
db.removeGroup(txn, f.getGroup());
} }
@Override @Override
@@ -268,6 +271,12 @@ class ForumManagerImpl extends BdfIncomingMessageHook implements ForumManager {
messageTracker.setReadFlag(txn, g, m, read)); 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 { private Forum parseForum(Group g) throws FormatException {
byte[] descriptor = g.getDescriptor(); byte[] descriptor = g.getDescriptor();
// Name, salt // Name, salt

View File

@@ -259,8 +259,14 @@ abstract class SharingManagerImpl<S extends Shareable>
@Override @Override
public void sendInvitation(GroupId shareableId, ContactId contactId, public void sendInvitation(GroupId shareableId, ContactId contactId,
@Nullable String text) throws DbException { @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); SessionId sessionId = getSessionId(shareableId);
Transaction txn = db.startTransaction(false);
try { try {
Contact contact = db.getContact(txn, contactId); Contact contact = db.getContact(txn, contactId);
if (!canBeShared(txn, shareableId, contact)) if (!canBeShared(txn, shareableId, contact))
@@ -286,11 +292,8 @@ abstract class SharingManagerImpl<S extends Shareable>
session = engine.onInviteAction(txn, session, text); session = engine.onInviteAction(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);
} }
} }
@@ -300,6 +303,12 @@ abstract class SharingManagerImpl<S extends Shareable>
respondToInvitation(c.getId(), getSessionId(s.getId()), accept); 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 @Override
public void respondToInvitation(ContactId c, SessionId id, boolean accept) public void respondToInvitation(ContactId c, SessionId id, boolean accept)
throws DbException { throws DbException {
@@ -307,6 +316,12 @@ abstract class SharingManagerImpl<S extends Shareable>
txn -> respondToInvitation(txn, c, id, accept, false)); 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, private void respondToInvitation(Transaction txn, ContactId c,
SessionId id, boolean accept, boolean isAutoDecline) SessionId id, boolean accept, boolean isAutoDecline)
throws DbException { throws DbException {
@@ -390,10 +405,15 @@ abstract class SharingManagerImpl<S extends Shareable>
@Override @Override
public Collection<SharingInvitationItem> getInvitations() public Collection<SharingInvitationItem> getInvitations()
throws DbException { throws DbException {
return db.transactionWithResult(true, this::getInvitations);
}
@Override
public Collection<SharingInvitationItem> getInvitations(Transaction txn)
throws DbException {
List<SharingInvitationItem> items = new ArrayList<>(); List<SharingInvitationItem> items = new ArrayList<>();
BdfDictionary query = messageParser.getInvitesAvailableToAnswerQuery(); BdfDictionary query = messageParser.getInvitesAvailableToAnswerQuery();
Map<S, Collection<Contact>> sharers = new HashMap<>(); Map<S, Collection<Contact>> sharers = new HashMap<>();
Transaction txn = db.startTransaction(true);
try { try {
// get invitations from each contact // get invitations from each contact
for (Contact c : db.getContacts(txn)) { for (Contact c : db.getContacts(txn)) {
@@ -423,12 +443,9 @@ abstract class SharingManagerImpl<S extends Shareable>
new SharingInvitationItem(s, subscribed, contacts); new SharingInvitationItem(s, subscribed, contacts);
items.add(invitation); items.add(invitation);
} }
db.commitTransaction(txn);
return items; return items;
} catch (FormatException e) { } catch (FormatException e) {
throw new DbException(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 { throws DbException {
// The group can't be shared unless the contact supports the client // The group can't be shared unless the contact supports the client
Visibility client = clientVersioningManager.getClientVisibility(txn, Visibility client = clientVersioningManager.getClientVisibility(txn,