Add badges to remote config

This commit is contained in:
Ravi Khadiwala authored and ravi-signal committed 2026-07-28 16:20:05 -05:00
1 parent 257dd76b68
commit 8e9253181c
4 files changed
+189 -3

No files matched your search

@@ -110,6 +110,7 @@ import org.whispersystems.textsecuregcm.captcha.CaptchaChecker;
import org.whispersystems.textsecuregcm.captcha.CaptchaClient;
import org.whispersystems.textsecuregcm.captcha.RegistrationCaptchaManager;
import org.whispersystems.textsecuregcm.captcha.ShortCodeExpander;
import org.whispersystems.textsecuregcm.configuration.BadgeConfiguration;
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
import org.whispersystems.textsecuregcm.configuration.secrets.SecretStore;
import org.whispersystems.textsecuregcm.configuration.secrets.SecretsModule;
@@ -1105,7 +1106,10 @@ public class WhisperServerService extends Application<WhisperServerConfiguration
new ProductConfigurationGrpcService(config.getSubscription(), config.getOneTimeDonations(),
List.of(stripeManager, braintreeManager), profileBadgeConverter,
config.getBackupConfiguration().maxTotalMediaSize()),
new RemoteConfigurationGrpcService(remoteConfigsManager))
new RemoteConfigurationGrpcService(remoteConfigsManager, profileBadgeConverter,
config.getBadges().getBadges().stream()
.map(BadgeConfiguration::getId)
.toList()))
.map(bindableService -> ServerInterceptors.intercept(bindableService,
// Note: interceptors run in the reverse order they are added; the remote deprecation filter
// depends on the user-agent context so it has to come first here!
@@ -12,25 +12,49 @@ import java.io.IOException;
import java.io.UncheckedIOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.signal.chat.common.Badge;
import org.signal.chat.remoteconfiguration.Badges;
import org.signal.chat.remoteconfiguration.Configuration;
import org.signal.chat.remoteconfiguration.GetBadgesRequest;
import org.signal.chat.remoteconfiguration.GetBadgesResponse;
import org.signal.chat.remoteconfiguration.GetConfigurationRequest;
import org.signal.chat.remoteconfiguration.GetConfigurationResponse;
import org.signal.chat.remoteconfiguration.SimpleRemoteConfigurationGrpc;
import org.signal.chat.remoteconfiguration.TaggedBadges;
import org.signal.chat.remoteconfiguration.TaggedConfiguration;
import org.whispersystems.textsecuregcm.auth.grpc.AuthenticationUtil;
import org.whispersystems.textsecuregcm.badges.BadgeTranslator;
import org.whispersystems.textsecuregcm.storage.RemoteConfigsManager;
public class RemoteConfigurationGrpcService extends SimpleRemoteConfigurationGrpc.RemoteConfigurationImplBase {
private static final GetConfigurationResponse CONFIGURATION_ETAG_MATCHED =
GetConfigurationResponse.newBuilder().setEtagMatched(true).build();
private static final GetBadgesResponse BADGES_ETAG_MATCHED =
GetBadgesResponse.newBuilder().setEtagMatched(true).build();
private final RemoteConfigsManager remoteConfigsManager;
private final BadgeTranslator badgeTranslator;
private final List<String> badgeIds;
public RemoteConfigurationGrpcService(final RemoteConfigsManager remoteConfigsManager) {
// Badge information varies based on the provided Accept-Language header. Here we cache the etag for the resolved
// language, so if the caller provides a matching etag we don't have to build the full badge response.
private final ConcurrentHashMap<Locale, ByteString> localeToBadgesEtag = new ConcurrentHashMap<>();
public RemoteConfigurationGrpcService(
final RemoteConfigsManager remoteConfigsManager,
final BadgeTranslator badgeTranslator,
final List<String> badgeIds) {
this.remoteConfigsManager = remoteConfigsManager;
this.badgeTranslator = badgeTranslator;
this.badgeIds = badgeIds;
}
@Override
@@ -54,6 +78,32 @@ public class RemoteConfigurationGrpcService extends SimpleRemoteConfigurationGrp
.build();
}
@Override
public GetBadgesResponse getBadges(final GetBadgesRequest request) {
final List<Locale> acceptableLanguages = RequestAttributesUtil.getAvailableAcceptedLocales();
final Locale locale = badgeTranslator.resolveLocale(acceptableLanguages);
final ByteString cachedEtag = localeToBadgesEtag.get(locale);
if (cachedEtag != null && cachedEtag.equals(request.getEtag())) {
return BADGES_ETAG_MATCHED;
}
final Map<String, Badge> badgesById = badgeIds.stream().collect(Collectors.toMap(
Function.identity(),
badgeId -> BadgeGrpcHelper.toGrpcBadge(badgeTranslator.translate(acceptableLanguages, badgeId))));
final Badges badges = Badges.newBuilder().putAllBadges(badgesById).build();
final TaggedBadges taggedBadges = TaggedBadges.newBuilder()
.setBadges(badges)
.setEtag(etag(badges))
.build();
// This could race and multiple threads could decide to build-and-cache. That's fine, they should all calculate
// the same etag.
localeToBadgesEtag.put(locale, taggedBadges.getEtag());
return GetBadgesResponse.newBuilder().setTaggedBadges(taggedBadges).build();
}
private static ByteString etag(final Message message) {
final ByteArrayOutputStream baos = new ByteArrayOutputStream(message.getSerializedSize());
final CodedOutputStream cos = CodedOutputStream.newInstance(baos);
@@ -9,6 +9,7 @@ option java_multiple_files = true;
package org.signal.chat.remoteconfiguration;
import "org/signal/chat/common.proto";
import "org/signal/chat/require.proto";
import "org/signal/chat/tag.proto";
@@ -20,6 +21,16 @@ service RemoteConfiguration {
// values are based on the authenticated account and caller's client platform,
// which is derived from the "User-Agent" header.
rpc GetConfiguration(GetConfigurationRequest) returns (GetConfigurationResponse) {}
// Returns detailed information for all configured badges, keyed by badge ID.
//
// Badge descriptions may contain localized strings. Callers should set their
// language preferences via an "Accept-Language" header on the request
// https://datatracker.ietf.org/doc/html/rfc3282#section-3
//
// Callers may cache a badges result for up to 1 day before checking (via
// etag) if there are any new updates.
rpc GetBadges(GetBadgesRequest) returns (GetBadgesResponse) {}
}
message GetConfigurationRequest {
@@ -54,3 +65,32 @@ message TaggedConfiguration {
// changed.
bytes etag = 2;
}
message GetBadgesRequest {
// If present, the etag from a prior GetBadgesResponse. If the provided etag
// matches the current badge etag, the server may elide the badge response.
bytes etag = 1;
}
message GetBadgesResponse {
oneof response {
// The full set of badges and corresponding etag.
TaggedBadges tagged_badges = 1;
// The etag in the request matched the current badge etag.
bool etag_matched = 2 [(tag.reason) = "etag_match"];
}
}
message Badges {
// A map of badge ID to the detailed information for that badge.
map<string, common.Badge> badges = 1;
}
message TaggedBadges {
Badges badges = 1;
// An entity tag for `badges`. This may be supplied in a subsequent request
// to optimize bandwidth when the set of badges has not changed.
bytes etag = 2;
}