notification.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package mastodon
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "time"
  8. )
  9. type NotificationPleroma struct {
  10. IsSeen bool `json:"is_seen"`
  11. }
  12. // Notification hold information for mastodon notification.
  13. type Notification struct {
  14. ID string `json:"id"`
  15. Type string `json:"type"`
  16. CreatedAt time.Time `json:"created_at"`
  17. Account Account `json:"account"`
  18. Status *Status `json:"status"`
  19. Pleroma *NotificationPleroma `json:"pleroma"`
  20. }
  21. // GetNotifications return notifications.
  22. func (c *Client) GetNotifications(ctx context.Context, pg *Pagination, excludes []string) ([]*Notification, error) {
  23. var notifications []*Notification
  24. params := url.Values{}
  25. for _, exclude := range excludes {
  26. params.Add("exclude_types[]", exclude)
  27. }
  28. err := c.doAPI(ctx, http.MethodGet, "/api/v1/notifications", params, &notifications, pg)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return notifications, nil
  33. }
  34. // GetNotification return notification.
  35. func (c *Client) GetNotification(ctx context.Context, id string) (*Notification, error) {
  36. var notification Notification
  37. err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/notifications/%v", id), nil, &notification, nil)
  38. if err != nil {
  39. return nil, err
  40. }
  41. return &notification, nil
  42. }
  43. // ClearNotifications clear notifications.
  44. func (c *Client) ClearNotifications(ctx context.Context) error {
  45. return c.doAPI(ctx, http.MethodPost, "/api/v1/notifications/clear", nil, nil, nil)
  46. }
  47. // ReadNotifications marks notifications as read
  48. // Currenly only works for Pleroma
  49. func (c *Client) ReadNotifications(ctx context.Context, maxID string) error {
  50. params := url.Values{}
  51. params.Set("max_id", maxID)
  52. return c.doAPI(ctx, http.MethodPost, "/api/v1/pleroma/notifications/read", params, nil, nil)
  53. }