Skip to content

Commit 624996b

Browse files
authored
fix: accept string versions in paid reactions (#230)
1 parent 0fc10ea commit 624996b

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

pkg/line/reaction.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ type PaidReactionType struct {
6161
Version int `json:"version,omitempty"`
6262
}
6363

64+
func (p *PaidReactionType) UnmarshalJSON(data []byte) error {
65+
var parsed struct {
66+
ProductID string `json:"productId"`
67+
EmojiID string `json:"emojiId"`
68+
ResourceType int `json:"resourceType,omitempty"`
69+
Version FlexInt `json:"version,omitempty"`
70+
}
71+
if err := json.Unmarshal(data, &parsed); err != nil {
72+
return err
73+
}
74+
p.ProductID = parsed.ProductID
75+
p.EmojiID = parsed.EmojiID
76+
p.ResourceType = parsed.ResourceType
77+
p.Version = parsed.Version.Val
78+
return nil
79+
}
80+
6481
type ReactionType struct {
6582
PredefinedReactionType int `json:"predefinedReactionType,omitempty"`
6683
PaidReactionType *PaidReactionType `json:"paidReactionType,omitempty"`

pkg/line/reaction_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,30 @@ func TestCancelReactionRequestBody(t *testing.T) {
101101
}
102102
}
103103

104+
func TestGetRecentMessagesV2AcceptsPaidReactionStringVersion(t *testing.T) {
105+
client := newReactionTestClientWithResponse(
106+
t,
107+
"/api/talk/thrift/Talk/TalkService/getRecentMessagesV2",
108+
`{"code":0,"message":"ok","data":[
109+
{"id":"newer"},
110+
{"id":"affected","reactions":[{"fromUserMid":"U-paid","atMillis":"1784930400456","reactionType":{"paidReactionType":{"productId":"product","emojiId":"143","resourceType":1,"version":"1"}}}]}
111+
]}`,
112+
nil,
113+
)
114+
115+
messages, err := client.GetRecentMessagesV2("U-chat", 50)
116+
if err != nil {
117+
t.Fatal(err)
118+
}
119+
if len(messages) != 2 {
120+
t.Fatalf("message count = %d, want 2", len(messages))
121+
}
122+
paid := messages[1].Reactions[0].ReactionType.PaidReactionType
123+
if paid == nil || paid.Version != 1 {
124+
t.Fatalf("paid reaction = %#v, want version 1", paid)
125+
}
126+
}
127+
104128
func TestReactNonZeroWrapperKeepsInvalidPaidReactionDetails(t *testing.T) {
105129
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)
106130
err := client.React(123, "616934195205767730", ReactionType{

pkg/line/structs_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestMessageUnmarshalsRecentMessageReactions(t *testing.T) {
6464
"productId":"670e0cce840a8236ddd4ee4c",
6565
"emojiId":"143",
6666
"resourceType":1,
67-
"version":1
67+
"version":"1"
6868
}
6969
}
7070
}
@@ -85,11 +85,22 @@ func TestMessageUnmarshalsRecentMessageReactions(t *testing.T) {
8585
if paid.FromUserMID != "U-paid" || paid.AtMillis.String() != "1784930400456" ||
8686
paid.ReactionType.PaidReactionType == nil ||
8787
paid.ReactionType.PaidReactionType.ProductID != "670e0cce840a8236ddd4ee4c" ||
88-
paid.ReactionType.PaidReactionType.EmojiID != "143" {
88+
paid.ReactionType.PaidReactionType.EmojiID != "143" ||
89+
paid.ReactionType.PaidReactionType.Version != 1 {
8990
t.Fatalf("paid reaction = %#v", paid)
9091
}
9192
}
9293

94+
func TestPaidReactionTypeUnmarshalsNumericVersion(t *testing.T) {
95+
var reaction PaidReactionType
96+
if err := json.Unmarshal([]byte(`{"version":1}`), &reaction); err != nil {
97+
t.Fatal(err)
98+
}
99+
if reaction.Version != 1 {
100+
t.Fatalf("version = %d, want 1", reaction.Version)
101+
}
102+
}
103+
93104
func TestMessageDistinguishesMissingAndEmptyReactions(t *testing.T) {
94105
var missing Message
95106
if err := json.Unmarshal([]byte(`{"id":"missing"}`), &missing); err != nil {

0 commit comments

Comments
 (0)