summaryrefslogtreecommitdiff
path: root/srv/chat
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2021-09-02 17:02:20 -0600
committerBrian Picciano <mediocregopher@gmail.com>2021-09-02 17:02:20 -0600
commit34f44cb5d5d6316009f242d27d2f3d69f4d5b90e (patch)
tree116a6e2db06dac246e794d1bc7f9e8b0c04f22e9 /srv/chat
parent6bebc3fae73b5f5f68e5de9e083635cfab9037b1 (diff)
implementation of basic chat page which can show history and not much else
Diffstat (limited to 'srv/chat')
-rw-r--r--srv/chat/chat.go13
1 files changed, 8 insertions, 5 deletions
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
}