mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-11-29 08:55:57 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
787d204e15
commit
a37f75b831
4 files changed
+158
-10
No files matched your search
+44
-10
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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": {
|
||||
|
||||
@@ -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": {
|
||||
|
||||
Reference in new issue
Block a user