Creates a storage object for badges
This commit is contained in:
parent
fc10108788
commit
84b3d324bb
|
@ -8,7 +8,10 @@ package org.whispersystems.textsecuregcm.storage;
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import java.time.Clock;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
@ -45,6 +48,9 @@ public class Account {
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
private String avatar;
|
private String avatar;
|
||||||
|
|
||||||
|
@JsonProperty
|
||||||
|
private Set<AccountBadge> badges = new HashSet<>();
|
||||||
|
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
private String registrationLock;
|
private String registrationLock;
|
||||||
|
|
||||||
|
@ -311,6 +317,32 @@ public class Account {
|
||||||
this.avatar = avatar;
|
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) {
|
public void setRegistrationLockFromAttributes(final AccountAttributes attributes) {
|
||||||
if (!Util.isEmpty(attributes.getRegistrationLock())) {
|
if (!Util.isEmpty(attributes.getRegistrationLock())) {
|
||||||
AuthenticationCredentials credentials = new AuthenticationCredentials(attributes.getRegistrationLock());
|
AuthenticationCredentials credentials = new AuthenticationCredentials(attributes.getRegistrationLock());
|
||||||
|
|
|
@ -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 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue