diff --git a/src/client/client.go b/src/client/client.go index 1c2e8a2..03c0442 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -229,12 +229,46 @@ type ListInstalledStickerPacksResponse struct { Author string `json:"author"` } +type ProfileBadge struct { + Id string `json:"id"` + Category string `json:"category"` + // Milliseconds since the epoch. Signal only discloses this for the account's own profile, so it is null for other contacts. + ExpirationTimestamp *int64 `json:"expiration_timestamp" extensions:"x-nullable=true"` + // Signal only discloses this for the account's own profile, so it is false for other contacts, whose badges are always displayed ones. + 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 { @@ -2863,11 +2897,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 { @@ -2957,6 +2992,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..69a5aa6 --- /dev/null +++ b/src/client/contacts_test.go @@ -0,0 +1,55 @@ +package client + +import ( + "encoding/json" + "testing" +) + +// TestSignalCliProfileBadgesToProfileBadges verifies that the badges of signal-cli's +// `listContacts` output are carried through to the REST response, both in the shape +// of the account's own badge and of another contact's, which has no expiration and +// is reported as not visible. +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 9dc4378..354eac0 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,32 @@ const docTemplate = `{ ], "type": "object" }, + "client.ProfileBadge": { + "properties": { + "category": { + "type": "string" + }, + "expiration_timestamp": { + "description": "Milliseconds since the epoch. Signal only discloses this for the account's own profile, so it is null for other contacts.", + "type": "integer", + "x-nullable": "true" + }, + "id": { + "type": "string" + }, + "visible": { + "description": "Signal only discloses this for the account's own profile, so it is false for other contacts, whose badges are always displayed ones.", + "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 19944ea..0bf91bc 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,32 @@ ], "type": "object" }, + "client.ProfileBadge": { + "properties": { + "category": { + "type": "string" + }, + "expiration_timestamp": { + "description": "Milliseconds since the epoch. Signal only discloses this for the account's own profile, so it is null for other contacts.", + "type": "integer", + "x-nullable": "true" + }, + "id": { + "type": "string" + }, + "visible": { + "description": "Signal only discloses this for the account's own profile, so it is false for other contacts, whose badges are always displayed ones.", + "type": "boolean" + } + }, + "required": [ + "category", + "expiration_timestamp", + "id", + "visible" + ], + "type": "object" + }, "client.SetUsernameResponse": { "properties": { "username": {