From c7498a9ed6d8b8e7411992c6acedab43004ffeb2 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Thu, 19 Sep 2024 02:56:47 +0200 Subject: [PATCH] Add notifications --- notifications.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 notifications.go diff --git a/notifications.go b/notifications.go new file mode 100644 index 0000000..6f36459 --- /dev/null +++ b/notifications.go @@ -0,0 +1,87 @@ +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 `json:"lastChecked"` + Cursor string `json:"cursor"` + Notifications []Notification `json:"notifications"` +} + +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 { + lastChecked + cursor + 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 +}