From 3240163fc642ccc059f548739fd0da428d36d960 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Tue, 2 Dec 2025 20:14:14 +0100 Subject: [PATCH] Fix parsing of PayIn response --- client.go | 18 +++++++++++++++ items.go | 69 +++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 67 insertions(+), 20 deletions(-) diff --git a/client.go b/client.go index 18c9f8d..19dc392 100644 --- a/client.go +++ b/client.go @@ -107,3 +107,21 @@ func (c *Client) checkForErrors(err []GqlError) error { } return nil } + +func (c *Client) checkForPayInErrors(payIn PayIn) error { + if payIn.PayInFailureReason != "" { + return fmt.Errorf("mutation failed: %s", payIn.PayInFailureReason) + } + + bolt11 := payIn.PayerPrivates.PayInBolt11 + if bolt11.Id != 0 { + return fmt.Errorf("mutation failed: bolt11 payment required") + } + + result := payIn.PayerPrivates.Result + if result.Id == 0 { + return fmt.Errorf("mutation failed: no result id") + } + + return nil +} diff --git a/items.go b/items.go index 0de1e00..4ff3554 100644 --- a/items.go +++ b/items.go @@ -62,8 +62,16 @@ type ItemsResponse struct { } type PayIn struct { - Id int `json:"id"` - Item Item `json:"item"` + Id int `json:"id"` + PayInFailureReason string `json:"payInFailureReason"` + PayerPrivates struct { + PayInBolt11 struct { + Id int `json:"id"` + } `json:"payInBolt11"` + Result struct { + Id int `json:"id"` + } `json:"result"` + } `json:"payerPrivates"` } type UpsertDiscussionResponse struct { @@ -226,8 +234,14 @@ func (c *Client) PostDiscussion(title string, text string, sub string) (int, err mutation upsertDiscussion($title: String!, $text: String, $sub: String) { upsertDiscussion(title: $title, text: $text, sub: $sub) { id - item { - id + payInFailureReason + payerPrivates { + payInBolt11 { + id + } + result { + id + } } } }`, @@ -256,12 +270,13 @@ func (c *Client) PostDiscussion(title string, text string, sub string) (int, err return -1, err } - item := respBody.Data.UpsertDiscussion.Item - if item.Id == 0 { - return -1, fmt.Errorf("API returned no item id") + payIn := respBody.Data.UpsertDiscussion + err = c.checkForPayInErrors(payIn) + if err != nil { + return -1, err } - return item.Id, nil + return payIn.PayerPrivates.Result.Id, nil } func (c *Client) PostLink(url string, title string, text string, sub string) (int, error) { @@ -270,8 +285,14 @@ func (c *Client) PostLink(url string, title string, text string, sub string) (in mutation upsertLink($url: String!, $title: String!, $text: String, $sub: String!) { upsertLink(url: $url, title: $title, text: $text, sub: $sub) { id - item { - id + payInFailureReason + payerPrivates { + payInBolt11 { + id + } + result { + id + } } } }`, @@ -301,12 +322,13 @@ func (c *Client) PostLink(url string, title string, text string, sub string) (in return -1, err } - item := respBody.Data.UpsertLink.Item - if item.Id == 0 { - return -1, fmt.Errorf("API returned no item id") + payIn := respBody.Data.UpsertLink + err = c.checkForPayInErrors(payIn) + if err != nil { + return -1, err } - return item.Id, nil + return payIn.PayerPrivates.Result.Id, nil } func (c *Client) CreateComment(parentId int, text string) (int, error) { @@ -315,8 +337,14 @@ func (c *Client) CreateComment(parentId int, text string) (int, error) { mutation upsertComment($parentId: ID!, $text: String!) { upsertComment(parentId: $parentId, text: $text) { id - item { - id + payInFailureReason + payerPrivates { + payInBolt11 { + id + } + result { + id + } } } }`, @@ -344,12 +372,13 @@ func (c *Client) CreateComment(parentId int, text string) (int, error) { return -1, err } - item := respBody.Data.UpsertComment.Item - if item.Id == 0 { - return -1, fmt.Errorf("API returned no item id") + payIn := respBody.Data.UpsertComment + err = c.checkForPayInErrors(payIn) + if err != nil { + return -1, err } - return item.Id, nil + return payIn.PayerPrivates.Result.Id, nil } func (c *Client) Dupes(url string) (*[]Dupe, error) {