From cf265dbe2ca8a0a80bd8d7d8898a51b469c7ad17 Mon Sep 17 00:00:00 2001 From: James Hillyerd Date: Sat, 17 Nov 2018 20:00:04 -0800 Subject: [PATCH] rest: Add posix-millis field for easier date parsing --- CHANGELOG.md | 6 ++++++ pkg/rest/apiv1_controller.go | 36 ++++++++++++++++--------------- pkg/rest/apiv1_controller_test.go | 3 +++ pkg/rest/model/apiv1_model.go | 18 +++++++++------- pkg/rest/socketv1_controller.go | 15 +++++++------ pkg/rest/testutils_test.go | 6 ++++-- pkg/webui/mailbox_controller.go | 2 ++ 7 files changed, 52 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 76d14cd..696e6ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ Change Log All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] + +### Added +- `posix-millis` field to REST message and header responses for easier date + parsing. + ## [v2.1.0-beta1] diff --git a/pkg/rest/apiv1_controller.go b/pkg/rest/apiv1_controller.go index 09ff079..1e68013 100644 --- a/pkg/rest/apiv1_controller.go +++ b/pkg/rest/apiv1_controller.go @@ -31,14 +31,15 @@ func MailboxListV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) ( jmessages := make([]*model.JSONMessageHeaderV1, len(messages)) for i, msg := range messages { jmessages[i] = &model.JSONMessageHeaderV1{ - Mailbox: name, - ID: msg.ID, - From: stringutil.StringAddress(msg.From), - To: stringutil.StringAddressList(msg.To), - Subject: msg.Subject, - Date: msg.Date, - Size: msg.Size, - Seen: msg.Seen, + Mailbox: name, + ID: msg.ID, + From: stringutil.StringAddress(msg.From), + To: stringutil.StringAddressList(msg.To), + Subject: msg.Subject, + Date: msg.Date, + PosixMillis: msg.Date.UnixNano() / 1000000, + Size: msg.Size, + Seen: msg.Seen, } } return web.RenderJSON(w, jmessages) @@ -77,15 +78,16 @@ func MailboxShowV1(w http.ResponseWriter, req *http.Request, ctx *web.Context) ( } return web.RenderJSON(w, &model.JSONMessageV1{ - Mailbox: name, - ID: msg.ID, - From: stringutil.StringAddress(msg.From), - To: stringutil.StringAddressList(msg.To), - Subject: msg.Subject, - Date: msg.Date, - Size: msg.Size, - Seen: msg.Seen, - Header: msg.Header(), + Mailbox: name, + ID: msg.ID, + From: stringutil.StringAddress(msg.From), + To: stringutil.StringAddressList(msg.To), + Subject: msg.Subject, + Date: msg.Date, + PosixMillis: msg.Date.UnixNano() / 1000000, + Size: msg.Size, + Seen: msg.Seen, + Header: msg.Header(), Body: &model.JSONMessageBodyV1{ Text: msg.Text(), HTML: msg.HTML(), diff --git a/pkg/rest/apiv1_controller_test.go b/pkg/rest/apiv1_controller_test.go index 0c52673..4b4e4d4 100644 --- a/pkg/rest/apiv1_controller_test.go +++ b/pkg/rest/apiv1_controller_test.go @@ -112,6 +112,7 @@ func TestRestMailboxList(t *testing.T) { decodedStringEquals(t, result, "[0]/to/[0]", "") decodedStringEquals(t, result, "[0]/subject", "subject 1") decodedStringEquals(t, result, "[0]/date", "2012-02-01T10:11:12.000000253-08:00") + decodedNumberEquals(t, result, "[0]/posix-millis", 1328119872000) decodedNumberEquals(t, result, "[0]/size", 0) decodedBoolEquals(t, result, "[0]/seen", false) decodedStringEquals(t, result, "[1]/mailbox", "good") @@ -120,6 +121,7 @@ func TestRestMailboxList(t *testing.T) { decodedStringEquals(t, result, "[1]/to/[0]", "") decodedStringEquals(t, result, "[1]/subject", "subject 2") decodedStringEquals(t, result, "[1]/date", "2012-07-01T10:11:12.000000253-07:00") + decodedNumberEquals(t, result, "[1]/posix-millis", 1341162672000) decodedNumberEquals(t, result, "[1]/size", 0) decodedBoolEquals(t, result, "[1]/seen", false) @@ -221,6 +223,7 @@ func TestRestMessage(t *testing.T) { decodedStringEquals(t, result, "to/[0]", "") decodedStringEquals(t, result, "subject", "subject 1") decodedStringEquals(t, result, "date", "2012-02-01T10:11:12.000000253-08:00") + decodedNumberEquals(t, result, "posix-millis", 1328119872000) decodedNumberEquals(t, result, "size", 0) decodedBoolEquals(t, result, "seen", true) decodedStringEquals(t, result, "body/text", "This is some text") diff --git a/pkg/rest/model/apiv1_model.go b/pkg/rest/model/apiv1_model.go index 2b18e13..9feff81 100644 --- a/pkg/rest/model/apiv1_model.go +++ b/pkg/rest/model/apiv1_model.go @@ -6,14 +6,15 @@ import ( // JSONMessageHeaderV1 contains the basic header data for a message type JSONMessageHeaderV1 struct { - Mailbox string `json:"mailbox"` - ID string `json:"id"` - From string `json:"from"` - To []string `json:"to"` - Subject string `json:"subject"` - Date time.Time `json:"date"` - Size int64 `json:"size"` - Seen bool `json:"seen"` + Mailbox string `json:"mailbox"` + ID string `json:"id"` + From string `json:"from"` + To []string `json:"to"` + Subject string `json:"subject"` + Date time.Time `json:"date"` + PosixMillis int64 `json:"posix-millis"` + Size int64 `json:"size"` + Seen bool `json:"seen"` } // JSONMessageV1 contains the same data as the header plus a JSONMessageBody @@ -24,6 +25,7 @@ type JSONMessageV1 struct { To []string `json:"to"` Subject string `json:"subject"` Date time.Time `json:"date"` + PosixMillis int64 `json:"posix-millis"` Size int64 `json:"size"` Seen bool `json:"seen"` Body *JSONMessageBodyV1 `json:"body"` diff --git a/pkg/rest/socketv1_controller.go b/pkg/rest/socketv1_controller.go index 7c5b019..84e6c72 100644 --- a/pkg/rest/socketv1_controller.go +++ b/pkg/rest/socketv1_controller.go @@ -110,13 +110,14 @@ func (ml *msgListener) WSWriter(conn *websocket.Conn) { return } header := &model.JSONMessageHeaderV1{ - Mailbox: msg.Mailbox, - ID: msg.ID, - From: msg.From, - To: msg.To, - Subject: msg.Subject, - Date: msg.Date, - Size: msg.Size, + Mailbox: msg.Mailbox, + ID: msg.ID, + From: msg.From, + To: msg.To, + Subject: msg.Subject, + Date: msg.Date, + PosixMillis: msg.Date.UnixNano() / 1000000, + Size: msg.Size, } if conn.WriteJSON(header) != nil { // Write failed diff --git a/pkg/rest/testutils_test.go b/pkg/rest/testutils_test.go index 0df7eec..ec1c016 100644 --- a/pkg/rest/testutils_test.go +++ b/pkg/rest/testutils_test.go @@ -79,12 +79,14 @@ func decodedNumberEquals(t *testing.T, json interface{}, path string, want float t.Errorf("JSON result%s", msg) return } - if got, ok := val.(float64); ok { + got, ok := val.(float64) + if ok { if got == want { return } } - t.Errorf("JSON result/%s == %v (%T), want: %v", path, val, val, want) + t.Errorf("JSON result/%s == %v (%T) %v (int64),\nwant: %v / %v", + path, val, val, int64(got), want, int64(want)) } func decodedStringEquals(t *testing.T, json interface{}, path string, want string) { diff --git a/pkg/webui/mailbox_controller.go b/pkg/webui/mailbox_controller.go index 37013aa..4ceab38 100644 --- a/pkg/webui/mailbox_controller.go +++ b/pkg/webui/mailbox_controller.go @@ -23,6 +23,7 @@ type JSONMessage struct { To []string `json:"to"` Subject string `json:"subject"` Date time.Time `json:"date"` + PosixMillis int64 `json:"posix-millis"` Size int64 `json:"size"` Seen bool `json:"seen"` Header map[string][]string `json:"header"` @@ -81,6 +82,7 @@ func MailboxMessage(w http.ResponseWriter, req *http.Request, ctx *web.Context) To: stringutil.StringAddressList(msg.To), Subject: msg.Subject, Date: msg.Date, + PosixMillis: msg.Date.UnixNano() / 1000000, Size: msg.Size, Seen: msg.Seen, Header: msg.Header(),