Compare commits

...

3 Commits

Author SHA1 Message Date
ekzyis 791eea60ef Add notifications 2024-09-19 02:56:47 +02:00
ekzyis d3f5fce46d Fix Item.DeletedAt missing 2024-09-19 02:46:51 +02:00
ekzyis c184d18431 Remove unused fields from items query 2024-09-19 02:46:03 +02:00
4 changed files with 92 additions and 60 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module github.com/ekzyis/snappy module github.com/ekzyis/snappy
go 1.20 go 1.20
require gopkg.in/guregu/null.v4 v4.0.0 // indirect

2
go.sum
View File

@ -0,0 +1,2 @@
gopkg.in/guregu/null.v4 v4.0.0 h1:1Wm3S1WEA2I26Kq+6vcW+w0gcDo44YKYD7YIEJNHDjg=
gopkg.in/guregu/null.v4 v4.0.0/go.mod h1:YoQhUrADuG3i9WqesrCmpNRwm1ypAgSHYqoOcTu/JrI=

View File

@ -4,6 +4,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"time" "time"
"gopkg.in/guregu/null.v4"
) )
type Item struct { type Item struct {
@ -14,6 +16,7 @@ type Item struct {
Text string `json:"text"` Text string `json:"text"`
Sats int `json:"sats"` Sats int `json:"sats"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
DeletedAt null.Time `json:"deletedAt"`
Comments []Comment `json:"comments"` Comments []Comment `json:"comments"`
NComments int `json:"ncomments"` NComments int `json:"ncomments"`
User User `json:"user"` User User `json:"user"`
@ -118,42 +121,17 @@ func (c *Client) Item(id int) (*Item, error) {
item(id: $id) { item(id: $id) {
id id
parentId parentId
createdAt
deletedAt
title title
url url
text
sats
createdAt
deletedAt
ncomments
user { user {
id id
name name
} }
otsHash
position
sats
boost
bounty
bountyPaidTo
path
upvotes
meSats
meDontLikeSats
meBookmark
meSubscription
outlawed
freebie
ncomments
commentSats
lastCommentAt
maxBid
isJob
company
location
remote
subName
pollCost
status
uploadId
mine
position
} }
}`, }`,
Variables: map[string]interface{}{ Variables: map[string]interface{}{
@ -194,43 +172,17 @@ func (c *Client) Items(query *ItemsQuery) (*ItemsCursor, error) {
items { items {
id id
parentId parentId
createdAt
deletedAt
title title
url url
text text
sats
createdAt
deletedAt
ncomments
user { user {
id id
name name
} }
otsHash
position
sats
boost
bounty
bountyPaidTo
path
upvotes
meSats
meDontLikeSats
meBookmark
meSubscription
outlawed
freebie
ncomments
commentSats
lastCommentAt
maxBid
isJob
company
location
remote
subName
pollCost
status
uploadId
mine
position
}, },
} }
}`, }`,

76
notifications.go Normal file
View File

@ -0,0 +1,76 @@
package sn
import (
"encoding/json"
"fmt"
"time"
)
type Notification struct {
Id int `json:"id,string"`
Type string `json:"__typename"`
Item Item `json:"item"`
User User `json:"user"`
}
type NotificationsCursor struct {
LastChecked time.Time
Cursor string
Notifications []Notification
}
type NotificationsResponse struct {
Errors []GqlError `json:"errors"`
Data struct {
Notifications NotificationsCursor `json:"notifications"`
} `json:"data"`
}
func (c *Client) Notifications() (*NotificationsCursor, error) {
body := GqlBody{
Query: `
query notifications {
notifications {
notifications {
__typename
... on Mention {
id
item {
id
user {
id
name
}
parentId
createdAt
deletedAt
title
text
}
}
}
}
}
`,
Variables: map[string]interface{}{},
}
resp, err := c.callApi(body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var respBody NotificationsResponse
err = json.NewDecoder(resp.Body).Decode(&respBody)
if err != nil {
err = fmt.Errorf("error decoding notifications: %w", err)
return nil, err
}
err = c.checkForErrors(respBody.Errors)
if err != nil {
return nil, err
}
return &respBody.Data.Notifications, nil
}