From 7864405efd85c6203ff7a6437047052512067ebe Mon Sep 17 00:00:00 2001 From: Ehren Kret Date: Mon, 27 Sep 2021 16:50:18 -0500 Subject: [PATCH] Remove single URL in favor of density based sprite sheets --- .../ConfiguredProfileBadgeConverter.java | 26 ++++++-- .../configuration/BadgeConfiguration.java | 57 ++++++++++++----- .../textsecuregcm/entities/Badge.java | 62 +++++++++++++++---- .../textsecuregcm/entities/SelfBadge.java | 16 ++--- .../ConfiguredProfileBadgeConverterTest.java | 18 ++---- .../controllers/ProfileControllerTest.java | 20 ++---- 6 files changed, 130 insertions(+), 69 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 d252829e1..52be05e45 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/badges/ConfiguredProfileBadgeConverter.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/badges/ConfiguredProfileBadgeConverter.java @@ -6,7 +6,6 @@ package org.whispersystems.textsecuregcm.badges; import com.google.common.annotations.VisibleForTesting; -import java.net.URL; import java.time.Clock; import java.time.Duration; import java.time.Instant; @@ -103,9 +102,14 @@ public class ConfiguredProfileBadgeConverter implements ProfileBadgeConverter { isSelf, accountBadge.getId(), configuration.getCategory(), - configuration.getImageUrl(), resourceBundle.getString(accountBadge.getId() + "_name"), resourceBundle.getString(accountBadge.getId() + "_description"), + configuration.getLdpi(), + configuration.getMdpi(), + configuration.getHdpi(), + configuration.getXhdpi(), + configuration.getXxhdpi(), + configuration.getXxxhdpi(), accountBadge.getExpiration(), accountBadge.isVisible()); }) @@ -116,9 +120,14 @@ public class ConfiguredProfileBadgeConverter implements ProfileBadgeConverter { isSelf, id, configuration.getCategory(), - configuration.getImageUrl(), resourceBundle.getString(id + "_name"), resourceBundle.getString(id + "_description"), + configuration.getLdpi(), + configuration.getMdpi(), + configuration.getHdpi(), + configuration.getXhdpi(), + configuration.getXxhdpi(), + configuration.getXxxhdpi(), now.plus(Duration.ofDays(1)), true); }).collect(Collectors.toList())); @@ -129,15 +138,20 @@ public class ConfiguredProfileBadgeConverter implements ProfileBadgeConverter { final boolean isSelf, final String id, final String category, - final URL imageUrl, final String name, final String description, + final String ldpi, + final String mdpi, + final String hdpi, + final String xhdpi, + final String xxhdpi, + final String xxxhdpi, final Instant expiration, final boolean visible) { if (isSelf) { - return new SelfBadge(id, category, imageUrl, name, description, expiration, visible); + return new SelfBadge(id, category, name, description, ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi, expiration, visible); } else { - return new Badge(id, category, imageUrl, name, description); + return new Badge(id, category, name, description, ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi); } } } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/configuration/BadgeConfiguration.java b/service/src/main/java/org/whispersystems/textsecuregcm/configuration/BadgeConfiguration.java index 54b8067b5..234277cc6 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/configuration/BadgeConfiguration.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/configuration/BadgeConfiguration.java @@ -7,27 +7,38 @@ package org.whispersystems.textsecuregcm.configuration; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; -import java.net.URL; import javax.validation.constraints.NotEmpty; -import javax.validation.constraints.NotNull; public class BadgeConfiguration { public static final String CATEGORY_TESTING = "testing"; private final String id; - private final URL imageUrl; private final String category; + private final String ldpi; + private final String mdpi; + private final String hdpi; + private final String xhdpi; + private final String xxhdpi; + private final String xxxhdpi; @JsonCreator public BadgeConfiguration( @JsonProperty("id") final String id, - @JsonProperty("imageUrl") @JsonDeserialize(converter = URLDeserializationConverter.class) final URL imageUrl, - @JsonProperty("category") final String category) { + @JsonProperty("category") final String category, + @JsonProperty("ldpi") final String ldpi, + @JsonProperty("mdpi") final String mdpi, + @JsonProperty("hdpi") final String hdpi, + @JsonProperty("xhdpi") final String xhdpi, + @JsonProperty("xxhdpi") final String xxhdpi, + @JsonProperty("xxxhdpi") final String xxxhdpi) { this.id = id; - this.imageUrl = imageUrl; this.category = category; + this.ldpi = ldpi; + this.mdpi = mdpi; + this.hdpi = hdpi; + this.xhdpi = xhdpi; + this.xxhdpi = xxhdpi; + this.xxxhdpi = xxxhdpi; } @NotEmpty @@ -35,17 +46,35 @@ public class BadgeConfiguration { return id; } - @NotNull - @JsonSerialize(converter = URLSerializationConverter.class) - public URL getImageUrl() { - return imageUrl; - } - @NotEmpty public String getCategory() { return category; } + public String getLdpi() { + return ldpi; + } + + public String getMdpi() { + return mdpi; + } + + public String getHdpi() { + return hdpi; + } + + public String getXhdpi() { + return xhdpi; + } + + public String getXxhdpi() { + return xxhdpi; + } + + public String getXxxhdpi() { + return xxxhdpi; + } + public boolean isTestBadge() { return CATEGORY_TESTING.equals(category); } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/entities/Badge.java b/service/src/main/java/org/whispersystems/textsecuregcm/entities/Badge.java index c7c9d213a..f294c0e78 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/entities/Badge.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/entities/Badge.java @@ -7,28 +7,42 @@ package org.whispersystems.textsecuregcm.entities; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import java.net.URL; import java.util.Objects; public class Badge { private final String id; private final String category; - private final URL imageUrl; private final String name; private final String description; + private final String ldpi; + private final String mdpi; + private final String hdpi; + private final String xhdpi; + private final String xxhdpi; + private final String xxxhdpi; @JsonCreator public Badge( @JsonProperty("id") final String id, @JsonProperty("category") final String category, - @JsonProperty("imageUrl") final URL imageUrl, @JsonProperty("name") final String name, - @JsonProperty("description") final String description) { + @JsonProperty("description") final String description, + @JsonProperty("ldpi") final String ldpi, + @JsonProperty("mdpi") final String mdpi, + @JsonProperty("hdpi") final String hdpi, + @JsonProperty("xhdpi") final String xhdpi, + @JsonProperty("xxhdpi") final String xxhdpi, + @JsonProperty("xxxhdpi") final String xxxhdpi) { this.id = id; this.category = category; - this.imageUrl = imageUrl; this.name = name; this.description = description; + this.ldpi = ldpi; + this.mdpi = mdpi; + this.hdpi = hdpi; + this.xhdpi = xhdpi; + this.xxhdpi = xxhdpi; + this.xxxhdpi = xxxhdpi; } public String getId() { @@ -39,10 +53,6 @@ public class Badge { return category; } - public URL getImageUrl() { - return imageUrl; - } - public String getName() { return name; } @@ -51,6 +61,30 @@ public class Badge { return description; } + public String getLdpi() { + return ldpi; + } + + public String getMdpi() { + return mdpi; + } + + public String getHdpi() { + return hdpi; + } + + public String getXhdpi() { + return xhdpi; + } + + public String getXxhdpi() { + return xxhdpi; + } + + public String getXxxhdpi() { + return xxxhdpi; + } + @Override public boolean equals(final Object o) { if (this == o) { @@ -61,13 +95,15 @@ public class Badge { } Badge badge = (Badge) o; return Objects.equals(id, badge.id) && Objects.equals(category, - badge.category) && Objects.equals(imageUrl, badge.imageUrl) - && Objects.equals(name, badge.name) && Objects.equals(description, - badge.description); + badge.category) && Objects.equals(name, badge.name) && Objects.equals( + description, badge.description) && Objects.equals(ldpi, badge.ldpi) + && Objects.equals(mdpi, badge.mdpi) && Objects.equals(hdpi, badge.hdpi) + && Objects.equals(xhdpi, badge.xhdpi) && Objects.equals(xxhdpi, + badge.xxhdpi) && Objects.equals(xxxhdpi, badge.xxxhdpi); } @Override public int hashCode() { - return Objects.hash(id, category, imageUrl, name, description); + return Objects.hash(id, category, name, description, ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi); } } 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 00778663b..8503ab89e 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/entities/SelfBadge.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/entities/SelfBadge.java @@ -5,9 +5,7 @@ package org.whispersystems.textsecuregcm.entities; -import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import java.net.URL; import java.time.Instant; import java.util.Objects; @@ -18,16 +16,20 @@ public class SelfBadge extends Badge { private final Instant expiration; private final boolean visible; - @JsonCreator public SelfBadge( @JsonProperty("id") final String id, @JsonProperty("category") final String category, - @JsonProperty("imageUrl") final URL imageUrl, @JsonProperty("name") final String name, @JsonProperty("description") final String description, - @JsonProperty("expiration") final Instant expiration, - @JsonProperty("visible") final boolean visible) { - super(id, category, imageUrl, name, description); + @JsonProperty("ldpi") final String ldpi, + @JsonProperty("mdpi") final String mdpi, + @JsonProperty("hdpi") final String hdpi, + @JsonProperty("xhdpi") final String xhdpi, + @JsonProperty("xxhdpi") final String xxhdpi, + @JsonProperty("xxxhdpi") final String xxxhdpi, + final Instant expiration, + final boolean visible) { + super(id, category, name, description, ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi); this.expiration = expiration; this.visible = visible; } diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/badges/ConfiguredProfileBadgeConverterTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/badges/ConfiguredProfileBadgeConverterTest.java index 0a9c9371f..0adf49c60 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/badges/ConfiguredProfileBadgeConverterTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/badges/ConfiguredProfileBadgeConverterTest.java @@ -13,8 +13,6 @@ import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.net.MalformedURLException; -import java.net.URL; import java.time.Clock; import java.time.Instant; import java.util.ArrayList; @@ -56,14 +54,6 @@ public class ConfiguredProfileBadgeConverterTest { return "Badge-" + i; } - private static URL imageUrlFor(int i) { - try { - return new URL("https://example.com/badge/" + i); - } catch (MalformedURLException e) { - throw new AssertionError(e); - } - } - private static String nameFor(int i) { return "TRANSLATED NAME " + i; } @@ -73,7 +63,7 @@ public class ConfiguredProfileBadgeConverterTest { } private static BadgeConfiguration newBadge(int i) { - return new BadgeConfiguration(idFor(i), imageUrlFor(i), "other"); + return new BadgeConfiguration(idFor(i), "other", "l", "m", "h", "x", "xx", "xxx"); } private BadgesConfiguration createBadges(int count) { @@ -143,15 +133,15 @@ public class ConfiguredProfileBadgeConverterTest { arguments(idFor(0), expired, false, false, null), arguments(idFor(0), notExpired, false, false, null), arguments(idFor(0), expired, true, false, null), - arguments(idFor(0), notExpired, true, false, new Badge(idFor(0), "other", imageUrlFor(0), nameFor(0), desriptionFor(0))), + arguments(idFor(0), notExpired, true, false, new Badge(idFor(0), "other", nameFor(0), desriptionFor(0), "l", "m", "h", "x", "xx", "xxx")), arguments(idFor(1), expired, false, false, null), arguments(idFor(1), notExpired, false, false, null), arguments(idFor(1), expired, true, false, null), arguments(idFor(1), notExpired, true, false, null), arguments(idFor(0), expired, false, true, null), - arguments(idFor(0), notExpired, false, true, new SelfBadge(idFor(0), "other", imageUrlFor(0), nameFor(0), desriptionFor(0), notExpired, false)), + arguments(idFor(0), notExpired, false, true, new SelfBadge(idFor(0), "other", nameFor(0), desriptionFor(0), "l", "m", "h", "x", "xx", "xxx", notExpired, false)), arguments(idFor(0), expired, true, true, null), - arguments(idFor(0), notExpired, true, true, new SelfBadge(idFor(0), "other", imageUrlFor(0), nameFor(0), desriptionFor(0), notExpired, true)), + arguments(idFor(0), notExpired, true, true, new SelfBadge(idFor(0), "other", nameFor(0), desriptionFor(0), "l", "m", "h", "x", "xx", "xxx", notExpired, true)), arguments(idFor(1), expired, false, true, null), arguments(idFor(1), notExpired, false, true, null), arguments(idFor(1), expired, true, true, null), diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/ProfileControllerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/ProfileControllerTest.java index e087aee02..bb6658b16 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/ProfileControllerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/tests/controllers/ProfileControllerTest.java @@ -22,8 +22,6 @@ import com.google.common.collect.ImmutableSet; import io.dropwizard.auth.PolymorphicAuthValueFactoryProvider; import io.dropwizard.testing.junit5.DropwizardExtensionsSupport; import io.dropwizard.testing.junit5.ResourceExtension; -import java.net.MalformedURLException; -import java.net.URL; import java.time.Clock; import java.time.Instant; import java.util.Collections; @@ -96,14 +94,6 @@ class ProfileControllerTest { private Account profileAccount; - private static URL makeURL(String url) { - try { - return new URL(url); - } catch (MalformedURLException e) { - throw new AssertionError(e); - } - } - private static final ResourceExtension resources = ResourceExtension.builder() .addProvider(AuthHelper.getAuthFilter()) .addProvider(new PolymorphicAuthValueFactoryProvider.Binder<>( @@ -118,13 +108,13 @@ class ProfileControllerTest { usernamesManager, dynamicConfigurationManager, (acceptableLanguages, accountBadges, isSelf) -> List.of( - new Badge("TEST", "other", makeURL("https://example.com/badge/test"), "Test Badge", "This badge is in unit tests.") + new Badge("TEST", "other", "Test Badge", "This badge is in unit tests.", "l", "m", "h", "x", "xx", "xxx") ), new BadgesConfiguration(List.of( - new BadgeConfiguration("TEST", makeURL("https://example.com/badge/test"), "other"), - new BadgeConfiguration("TEST1", makeURL("https://example.com/badge/1"), "testing"), - new BadgeConfiguration("TEST2", makeURL("https://example.com/badge/2"), "testing"), - new BadgeConfiguration("TEST3", makeURL("https://example.com/badge/3"), "testing") + new BadgeConfiguration("TEST", "other", "l", "m", "h", "x", "xx", "xxx"), + new BadgeConfiguration("TEST1", "testing", "l", "m", "h", "x", "xx", "xxx"), + new BadgeConfiguration("TEST2", "testing", "l", "m", "h", "x", "xx", "xxx"), + new BadgeConfiguration("TEST3", "testing", "l", "m", "h", "x", "xx", "xxx") ), List.of("TEST1")), s3client, postPolicyGenerator,