From 34f44cb5d5d6316009f242d27d2f3d69f4d5b90e Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 2 Sep 2021 17:02:20 -0600 Subject: implementation of basic chat page which can show history and not much else --- srv/chat/chat.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'srv/chat/chat.go') diff --git a/srv/chat/chat.go b/srv/chat/chat.go index acb7b2d..0a88d3b 100644 --- a/srv/chat/chat.go +++ b/srv/chat/chat.go @@ -31,9 +31,10 @@ var ( // Message describes a message which has been posted to a Room. type Message struct { - ID string `json:"id"` - UserID UserID `json:"userID"` - Body string `json:"body"` + ID string `json:"id"` + UserID UserID `json:"userID"` + Body string `json:"body"` + CreatedAt int64 `json:"createdAt,omitempty"` } func msgFromStreamEntry(entry radix.StreamEntry) (Message, error) { @@ -59,6 +60,7 @@ func msgFromStreamEntry(entry radix.StreamEntry) (Message, error) { } msg.ID = entry.ID.String() + msg.CreatedAt = int64(entry.ID.Time / 1000) return msg, nil } @@ -211,7 +213,7 @@ func (r *room) Append(ctx context.Context, msg Message) (Message, error) { maxLen := strconv.Itoa(r.params.MaxMessages) body := string(b) - var id string + var id radix.StreamEntryID err = r.params.Redis.Do(ctx, radix.Cmd( &id, "XADD", key, "MAXLEN", "=", maxLen, "*", "json", body, @@ -221,7 +223,8 @@ func (r *room) Append(ctx context.Context, msg Message) (Message, error) { return Message{}, fmt.Errorf("posting message to redis: %w", err) } - msg.ID = id + msg.ID = id.String() + msg.CreatedAt = int64(id.Time / 1000) return msg, nil } -- cgit v1.2.3