From a3a8c44179440a8f1d8c26a66840c1a9332e51c3 Mon Sep 17 00:00:00 2001 From: highesttt Date: Mon, 10 Aug 2026 21:10:08 -0400 Subject: [PATCH] fix: accept string versions in paid reactions --- pkg/line/reaction.go | 17 +++++++++++++++++ pkg/line/reaction_test.go | 24 ++++++++++++++++++++++++ pkg/line/structs_test.go | 15 +++++++++++++-- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/pkg/line/reaction.go b/pkg/line/reaction.go index 74869b3..d5d0996 100644 --- a/pkg/line/reaction.go +++ b/pkg/line/reaction.go @@ -61,6 +61,23 @@ type PaidReactionType struct { Version int `json:"version,omitempty"` } +func (p *PaidReactionType) UnmarshalJSON(data []byte) error { + var parsed struct { + ProductID string `json:"productId"` + EmojiID string `json:"emojiId"` + ResourceType int `json:"resourceType,omitempty"` + Version FlexInt `json:"version,omitempty"` + } + if err := json.Unmarshal(data, &parsed); err != nil { + return err + } + p.ProductID = parsed.ProductID + p.EmojiID = parsed.EmojiID + p.ResourceType = parsed.ResourceType + p.Version = parsed.Version.Val + return nil +} + type ReactionType struct { PredefinedReactionType int `json:"predefinedReactionType,omitempty"` PaidReactionType *PaidReactionType `json:"paidReactionType,omitempty"` diff --git a/pkg/line/reaction_test.go b/pkg/line/reaction_test.go index fa3afec..9b89ce6 100644 --- a/pkg/line/reaction_test.go +++ b/pkg/line/reaction_test.go @@ -101,6 +101,30 @@ func TestCancelReactionRequestBody(t *testing.T) { } } +func TestGetRecentMessagesV2AcceptsPaidReactionStringVersion(t *testing.T) { + client := newReactionTestClientWithResponse( + t, + "/api/talk/thrift/Talk/TalkService/getRecentMessagesV2", + `{"code":0,"message":"ok","data":[ + {"id":"newer"}, + {"id":"affected","reactions":[{"fromUserMid":"U-paid","atMillis":"1784930400456","reactionType":{"paidReactionType":{"productId":"product","emojiId":"143","resourceType":1,"version":"1"}}}]} + ]}`, + nil, + ) + + messages, err := client.GetRecentMessagesV2("U-chat", 50) + if err != nil { + t.Fatal(err) + } + if len(messages) != 2 { + t.Fatalf("message count = %d, want 2", len(messages)) + } + paid := messages[1].Reactions[0].ReactionType.PaidReactionType + if paid == nil || paid.Version != 1 { + t.Fatalf("paid reaction = %#v, want version 1", paid) + } +} + func TestReactNonZeroWrapperKeepsInvalidPaidReactionDetails(t *testing.T) { client := newReactionTestClientWithResponse(t, "/api/talk/thrift/Talk/TalkService/react", `{"code":10051,"message":"RESPONSE_ERROR","data":{"name":"TalkException","message":"TalkException","code":0,"reason":"Invalid paidReactionType in reactionType","parameterMap":null}}`, nil) err := client.React(123, "616934195205767730", ReactionType{ diff --git a/pkg/line/structs_test.go b/pkg/line/structs_test.go index 8f6eb50..2536de1 100644 --- a/pkg/line/structs_test.go +++ b/pkg/line/structs_test.go @@ -64,7 +64,7 @@ func TestMessageUnmarshalsRecentMessageReactions(t *testing.T) { "productId":"670e0cce840a8236ddd4ee4c", "emojiId":"143", "resourceType":1, - "version":1 + "version":"1" } } } @@ -85,11 +85,22 @@ func TestMessageUnmarshalsRecentMessageReactions(t *testing.T) { if paid.FromUserMID != "U-paid" || paid.AtMillis.String() != "1784930400456" || paid.ReactionType.PaidReactionType == nil || paid.ReactionType.PaidReactionType.ProductID != "670e0cce840a8236ddd4ee4c" || - paid.ReactionType.PaidReactionType.EmojiID != "143" { + paid.ReactionType.PaidReactionType.EmojiID != "143" || + paid.ReactionType.PaidReactionType.Version != 1 { t.Fatalf("paid reaction = %#v", paid) } } +func TestPaidReactionTypeUnmarshalsNumericVersion(t *testing.T) { + var reaction PaidReactionType + if err := json.Unmarshal([]byte(`{"version":1}`), &reaction); err != nil { + t.Fatal(err) + } + if reaction.Version != 1 { + t.Fatalf("version = %d, want 1", reaction.Version) + } +} + func TestMessageDistinguishesMissingAndEmptyReactions(t *testing.T) { var missing Message if err := json.Unmarshal([]byte(`{"id":"missing"}`), &missing); err != nil {