package mastodon import ( "crypto/ecdsa" "crypto/elliptic" "encoding/base64" "fmt" "net/http" "net/url" "strconv" "time" ) type Notification struct { ID ID `json:"id"` Type string `json:"type"` CreatedAt time.Time `json:"created_at"` Account Account `json:"account"` Status *Status `json:"status"` } func (n *Notification) GetID() string { if n != nil { return string(n.ID) } else { return "" } } type PushSubscription struct { ID ID `json:"id"` Endpoint string `json:"endpoint"` ServerKey string `json:"server_key"` Alerts *PushAlerts `json:"alerts"` } type PushAlerts struct { Follow *Sbool `json:"follow"` Favourite *Sbool `json:"favourite"` Reblog *Sbool `json:"reblog"` Mention *Sbool `json:"mention"` } func GetNotifications(pg *Pagination) ([]*Notification, error) { var notifications []*Notification err := doAPI(http.MethodGet, "/api/v1/notifications", nil, ¬ifications, pg) if err != nil { return nil, err } return notifications, nil } func GetNotification(id ID) (*Notification, error) { var notification Notification err := doAPI(http.MethodGet, fmt.Sprintf("/api/v1/notifications/%v", id), nil, ¬ification, nil) if err != nil { return nil, err } return ¬ification, nil } func (n *Notification) Dismiss() error { return doAPI(http.MethodPost, fmt.Sprintf("/api/v1/notifications/%v/dismiss", n.GetID()), nil, nil, nil) } func ClearNotifications() error { return doAPI(http.MethodPost, "/api/v1/notifications/clear", nil, nil, nil) } func AddPushSubscription(endpoint string, public ecdsa.PublicKey, shared []byte, alerts PushAlerts) (*PushSubscription, error) { var subscription PushSubscription pk := elliptic.Marshal(public.Curve, public.X, public.Y) params := url.Values{} params.Add("subscription[endpoint]", endpoint) params.Add("subscription[keys][p256dh]", base64.RawURLEncoding.EncodeToString(pk)) params.Add("subscription[keys][auth]", base64.RawURLEncoding.EncodeToString(shared)) if alerts.Follow != nil { params.Add("data[alerts][follow]", strconv.FormatBool(bool(*alerts.Follow))) } if alerts.Favourite != nil { params.Add("data[alerts][favourite]", strconv.FormatBool(bool(*alerts.Favourite))) } if alerts.Reblog != nil { params.Add("data[alerts][reblog]", strconv.FormatBool(bool(*alerts.Reblog))) } if alerts.Mention != nil { params.Add("data[alerts][mention]", strconv.FormatBool(bool(*alerts.Mention))) } err := doAPI(http.MethodPost, "/api/v1/push/subscription", params, &subscription, nil) if err != nil { return nil, err } return &subscription, nil } // UpdatePushSubscription updates which type of notifications are sent for the active push subscription. func UpdatePushSubscription(alerts *PushAlerts) (*PushSubscription, error) { var subscription PushSubscription params := url.Values{} if alerts.Follow != nil { params.Add("data[alerts][follow]", strconv.FormatBool(bool(*alerts.Follow))) } if alerts.Mention != nil { params.Add("data[alerts][favourite]", strconv.FormatBool(bool(*alerts.Favourite))) } if alerts.Reblog != nil { params.Add("data[alerts][reblog]", strconv.FormatBool(bool(*alerts.Reblog))) } if alerts.Mention != nil { params.Add("data[alerts][mention]", strconv.FormatBool(bool(*alerts.Mention))) } err := doAPI(http.MethodPut, "/api/v1/push/subscription", params, &subscription, nil) if err != nil { return nil, err } return &subscription, nil } func RemovePushSubscription() error { return doAPI(http.MethodDelete, "/api/v1/push/subscription", nil, nil, nil) } func GetPushSubscription() (*PushSubscription, error) { var subscription PushSubscription err := doAPI(http.MethodGet, "/api/v1/push/subscription", nil, &subscription, nil) if err != nil { return nil, err } return &subscription, nil }