From e0de6af20986117d5466e233bc162a2dbbf8824f Mon Sep 17 00:00:00 2001 From: Yamagishi Kazutoshi Date: Mon, 29 Jan 2018 12:29:39 +0900 Subject: [PATCH] Add GetInstanceActivity and GetInstancePeers --- README.md | 2 ++ instance.go | 28 +++++++++++++++++++++ instance_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ unixtime.go | 20 +++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 unixtime.go diff --git a/README.md b/README.md index a66ccb7..efc5630 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,8 @@ func main() { * [x] POST /api/v1/follow_requests/:id/reject * [x] POST /api/v1/follows * [x] GET /api/v1/instance +* [x] GET /api/v1/instance/activity +* [x] GET /api/v1/instance/peers * [x] POST /api/v1/media * [x] GET /api/v1/mutes * [x] GET /api/v1/notifications diff --git a/instance.go b/instance.go index 9b45c25..3d45591 100644 --- a/instance.go +++ b/instance.go @@ -22,3 +22,31 @@ func (c *Client) GetInstance(ctx context.Context) (*Instance, error) { } return &instance, nil } + +// WeeklyActivity hold information for mastodon weekly activity. +type WeeklyActivity struct { + Week Unixtime `json:"week"` + Statuses int64 `json:"statuses,string"` + Logins int64 `json:"logins,string"` + Registrations int64 `json:"registrations,string"` +} + +// GetInstanceActivity return instance activity. +func (c *Client) GetInstanceActivity(ctx context.Context) ([]*WeeklyActivity, error) { + var activity []*WeeklyActivity + err := c.doAPI(ctx, http.MethodGet, "/api/v1/instance/activity", nil, &activity, nil) + if err != nil { + return nil, err + } + return activity, nil +} + +// GetInstancePeers return instance peers. +func (c *Client) GetInstancePeers(ctx context.Context) ([]string, error) { + var peers []string + err := c.doAPI(ctx, http.MethodGet, "/api/v1/instance/peers", nil, &peers, nil) + if err != nil { + return nil, err + } + return peers, nil +} diff --git a/instance_test.go b/instance_test.go index 6724b53..b811c6c 100644 --- a/instance_test.go +++ b/instance_test.go @@ -6,6 +6,7 @@ import ( "net/http" "net/http/httptest" "testing" + "time" ) func TestGetInstance(t *testing.T) { @@ -38,3 +39,65 @@ func TestGetInstance(t *testing.T) { t.Fatalf("want %q but %q", "mastodon", ins.Title) } } + +func TestGetInstanceActivity(t *testing.T) { + canErr := true + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if canErr { + canErr = false + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + fmt.Fprintln(w, `[{"week":"1516579200","statuses":"1","logins":"1","registrations":"0"}]`) + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + }) + _, err := client.GetInstanceActivity(context.Background()) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + activity, err := client.GetInstanceActivity(context.Background()) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if activity[0].Week != Unixtime(time.Unix(1516579200, 0)) { + t.Fatalf("want %q but %q", Unixtime(time.Unix(1516579200, 0)), activity[0].Week) + } + if activity[0].Logins != 1 { + t.Fatalf("want %q but %q", 1, activity[0].Logins) + } +} + +func TestGetInstancePeers(t *testing.T) { + canErr := true + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if canErr { + canErr = false + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + fmt.Fprintln(w, `["mastodon.social","mstdn.jp"]`) + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + }) + _, err := client.GetInstancePeers(context.Background()) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + peers, err := client.GetInstancePeers(context.Background()) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if peers[0] != "mastodon.social" { + t.Fatalf("want %q but %q", "mastodon.social", peers[0]) + } + if peers[1] != "mstdn.jp" { + t.Fatalf("want %q but %q", "mstdn.jp", peers[1]) + } +} diff --git a/unixtime.go b/unixtime.go new file mode 100644 index 0000000..a935a9e --- /dev/null +++ b/unixtime.go @@ -0,0 +1,20 @@ +package mastodon + +import ( + "strconv" + "time" +) + +type Unixtime time.Time + +func (t *Unixtime) UnmarshalJSON(data []byte) error { + if len(data) > 0 && data[0] == '"' && data[len(data)-1] == '"' { + data = data[1 : len(data)-1] + } + ts, err := strconv.ParseInt(string(data), 10, 64) + if err != nil { + return err + } + *t = Unixtime(time.Unix(ts, 0)) + return nil +}