2019-12-13 18:08:26 +00:00
|
|
|
package mastodon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-12-15 17:37:58 +00:00
|
|
|
"net/url"
|
2019-12-13 18:08:26 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-12-15 17:37:58 +00:00
|
|
|
type NotificationPleroma struct {
|
|
|
|
IsSeen bool `json:"is_seen"`
|
|
|
|
}
|
|
|
|
|
2019-12-13 18:08:26 +00:00
|
|
|
// Notification hold information for mastodon notification.
|
|
|
|
type Notification struct {
|
2019-12-15 17:37:58 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
Account Account `json:"account"`
|
|
|
|
Status *Status `json:"status"`
|
|
|
|
Pleroma *NotificationPleroma `json:"pleroma"`
|
2019-12-13 18:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetNotifications return notifications.
|
2021-12-13 13:58:15 +00:00
|
|
|
func (c *Client) GetNotifications(ctx context.Context, pg *Pagination, includes, excludes []string) ([]*Notification, error) {
|
2019-12-13 18:08:26 +00:00
|
|
|
var notifications []*Notification
|
2020-08-28 22:27:36 +00:00
|
|
|
params := url.Values{}
|
2021-12-13 13:58:15 +00:00
|
|
|
for _, include := range includes {
|
|
|
|
params.Add("include_types[]", include)
|
|
|
|
}
|
2020-08-28 22:27:36 +00:00
|
|
|
for _, exclude := range excludes {
|
|
|
|
params.Add("exclude_types[]", exclude)
|
|
|
|
}
|
|
|
|
err := c.doAPI(ctx, http.MethodGet, "/api/v1/notifications", params, ¬ifications, pg)
|
2019-12-13 18:08:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return notifications, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNotification return notification.
|
|
|
|
func (c *Client) GetNotification(ctx context.Context, id string) (*Notification, error) {
|
|
|
|
var notification Notification
|
|
|
|
err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/notifications/%v", id), nil, ¬ification, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ¬ification, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClearNotifications clear notifications.
|
|
|
|
func (c *Client) ClearNotifications(ctx context.Context) error {
|
|
|
|
return c.doAPI(ctx, http.MethodPost, "/api/v1/notifications/clear", nil, nil, nil)
|
|
|
|
}
|
2019-12-15 17:37:58 +00:00
|
|
|
|
|
|
|
// ReadNotifications marks notifications as read
|
|
|
|
// Currenly only works for Pleroma
|
|
|
|
func (c *Client) ReadNotifications(ctx context.Context, maxID string) error {
|
|
|
|
params := url.Values{}
|
|
|
|
params.Set("max_id", maxID)
|
|
|
|
return c.doAPI(ctx, http.MethodPost, "/api/v1/pleroma/notifications/read", params, nil, nil)
|
|
|
|
}
|