Add transactional getMembers() method to PrivateGroupManager.

This commit is contained in:
akwizgran
2021-01-27 15:12:29 +00:00
committed by Torsten Grote
parent c62a57e8b2
commit f2eca0fdb6
2 changed files with 33 additions and 28 deletions

View File

@@ -129,6 +129,12 @@ public interface PrivateGroupManager {
*/ */
Collection<GroupMember> getMembers(GroupId g) throws DbException; Collection<GroupMember> getMembers(GroupId g) throws DbException;
/**
* Returns all members of the given private group.
*/
Collection<GroupMember> getMembers(Transaction txn, GroupId g)
throws DbException;
/** /**
* Returns true if the given author is a member of the given private group. * Returns true if the given author is a member of the given private group.
*/ */

View File

@@ -408,35 +408,34 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
@Override @Override
public Collection<GroupMember> getMembers(GroupId g) throws DbException { public Collection<GroupMember> getMembers(GroupId g) throws DbException {
Transaction txn = db.startTransaction(true); return db.transactionWithResult(true, txn -> getMembers(txn, g));
try {
Collection<GroupMember> members = new ArrayList<>();
Map<Author, Visibility> authors = getMembers(txn, g);
LocalAuthor la = identityManager.getLocalAuthor(txn);
PrivateGroup privateGroup = getPrivateGroup(txn, g);
for (Entry<Author, Visibility> m : authors.entrySet()) {
Author a = m.getKey();
AuthorInfo authorInfo =
authorManager.getAuthorInfo(txn, a.getId());
Status status = authorInfo.getStatus();
Visibility v = m.getValue();
ContactId c = null;
if (v != INVISIBLE &&
(status == VERIFIED || status == UNVERIFIED)) {
c = contactManager.getContact(txn, a.getId(), la.getId())
.getId();
}
boolean isCreator = privateGroup.getCreator().equals(a);
members.add(new GroupMember(a, authorInfo, isCreator, c, v));
}
db.commitTransaction(txn);
return members;
} finally {
db.endTransaction(txn);
}
} }
private Map<Author, Visibility> getMembers(Transaction txn, GroupId g) @Override
public Collection<GroupMember> getMembers(Transaction txn, GroupId g)
throws DbException {
Collection<GroupMember> members = new ArrayList<>();
Map<Author, Visibility> authors = getMemberAuthors(txn, g);
LocalAuthor la = identityManager.getLocalAuthor(txn);
PrivateGroup privateGroup = getPrivateGroup(txn, g);
for (Entry<Author, Visibility> m : authors.entrySet()) {
Author a = m.getKey();
AuthorInfo authorInfo = authorManager.getAuthorInfo(txn, a.getId());
Status status = authorInfo.getStatus();
Visibility v = m.getValue();
ContactId c = null;
if (v != INVISIBLE &&
(status == VERIFIED || status == UNVERIFIED)) {
c = contactManager.getContact(txn, a.getId(), la.getId())
.getId();
}
boolean isCreator = privateGroup.getCreator().equals(a);
members.add(new GroupMember(a, authorInfo, isCreator, c, v));
}
return members;
}
private Map<Author, Visibility> getMemberAuthors(Transaction txn, GroupId g)
throws DbException { throws DbException {
try { try {
BdfDictionary meta = BdfDictionary meta =
@@ -458,7 +457,7 @@ class PrivateGroupManagerImpl extends BdfIncomingMessageHook
@Override @Override
public boolean isMember(Transaction txn, GroupId g, Author a) public boolean isMember(Transaction txn, GroupId g, Author a)
throws DbException { throws DbException {
for (Author member : getMembers(txn, g).keySet()) { for (Author member : getMemberAuthors(txn, g).keySet()) {
if (member.equals(a)) return true; if (member.equals(a)) return true;
} }
return false; return false;