mediocre-blog/srv/api/chat.go

212 lines
4.5 KiB
Go
Raw Normal View History

2021-08-18 23:13:25 +00:00
package api
import (
"context"
2021-08-18 23:13:25 +00:00
"errors"
"fmt"
"net/http"
2021-08-19 00:13:18 +00:00
"strings"
"unicode"
2021-08-18 23:13:25 +00:00
"github.com/gorilla/websocket"
"github.com/mediocregopher/blog.mediocregopher.com/srv/api/apiutils"
2021-08-18 23:13:25 +00:00
"github.com/mediocregopher/blog.mediocregopher.com/srv/chat"
)
type chatHandler struct {
*http.ServeMux
room chat.Room
userIDCalc *chat.UserIDCalculator
wsUpgrader websocket.Upgrader
}
func newChatHandler(
room chat.Room, userIDCalc *chat.UserIDCalculator,
requirePowMiddleware func(http.Handler) http.Handler,
) http.Handler {
c := &chatHandler{
ServeMux: http.NewServeMux(),
room: room,
userIDCalc: userIDCalc,
wsUpgrader: websocket.Upgrader{},
}
c.Handle("/history", c.historyHandler())
c.Handle("/user-id", requirePowMiddleware(c.userIDHandler()))
c.Handle("/append", requirePowMiddleware(c.appendHandler()))
c.Handle("/listen", c.listenHandler())
return c
}
func (c *chatHandler) historyHandler() http.Handler {
2021-08-18 23:13:25 +00:00
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
limit, err := apiutils.StrToInt(r.PostFormValue("limit"), 0)
2021-08-18 23:13:25 +00:00
if err != nil {
apiutils.BadRequest(rw, r, fmt.Errorf("invalid limit parameter: %w", err))
2021-08-18 23:13:25 +00:00
return
}
cursor := r.PostFormValue("cursor")
2021-08-18 23:13:25 +00:00
cursor, msgs, err := c.room.History(r.Context(), chat.HistoryOpts{
2021-08-18 23:13:25 +00:00
Limit: limit,
Cursor: cursor,
})
if argErr := (chat.ErrInvalidArg{}); errors.As(err, &argErr) {
apiutils.BadRequest(rw, r, argErr.Err)
2021-08-18 23:13:25 +00:00
return
} else if err != nil {
apiutils.InternalServerError(rw, r, err)
2021-08-18 23:13:25 +00:00
}
apiutils.JSONResult(rw, r, struct {
2021-08-18 23:13:25 +00:00
Cursor string `json:"cursor"`
Messages []chat.Message `json:"messages"`
}{
Cursor: cursor,
Messages: msgs,
})
})
}
2021-08-19 00:13:18 +00:00
func (c *chatHandler) userID(r *http.Request) (chat.UserID, error) {
2021-08-19 00:13:18 +00:00
name := r.PostFormValue("name")
if l := len(name); l == 0 {
return chat.UserID{}, errors.New("name is required")
} else if l > 16 {
return chat.UserID{}, errors.New("name too long")
}
nameClean := strings.Map(func(r rune) rune {
if !unicode.IsPrint(r) {
return -1
}
return r
}, name)
if nameClean != name {
return chat.UserID{}, errors.New("name contains invalid characters")
}
password := r.PostFormValue("password")
if l := len(password); l == 0 {
return chat.UserID{}, errors.New("password is required")
} else if l > 128 {
return chat.UserID{}, errors.New("password too long")
}
return c.userIDCalc.Calculate(name, password), nil
2021-08-19 00:13:18 +00:00
}
func (c *chatHandler) userIDHandler() http.Handler {
2021-08-19 00:13:18 +00:00
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
userID, err := c.userID(r)
2021-08-19 00:13:18 +00:00
if err != nil {
apiutils.BadRequest(rw, r, err)
2021-08-19 00:13:18 +00:00
return
}
apiutils.JSONResult(rw, r, struct {
2021-08-19 00:13:18 +00:00
UserID chat.UserID `json:"userID"`
}{
UserID: userID,
})
})
}
func (c *chatHandler) appendHandler() http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
userID, err := c.userID(r)
if err != nil {
apiutils.BadRequest(rw, r, err)
return
}
body := r.PostFormValue("body")
if l := len(body); l == 0 {
apiutils.BadRequest(rw, r, errors.New("body is required"))
return
} else if l > 300 {
apiutils.BadRequest(rw, r, errors.New("body too long"))
return
}
msg, err := c.room.Append(r.Context(), chat.Message{
UserID: userID,
Body: body,
})
if err != nil {
apiutils.InternalServerError(rw, r, err)
return
}
apiutils.JSONResult(rw, r, struct {
MessageID string `json:"messageID"`
}{
MessageID: msg.ID,
})
})
}
func (c *chatHandler) listenHandler() http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
ctx := r.Context()
sinceID := r.FormValue("sinceID")
conn, err := c.wsUpgrader.Upgrade(rw, r, nil)
if err != nil {
apiutils.BadRequest(rw, r, err)
return
}
defer conn.Close()
it, err := c.room.Listen(ctx, sinceID)
if errors.As(err, new(chat.ErrInvalidArg)) {
apiutils.BadRequest(rw, r, err)
return
} else if errors.Is(err, context.Canceled) {
return
} else if err != nil {
apiutils.InternalServerError(rw, r, err)
return
}
defer it.Close()
for {
msg, err := it.Next(ctx)
if errors.Is(err, context.Canceled) {
return
} else if err != nil {
apiutils.InternalServerError(rw, r, err)
return
}
err = conn.WriteJSON(struct {
Message chat.Message `json:"message"`
}{
Message: msg,
})
if err != nil {
apiutils.GetRequestLogger(r).Error(ctx, "couldn't write message", err)
return
}
}
})
}