From a8da0f64ac527503e2a3c2806465bdea43e32074 Mon Sep 17 00:00:00 2001 From: Jon Chambers Date: Wed, 30 Oct 2024 16:22:13 -0400 Subject: [PATCH] Extract device capability enumeration translation to a utility class --- .../grpc/DeviceCapabilityUtil.java | 36 +++++++++++++++++++ .../grpc/DevicesGrpcService.java | 8 +---- .../textsecuregcm/grpc/ProfileGrpcHelper.java | 8 +---- 3 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 service/src/main/java/org/whispersystems/textsecuregcm/grpc/DeviceCapabilityUtil.java diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/DeviceCapabilityUtil.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/DeviceCapabilityUtil.java new file mode 100644 index 000000000..269806b04 --- /dev/null +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/DeviceCapabilityUtil.java @@ -0,0 +1,36 @@ +/* + * Copyright 2024 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.whispersystems.textsecuregcm.grpc; + +import io.grpc.Status; +import org.whispersystems.textsecuregcm.storage.DeviceCapability; + +public class DeviceCapabilityUtil { + + private DeviceCapabilityUtil() { + } + + public static DeviceCapability fromGrpcDeviceCapability(final org.signal.chat.common.DeviceCapability grpcDeviceCapability) { + return switch (grpcDeviceCapability) { + case DEVICE_CAPABILITY_STORAGE -> DeviceCapability.STORAGE; + case DEVICE_CAPABILITY_TRANSFER -> DeviceCapability.TRANSFER; + case DEVICE_CAPABILITY_DELETE_SYNC -> DeviceCapability.DELETE_SYNC; + case DEVICE_CAPABILITY_VERSIONED_EXPIRATION_TIMER -> DeviceCapability.VERSIONED_EXPIRATION_TIMER; + case DEVICE_CAPABILITY_STORAGE_SERVICE_RECORD_KEY_ROTATION -> DeviceCapability.STORAGE_SERVICE_RECORD_KEY_ROTATION; + case DEVICE_CAPABILITY_UNSPECIFIED, UNRECOGNIZED -> throw Status.INVALID_ARGUMENT.withDescription("Unrecognized device capability").asRuntimeException(); + }; + } + + public static org.signal.chat.common.DeviceCapability toGrpcDeviceCapability(final DeviceCapability deviceCapability) { + return switch (deviceCapability) { + case STORAGE -> org.signal.chat.common.DeviceCapability.DEVICE_CAPABILITY_STORAGE; + case TRANSFER -> org.signal.chat.common.DeviceCapability.DEVICE_CAPABILITY_TRANSFER; + case DELETE_SYNC -> org.signal.chat.common.DeviceCapability.DEVICE_CAPABILITY_DELETE_SYNC; + case VERSIONED_EXPIRATION_TIMER -> org.signal.chat.common.DeviceCapability.DEVICE_CAPABILITY_VERSIONED_EXPIRATION_TIMER; + case STORAGE_SERVICE_RECORD_KEY_ROTATION -> org.signal.chat.common.DeviceCapability.DEVICE_CAPABILITY_STORAGE_SERVICE_RECORD_KEY_ROTATION; + }; + } +} diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcService.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcService.java index 026ca493f..47f51b7ed 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcService.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/DevicesGrpcService.java @@ -204,13 +204,7 @@ public class DevicesGrpcService extends ReactorDevicesGrpc.DevicesImplBase { final AuthenticatedDevice authenticatedDevice = AuthenticationUtil.requireAuthenticatedDevice(); final Set capabilities = request.getCapabilitiesList().stream() - .map(capability -> switch (capability) { - case DEVICE_CAPABILITY_STORAGE -> DeviceCapability.STORAGE; - case DEVICE_CAPABILITY_TRANSFER -> DeviceCapability.TRANSFER; - case DEVICE_CAPABILITY_DELETE_SYNC -> DeviceCapability.DELETE_SYNC; - case DEVICE_CAPABILITY_VERSIONED_EXPIRATION_TIMER -> DeviceCapability.VERSIONED_EXPIRATION_TIMER; - default -> throw Status.INVALID_ARGUMENT.withDescription("Unrecognized device capability").asRuntimeException(); - }) + .map(DeviceCapabilityUtil::fromGrpcDeviceCapability) .collect(Collectors.toSet()); return Mono.fromFuture(() -> accountsManager.getByAccountIdentifierAsync(authenticatedDevice.accountIdentifier())) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcHelper.java b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcHelper.java index 3910d7849..24fb5f487 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcHelper.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/grpc/ProfileGrpcHelper.java @@ -87,13 +87,7 @@ public class ProfileGrpcHelper { Arrays.stream(DeviceCapability.values()) .filter(DeviceCapability::includeInProfile) .filter(account::hasCapability) - .map(capability -> switch (capability) { - case STORAGE -> org.signal.chat.common.DeviceCapability.DEVICE_CAPABILITY_STORAGE; - case TRANSFER -> org.signal.chat.common.DeviceCapability.DEVICE_CAPABILITY_TRANSFER; - case DELETE_SYNC -> org.signal.chat.common.DeviceCapability.DEVICE_CAPABILITY_DELETE_SYNC; - case VERSIONED_EXPIRATION_TIMER -> org.signal.chat.common.DeviceCapability.DEVICE_CAPABILITY_VERSIONED_EXPIRATION_TIMER; - case STORAGE_SERVICE_RECORD_KEY_ROTATION -> org.signal.chat.common.DeviceCapability.DEVICE_CAPABILITY_STORAGE_SERVICE_RECORD_KEY_ROTATION; - }) + .map(DeviceCapabilityUtil::toGrpcDeviceCapability) .forEach(capabilitiesBuilder::addCapabilities); return capabilitiesBuilder.build();