From fece4dac9e6d54e7dd78be95972f105cc6fa2611 Mon Sep 17 00:00:00 2001 From: Ehren Kret Date: Thu, 28 Apr 2022 23:47:50 -0500 Subject: [PATCH] Add duration to boost badges Lets clients know how long the badge will last for after purchase. --- .../controllers/SubscriptionController.java | 11 +-- .../entities/PurchasableBadge.java | 74 +++++++++++++++++++ .../textsecuregcm/entities/SelfBadge.java | 3 +- 3 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 service/src/main/java/org/whispersystems/textsecuregcm/entities/PurchasableBadge.java diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/SubscriptionController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/SubscriptionController.java index 8a5543964..7e8cbd53f 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/SubscriptionController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/SubscriptionController.java @@ -81,6 +81,7 @@ import org.whispersystems.textsecuregcm.configuration.SubscriptionConfiguration; import org.whispersystems.textsecuregcm.configuration.SubscriptionLevelConfiguration; import org.whispersystems.textsecuregcm.configuration.SubscriptionPriceConfiguration; import org.whispersystems.textsecuregcm.entities.Badge; +import org.whispersystems.textsecuregcm.entities.PurchasableBadge; import org.whispersystems.textsecuregcm.metrics.UserAgentTagUtil; import org.whispersystems.textsecuregcm.storage.IssuedReceiptsManager; import org.whispersystems.textsecuregcm.storage.SubscriptionManager; @@ -428,15 +429,15 @@ public class SubscriptionController { public static class GetBoostBadgesResponse { public static class Level { - private final Badge badge; + private final PurchasableBadge badge; @JsonCreator public Level( - @JsonProperty("badge") Badge badge) { + @JsonProperty("badge") PurchasableBadge badge) { this.badge = badge; } - public Badge getBadge() { + public PurchasableBadge getBadge() { return badge; } } @@ -466,8 +467,8 @@ public class SubscriptionController { String giftBadge = giftConfiguration.badge(); List acceptableLanguages = getAcceptableLanguagesForRequest(containerRequestContext); GetBoostBadgesResponse getBoostBadgesResponse = new GetBoostBadgesResponse(Map.of( - boostLevel, new GetBoostBadgesResponse.Level(badgeTranslator.translate(acceptableLanguages, boostBadge)), - giftLevel, new GetBoostBadgesResponse.Level(badgeTranslator.translate(acceptableLanguages, giftBadge)))); + boostLevel, new GetBoostBadgesResponse.Level(new PurchasableBadge(badgeTranslator.translate(acceptableLanguages, boostBadge), boostConfiguration.getExpiration())), + giftLevel, new GetBoostBadgesResponse.Level(new PurchasableBadge(badgeTranslator.translate(acceptableLanguages, giftBadge), giftConfiguration.expiration())))); return Response.ok(getBoostBadgesResponse).build(); }); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/entities/PurchasableBadge.java b/service/src/main/java/org/whispersystems/textsecuregcm/entities/PurchasableBadge.java new file mode 100644 index 000000000..05aa14f3e --- /dev/null +++ b/service/src/main/java/org/whispersystems/textsecuregcm/entities/PurchasableBadge.java @@ -0,0 +1,74 @@ +/* + * Copyright 2022 Signal Messenger, LLC + * SPDX-License-Identifier: AGPL-3.0-only + */ + +package org.whispersystems.textsecuregcm.entities; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import java.time.Duration; +import java.util.List; +import java.util.Objects; + +public class PurchasableBadge extends Badge { + private final Duration duration; + + @JsonCreator + public PurchasableBadge( + @JsonProperty("id") final String id, + @JsonProperty("category") final String category, + @JsonProperty("name") final String name, + @JsonProperty("description") final String description, + @JsonProperty("sprites6") final List sprites6, + @JsonProperty("svg") final String svg, + @JsonProperty("svgs") final List svgs, + @JsonProperty("duration") final Duration duration) { + super(id, category, name, description, sprites6, svg, svgs); + this.duration = duration; + } + + public PurchasableBadge(final Badge badge, final Duration duration) { + super( + badge.getId(), + badge.getCategory(), + badge.getName(), + badge.getDescription(), + badge.getSprites6(), + badge.getSvg(), + badge.getSvgs()); + this.duration = duration; + } + + public Duration getDuration() { + return duration; + } + + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + if (!super.equals(o)) { + return false; + } + PurchasableBadge that = (PurchasableBadge) o; + return Objects.equals(duration, that.duration); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), duration); + } + + @Override + public String toString() { + return "PurchasableBadge{" + + "super=" + super.toString() + + ", duration=" + duration + + '}'; + } +} diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/entities/SelfBadge.java b/service/src/main/java/org/whispersystems/textsecuregcm/entities/SelfBadge.java index 83fb75be2..20593a645 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/entities/SelfBadge.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/entities/SelfBadge.java @@ -63,7 +63,8 @@ public class SelfBadge extends Badge { @Override public String toString() { return "SelfBadge{" + - "expiration=" + expiration + + "super=" + super.toString() + + ", expiration=" + expiration + ", visible=" + visible + '}'; }