Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/line/reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
24 changes: 24 additions & 0 deletions pkg/line/reaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
15 changes: 13 additions & 2 deletions pkg/line/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestMessageUnmarshalsRecentMessageReactions(t *testing.T) {
"productId":"670e0cce840a8236ddd4ee4c",
"emojiId":"143",
"resourceType":1,
"version":1
"version":"1"
}
}
}
Expand All @@ -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 {
Expand Down
Loading