Update groups when using listGroups command

Fixes #1517
This commit is contained in:
AsamK 2024-06-06 10:03:27 +02:00
parent 17596795c2
commit 5a97b9e134
3 changed files with 45 additions and 27 deletions

View file

@ -79,6 +79,12 @@ public class GroupHelper {
return getGroup(groupId, false); return getGroup(groupId, false);
} }
public List<GroupInfo> getGroups() {
final var groups = account.getGroupStore().getGroups();
groups.forEach(group -> fillOrUpdateGroup(group, false));
return groups;
}
public boolean isGroupBlocked(final GroupId groupId) { public boolean isGroupBlocked(final GroupId groupId) {
var group = getGroup(groupId); var group = getGroup(groupId);
return group != null && group.isBlocked(); return group != null && group.isBlocked();
@ -382,17 +388,33 @@ public class GroupHelper {
private GroupInfo getGroup(GroupId groupId, boolean forceUpdate) { private GroupInfo getGroup(GroupId groupId, boolean forceUpdate) {
final var group = account.getGroupStore().getGroup(groupId); final var group = account.getGroupStore().getGroup(groupId);
if (group instanceof GroupInfoV2 groupInfoV2) { fillOrUpdateGroup(group, forceUpdate);
if (forceUpdate || (!groupInfoV2.isPermissionDenied() && groupInfoV2.getGroup() == null)) { return group;
}
private void fillOrUpdateGroup(final GroupInfo group, final boolean forceUpdate) {
if (!(group instanceof GroupInfoV2 groupInfoV2)) {
return;
}
if (!forceUpdate && (groupInfoV2.isPermissionDenied() || groupInfoV2.getGroup() != null)) {
return;
}
final var groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupInfoV2.getMasterKey()); final var groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupInfoV2.getMasterKey());
DecryptedGroup decryptedGroup; DecryptedGroup decryptedGroup;
try { try {
decryptedGroup = context.getGroupV2Helper().getDecryptedGroup(groupSecretParams); decryptedGroup = context.getGroupV2Helper().getDecryptedGroup(groupSecretParams);
} catch (NotAGroupMemberException e) { } catch (NotAGroupMemberException e) {
groupInfoV2.setPermissionDenied(true); groupInfoV2.setPermissionDenied(true);
decryptedGroup = null; account.getGroupStore().updateGroup(group);
return;
} }
if (decryptedGroup != null) {
if (decryptedGroup == null) {
return;
}
try { try {
storeProfileKeysFromHistory(groupSecretParams, groupInfoV2, decryptedGroup); storeProfileKeysFromHistory(groupSecretParams, groupInfoV2, decryptedGroup);
} catch (NotAGroupMemberException ignored) { } catch (NotAGroupMemberException ignored) {
@ -402,13 +424,9 @@ public class GroupHelper {
if (!avatar.isEmpty()) { if (!avatar.isEmpty()) {
downloadGroupAvatar(groupInfoV2.getGroupId(), groupSecretParams, avatar); downloadGroupAvatar(groupInfoV2.getGroupId(), groupSecretParams, avatar);
} }
}
groupInfoV2.setGroup(decryptedGroup); groupInfoV2.setGroup(decryptedGroup);
account.getGroupStore().updateGroup(group); account.getGroupStore().updateGroup(group);
} }
}
return group;
}
private void downloadGroupAvatar(GroupIdV2 groupId, GroupSecretParams groupSecretParams, String cdnKey) { private void downloadGroupAvatar(GroupIdV2 groupId, GroupSecretParams groupSecretParams, String cdnKey) {
try { try {

View file

@ -261,7 +261,7 @@ public class SendHelper {
} }
final var groupId = messageSendLogEntry.groupId().get(); final var groupId = messageSendLogEntry.groupId().get();
final var group = account.getGroupStore().getGroup(groupId); final var group = context.getGroupHelper().getGroup(groupId);
if (group == null) { if (group == null) {
logger.debug("Could not find a matching group for the groupId {}! Skipping message send.", logger.debug("Could not find a matching group for the groupId {}! Skipping message send.",

View file

@ -511,7 +511,7 @@ public class ManagerImpl implements Manager {
@Override @Override
public List<Group> getGroups() { public List<Group> getGroups() {
return account.getGroupStore().getGroups().stream().map(this::toGroup).toList(); return context.getGroupHelper().getGroups().stream().map(this::toGroup).toList();
} }
private Group toGroup(final GroupInfo groupInfo) { private Group toGroup(final GroupInfo groupInfo) {