+ } else {
+ System.err.println("Received a group v1 message for a v2 group: " + group.getTitle());
+ }
+ }
+ if (message.getGroupContext().get().getGroupV2().isPresent()) {
+ final SignalServiceGroupV2 groupContext = message.getGroupContext().get().getGroupV2().get();
+ final GroupMasterKey groupMasterKey = groupContext.getMasterKey();
+
+ final GroupSecretParams groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupMasterKey);
+
+ byte[] groupId = groupSecretParams.getPublicParams().getGroupIdentifier().serialize();
+ GroupInfo groupInfo = account.getGroupStore().getGroup(groupId);
+ if (groupInfo instanceof GroupInfoV1) {
+ // TODO upgrade group
+ } else if (groupInfo == null || groupInfo instanceof GroupInfoV2) {
+ GroupInfoV2 groupInfoV2 = groupInfo == null
+ ? new GroupInfoV2(groupId, groupMasterKey)
+ : (GroupInfoV2) groupInfo;
+
+ if (groupInfoV2.getGroup() == null || groupInfoV2.getGroup().getRevision() < groupContext.getRevision()) {
+ // TODO check if revision is only 1 behind and a signedGroupChange is available
+ try {
+ final GroupsV2AuthorizationString groupsV2AuthorizationString = getGroupAuthForToday(groupSecretParams);
+ final DecryptedGroup group = groupsV2Api.getGroup(groupSecretParams, groupsV2AuthorizationString);
+ groupInfoV2.setGroup(group);
+ for (DecryptedMember member : group.getMembersList()) {
+ final SignalServiceAddress address = resolveSignalServiceAddress(new SignalServiceAddress(UuidUtil.parseOrThrow(member.getUuid().toByteArray()), null));
+ try {
+ account.getProfileStore().storeProfileKey(address, new ProfileKey(member.getProfileKey().toByteArray()));
+ } catch (InvalidInputException ignored) {
+ }
+ }
+ } catch (IOException | VerificationFailedException | InvalidGroupStateException e) {
+ System.err.println("Failed to retrieve Group V2 info, ignoring ...");
+ }
+ account.getGroupStore().updateGroup(groupInfoV2);