# HG changeset patch # User Mikael Berthe # Date 1521641214 -3600 # Node ID f9228d1f42673e22a49f70cbb70d6c3c62251e55 # Parent 8794323cabbe634f849060ef945627ee91361628 instance activity: Add a WeekActivity type diff -r 8794323cabbe -r f9228d1f4267 instance.go --- a/instance.go Wed Mar 21 13:19:50 2018 +0100 +++ b/instance.go Wed Mar 21 15:06:54 2018 +0100 @@ -7,6 +7,11 @@ package madon import ( + "fmt" + "strconv" + "strings" + "time" + "github.com/sendgrid/rest" ) @@ -20,6 +25,8 @@ } // GetInstancePeers returns current instance peers +// The peers are defined as the domains of users the instance has previously +// resolved. func (mc *Client) GetInstancePeers() ([]InstancePeer, error) { var peers []InstancePeer if err := mc.apiCall("instance/peers", rest.Get, nil, nil, nil, &peers); err != nil { @@ -29,10 +36,31 @@ } // GetInstanceActivity returns current instance activity -func (mc *Client) GetInstanceActivity() (interface{}, error) { - var activity interface{} +func (mc *Client) GetInstanceActivity() ([]WeekActivity, error) { + var activity []WeekActivity if err := mc.apiCall("instance/activity", rest.Get, nil, nil, nil, &activity); err != nil { return nil, err } return activity, nil } + +/* Activity time handling */ + +// UnmarshalJSON handles deserialization for custom ActivityTime type +func (act *ActivityTime) UnmarshalJSON(b []byte) error { + s, err := strconv.ParseInt(strings.Trim(string(b), "\""), 10, 64) + if err != nil { + return err + } + if s == 0 { + act.Time = time.Time{} + return nil + } + act.Time = time.Unix(s, 0) + return nil +} + +// MarshalJSON handles serialization for custom ActivityTime type +func (act *ActivityTime) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf("\"%d\"", act.Unix())), nil +} diff -r 8794323cabbe -r f9228d1f4267 types.go --- a/types.go Wed Mar 21 13:19:50 2018 +0100 +++ b/types.go Wed Mar 21 15:06:54 2018 +0100 @@ -11,6 +11,11 @@ "time" ) +// ActivityTime is a custom type for the time returned by instance/activity +type ActivityTime struct { + time.Time +} + // Client contains data for a madon client application type Client struct { Name string // Name of the client @@ -227,3 +232,11 @@ Name string `json:"name"` URL string `json:"url"` } + +// WeekActivity represents a Mastodon instance activity "week" entity +type WeekActivity struct { + Week ActivityTime `json:"week"` + Statuses int64 `json:"statuses,string"` + Logins int64 `json:"logins,string"` + Registrations int64 `json:"registrations,string"` +}