From e6c93d21d52d49d84665ccfcb073858a5545c8d8 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Wed, 23 Aug 2023 01:28:37 +0200 Subject: [PATCH] Add items query --- items.go | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ post.go | 75 ------------------------- types.go | 24 ++++++-- 3 files changed, 188 insertions(+), 79 deletions(-) create mode 100644 items.go delete mode 100644 post.go diff --git a/items.go b/items.go new file mode 100644 index 0000000..3fef1a6 --- /dev/null +++ b/items.go @@ -0,0 +1,168 @@ +package sn + +import ( + "encoding/json" + "fmt" +) + +func Items(query *ItemsQuery) (*ItemsCursor, error) { + if query == nil { + query = &ItemsQuery{} + } + + if sub := query.Sub; sub != "" { + if !(sub == "bitcoin" || sub == "nostr" || sub == "tech" || sub == "meta") { + return nil, fmt.Errorf("invalid sub: %s", sub) + } + } + + body := GraphQLPayload{ + Query: ` + query items($sub: String, $sort: String, $cursor: String, $type: String, $name: String, $when: String, $by: String, $limit: Int) { + items(sub: $sub, sort: $sort, cursor: $cursor, type: $type, name: $name, when: $when, by: $by, limit: $limit) { + cursor + items { + id + parentId + createdAt + deletedAt + title + url + user { + id + name + } + fwdUserId + otsHash + position + sats + boost + bounty + bountyPaidTo + path + upvotes + meSats + meDontLike + meBookmark + meSubscription + outlawed + freebie + ncomments + commentSats + lastCommentAt + maxBid + isJob + company + location + remote + subName + pollCost + status + uploadId + mine + position + }, + } + }`, + Variables: map[string]interface{}{ + "sub": query.Sub, + "sort": query.Sort, + "type": query.Type, + "cursor": query.Cursor, + "name": query.Name, + "when": query.When, + "by": query.By, + "limit": query.Limit, + }, + } + if query.Limit == 0 { + body.Variables["limit"] = 21 + } + + resp, err := MakeStackerNewsRequest(body) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + var respBody ItemsResponse + err = json.NewDecoder(resp.Body).Decode(&respBody) + if err != nil { + err = fmt.Errorf("error decoding items: %w", err) + return nil, err + } + err = CheckForErrors(respBody.Errors) + if err != nil { + return nil, err + } + return &respBody.Data.Items, nil +} + +// Create a new LINK post +func PostLink(url string, title string, sub string) (int, error) { + body := GraphQLPayload{ + Query: ` + mutation upsertLink($url: String!, $title: String!, $sub: String!) { + upsertLink(url: $url, title: $title, sub: $sub) { + id + } + }`, + Variables: map[string]interface{}{ + "url": url, + "title": title, + "sub": sub, + }, + } + resp, err := MakeStackerNewsRequest(body) + if err != nil { + return -1, err + } + defer resp.Body.Close() + + var respBody UpsertLinkResponse + err = json.NewDecoder(resp.Body).Decode(&respBody) + if err != nil { + err = fmt.Errorf("error decoding SN upsertLink: %w", err) + return -1, err + } + err = CheckForErrors(respBody.Errors) + if err != nil { + return -1, err + } + itemId := respBody.Data.UpsertLink.Id + return itemId, nil +} + +// Create a new comment +func CreateComment(parentId int, text string) (int, error) { + body := GraphQLPayload{ + Query: ` + mutation createComment($text: String!, $parentId: ID!) { + createComment(text: $text, parentId: $parentId) { + id + } + }`, + Variables: map[string]interface{}{ + "text": text, + "parentId": parentId, + }, + } + resp, err := MakeStackerNewsRequest(body) + if err != nil { + return -1, err + } + defer resp.Body.Close() + + var respBody CreateCommentsResponse + err = json.NewDecoder(resp.Body).Decode(&respBody) + if err != nil { + err = fmt.Errorf("error decoding SN createComment: %w", err) + return -1, err + } + err = CheckForErrors(respBody.Errors) + if err != nil { + return -1, err + } + + return parentId, nil +} diff --git a/post.go b/post.go deleted file mode 100644 index 14713d2..0000000 --- a/post.go +++ /dev/null @@ -1,75 +0,0 @@ -package sn - -import ( - "encoding/json" - "fmt" -) - -// Create a new LINK post -func PostLink(url string, title string, sub string) (int, error) { - body := GraphQLPayload{ - Query: ` - mutation upsertLink($url: String!, $title: String!, $sub: String!) { - upsertLink(url: $url, title: $title, sub: $sub) { - id - } - }`, - Variables: map[string]interface{}{ - "url": url, - "title": title, - "sub": sub, - }, - } - resp, err := MakeStackerNewsRequest(body) - if err != nil { - return -1, err - } - defer resp.Body.Close() - - var respBody UpsertLinkResponse - err = json.NewDecoder(resp.Body).Decode(&respBody) - if err != nil { - err = fmt.Errorf("error decoding SN upsertLink: %w", err) - return -1, err - } - err = CheckForErrors(respBody.Errors) - if err != nil { - return -1, err - } - itemId := respBody.Data.UpsertLink.Id - return itemId, nil -} - -// Create a new comment -func CreateComment(parentId int, text string) (int, error) { - body := GraphQLPayload{ - Query: ` - mutation createComment($text: String!, $parentId: ID!) { - createComment(text: $text, parentId: $parentId) { - id - } - }`, - Variables: map[string]interface{}{ - "text": text, - "parentId": parentId, - }, - } - resp, err := MakeStackerNewsRequest(body) - if err != nil { - return -1, err - } - defer resp.Body.Close() - - var respBody CreateCommentsResponse - err = json.NewDecoder(resp.Body).Decode(&respBody) - if err != nil { - err = fmt.Errorf("error decoding SN createComment: %w", err) - return -1, err - } - err = CheckForErrors(respBody.Errors) - if err != nil { - return -1, err - } - - return parentId, nil -} diff --git a/types.go b/types.go index ecbfae1..1cddd93 100644 --- a/types.go +++ b/types.go @@ -15,6 +15,7 @@ type GraphQLError struct { } type User struct { + Id int `json:"id,string"` Name string `json:"name"` } @@ -34,12 +35,14 @@ type CreateCommentsResponse struct { type Item struct { Id int `json:"id,string"` + ParentId int `json:"parentId,string"` Title string `json:"title"` Url string `json:"url"` Sats int `json:"sats"` CreatedAt time.Time `json:"createdAt"` Comments []Comment `json:"comments"` NComments int `json:"ncomments"` + User User `json:"user"` } type UpsertLinkResponse struct { @@ -52,13 +55,15 @@ type UpsertLinkResponse struct { type ItemsResponse struct { Errors []GraphQLError `json:"errors"` Data struct { - Items struct { - Items []Item `json:"items"` - Cursor string `json:"cursor"` - } `json:"items"` + Items ItemsCursor `json:"items"` } `json:"data"` } +type ItemsCursor struct { + Items []Item `json:"items"` + Cursor string `json:"cursor"` +} + type HasNewNotesResponse struct { Errors []GraphQLError `json:"errors"` Data struct { @@ -120,3 +125,14 @@ type RssDate struct { type RssAuthor struct { Name string `xml:"name"` } + +type ItemsQuery struct { + Sub string + Sort string + Type string + Cursor string + Name string + When string + By string + Limit int +}