notification.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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, includes, excludes []string) ([]*Notification, error) {
  23. var notifications []*Notification
  24. params := url.Values{}
  25. for _, include := range includes {
  26. params.Add("include_types[]", include)
  27. }
  28. for _, exclude := range excludes {
  29. params.Add("exclude_types[]", exclude)
  30. }
  31. err := c.doAPI(ctx, http.MethodGet, "/api/v1/notifications", params, &notifications, pg)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return notifications, nil
  36. }
  37. // GetNotification return notification.
  38. func (c *Client) GetNotification(ctx context.Context, id string) (*Notification, error) {
  39. var notification Notification
  40. err := c.doAPI(ctx, http.MethodGet, fmt.Sprintf("/api/v1/notifications/%v", id), nil, &notification, nil)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return &notification, nil
  45. }
  46. // ClearNotifications clear notifications.
  47. func (c *Client) ClearNotifications(ctx context.Context) error {
  48. return c.doAPI(ctx, http.MethodPost, "/api/v1/notifications/clear", nil, nil, nil)
  49. }
  50. // ReadNotifications marks notifications as read
  51. // Currenly only works for Pleroma
  52. func (c *Client) ReadNotifications(ctx context.Context, maxID string) error {
  53. params := url.Values{}
  54. params.Set("max_id", maxID)
  55. return c.doAPI(ctx, http.MethodPost, "/api/v1/pleroma/notifications/read", params, nil, nil)
  56. }