Add items query

This commit is contained in:
ekzyis 2023-08-23 01:28:37 +02:00
parent 76cb7483f7
commit e6c93d21d5
3 changed files with 188 additions and 79 deletions

168
items.go Normal file
View File

@ -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
}

75
post.go
View File

@ -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
}

View File

@ -15,6 +15,7 @@ type GraphQLError struct {
} }
type User struct { type User struct {
Id int `json:"id,string"`
Name string `json:"name"` Name string `json:"name"`
} }
@ -34,12 +35,14 @@ type CreateCommentsResponse struct {
type Item struct { type Item struct {
Id int `json:"id,string"` Id int `json:"id,string"`
ParentId int `json:"parentId,string"`
Title string `json:"title"` Title string `json:"title"`
Url string `json:"url"` Url string `json:"url"`
Sats int `json:"sats"` Sats int `json:"sats"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
Comments []Comment `json:"comments"` Comments []Comment `json:"comments"`
NComments int `json:"ncomments"` NComments int `json:"ncomments"`
User User `json:"user"`
} }
type UpsertLinkResponse struct { type UpsertLinkResponse struct {
@ -52,13 +55,15 @@ type UpsertLinkResponse struct {
type ItemsResponse struct { type ItemsResponse struct {
Errors []GraphQLError `json:"errors"` Errors []GraphQLError `json:"errors"`
Data struct { Data struct {
Items struct { Items ItemsCursor `json:"items"`
Items []Item `json:"items"`
Cursor string `json:"cursor"`
} `json:"items"`
} `json:"data"` } `json:"data"`
} }
type ItemsCursor struct {
Items []Item `json:"items"`
Cursor string `json:"cursor"`
}
type HasNewNotesResponse struct { type HasNewNotesResponse struct {
Errors []GraphQLError `json:"errors"` Errors []GraphQLError `json:"errors"`
Data struct { Data struct {
@ -120,3 +125,14 @@ type RssDate struct {
type RssAuthor struct { type RssAuthor struct {
Name string `xml:"name"` Name string `xml:"name"`
} }
type ItemsQuery struct {
Sub string
Sort string
Type string
Cursor string
Name string
When string
By string
Limit int
}