Add utility method for creating AccountIdentityResponse

This commit is contained in:
Ravi Khadiwala 2024-12-11 14:20:52 -06:00 committed by ravi-signal
parent 5a35d69ed0
commit d5b39cd496
4 changed files with 48 additions and 31 deletions

View File

@ -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

View File

@ -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)

View File

@ -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();
}
}

View File

@ -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();
}
}