Add current entitlements to whoami response

This commit is contained in:
Ravi Khadiwala
2024-12-11 11:51:38 -06:00
committed by ravi-signal
parent d5b39cd496
commit 33c0a27b85
6 changed files with 165 additions and 3 deletions

View File

@@ -4,7 +4,11 @@
*/
package org.whispersystems.textsecuregcm.controllers;
import java.time.Clock;
import java.util.List;
import java.util.Optional;
import org.whispersystems.textsecuregcm.entities.AccountIdentityResponse;
import org.whispersystems.textsecuregcm.entities.Entitlements;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.storage.DeviceCapability;
@@ -12,10 +16,12 @@ public class AccountIdentityResponseBuilder {
private final Account account;
private boolean storageCapable;
private Clock clock;
public AccountIdentityResponseBuilder(Account account) {
this.account = account;
this.storageCapable = account.hasCapability(DeviceCapability.STORAGE);
this.clock = Clock.systemUTC();
}
public AccountIdentityResponseBuilder storageCapable(boolean storageCapable) {
@@ -23,13 +29,31 @@ public class AccountIdentityResponseBuilder {
return this;
}
public AccountIdentityResponseBuilder clock(Clock clock) {
this.clock = clock;
return this;
}
public AccountIdentityResponse build() {
final List<Entitlements.BadgeEntitlement> badges = account.getBadges()
.stream()
.filter(bv -> bv.expiration().isAfter(clock.instant()))
.map(badge -> new Entitlements.BadgeEntitlement(badge.id(), badge.expiration(), badge.visible()))
.toList();
final Entitlements.BackupEntitlement backupEntitlement = Optional
.ofNullable(account.getBackupVoucher())
.filter(bv -> bv.expiration().isAfter(clock.instant()))
.map(bv -> new Entitlements.BackupEntitlement(bv.receiptLevel(), bv.expiration()))
.orElse(null);
return new AccountIdentityResponse(account.getUuid(),
account.getNumber(),
account.getPhoneNumberIdentifier(),
account.getUsernameHash().filter(h -> h.length > 0).orElse(null),
account.getUsernameLinkHandle(),
storageCapable);
storageCapable,
new Entitlements(badges, backupEntitlement));
}
public static AccountIdentityResponse fromAccount(final Account account) {