instance activity: Add a WeekActivity type
authorMikael Berthe <mikael@lilotux.net>
Wed, 21 Mar 2018 15:06:54 +0100
changeset 221 f9228d1f4267
parent 220 8794323cabbe
child 222 348fce59d83d
instance activity: Add a WeekActivity type
instance.go
types.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
+}
--- 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"`
+}