Add support for /api/v1/push/subscription

pull/118/head
buckket 2019-05-17 23:47:32 +02:00 committed by mattn
parent 25da74b864
commit 2abdb8e37c
4 changed files with 190 additions and 0 deletions

View File

@ -109,6 +109,10 @@ func main() {
* [x] GET /api/v1/notifications/:id * [x] GET /api/v1/notifications/:id
* [x] POST /api/v1/notifications/dismiss * [x] POST /api/v1/notifications/dismiss
* [x] POST /api/v1/notifications/clear * [x] POST /api/v1/notifications/clear
* [x] POST /api/v1/push/subscription
* [x] GET /api/v1/push/subscription
* [x] PUT /api/v1/push/subscription
* [x] DELETE /api/v1/push/subscription
* [x] GET /api/v1/reports * [x] GET /api/v1/reports
* [x] POST /api/v1/reports * [x] POST /api/v1/reports
* [x] GET /api/v1/search * [x] GET /api/v1/search

View File

@ -3,6 +3,7 @@ package mastodon
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"strconv"
) )
type ID string type ID string
@ -23,3 +24,26 @@ func (id *ID) UnmarshalJSON(data []byte) error {
*id = ID(fmt.Sprint(n)) *id = ID(fmt.Sprint(n))
return nil return nil
} }
type Sbool bool
func (s *Sbool) UnmarshalJSON(data []byte) error {
if len(data) > 0 && data[0] == '"' && data[len(data)-1] == '"' {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
b, err := strconv.ParseBool(str)
if err != nil {
return err
}
*s = Sbool(b)
return nil
}
var b bool
if err := json.Unmarshal(data, &b); err != nil {
return err
}
*s = Sbool(b)
return nil
}

View File

@ -2,9 +2,13 @@ package mastodon
import ( import (
"context" "context"
"crypto/ecdsa"
"crypto/elliptic"
"encoding/base64"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"time" "time"
) )
@ -17,6 +21,20 @@ type Notification struct {
Status *Status `json:"status"` Status *Status `json:"status"`
} }
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"`
}
// GetNotifications return notifications. // GetNotifications return notifications.
func (c *Client) GetNotifications(ctx context.Context, pg *Pagination) ([]*Notification, error) { func (c *Client) GetNotifications(ctx context.Context, pg *Pagination) ([]*Notification, error) {
var notifications []*Notification var notifications []*Notification
@ -52,3 +70,68 @@ func (c *Client) DismissNotification(ctx context.Context, id ID) error {
func (c *Client) ClearNotifications(ctx context.Context) error { func (c *Client) ClearNotifications(ctx context.Context) error {
return c.doAPI(ctx, http.MethodPost, "/api/v1/notifications/clear", nil, nil, nil) return c.doAPI(ctx, http.MethodPost, "/api/v1/notifications/clear", nil, nil, nil)
} }
// AddPushSubscription adds a new push subscription.
func (c *Client) AddPushSubscription(ctx context.Context, 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 := c.doAPI(ctx, 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 (c *Client) UpdatePushSubscription(ctx context.Context, 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 := c.doAPI(ctx, http.MethodPut, "/api/v1/push/subscription", params, &subscription, nil)
if err != nil {
return nil, err
}
return &subscription, nil
}
// RemovePushSubscription deletes the active push subscription.
func (c *Client) RemovePushSubscription(ctx context.Context) error {
return c.doAPI(ctx, http.MethodDelete, "/api/v1/push/subscription", nil, nil, nil)
}
// GetPushSubscription retrieves information about the active push subscription.
func (c *Client) GetPushSubscription(ctx context.Context) (*PushSubscription, error) {
var subscription PushSubscription
err := c.doAPI(ctx, http.MethodGet, "/api/v1/push/subscription", nil, &subscription, nil)
if err != nil {
return nil, err
}
return &subscription, nil
}

View File

@ -2,6 +2,9 @@ package mastodon
import ( import (
"context" "context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -64,3 +67,79 @@ func TestGetNotifications(t *testing.T) {
t.Fatalf("should not be fail: %v", err) t.Fatalf("should not be fail: %v", err)
} }
} }
func TestPushSubscription(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/push/subscription":
fmt.Fprintln(w, ` {"id":1,"endpoint":"https://example.org","alerts":{"follow":"true","favourite":"true","reblog":"true","mention":"true"},"server_key":"foobar"}`)
return
}
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
enabled := new(Sbool)
*enabled = true
alerts := PushAlerts{Follow: enabled, Favourite: enabled, Reblog: enabled, Mention: enabled}
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
shared := make([]byte, 16)
_, err = rand.Read(shared)
if err != nil {
t.Fatal(err)
}
testSub := func(sub *PushSubscription, err error) {
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if sub.ID != "1" {
t.Fatalf("want %v but %v", "1", sub.ID)
}
if sub.Endpoint != "https://example.org" {
t.Fatalf("want %v but %v", "https://example.org", sub.Endpoint)
}
if sub.ServerKey != "foobar" {
t.Fatalf("want %v but %v", "foobar", sub.ServerKey)
}
if *sub.Alerts.Favourite != true {
t.Fatalf("want %v but %v", true, *sub.Alerts.Favourite)
}
if *sub.Alerts.Mention != true {
t.Fatalf("want %v but %v", true, *sub.Alerts.Mention)
}
if *sub.Alerts.Reblog != true {
t.Fatalf("want %v but %v", true, *sub.Alerts.Reblog)
}
if *sub.Alerts.Follow != true {
t.Fatalf("want %v but %v", true, *sub.Alerts.Follow)
}
}
sub, err := client.AddPushSubscription(context.Background(), "http://example.org", priv.PublicKey, shared, alerts)
testSub(sub, err)
sub, err = client.GetPushSubscription(context.Background())
testSub(sub, err)
sub, err = client.UpdatePushSubscription(context.Background(), &alerts)
testSub(sub, err)
err = client.RemovePushSubscription(context.Background())
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
}