Add utility method for creating AccountIdentityResponse
This commit is contained in:
parent
5a35d69ed0
commit
d5b39cd496
|
@ -268,23 +268,14 @@ public class AccountController {
|
||||||
@Deprecated() // use whoami
|
@Deprecated() // use whoami
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public AccountIdentityResponse getMe(@ReadOnly @Auth AuthenticatedDevice auth) {
|
public AccountIdentityResponse getMe(@ReadOnly @Auth AuthenticatedDevice auth) {
|
||||||
return buildAccountIdentityResponse(auth);
|
return AccountIdentityResponseBuilder.fromAccount(auth.getAccount());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/whoami")
|
@Path("/whoami")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public AccountIdentityResponse whoAmI(@ReadOnly @Auth AuthenticatedDevice auth) {
|
public AccountIdentityResponse whoAmI(@ReadOnly @Auth AuthenticatedDevice auth) {
|
||||||
return buildAccountIdentityResponse(auth);
|
return AccountIdentityResponseBuilder.fromAccount(auth.getAccount());
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@DELETE
|
@DELETE
|
||||||
|
|
|
@ -146,13 +146,7 @@ public class AccountControllerV2 {
|
||||||
request.deviceMessages(),
|
request.deviceMessages(),
|
||||||
request.pniRegistrationIds());
|
request.pniRegistrationIds());
|
||||||
|
|
||||||
return new AccountIdentityResponse(
|
return AccountIdentityResponseBuilder.fromAccount(updatedAccount);
|
||||||
updatedAccount.getUuid(),
|
|
||||||
updatedAccount.getNumber(),
|
|
||||||
updatedAccount.getPhoneNumberIdentifier(),
|
|
||||||
updatedAccount.getUsernameHash().orElse(null),
|
|
||||||
updatedAccount.getUsernameLinkHandle(),
|
|
||||||
updatedAccount.hasCapability(DeviceCapability.STORAGE));
|
|
||||||
} catch (MismatchedDevicesException e) {
|
} catch (MismatchedDevicesException e) {
|
||||||
throw new WebApplicationException(Response.status(409)
|
throw new WebApplicationException(Response.status(409)
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
|
@ -205,13 +199,7 @@ public class AccountControllerV2 {
|
||||||
request.deviceMessages(),
|
request.deviceMessages(),
|
||||||
request.pniRegistrationIds());
|
request.pniRegistrationIds());
|
||||||
|
|
||||||
return new AccountIdentityResponse(
|
return AccountIdentityResponseBuilder.fromAccount(updatedAccount);
|
||||||
updatedAccount.getUuid(),
|
|
||||||
updatedAccount.getNumber(),
|
|
||||||
updatedAccount.getPhoneNumberIdentifier(),
|
|
||||||
updatedAccount.getUsernameHash().orElse(null),
|
|
||||||
updatedAccount.getUsernameLinkHandle(),
|
|
||||||
updatedAccount.hasCapability(DeviceCapability.STORAGE));
|
|
||||||
} catch (MismatchedDevicesException e) {
|
} catch (MismatchedDevicesException e) {
|
||||||
throw new WebApplicationException(Response.status(409)
|
throw new WebApplicationException(Response.status(409)
|
||||||
.type(MediaType.APPLICATION_JSON_TYPE)
|
.type(MediaType.APPLICATION_JSON_TYPE)
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -166,12 +166,12 @@ public class RegistrationController {
|
||||||
Tag.of(VERIFICATION_TYPE_TAG_NAME, verificationType.name())))
|
Tag.of(VERIFICATION_TYPE_TAG_NAME, verificationType.name())))
|
||||||
.increment();
|
.increment();
|
||||||
|
|
||||||
return new AccountIdentityResponse(account.getUuid(),
|
return new AccountIdentityResponseBuilder(account)
|
||||||
account.getNumber(),
|
// If there was an existing account, return whether it could have had something in the storage service
|
||||||
account.getPhoneNumberIdentifier(),
|
.storageCapable(existingAccount
|
||||||
account.getUsernameHash().orElse(null),
|
.map(a -> a.hasCapability(DeviceCapability.STORAGE))
|
||||||
account.getUsernameLinkHandle(),
|
.orElse(false))
|
||||||
existingAccount.map(a -> a.hasCapability(DeviceCapability.STORAGE)).orElse(false));
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue