Add notifications

This commit is contained in:
ekzyis 2024-09-19 02:56:47 +02:00
parent d3f5fce46d
commit bec781cd09
1 changed files with 85 additions and 0 deletions

85
notifications.go Normal file
View File

@ -0,0 +1,85 @@
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: `
fragment ItemFields on Item {
id
user {
id
name
}
parentId
createdAt
deletedAt
title
text
}
query notifications {
notifications {
notifications {
__typename
... on Reply {
id
item {
...ItemFields
}
}
... on Mention {
id
item {
...ItemFields
}
}
}
}
}
`,
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
}