This follows on from #30027, which added `api.CreateChatAsUser`, and does the same for sending a message to a chat and interrupting it. These aren't new endpoints, they're Go methods for code running inside coderd (say, a Slack bot) that needs to keep a conversation going in a chat it created, without an API key. Nothing calls them yet. We'll move the bodies of `POST /api/v2/chats/{chat}/messages` and `POST /api/v2/chats/{chat}/interrupt` into functions that both the handlers and `api.SendChatMessageAsUser` / `api.InterruptChatAsUser` call. The caller passes a user ID, and the call runs with that user's own roles, so it can't do anything that user couldn't already do over the API. The endpoints' behaviour doesn't change.
64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
package httpmw
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
|
"github.com/coder/coder/v2/coderd/httpapi/httperror"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
)
|
|
|
|
type chatParamContextKey struct{}
|
|
|
|
// ChatParam returns the chat from the ExtractChatParam handler.
|
|
func ChatParam(r *http.Request) database.Chat {
|
|
chat, ok := r.Context().Value(chatParamContextKey{}).(database.Chat)
|
|
if !ok {
|
|
panic("developer error: chat param middleware not provided")
|
|
}
|
|
return chat
|
|
}
|
|
|
|
// FetchChat returns the chat as the actor in ctx sees it. A missing chat, or
|
|
// a chat the actor may not read, gives a 404. Every error implements
|
|
// httperror.Responder.
|
|
func FetchChat(ctx context.Context, db database.Store, chatID uuid.UUID) (database.Chat, error) {
|
|
chat, err := db.GetChatByID(ctx, chatID)
|
|
if httpapi.Is404Error(err) {
|
|
return database.Chat{}, httperror.WrapResponseError(http.StatusNotFound, httpapi.ResourceNotFoundResponse, err)
|
|
}
|
|
if err != nil {
|
|
return database.Chat{}, httperror.WrapResponseError(http.StatusInternalServerError, codersdk.Response{
|
|
Message: "Internal error fetching chat.",
|
|
Detail: err.Error(),
|
|
}, err)
|
|
}
|
|
return chat, nil
|
|
}
|
|
|
|
// ExtractChatParam grabs a chat from the "chat" URL parameter.
|
|
func ExtractChatParam(db database.Store) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
chatID, parsed := ParseUUIDParam(rw, r, "chat")
|
|
if !parsed {
|
|
return
|
|
}
|
|
|
|
chat, err := FetchChat(ctx, db, chatID)
|
|
if err != nil {
|
|
httperror.WriteResponseError(ctx, rw, err)
|
|
return
|
|
}
|
|
|
|
ctx = context.WithValue(ctx, chatParamContextKey{}, chat)
|
|
next.ServeHTTP(rw, r.WithContext(ctx))
|
|
})
|
|
}
|
|
}
|