Creates a storage object for badges

This commit is contained in:
Ehren Kret 2021-09-03 15:29:40 -05:00
parent fc10108788
commit 84b3d324bb
2 changed files with 91 additions and 0 deletions

View File

@ -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<AccountBadge> badges = new HashSet<>();
@JsonProperty
private String registrationLock;
@ -311,6 +317,32 @@ public class Account {
this.avatar = avatar;
}
public Set<AccountBadge> 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());

View File

@ -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 +
'}';
}
}