add GetNotificationsExclude

pull/171/head
Rasmus Lindroth 2022-12-30 11:18:51 +01:00
parent ad3aa348dd
commit fe547bf14f
2 changed files with 27 additions and 2 deletions

View File

@ -37,8 +37,19 @@ type PushAlerts struct {
// GetNotifications returns notifications.
func (c *Client) GetNotifications(ctx context.Context, pg *Pagination) ([]*Notification, error) {
return c.GetNotificationsExclude(ctx, nil, pg)
}
// GetNotificationsExclude returns notifications with excluded notifications
func (c *Client) GetNotificationsExclude(ctx context.Context, exclude *[]string, pg *Pagination) ([]*Notification, error) {
var notifications []*Notification
err := c.doAPI(ctx, http.MethodGet, "/api/v1/notifications", nil, &notifications, pg)
params := url.Values{}
if exclude != nil {
for _, ex := range *exclude {
params.Add("exclude_types[]", ex)
}
}
err := c.doAPI(ctx, http.MethodGet, "/api/v1/notifications", params, &notifications, pg)
if err != nil {
return nil, err
}

View File

@ -15,7 +15,11 @@ func TestGetNotifications(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v1/notifications":
if r.URL.Query().Get("exclude_types[]") == "follow" {
fmt.Fprintln(w, `[{"id": 321, "action_taken": true}]`)
} else {
fmt.Fprintln(w, `[{"id": 122, "action_taken": false}, {"id": 123, "action_taken": true}]`)
}
return
case "/api/v1/notifications/123":
fmt.Fprintln(w, `{"id": 123, "action_taken": true}`)
@ -50,6 +54,16 @@ func TestGetNotifications(t *testing.T) {
if ns[1].ID != "123" {
t.Fatalf("want %v but %v", "123", ns[1].ID)
}
nse, err := client.GetNotificationsExclude(context.Background(), &[]string{"follow"}, nil)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(nse) != 1 {
t.Fatalf("result should be one: %d", len(nse))
}
if nse[0].ID != "321" {
t.Fatalf("want %v but %v", "321", nse[0].ID)
}
n, err := client.GetNotification(context.Background(), "123")
if err != nil {
t.Fatalf("should not be fail: %v", err)