Introduce new type: MastodonDate
authorMikael Berthe <mikael@lilotux.net>
Thu, 06 Sep 2018 11:28:42 +0200
changeset 239 ca5639b4768e
parent 238 1c0042e76902
child 240 80c81e9b77b4
Introduce new type: MastodonDate This type replaces the previous 'ActivityTime' since this weird timestamp format is now used by several Mastodon API endpoints...
api.go
instance.go
types.go
--- a/api.go	Thu Sep 06 01:07:40 2018 +0200
+++ b/api.go	Thu Sep 06 11:28:42 2018 +0200
@@ -16,6 +16,7 @@
 	"regexp"
 	"strconv"
 	"strings"
+	"time"
 
 	"github.com/pkg/errors"
 	"github.com/sendgrid/rest"
@@ -229,3 +230,29 @@
 	}
 	return nil
 }
+
+/* Mastodon timestamp handling */
+
+// MastodonDate is a custom type for the timestamps returned by some API calls
+// It is used, for example, by 'v1/instance/activity' and 'v2/search'.
+// The date returned by those Mastodon API calls is a string containing a
+// timestamp in seconds...
+
+// UnmarshalJSON handles deserialization for custom MastodonDate type
+func (act *MastodonDate) 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 MastodonDate type
+func (act *MastodonDate) MarshalJSON() ([]byte, error) {
+	return []byte(fmt.Sprintf("\"%d\"", act.Unix())), nil
+}
--- a/instance.go	Thu Sep 06 01:07:40 2018 +0200
+++ b/instance.go	Thu Sep 06 11:28:42 2018 +0200
@@ -7,11 +7,6 @@
 package madon
 
 import (
-	"fmt"
-	"strconv"
-	"strings"
-	"time"
-
 	"github.com/sendgrid/rest"
 )
 
@@ -45,24 +40,3 @@
 	}
 	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	Thu Sep 06 01:07:40 2018 +0200
+++ b/types.go	Thu Sep 06 11:28:42 2018 +0200
@@ -11,8 +11,8 @@
 	"time"
 )
 
-// ActivityTime is a custom type for the time returned by instance/activity
-type ActivityTime struct {
+// MastodonDate is a custom type for the timestamps returned by some API calls
+type MastodonDate struct {
 	time.Time
 }
 
@@ -234,15 +234,15 @@
 	Name    string `json:"name"`
 	URL     string `json:"url"`
 	History []struct {
-		Day      int64 `json:"day"`
-		Uses     int64 `json:"uses"`
-		Accounts int64 `json:"accounts"`
+		Day      MastodonDate `json:"day"`
+		Uses     int64        `json:"uses,string"`
+		Accounts int64        `json:"accounts,string"`
 	} `json:"history"`
 }
 
 // WeekActivity represents a Mastodon instance activity "week" entity
 type WeekActivity struct {
-	Week          ActivityTime `json:"week"`
+	Week          MastodonDate `json:"week"`
 	Statuses      int64        `json:"statuses,string"`
 	Logins        int64        `json:"logins,string"`
 	Registrations int64        `json:"registrations,string"`