mirror of
https://github.com/AsamK/signal-cli.git
synced 2026-11-29 08:55:48 +00:00
Expose profile badges in listContacts (#2162)
* Expose profile badges in listContacts Closes #2161 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Add changelog entry for profile badges Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
edcb3ef220
commit
2a8a141e9e
12 files changed
+234
-18
No files matched your search
@@ -3,6 +3,7 @@ package org.asamk.signal.manager.api;
|
||||
import org.whispersystems.signalservice.internal.util.Util;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -28,6 +29,8 @@ public class Profile {
|
||||
|
||||
private final PhoneNumberSharingMode phoneNumberSharingMode;
|
||||
|
||||
private final List<Badge> badges;
|
||||
|
||||
public Profile(
|
||||
final long lastUpdateTimestamp,
|
||||
final String givenName,
|
||||
@@ -38,7 +41,8 @@ public class Profile {
|
||||
final byte[] mobileCoinAddress,
|
||||
final UnidentifiedAccessMode unidentifiedAccessMode,
|
||||
final Set<Capability> capabilities,
|
||||
final PhoneNumberSharingMode phoneNumberSharingMode
|
||||
final PhoneNumberSharingMode phoneNumberSharingMode,
|
||||
final List<Badge> badges
|
||||
) {
|
||||
this.lastUpdateTimestamp = lastUpdateTimestamp;
|
||||
this.givenName = givenName;
|
||||
@@ -50,6 +54,7 @@ public class Profile {
|
||||
this.unidentifiedAccessMode = unidentifiedAccessMode;
|
||||
this.capabilities = capabilities;
|
||||
this.phoneNumberSharingMode = phoneNumberSharingMode;
|
||||
this.badges = badges == null ? List.of() : List.copyOf(badges);
|
||||
}
|
||||
|
||||
private Profile(final Builder builder) {
|
||||
@@ -63,6 +68,7 @@ public class Profile {
|
||||
unidentifiedAccessMode = builder.unidentifiedAccessMode;
|
||||
capabilities = builder.capabilities;
|
||||
phoneNumberSharingMode = builder.phoneNumberSharingMode;
|
||||
badges = builder.badges;
|
||||
}
|
||||
|
||||
public static Builder newBuilder() {
|
||||
@@ -80,6 +86,7 @@ public class Profile {
|
||||
builder.mobileCoinAddress = copy.getMobileCoinAddress();
|
||||
builder.unidentifiedAccessMode = copy.getUnidentifiedAccessMode();
|
||||
builder.capabilities = copy.getCapabilities();
|
||||
builder.badges = copy.getBadges();
|
||||
return builder;
|
||||
}
|
||||
|
||||
@@ -145,6 +152,10 @@ public class Profile {
|
||||
return phoneNumberSharingMode;
|
||||
}
|
||||
|
||||
public List<Badge> getBadges() {
|
||||
return badges;
|
||||
}
|
||||
|
||||
public enum UnidentifiedAccessMode {
|
||||
UNKNOWN,
|
||||
DISABLED,
|
||||
@@ -173,6 +184,13 @@ public class Profile {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A badge displayed on a profile, e.g. a donor badge.
|
||||
*
|
||||
* @param expirationTimestamp the time in milliseconds when the badge expires, or null if it doesn't expire
|
||||
*/
|
||||
public record Badge(String id, String category, Long expirationTimestamp, boolean visible) {}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object o) {
|
||||
if (this == o) return true;
|
||||
@@ -185,7 +203,8 @@ public class Profile {
|
||||
&& Objects.equals(aboutEmoji, profile.aboutEmoji)
|
||||
&& Objects.equals(avatarUrlPath, profile.avatarUrlPath)
|
||||
&& unidentifiedAccessMode == profile.unidentifiedAccessMode
|
||||
&& Objects.equals(capabilities, profile.capabilities);
|
||||
&& Objects.equals(capabilities, profile.capabilities)
|
||||
&& Objects.equals(badges, profile.badges);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -197,7 +216,8 @@ public class Profile {
|
||||
aboutEmoji,
|
||||
avatarUrlPath,
|
||||
unidentifiedAccessMode,
|
||||
capabilities);
|
||||
capabilities,
|
||||
badges);
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
@@ -211,6 +231,7 @@ public class Profile {
|
||||
private UnidentifiedAccessMode unidentifiedAccessMode = UnidentifiedAccessMode.UNKNOWN;
|
||||
private Set<Capability> capabilities = Collections.emptySet();
|
||||
private PhoneNumberSharingMode phoneNumberSharingMode;
|
||||
private List<Badge> badges = List.of();
|
||||
private long lastUpdateTimestamp = 0;
|
||||
|
||||
private Builder() {
|
||||
@@ -256,6 +277,11 @@ public class Profile {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withBadges(final List<Badge> val) {
|
||||
badges = val == null ? List.of() : List.copyOf(val);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Profile build() {
|
||||
return new Profile(this);
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ import java.util.UUID;
|
||||
public class AccountDatabase extends Database {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AccountDatabase.class);
|
||||
private static final long DATABASE_VERSION = 31;
|
||||
private static final long DATABASE_VERSION = 32;
|
||||
|
||||
private AccountDatabase(final HikariDataSource dataSource) {
|
||||
super(logger, DATABASE_VERSION, dataSource);
|
||||
@@ -653,6 +653,14 @@ public class AccountDatabase extends Database {
|
||||
""");
|
||||
}
|
||||
}
|
||||
if (oldVersion < 32) {
|
||||
logger.debug("Updating database: Create profile_badges column");
|
||||
try (final var statement = connection.createStatement()) {
|
||||
statement.executeUpdate("""
|
||||
ALTER TABLE recipient ADD profile_badges TEXT;
|
||||
""");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void createUuidMappingTable(
|
||||
|
||||
@@ -1035,7 +1035,8 @@ public class SignalAccount implements Closeable {
|
||||
? Profile.UnidentifiedAccessMode.ENABLED
|
||||
: Profile.UnidentifiedAccessMode.DISABLED,
|
||||
capabilities,
|
||||
null);
|
||||
null,
|
||||
List.of());
|
||||
getProfileStore().storeProfile(recipientId, newProfile);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -90,7 +90,8 @@ public class LegacyRecipientStore2 {
|
||||
.map(Profile.Capability::valueOfOrNull)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet()),
|
||||
null);
|
||||
null,
|
||||
List.of());
|
||||
}
|
||||
|
||||
return new Recipient(recipientId,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package org.asamk.signal.manager.storage.recipients;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
|
||||
import org.asamk.signal.manager.api.Contact;
|
||||
import org.asamk.signal.manager.api.Pair;
|
||||
import org.asamk.signal.manager.api.PhoneNumberSharingMode;
|
||||
@@ -21,6 +23,7 @@ import org.slf4j.LoggerFactory;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.signalservice.api.storage.StorageId;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -42,6 +45,7 @@ import java.util.stream.Collectors;
|
||||
public class RecipientStore implements RecipientIdCreator, RecipientResolver, RecipientTrustedResolver, ContactsStore, ProfileStore {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(RecipientStore.class);
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private static final String TABLE_RECIPIENT = "recipient";
|
||||
private static final String SQL_IS_CONTACT = "r.given_name IS NOT NULL OR r.family_name IS NOT NULL OR r.nick_name IS NOT NULL OR r.nick_name_given_name IS NOT NULL OR r.nick_name_family_name IS NOT NULL OR r.note IS NOT NULL OR r.expiration_time > 0 OR r.profile_sharing = TRUE OR r.color IS NOT NULL OR r.blocked = TRUE OR r.archived = TRUE";
|
||||
|
||||
@@ -111,7 +115,8 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
|
||||
profile_mobile_coin_address BLOB,
|
||||
profile_unidentified_access_mode TEXT,
|
||||
profile_capabilities TEXT,
|
||||
profile_phone_number_sharing TEXT
|
||||
profile_phone_number_sharing TEXT,
|
||||
profile_badges TEXT
|
||||
) STRICT;
|
||||
""");
|
||||
}
|
||||
@@ -378,7 +383,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
|
||||
r.profile_key, r.profile_key_credential,
|
||||
r.pni_signature_verified,
|
||||
r.given_name, r.family_name, r.nick_name, r.nick_name_given_name, r.nick_name_family_name, r.note, r.expiration_time, r.expiration_time_version, r.mute_until, r.hide_story, r.profile_sharing, r.color, r.blocked, r.blocked_at, r.archived, r.hidden, r.unregistered_timestamp,
|
||||
r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing,
|
||||
r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing, r.profile_badges,
|
||||
r.discoverable,
|
||||
r.storage_record
|
||||
FROM %s r
|
||||
@@ -399,7 +404,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
|
||||
r.profile_key, r.profile_key_credential,
|
||||
r.pni_signature_verified,
|
||||
r.given_name, r.family_name, r.nick_name, r.nick_name_given_name, r.nick_name_family_name, r.note, r.expiration_time, r.expiration_time_version, r.mute_until, r.hide_story, r.profile_sharing, r.color, r.blocked, r.blocked_at, r.archived, r.hidden, r.unregistered_timestamp,
|
||||
r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing,
|
||||
r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing, r.profile_badges,
|
||||
r.discoverable,
|
||||
r.storage_record
|
||||
FROM %s r
|
||||
@@ -449,7 +454,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
|
||||
r.profile_key, r.profile_key_credential,
|
||||
r.pni_signature_verified,
|
||||
r.given_name, r.family_name, r.nick_name, r.nick_name_given_name, r.nick_name_family_name, r.note, r.expiration_time, r.expiration_time_version, r.mute_until, r.hide_story, r.profile_sharing, r.color, r.blocked, r.blocked_at, r.archived, r.hidden, r.unregistered_timestamp,
|
||||
r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing,
|
||||
r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing, r.profile_badges,
|
||||
r.discoverable,
|
||||
r.storage_record
|
||||
FROM %s r
|
||||
@@ -1226,7 +1231,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
|
||||
final var sql = (
|
||||
"""
|
||||
UPDATE %s
|
||||
SET profile_last_update_timestamp = ?, profile_given_name = ?, profile_family_name = ?, profile_about = ?, profile_about_emoji = ?, profile_avatar_url_path = ?, profile_mobile_coin_address = ?, profile_unidentified_access_mode = ?, profile_capabilities = ?, profile_phone_number_sharing = ?
|
||||
SET profile_last_update_timestamp = ?, profile_given_name = ?, profile_family_name = ?, profile_about = ?, profile_about_emoji = ?, profile_avatar_url_path = ?, profile_mobile_coin_address = ?, profile_unidentified_access_mode = ?, profile_capabilities = ?, profile_phone_number_sharing = ?, profile_badges = ?
|
||||
WHERE _id = ?
|
||||
"""
|
||||
).formatted(TABLE_RECIPIENT);
|
||||
@@ -1247,7 +1252,8 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
|
||||
profile == null || profile.getPhoneNumberSharingMode() == null
|
||||
? null
|
||||
: profile.getPhoneNumberSharingMode().name());
|
||||
statement.setLong(11, recipientId.id());
|
||||
statement.setString(11, profile == null ? null : serializeBadges(profile.getBadges()));
|
||||
statement.setLong(12, recipientId.id());
|
||||
statement.executeUpdate();
|
||||
}
|
||||
rotateStorageId(connection, recipientId);
|
||||
@@ -1697,7 +1703,7 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
|
||||
public Profile getProfile(final Connection connection, final RecipientId recipientId) throws SQLException {
|
||||
final var sql = (
|
||||
"""
|
||||
SELECT r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing
|
||||
SELECT r.profile_last_update_timestamp, r.profile_given_name, r.profile_family_name, r.profile_about, r.profile_about_emoji, r.profile_avatar_url_path, r.profile_mobile_coin_address, r.profile_unidentified_access_mode, r.profile_capabilities, r.profile_phone_number_sharing, r.profile_badges
|
||||
FROM %s r
|
||||
WHERE r._id = ? AND r.profile_capabilities IS NOT NULL
|
||||
"""
|
||||
@@ -1794,7 +1800,43 @@ public class RecipientStore implements RecipientIdCreator, RecipientResolver, Re
|
||||
.map(Profile.Capability::valueOfOrNull)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet()),
|
||||
PhoneNumberSharingMode.valueOfOrNull(resultSet.getString("profile_phone_number_sharing")));
|
||||
PhoneNumberSharingMode.valueOfOrNull(resultSet.getString("profile_phone_number_sharing")),
|
||||
deserializeBadges(resultSet.getString("profile_badges")));
|
||||
}
|
||||
|
||||
static String serializeBadges(final List<Profile.Badge> badges) {
|
||||
if (badges.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
final var array = objectMapper.createArrayNode();
|
||||
for (final var badge : badges) {
|
||||
final var node = array.addObject();
|
||||
node.put("id", badge.id());
|
||||
node.put("category", badge.category());
|
||||
node.put("expirationTimestamp", badge.expirationTimestamp());
|
||||
node.put("visible", badge.visible());
|
||||
}
|
||||
return array.toString();
|
||||
}
|
||||
|
||||
static List<Profile.Badge> deserializeBadges(final String badges) {
|
||||
if (badges == null) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
final var result = new ArrayList<Profile.Badge>();
|
||||
for (final var node : objectMapper.readTree(badges)) {
|
||||
final var expirationTimestamp = node.path("expirationTimestamp");
|
||||
result.add(new Profile.Badge(node.path("id").asText(null),
|
||||
node.path("category").asText(null),
|
||||
expirationTimestamp.isNumber() ? expirationTimestamp.asLong() : null,
|
||||
node.path("visible").asBoolean(false)));
|
||||
}
|
||||
return result;
|
||||
} catch (IOException e) {
|
||||
logger.warn("Failed to parse stored profile badges, ignoring: {}", e.getMessage());
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
private ProfileKey getProfileKeyFromResultSet(ResultSet resultSet) throws SQLException {
|
||||
|
||||
@@ -15,8 +15,11 @@ import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
|
||||
import org.whispersystems.signalservice.internal.push.PaymentAddress;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Base64;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ProfileUtils {
|
||||
@@ -54,7 +57,8 @@ public class ProfileUtils {
|
||||
identityKey.getPublicKey()),
|
||||
getUnidentifiedAccessMode(encryptedProfile, profileCipher),
|
||||
getCapabilities(encryptedProfile),
|
||||
remotePhoneNumberSharing);
|
||||
remotePhoneNumberSharing,
|
||||
getBadges(encryptedProfile));
|
||||
} catch (InvalidCiphertextException e) {
|
||||
logger.debug("Failed to decrypt profile for {}", encryptedProfile.getServiceId(), e);
|
||||
return null;
|
||||
@@ -79,6 +83,22 @@ public class ProfileUtils {
|
||||
return Profile.UnidentifiedAccessMode.DISABLED;
|
||||
}
|
||||
|
||||
static List<Profile.Badge> getBadges(final SignalServiceProfile encryptedProfile) {
|
||||
if (encryptedProfile.getBadges() == null) {
|
||||
return List.of();
|
||||
}
|
||||
return encryptedProfile.getBadges()
|
||||
.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.map(badge -> new Profile.Badge(badge.getId(),
|
||||
badge.getCategory(),
|
||||
badge.getExpiration() == null
|
||||
? null
|
||||
: badge.getExpiration().multiply(BigDecimal.valueOf(1000)).longValue(),
|
||||
badge.getVisible()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
public static HashSet<Profile.Capability> getCapabilities(final SignalServiceProfile encryptedProfile) {
|
||||
final var capabilities = new HashSet<Profile.Capability>();
|
||||
if (encryptedProfile.getCapabilities() != null) {
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package org.asamk.signal.manager.storage.recipients;
|
||||
|
||||
import org.asamk.signal.manager.api.Contact;
|
||||
import org.asamk.signal.manager.api.Profile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class RecipientStoreTest {
|
||||
@@ -90,4 +94,23 @@ class RecipientStoreTest {
|
||||
assertEquals(100, merged.muteUntil());
|
||||
assertTrue(merged.isHidden());
|
||||
}
|
||||
|
||||
@Test
|
||||
void profileBadgesRoundTrip() {
|
||||
final var badges = List.of(new Profile.Badge("BOOST", "donor", 1767225600000L, true),
|
||||
new Profile.Badge("TEST", "other", null, false));
|
||||
|
||||
assertEquals(badges, RecipientStore.deserializeBadges(RecipientStore.serializeBadges(badges)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyProfileBadgesAreStoredAsNull() {
|
||||
assertNull(RecipientStore.serializeBadges(List.of()));
|
||||
assertEquals(List.of(), RecipientStore.deserializeBadges(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidStoredProfileBadgesAreIgnored() {
|
||||
assertEquals(List.of(), RecipientStore.deserializeBadges("not json"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.asamk.signal.manager.util;
|
||||
|
||||
import org.asamk.signal.manager.api.Profile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.whispersystems.signalservice.api.profiles.SignalServiceProfile;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class ProfileUtilsTest {
|
||||
|
||||
@Test
|
||||
void badgesAreConvertedFromServiceProfile() {
|
||||
final var profile = profileWithBadges(List.of(badge("BOOST", "donor", new BigDecimal("1767225600"), true),
|
||||
badge("TEST", "other", null, false)));
|
||||
|
||||
assertEquals(List.of(new Profile.Badge("BOOST", "donor", 1767225600000L, true),
|
||||
new Profile.Badge("TEST", "other", null, false)), ProfileUtils.getBadges(profile));
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingBadgesResultInEmptyList() {
|
||||
assertEquals(List.of(), ProfileUtils.getBadges(profileWithBadges(null)));
|
||||
}
|
||||
|
||||
private static SignalServiceProfile.Badge badge(
|
||||
final String id,
|
||||
final String category,
|
||||
final BigDecimal expiration,
|
||||
final boolean visible
|
||||
) {
|
||||
return new SignalServiceProfile.Badge(id, category, "", "", List.of(), expiration, visible, 0);
|
||||
}
|
||||
|
||||
private static SignalServiceProfile profileWithBadges(final List<SignalServiceProfile.Badge> badges) {
|
||||
return new SignalServiceProfile(null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
false,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
badges,
|
||||
null);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user