Private
Public Access
1
0

Ensure all dates returned are ISO8601

This commit is contained in:
James Magahern
2024-01-05 16:26:19 -08:00
parent a8043e53b3
commit 831636216d
8 changed files with 53 additions and 24 deletions

View File

@@ -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,
}
}

View File

@@ -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 {

View File

@@ -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)

30
model/date.go Normal file
View File

@@ -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
}

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

View File

@@ -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
}