+ } else {
+ // Received a group v1 message for a v2 group
+ }
+ }
+ 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().getGroupByV2Id(groupId);
+ if (groupInfo instanceof GroupInfoV1) {
+ // Received a v2 group message for a v2 group, we need to locally migrate the group
+ account.getGroupStore().deleteGroup(groupInfo.groupId);
+ GroupInfoV2 groupInfoV2 = new GroupInfoV2(groupId, groupMasterKey);
+ groupInfoV2.setGroup(getDecryptedGroup(groupSecretParams));
+ account.getGroupStore().updateGroup(groupInfoV2);
+ System.err.println("Locally migrated group "
+ + Base64.encodeBytes(groupInfo.groupId)
+ + " to group v2, id: "
+ + Base64.encodeBytes(groupInfoV2.groupId)
+ + " !!!");
+ } 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()) {
+ DecryptedGroup group = null;
+ if (groupContext.hasSignedGroupChange()
+ && groupInfoV2.getGroup() != null
+ && groupInfoV2.getGroup().getRevision() + 1 == groupContext.getRevision()) {
+ group = groupHelper.getUpdatedDecryptedGroup(groupInfoV2.getGroup(),
+ groupContext.getSignedGroupChange(),
+ groupMasterKey);
+ if (group != null) {
+ storeProfileKeysFromMembers(group);
+ }
+ }
+ if (group == null) {
+ group = getDecryptedGroup(groupSecretParams);
+ }
+ groupInfoV2.setGroup(group);
+ account.getGroupStore().updateGroup(groupInfoV2);