Skip to content

Commit

Permalink
Add utility method for creating AccountIdentityResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
ravi-signal committed Dec 12, 2024
1 parent 5a35d69 commit d5b39cd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,23 +268,14 @@ public void setAccountAttributes(
@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,7 @@ public AccountIdentityResponse changeNumber(@Mutable @Auth final AuthenticatedDe
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)
Expand Down Expand Up @@ -205,13 +199,7 @@ public AccountIdentityResponse distributePhoneNumberIdentityKeys(
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ public AccountIdentityResponse register(
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();
}

}

0 comments on commit d5b39cd

Please sign in to comment.