diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java index de3c12c05..3b7b8d41a 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java @@ -8,7 +8,10 @@ package org.whispersystems.textsecuregcm.storage; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.annotations.VisibleForTesting; +import java.time.Clock; +import java.time.Instant; import java.util.HashSet; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -45,6 +48,9 @@ public class Account { @JsonProperty private String avatar; + @JsonProperty + private Set badges = new HashSet<>(); + @JsonProperty private String registrationLock; @@ -311,6 +317,32 @@ public class Account { this.avatar = avatar; } + public Set getBadges() { + requireNotStale(); + + return badges; + } + + public void addBadge(AccountBadge badge) { + requireNotStale(); + + badges.add(badge); + purgeStaleBadges(); + } + + public void removeBadge(String name) { + requireNotStale(); + + badges.removeIf(accountBadge -> Objects.equals(accountBadge.getName(), name)); + purgeStaleBadges(); + } + + private void purgeStaleBadges() { + final Instant now = Clock.systemUTC().instant(); + + badges.removeIf(accountBadge -> now.isAfter(accountBadge.getExpiration())); + } + public void setRegistrationLockFromAttributes(final AccountAttributes attributes) { if (!Util.isEmpty(attributes.getRegistrationLock())) { AuthenticationCredentials credentials = new AuthenticationCredentials(attributes.getRegistrationLock()); diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountBadge.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountBadge.java new file mode 100644 index 000000000..08d696ce8 --- /dev/null +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountBadge.java @@ -0,0 +1,59 @@ +/* + * Copyright 2021 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.whispersystems.textsecuregcm.storage; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import java.time.Instant; +import java.util.Objects; + +public class AccountBadge { + + private final String name; + private final Instant expiration; + + @JsonCreator + public AccountBadge( + @JsonProperty("name") String name, + @JsonProperty("expiration") Instant expiration) { + this.name = name; + this.expiration = expiration; + } + + public String getName() { + return name; + } + + public Instant getExpiration() { + return expiration; + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + AccountBadge that = (AccountBadge) o; + return Objects.equals(name, that.name) + && Objects.equals(expiration, that.expiration); + } + + @Override + public int hashCode() { + return Objects.hash(name, expiration); + } + + @Override + public String toString() { + return "AccountBadge{" + + "name='" + name + '\'' + + ", expiration=" + expiration + + '}'; + } +}