Add GetInstanceActivity and GetInstancePeers
parent
0d8819ecaf
commit
e0de6af209
|
@ -92,6 +92,8 @@ func main() {
|
||||||
* [x] POST /api/v1/follow_requests/:id/reject
|
* [x] POST /api/v1/follow_requests/:id/reject
|
||||||
* [x] POST /api/v1/follows
|
* [x] POST /api/v1/follows
|
||||||
* [x] GET /api/v1/instance
|
* [x] GET /api/v1/instance
|
||||||
|
* [x] GET /api/v1/instance/activity
|
||||||
|
* [x] GET /api/v1/instance/peers
|
||||||
* [x] POST /api/v1/media
|
* [x] POST /api/v1/media
|
||||||
* [x] GET /api/v1/mutes
|
* [x] GET /api/v1/mutes
|
||||||
* [x] GET /api/v1/notifications
|
* [x] GET /api/v1/notifications
|
||||||
|
|
28
instance.go
28
instance.go
|
@ -22,3 +22,31 @@ func (c *Client) GetInstance(ctx context.Context) (*Instance, error) {
|
||||||
}
|
}
|
||||||
return &instance, nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetInstance(t *testing.T) {
|
func TestGetInstance(t *testing.T) {
|
||||||
|
@ -38,3 +39,65 @@ func TestGetInstance(t *testing.T) {
|
||||||
t.Fatalf("want %q but %q", "mastodon", ins.Title)
|
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])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue