diff --git a/data/generators.go b/data/generators.go index 95e73f4..7cfe124 100644 --- a/data/generators.go +++ b/data/generators.go @@ -336,7 +336,7 @@ func GenerateRandomConversation() model.Conversation { Participants: []string{GenerateRandomName()}, UnreadCount: 0, Guid: uuid.New().String(), - Date: time.Now().Add(-1 * time.Duration(rand.Intn(1000000)) * time.Second), + Date: model.Date(time.Now().Add(-1 * time.Duration(rand.Intn(1000000)) * time.Second)), } return conversation @@ -355,7 +355,7 @@ func GenerateRandomMessage(participants []string) model.Message { return model.Message{ Text: GenerateRandomMessageBody(), Guid: uuid.New().String(), - Date: time.Now().Add(-1 * time.Duration(rand.Intn(1000000)) * time.Second), + Date: model.Date(time.Now().Add(-1 * time.Duration(rand.Intn(1000000)) * time.Second)), Sender: sender, } } diff --git a/model/authtoken.go b/model/authtoken.go index 264d798..75dd6c1 100644 --- a/model/authtoken.go +++ b/model/authtoken.go @@ -4,8 +4,8 @@ import ( "encoding/base64" "time" - "github.com/rs/zerolog/log" "github.com/dgrijalva/jwt-go" + "github.com/rs/zerolog/log" ) type AuthToken struct { diff --git a/model/conversation.go b/model/conversation.go index a23531b..549bebe 100644 --- a/model/conversation.go +++ b/model/conversation.go @@ -8,13 +8,13 @@ import ( ) type Conversation struct { - Date time.Time `json:"date"` - Participants []string `json:"participantDisplayNames"` - DisplayName *string `json:"displayName,omitempty"` // Optional - UnreadCount int `json:"unreadCount"` - LastMessagePreview string `json:"lastMessagePreview"` - LastMessage Message `json:"lastMessage"` - Guid string `json:"guid"` + Date Date `json:"date"` + Participants []string `json:"participantDisplayNames"` + DisplayName *string `json:"displayName,omitempty"` // Optional + UnreadCount int `json:"unreadCount"` + LastMessagePreview string `json:"lastMessagePreview"` + LastMessage Message `json:"lastMessage"` + Guid string `json:"guid"` } func (c *Conversation) GetDisplayName() string { @@ -27,7 +27,7 @@ func (c *Conversation) GetDisplayName() string { func (c *Conversation) MarshalZerologObject(e *zerolog.Event) { e.Str("guid", c.Guid) - e.Time("date", c.Date) + e.Time("date", time.Time(c.Date)) e.Int("unreadCount", c.UnreadCount) e.Str("lastMessagePreview", c.LastMessagePreview) e.Strs("participants", c.Participants) diff --git a/model/date.go b/model/date.go new file mode 100644 index 0000000..552e20e --- /dev/null +++ b/model/date.go @@ -0,0 +1,30 @@ +package model + +import ( + "fmt" + "time" +) + +type Date time.Time + +func (d Date) Equal(other Date) bool { + return time.Time(d).Equal(time.Time(other)) +} + +func (d Date) After(other Date) bool { + return time.Time(d).After(time.Time(other)) +} + +func (d Date) Before(other Date) bool { + return time.Time(d).Before(time.Time(other)) +} + +func (d Date) Format(layout string) string { + return time.Time(d).Format(layout) +} + +func (t Date) MarshalJSON() ([]byte, error) { + // Must use ISO8601 + formatted := fmt.Sprintf("\"%s\"", time.Time(t).Format("2006-01-02T15:04:05+00:00")) + return []byte(formatted), nil +} diff --git a/model/message.go b/model/message.go index c173abd..d88768a 100644 --- a/model/message.go +++ b/model/message.go @@ -1,16 +1,15 @@ package model import ( - "time" - "github.com/rs/zerolog" + "time" ) type Message struct { - Text string `json:"text"` - Guid string `json:"guid"` - Sender *string `json:"sender,omitempty"` // Optional: nil means from "me" - Date time.Time `json:"date"` + Text string `json:"text"` + Guid string `json:"guid"` + Sender *string `json:"sender,omitempty"` // Optional: nil means from "me" + Date Date `json:"date"` // Map of attachment GUID to attachment metadata Attachments *map[string]AttributionInfo `json:"attachmentMetadata,omitempty"` // Optional @@ -24,7 +23,7 @@ type AttributionInfo struct { func (c Message) MarshalZerologObject(e *zerolog.Event) { e.Str("guid", c.Guid) e.Str("text", c.Text) - e.Time("date", c.Date) + e.Time("date", time.Time(c.Date)) if c.Sender != nil { e.Str("sender", *c.Sender) diff --git a/prompt/prompt.go b/prompt/prompt.go index ff88239..0e7959f 100644 --- a/prompt/prompt.go +++ b/prompt/prompt.go @@ -85,7 +85,7 @@ func (p *Prompt) receiveMessage(guid string, text string) { Guid: uuid.New().String(), Sender: &conversation.Participants[0], Text: text, - Date: time.Now(), + Date: model.Date(time.Now()), } p.server.ReceiveMessage(conversation, message) diff --git a/server/server.go b/server/server.go index 0340a54..b49c9f0 100644 --- a/server/server.go +++ b/server/server.go @@ -2,7 +2,6 @@ package server import ( "sort" - "time" "code.severnaya.net/kordophone-mock/v2/data" "code.severnaya.net/kordophone-mock/v2/model" @@ -28,7 +27,7 @@ type Server struct { type MessagesQuery struct { ConversationGUID string - BeforeDate *time.Time + BeforeDate *model.Date AfterGUID *string BeforeGUID *string Limit *int diff --git a/web/server.go b/web/server.go index 344e33e..b926513 100644 --- a/web/server.go +++ b/web/server.go @@ -139,12 +139,13 @@ func (m *MockHTTPServer) handleMessages(w http.ResponseWriter, r *http.Request) return &s } - dateOrNil := func(s string) *time.Time { + dateOrNil := func(s string) *model.Date { if len(s) == 0 { return nil } t, _ := time.Parse(time.RFC3339, s) - return &t + date := model.Date(t) + return &date } intOrNil := func(s string) *int { @@ -241,7 +242,7 @@ func (m *MockHTTPServer) handleSendMessage(w http.ResponseWriter, r *http.Reques message := model.Message{ Guid: uuid.New().String(), Text: sendMessageReq.Body, - Date: time.Now(), + Date: model.Date(time.Now()), Sender: nil, // me }