Return a badge with additional properties when fetching your own profile

This commit is contained in:
Ehren Kret
2021-09-17 13:20:13 -05:00
parent 5c1cde1b28
commit 44bc90e5ab
6 changed files with 138 additions and 34 deletions

View File

@@ -6,7 +6,9 @@
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;
import java.util.ArrayList;
import java.util.List;
@@ -20,6 +22,7 @@ import java.util.stream.Collectors;
import org.whispersystems.textsecuregcm.configuration.BadgeConfiguration;
import org.whispersystems.textsecuregcm.configuration.BadgesConfiguration;
import org.whispersystems.textsecuregcm.entities.Badge;
import org.whispersystems.textsecuregcm.entities.SelfBadge;
import org.whispersystems.textsecuregcm.storage.AccountBadge;
public class ConfiguredProfileBadgeConverter implements ProfileBadgeConverter {
@@ -54,7 +57,8 @@ public class ConfiguredProfileBadgeConverter implements ProfileBadgeConverter {
@Override
public List<Badge> convert(
final List<Locale> acceptableLanguages,
final List<AccountBadge> accountBadges) {
final List<AccountBadge> accountBadges,
final boolean isSelf) {
if (accountBadges.isEmpty() && badgeIdsEnabledForAll.isEmpty()) {
return List.of();
}
@@ -95,23 +99,45 @@ public class ConfiguredProfileBadgeConverter implements ProfileBadgeConverter {
&& knownBadges.containsKey(accountBadge.getId()))
.map(accountBadge -> {
BadgeConfiguration configuration = knownBadges.get(accountBadge.getId());
return new Badge(
return newBadge(
isSelf,
accountBadge.getId(),
configuration.getCategory(),
configuration.getImageUrl(),
resourceBundle.getString(accountBadge.getId() + "_name"),
resourceBundle.getString(accountBadge.getId() + "_description"));
resourceBundle.getString(accountBadge.getId() + "_description"),
accountBadge.getExpiration(),
accountBadge.isVisible());
})
.collect(Collectors.toCollection(ArrayList::new));
badges.addAll(badgeIdsEnabledForAll.stream().filter(knownBadges::containsKey).map(id -> {
BadgeConfiguration configuration = knownBadges.get(id);
return new Badge(
return newBadge(
isSelf,
id,
configuration.getCategory(),
configuration.getImageUrl(),
resourceBundle.getString(id + "_name"),
resourceBundle.getString(id + "_description"));
resourceBundle.getString(id + "_description"),
now.plus(Duration.ofDays(1)),
true);
}).collect(Collectors.toList()));
return badges;
}
private Badge newBadge(
final boolean isSelf,
final String id,
final String category,
final URL imageUrl,
final String name,
final String description,
final Instant expiration,
final boolean visible) {
if (isSelf) {
return new SelfBadge(id, category, imageUrl, name, description, expiration, visible);
} else {
return new Badge(id, category, imageUrl, name, description);
}
}
}

View File

@@ -16,5 +16,5 @@ public interface ProfileBadgeConverter {
* Converts the {@link AccountBadge}s for an account into the objects
* that can be returned on a profile fetch.
*/
List<Badge> convert(List<Locale> acceptableLanguages, List<AccountBadge> accountBadges);
List<Badge> convert(List<Locale> acceptableLanguages, List<AccountBadge> accountBadges, boolean isSelf);
}