From 865e3c5bdedee1ce7d085b2d42bdb3c42f954361 Mon Sep 17 00:00:00 2001 From: Jon Chambers Date: Wed, 16 Oct 2024 12:45:00 -0400 Subject: [PATCH] Convert `AccountBadge` to a record --- .../ConfiguredProfileBadgeConverter.java | 18 +++--- .../entities/AccountDataReportResponse.java | 2 +- .../textsecuregcm/storage/Account.java | 10 ++-- .../textsecuregcm/storage/AccountBadge.java | 60 +------------------ .../textsecuregcm/util/ProfileHelper.java | 4 +- .../textsecuregcm/storage/AccountTest.java | 12 ++-- 6 files changed, 25 insertions(+), 81 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/badges/ConfiguredProfileBadgeConverter.java b/service/src/main/java/org/whispersystems/textsecuregcm/badges/ConfiguredProfileBadgeConverter.java index 40a9f2dcc..820d76aaf 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/badges/ConfiguredProfileBadgeConverter.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/badges/ConfiguredProfileBadgeConverter.java @@ -76,22 +76,22 @@ public class ConfiguredProfileBadgeConverter implements ProfileBadgeConverter, B final ResourceBundle resourceBundle = headerControlledResourceBundleLookup.getResourceBundle(BASE_NAME, acceptableLanguages); List badges = accountBadges.stream() - .filter(accountBadge -> (isSelf || accountBadge.isVisible()) - && now.isBefore(accountBadge.getExpiration()) - && knownBadges.containsKey(accountBadge.getId())) + .filter(accountBadge -> (isSelf || accountBadge.visible()) + && now.isBefore(accountBadge.expiration()) + && knownBadges.containsKey(accountBadge.id())) .map(accountBadge -> { - BadgeConfiguration configuration = knownBadges.get(accountBadge.getId()); + BadgeConfiguration configuration = knownBadges.get(accountBadge.id()); return newBadge( isSelf, - accountBadge.getId(), + accountBadge.id(), configuration.getCategory(), - resourceBundle.getString(accountBadge.getId() + "_name"), - resourceBundle.getString(accountBadge.getId() + "_description"), + resourceBundle.getString(accountBadge.id() + "_name"), + resourceBundle.getString(accountBadge.id() + "_description"), configuration.getSprites(), configuration.getSvg(), configuration.getSvgs(), - accountBadge.getExpiration(), - accountBadge.isVisible()); + accountBadge.expiration(), + accountBadge.visible()); }) .collect(Collectors.toCollection(ArrayList::new)); badges.addAll(badgeIdsEnabledForAll.stream().filter(knownBadges::containsKey).map(id -> { diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/entities/AccountDataReportResponse.java b/service/src/main/java/org/whispersystems/textsecuregcm/entities/AccountDataReportResponse.java index 6d799ed4f..19e24cff3 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/entities/AccountDataReportResponse.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/entities/AccountDataReportResponse.java @@ -114,7 +114,7 @@ public record AccountDataReportResponse(UUID reportId, boolean visible) { public BadgeDataReport(AccountBadge badge) { - this(badge.getId(), badge.getExpiration(), badge.isVisible()); + this(badge.id(), badge.expiration(), badge.visible()); } } 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 7d42d5b7c..c181342f8 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/Account.java @@ -377,7 +377,7 @@ public class Account { boolean added = false; for (int i = 0; i < badges.size(); i++) { final AccountBadge badgeInList = badges.get(i); - if (Objects.equals(badgeInList.getId(), badge.getId())) { + if (Objects.equals(badgeInList.id(), badge.id())) { if (added) { badges.remove(i); i--; @@ -399,14 +399,14 @@ public class Account { requireNotStale(); // early exit if it's already the first item in the list - if (!badges.isEmpty() && Objects.equals(badges.get(0).getId(), badgeId)) { + if (!badges.isEmpty() && Objects.equals(badges.get(0).id(), badgeId)) { purgeStaleBadges(clock); return; } int indexOfBadge = -1; for (int i = 1; i < badges.size(); i++) { - if (Objects.equals(badgeId, badges.get(i).getId())) { + if (Objects.equals(badgeId, badges.get(i).id())) { indexOfBadge = i; break; } @@ -422,13 +422,13 @@ public class Account { public void removeBadge(final Clock clock, final String id) { requireNotStale(); - badges.removeIf(accountBadge -> Objects.equals(accountBadge.getId(), id)); + badges.removeIf(accountBadge -> Objects.equals(accountBadge.id(), id)); purgeStaleBadges(clock); } private void purgeStaleBadges(final Clock clock) { final Instant now = clock.instant(); - badges.removeIf(accountBadge -> now.isAfter(accountBadge.getExpiration())); + badges.removeIf(accountBadge -> now.isAfter(accountBadge.expiration())); } public void setRegistrationLockFromAttributes(final AccountAttributes attributes) { diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountBadge.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountBadge.java index c058ed742..e2f00d142 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountBadge.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountBadge.java @@ -5,31 +5,14 @@ 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 id; - private final Instant expiration; - private final boolean visible; - - @JsonCreator - public AccountBadge( - @JsonProperty("id") String id, - @JsonProperty("expiration") Instant expiration, - @JsonProperty("visible") boolean visible) { - this.id = id; - this.expiration = expiration; - this.visible = visible; - } +public record AccountBadge(String id, Instant expiration, boolean visible) { /** * Returns a new AccountBadge that is a merging of the two originals. IDs must match for this operation to make sense. - * The expiration will be the later of the two. - * Visibility will be set if either of the passed in objects is visible. + * The expiration will be the later of the two. Visibility will be set if either of the passed in objects is visible. */ public AccountBadge mergeWith(AccountBadge other) { if (!Objects.equals(other.id, id)) { @@ -62,43 +45,4 @@ public class AccountBadge { visible); } } - - public String getId() { - return id; - } - - public Instant getExpiration() { - return expiration; - } - - public boolean isVisible() { - return visible; - } - - @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 visible == that.visible && Objects.equals(id, that.id) - && Objects.equals(expiration, that.expiration); - } - - @Override - public int hashCode() { - return Objects.hash(id, expiration, visible); - } - - @Override - public String toString() { - return "AccountBadge{" + - "id='" + id + '\'' + - ", expiration=" + expiration + - ", visible=" + visible + - '}'; - } } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/util/ProfileHelper.java b/service/src/main/java/org/whispersystems/textsecuregcm/util/ProfileHelper.java index 0c4a81497..cc6cf3704 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/util/ProfileHelper.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/util/ProfileHelper.java @@ -37,7 +37,7 @@ public class ProfileHelper { final List accountBadges) { LinkedHashMap existingBadges = new LinkedHashMap<>(accountBadges.size()); for (final AccountBadge accountBadge : accountBadges) { - existingBadges.putIfAbsent(accountBadge.getId(), accountBadge); + existingBadges.putIfAbsent(accountBadge.id(), accountBadge); } LinkedHashMap result = new LinkedHashMap<>(accountBadges.size()); @@ -67,7 +67,7 @@ public class ProfileHelper { for (final Map.Entry entry : existingBadges.entrySet()) { if (!result.containsKey(entry.getKey())) { AccountBadge accountBadge = entry.getValue().withVisibility(false); - result.put(accountBadge.getId(), accountBadge); + result.put(accountBadge.id(), accountBadge); } } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java index 1263f8090..7403c4ff7 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountTest.java @@ -233,17 +233,17 @@ class AccountTest { account.addBadge(clock, new AccountBadge("foo", Instant.ofEpochSecond(50), false)); assertThat(account.getBadges()).hasSize(2).element(0).satisfies(badge -> { - assertThat(badge.getId()).isEqualTo("foo"); - assertThat(badge.getExpiration().getEpochSecond()).isEqualTo(50); - assertThat(badge.isVisible()).isFalse(); + assertThat(badge.id()).isEqualTo("foo"); + assertThat(badge.expiration().getEpochSecond()).isEqualTo(50); + assertThat(badge.visible()).isFalse(); }); account.addBadge(clock, new AccountBadge("foo", Instant.ofEpochSecond(51), true)); assertThat(account.getBadges()).hasSize(2).element(0).satisfies(badge -> { - assertThat(badge.getId()).isEqualTo("foo"); - assertThat(badge.getExpiration().getEpochSecond()).isEqualTo(51); - assertThat(badge.isVisible()).isTrue(); + assertThat(badge.id()).isEqualTo("foo"); + assertThat(badge.expiration().getEpochSecond()).isEqualTo(51); + assertThat(badge.visible()).isTrue(); }); }