From a37f75b831046d81aee4811f216b63719a8b4616 Mon Sep 17 00:00:00 2001 From: Doaky Date: Sun, 8 Nov 2026 17:31:09 -0500 Subject: [PATCH] Expose profile badges on the /v1/contacts endpoints signal-cli 0.14.10 includes profile badges (e.g. donor badges) in its listContacts output. Map them to the REST response, which copies the profile fields one by one and would otherwise drop them. Co-Authored-By: Claude Opus 5.5 --- src/client/client.go | 54 ++++++++++++++++++++++++++++++------- src/client/contacts_test.go | 54 +++++++++++++++++++++++++++++++++++++ src/docs/docs.go | 30 +++++++++++++++++++++ src/docs/swagger.json | 30 +++++++++++++++++++++ 4 files changed, 158 insertions(+), 10 deletions(-) create mode 100644 src/client/contacts_test.go diff --git a/src/client/client.go b/src/client/client.go index 6dc0d1f..569509e 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -229,12 +229,44 @@ type ListInstalledStickerPacksResponse struct { Author string `json:"author"` } +type ProfileBadge struct { + Id string `json:"id"` + Category string `json:"category"` + ExpirationTimestamp *int64 `json:"expiration_timestamp"` + Visible bool `json:"visible"` +} + type ContactProfile struct { - GivenName string `json:"given_name"` - FamilyName string `json:"lastname"` - About string `json:"about"` - HasAvatar bool `json:"has_avatar"` - LastUpdatedTimestamp int64 `json:"last_updated_timestamp"` + GivenName string `json:"given_name"` + FamilyName string `json:"lastname"` + About string `json:"about"` + HasAvatar bool `json:"has_avatar"` + LastUpdatedTimestamp int64 `json:"last_updated_timestamp"` + Badges []ProfileBadge `json:"badges"` +} + +// SignalCliProfileBadge mirrors a badge in the profile of signal-cli's `listContacts` output. +type SignalCliProfileBadge struct { + Id string `json:"id"` + Category string `json:"category"` + ExpirationTimestamp *int64 `json:"expirationTimestamp"` + Visible bool `json:"visible"` +} + +// signalCliProfileBadgesToProfileBadges always returns a non-nil slice, so that +// the badges are serialized as [] when signal-cli doesn't report any (signal-cli +// versions before 0.14.10 don't include badges in their output). +func signalCliProfileBadgesToProfileBadges(badges []SignalCliProfileBadge) []ProfileBadge { + result := make([]ProfileBadge, 0, len(badges)) + for _, badge := range badges { + result = append(result, ProfileBadge{ + Id: badge.Id, + Category: badge.Category, + ExpirationTimestamp: badge.ExpirationTimestamp, + Visible: badge.Visible, + }) + } + return result } type Nickname struct { @@ -2838,11 +2870,12 @@ func (s *SignalClient) AddStickerPack(number string, packId string, packKey stri func (s *SignalClient) ListContacts(number string, allRecipients bool, recipient string) ([]ListContactsResponse, error) { type SignalCliProfileResponse struct { - LastUpdateTimestamp int64 `json:"lastUpdateTimestamp"` - GivenName string `json:"givenName"` - FamilyName string `json:"familyName"` - About string `json:"about"` - HasAvatar bool `json:"hasAvatar"` + LastUpdateTimestamp int64 `json:"lastUpdateTimestamp"` + GivenName string `json:"givenName"` + FamilyName string `json:"familyName"` + About string `json:"about"` + HasAvatar bool `json:"hasAvatar"` + Badges []SignalCliProfileBadge `json:"badges"` } type ListContactsSignalCliResponse struct { @@ -2932,6 +2965,7 @@ func (s *SignalClient) ListContacts(number string, allRecipients bool, recipient entry.Profile.LastUpdatedTimestamp = value.Profile.LastUpdateTimestamp entry.Profile.GivenName = value.Profile.GivenName entry.Profile.FamilyName = value.Profile.FamilyName + entry.Profile.Badges = signalCliProfileBadgesToProfileBadges(value.Profile.Badges) entry.Nickname.Name = value.Nickname entry.Nickname.GivenName = value.NickGivenName entry.Nickname.FamilyName = value.NickFamilyName diff --git a/src/client/contacts_test.go b/src/client/contacts_test.go new file mode 100644 index 0000000..1b64d07 --- /dev/null +++ b/src/client/contacts_test.go @@ -0,0 +1,54 @@ +package client + +import ( + "encoding/json" + "testing" +) + +// TestSignalCliProfileBadgesToProfileBadges verifies that the badges of signal-cli's +// `listContacts` output are carried through to the REST response, including badges +// without an expiration. +func TestSignalCliProfileBadgesToProfileBadges(t *testing.T) { + const sampleBadgesJSON = `[ + {"id": "BOOST", "category": "donor", "expirationTimestamp": 1767225600000, "visible": true}, + {"id": "R_LOW", "category": "donor", "expirationTimestamp": null, "visible": false} + ]` + + var signalCliBadges []SignalCliProfileBadge + if err := json.Unmarshal([]byte(sampleBadgesJSON), &signalCliBadges); err != nil { + t.Fatalf("failed to unmarshal sample badges: %v", err) + } + + badges := signalCliProfileBadgesToProfileBadges(signalCliBadges) + if len(badges) != 2 { + t.Fatalf("expected 2 badges, got %d", len(badges)) + } + + first := badges[0] + if first.Id != "BOOST" || first.Category != "donor" || !first.Visible { + t.Errorf("unexpected badge: %+v", first) + } + if first.ExpirationTimestamp == nil || *first.ExpirationTimestamp != 1767225600000 { + t.Errorf("unexpected expiration timestamp: %v", first.ExpirationTimestamp) + } + + second := badges[1] + if second.Id != "R_LOW" || second.Visible { + t.Errorf("unexpected badge: %+v", second) + } + if second.ExpirationTimestamp != nil { + t.Errorf("expected no expiration timestamp, got %d", *second.ExpirationTimestamp) + } +} + +// TestSignalCliProfileBadgesToProfileBadgesWithoutBadges verifies that badges are +// serialized as an empty list, rather than null, when signal-cli doesn't report any. +func TestSignalCliProfileBadgesToProfileBadgesWithoutBadges(t *testing.T) { + encoded, err := json.Marshal(signalCliProfileBadgesToProfileBadges(nil)) + if err != nil { + t.Fatalf("failed to marshal badges: %v", err) + } + if string(encoded) != "[]" { + t.Errorf("expected [], got %s", encoded) + } +} diff --git a/src/docs/docs.go b/src/docs/docs.go index db26180..4baf3bc 100644 --- a/src/docs/docs.go +++ b/src/docs/docs.go @@ -770,6 +770,12 @@ const docTemplate = `{ "about": { "type": "string" }, + "badges": { + "items": { + "$ref": "#/definitions/client.ProfileBadge" + }, + "type": "array" + }, "given_name": { "type": "string" }, @@ -785,6 +791,7 @@ const docTemplate = `{ }, "required": [ "about", + "badges", "given_name", "has_avatar", "last_updated_timestamp", @@ -1014,6 +1021,29 @@ const docTemplate = `{ ], "type": "object" }, + "client.ProfileBadge": { + "properties": { + "category": { + "type": "string" + }, + "expiration_timestamp": { + "type": "integer" + }, + "id": { + "type": "string" + }, + "visible": { + "type": "boolean" + } + }, + "required": [ + "category", + "expiration_timestamp", + "id", + "visible" + ], + "type": "object" + }, "client.SetUsernameResponse": { "properties": { "username": { diff --git a/src/docs/swagger.json b/src/docs/swagger.json index c8e910b..1b51e28 100644 --- a/src/docs/swagger.json +++ b/src/docs/swagger.json @@ -765,6 +765,12 @@ "about": { "type": "string" }, + "badges": { + "items": { + "$ref": "#/definitions/client.ProfileBadge" + }, + "type": "array" + }, "given_name": { "type": "string" }, @@ -780,6 +786,7 @@ }, "required": [ "about", + "badges", "given_name", "has_avatar", "last_updated_timestamp", @@ -1009,6 +1016,29 @@ ], "type": "object" }, + "client.ProfileBadge": { + "properties": { + "category": { + "type": "string" + }, + "expiration_timestamp": { + "type": "integer" + }, + "id": { + "type": "string" + }, + "visible": { + "type": "boolean" + } + }, + "required": [ + "category", + "expiration_timestamp", + "id", + "visible" + ], + "type": "object" + }, "client.SetUsernameResponse": { "properties": { "username": {