+ public GroupInfoV2 getGroupOrPartialMigrate(
+ Connection connection, final GroupMasterKey groupMasterKey
+ ) throws SQLException {
+ final var groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupMasterKey);
+ final var groupId = GroupUtils.getGroupIdV2(groupSecretParams);
+
+ return getGroupOrPartialMigrate(connection, groupMasterKey, groupId);
+ }
+
+ public GroupInfoV2 getGroupOrPartialMigrate(
+ final GroupMasterKey groupMasterKey, final GroupIdV2 groupId
+ ) {
+ try (final var connection = database.getConnection()) {
+ return getGroupOrPartialMigrate(connection, groupMasterKey, groupId);
+ } catch (SQLException e) {
+ throw new RuntimeException("Failed read from group store", e);
+ }
+ }
+
+ private GroupInfoV2 getGroupOrPartialMigrate(
+ Connection connection, final GroupMasterKey groupMasterKey, final GroupIdV2 groupId
+ ) throws SQLException {
+ switch (getGroup(groupId)) {
+ case GroupInfoV1 groupInfoV1 -> {
+ // Received a v2 group message for a v1 group, we need to locally migrate the group
+ deleteGroup(connection, groupInfoV1.getGroupId());
+ final var groupInfoV2 = new GroupInfoV2(groupId, groupMasterKey, recipientResolver);
+ groupInfoV2.setBlocked(groupInfoV1.isBlocked());
+ updateGroup(connection, groupInfoV2);
+ logger.debug("Locally migrated group {} to group v2, id: {}",
+ groupInfoV1.getGroupId().toBase64(),
+ groupInfoV2.getGroupId().toBase64());
+ return groupInfoV2;
+ }
+ case GroupInfoV2 groupInfoV2 -> {
+ return groupInfoV2;
+ }
+ case null -> {
+ return new GroupInfoV2(groupId, groupMasterKey, recipientResolver);
+ }
+ }
+ }
+