diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java index 8d385a520..753d48778 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java @@ -268,23 +268,14 @@ public class AccountController { @Deprecated() // use whoami @Produces(MediaType.APPLICATION_JSON) public AccountIdentityResponse getMe(@ReadOnly @Auth AuthenticatedDevice auth) { - return buildAccountIdentityResponse(auth); + return AccountIdentityResponseBuilder.fromAccount(auth.getAccount()); } @GET @Path("/whoami") @Produces(MediaType.APPLICATION_JSON) public AccountIdentityResponse whoAmI(@ReadOnly @Auth AuthenticatedDevice auth) { - return buildAccountIdentityResponse(auth); - } - - private AccountIdentityResponse buildAccountIdentityResponse(AccountAndAuthenticatedDeviceHolder auth) { - return new AccountIdentityResponse(auth.getAccount().getUuid(), - auth.getAccount().getNumber(), - auth.getAccount().getPhoneNumberIdentifier(), - auth.getAccount().getUsernameHash().filter(h -> h.length > 0).orElse(null), - auth.getAccount().getUsernameLinkHandle(), - auth.getAccount().hasCapability(DeviceCapability.STORAGE)); + return AccountIdentityResponseBuilder.fromAccount(auth.getAccount()); } @DELETE diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2.java index f41698740..59260032e 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountControllerV2.java @@ -146,13 +146,7 @@ public class AccountControllerV2 { request.deviceMessages(), request.pniRegistrationIds()); - return new AccountIdentityResponse( - updatedAccount.getUuid(), - updatedAccount.getNumber(), - updatedAccount.getPhoneNumberIdentifier(), - updatedAccount.getUsernameHash().orElse(null), - updatedAccount.getUsernameLinkHandle(), - updatedAccount.hasCapability(DeviceCapability.STORAGE)); + return AccountIdentityResponseBuilder.fromAccount(updatedAccount); } catch (MismatchedDevicesException e) { throw new WebApplicationException(Response.status(409) .type(MediaType.APPLICATION_JSON_TYPE) @@ -205,13 +199,7 @@ public class AccountControllerV2 { request.deviceMessages(), request.pniRegistrationIds()); - return new AccountIdentityResponse( - updatedAccount.getUuid(), - updatedAccount.getNumber(), - updatedAccount.getPhoneNumberIdentifier(), - updatedAccount.getUsernameHash().orElse(null), - updatedAccount.getUsernameLinkHandle(), - updatedAccount.hasCapability(DeviceCapability.STORAGE)); + return AccountIdentityResponseBuilder.fromAccount(updatedAccount); } catch (MismatchedDevicesException e) { throw new WebApplicationException(Response.status(409) .type(MediaType.APPLICATION_JSON_TYPE) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountIdentityResponseBuilder.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountIdentityResponseBuilder.java new file mode 100644 index 000000000..9c38090f8 --- /dev/null +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountIdentityResponseBuilder.java @@ -0,0 +1,38 @@ +/* + * Copyright 2024 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ +package org.whispersystems.textsecuregcm.controllers; + +import org.whispersystems.textsecuregcm.entities.AccountIdentityResponse; +import org.whispersystems.textsecuregcm.storage.Account; +import org.whispersystems.textsecuregcm.storage.DeviceCapability; + +public class AccountIdentityResponseBuilder { + + private final Account account; + private boolean storageCapable; + + public AccountIdentityResponseBuilder(Account account) { + this.account = account; + this.storageCapable = account.hasCapability(DeviceCapability.STORAGE); + } + + public AccountIdentityResponseBuilder storageCapable(boolean storageCapable) { + this.storageCapable = storageCapable; + return this; + } + + public AccountIdentityResponse build() { + return new AccountIdentityResponse(account.getUuid(), + account.getNumber(), + account.getPhoneNumberIdentifier(), + account.getUsernameHash().filter(h -> h.length > 0).orElse(null), + account.getUsernameLinkHandle(), + storageCapable); + } + + public static AccountIdentityResponse fromAccount(final Account account) { + return new AccountIdentityResponseBuilder(account).build(); + } +} diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java index 79398b76f..8ace819c9 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/RegistrationController.java @@ -166,12 +166,12 @@ public class RegistrationController { Tag.of(VERIFICATION_TYPE_TAG_NAME, verificationType.name()))) .increment(); - return new AccountIdentityResponse(account.getUuid(), - account.getNumber(), - account.getPhoneNumberIdentifier(), - account.getUsernameHash().orElse(null), - account.getUsernameLinkHandle(), - existingAccount.map(a -> a.hasCapability(DeviceCapability.STORAGE)).orElse(false)); + return new AccountIdentityResponseBuilder(account) + // If there was an existing account, return whether it could have had something in the storage service + .storageCapable(existingAccount + .map(a -> a.hasCapability(DeviceCapability.STORAGE)) + .orElse(false)) + .build(); } }