# HG changeset patch # User Mikael Berthe # Date 1536226122 -7200 # Node ID ca5639b4768e0ed99346a211935c929da04b913f # Parent 1c0042e76902c69b7f7ea1b5ab43b5c1d835dc22 Introduce new type: MastodonDate This type replaces the previous 'ActivityTime' since this weird timestamp format is now used by several Mastodon API endpoints... diff -r 1c0042e76902 -r ca5639b4768e api.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 +} diff -r 1c0042e76902 -r ca5639b4768e instance.go --- 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 -} diff -r 1c0042e76902 -r ca5639b4768e types.go --- 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"`