summaryrefslogtreecommitdiff
path: root/srv/api/chat.go
diff options
context:
space:
mode:
authorBrian Picciano <mediocregopher@gmail.com>2021-08-18 17:13:25 -0600
committerBrian Picciano <mediocregopher@gmail.com>2021-08-18 17:13:25 -0600
commitbec64827d19d64aae86ca3d2b26de800b427a540 (patch)
tree43aaf5b4b3681cff8e624819f311d8d36e9bd27f /srv/api/chat.go
parenteaccf41563a5696996c0c75ceff1f270e88fc207 (diff)
implement basic chat history endpoint
Diffstat (limited to 'srv/api/chat.go')
-rw-r--r--srv/api/chat.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/srv/api/chat.go b/srv/api/chat.go
new file mode 100644
index 0000000..84fffde
--- /dev/null
+++ b/srv/api/chat.go
@@ -0,0 +1,41 @@
+package api
+
+import (
+ "errors"
+ "fmt"
+ "net/http"
+
+ "github.com/mediocregopher/blog.mediocregopher.com/srv/chat"
+)
+
+func (a *api) chatHistoryHandler() http.Handler {
+ return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
+ limit, err := strToInt(r.FormValue("limit"), 0)
+ if err != nil {
+ badRequest(rw, r, fmt.Errorf("invalid limit parameter: %w", err))
+ return
+ }
+
+ cursor := r.FormValue("cursor")
+
+ cursor, msgs, err := a.params.GlobalRoom.History(r.Context(), chat.HistoryOpts{
+ Limit: limit,
+ Cursor: cursor,
+ })
+
+ if argErr := (chat.ErrInvalidArg{}); errors.As(err, &argErr) {
+ badRequest(rw, r, argErr.Err)
+ return
+ } else if err != nil {
+ internalServerError(rw, r, err)
+ }
+
+ jsonResult(rw, r, struct {
+ Cursor string `json:"cursor"`
+ Messages []chat.Message `json:"messages"`
+ }{
+ Cursor: cursor,
+ Messages: msgs,
+ })
+ })
+}