Skip to content

Commit

Permalink
Convert AccountBadge to a record
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-signal committed Oct 17, 2024
1 parent c2270e5 commit 865e3c5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,22 @@ public List<Badge> convert(
final ResourceBundle resourceBundle = headerControlledResourceBundleLookup.getResourceBundle(BASE_NAME,
acceptableLanguages);
List<Badge> 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 -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public record BadgeDataReport(String id,
boolean visible) {

public BadgeDataReport(AccountBadge badge) {
this(badge.getId(), badge.getExpiration(), badge.isVisible());
this(badge.id(), badge.expiration(), badge.visible());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public void addBadge(final Clock clock, final AccountBadge badge) {
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--;
Expand All @@ -399,14 +399,14 @@ public void makeBadgePrimaryIfExists(final Clock clock, final String badgeId) {
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;
}
Expand All @@ -422,13 +422,13 @@ public void makeBadgePrimaryIfExists(final Clock clock, final String badgeId) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -62,43 +45,4 @@ public AccountBadge withVisibility(boolean visible) {
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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static List<AccountBadge> mergeBadgeIdsWithExistingAccountBadges(
final List<AccountBadge> accountBadges) {
LinkedHashMap<String, AccountBadge> existingBadges = new LinkedHashMap<>(accountBadges.size());
for (final AccountBadge accountBadge : accountBadges) {
existingBadges.putIfAbsent(accountBadge.getId(), accountBadge);
existingBadges.putIfAbsent(accountBadge.id(), accountBadge);
}

LinkedHashMap<String, AccountBadge> result = new LinkedHashMap<>(accountBadges.size());
Expand Down Expand Up @@ -67,7 +67,7 @@ public static List<AccountBadge> mergeBadgeIdsWithExistingAccountBadges(
for (final Map.Entry<String, AccountBadge> entry : existingBadges.entrySet()) {
if (!result.containsKey(entry.getKey())) {
AccountBadge accountBadge = entry.getValue().withVisibility(false);
result.put(accountBadge.getId(), accountBadge);
result.put(accountBadge.id(), accountBadge);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,17 @@ void addAndRemoveBadges() {
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();
});
}

Expand Down

0 comments on commit 865e3c5

Please sign in to comment.