Skip to content

Commit

Permalink
Update groups when using listGroups command
Browse files Browse the repository at this point in the history
Fixes #1517
  • Loading branch information
AsamK committed Jun 6, 2024
1 parent 1759679 commit 5a97b9e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
68 changes: 43 additions & 25 deletions lib/src/main/java/org/asamk/signal/manager/helper/GroupHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public GroupInfo getGroup(GroupId groupId) {
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) {
var group = getGroup(groupId);
return group != null && group.isBlocked();
Expand Down Expand Up @@ -382,34 +388,46 @@ public SendGroupMessageResults sendGroupInfoMessage(

private GroupInfo getGroup(GroupId groupId, boolean forceUpdate) {
final var group = account.getGroupStore().getGroup(groupId);
if (group instanceof GroupInfoV2 groupInfoV2) {
if (forceUpdate || (!groupInfoV2.isPermissionDenied() && groupInfoV2.getGroup() == null)) {
final var groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupInfoV2.getMasterKey());
DecryptedGroup decryptedGroup;
try {
decryptedGroup = context.getGroupV2Helper().getDecryptedGroup(groupSecretParams);
} catch (NotAGroupMemberException e) {
groupInfoV2.setPermissionDenied(true);
decryptedGroup = null;
}
if (decryptedGroup != null) {
try {
storeProfileKeysFromHistory(groupSecretParams, groupInfoV2, decryptedGroup);
} catch (NotAGroupMemberException ignored) {
}
storeProfileKeysFromMembers(decryptedGroup);
final var avatar = decryptedGroup.avatar;
if (!avatar.isEmpty()) {
downloadGroupAvatar(groupInfoV2.getGroupId(), groupSecretParams, avatar);
}
}
groupInfoV2.setGroup(decryptedGroup);
account.getGroupStore().updateGroup(group);
}
}
fillOrUpdateGroup(group, forceUpdate);
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());
DecryptedGroup decryptedGroup;
try {
decryptedGroup = context.getGroupV2Helper().getDecryptedGroup(groupSecretParams);
} catch (NotAGroupMemberException e) {
groupInfoV2.setPermissionDenied(true);
account.getGroupStore().updateGroup(group);
return;
}

if (decryptedGroup == null) {
return;
}

try {
storeProfileKeysFromHistory(groupSecretParams, groupInfoV2, decryptedGroup);
} catch (NotAGroupMemberException ignored) {
}
storeProfileKeysFromMembers(decryptedGroup);
final var avatar = decryptedGroup.avatar;
if (!avatar.isEmpty()) {
downloadGroupAvatar(groupInfoV2.getGroupId(), groupSecretParams, avatar);
}
groupInfoV2.setGroup(decryptedGroup);
account.getGroupStore().updateGroup(group);
}

private void downloadGroupAvatar(GroupIdV2 groupId, GroupSecretParams groupSecretParams, String cdnKey) {
try {
context.getAvatarStore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public SendMessageResult resendMessage(
}

final var groupId = messageSendLogEntry.groupId().get();
final var group = account.getGroupStore().getGroup(groupId);
final var group = context.getGroupHelper().getGroup(groupId);

if (group == null) {
logger.debug("Could not find a matching group for the groupId {}! Skipping message send.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ void refreshPreKeys() throws IOException {

@Override
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) {
Expand Down

0 comments on commit 5a97b9e

Please sign in to comment.