ExtractChatParam grabs a chat from the "chat" URL parameter.
(db database.Store)
| 22 | |
| 23 | // ExtractChatParam grabs a chat from the "chat" URL parameter. |
| 24 | func ExtractChatParam(db database.Store) func(http.Handler) http.Handler { |
| 25 | return func(next http.Handler) http.Handler { |
| 26 | return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 27 | ctx := r.Context() |
| 28 | chatID, parsed := ParseUUIDParam(rw, r, "chat") |
| 29 | if !parsed { |
| 30 | return |
| 31 | } |
| 32 | |
| 33 | chat, err := db.GetChatByID(ctx, chatID) |
| 34 | if httpapi.Is404Error(err) { |
| 35 | httpapi.ResourceNotFound(rw) |
| 36 | return |
| 37 | } |
| 38 | if err != nil { |
| 39 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 40 | Message: "Internal error fetching chat.", |
| 41 | Detail: err.Error(), |
| 42 | }) |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | ctx = context.WithValue(ctx, chatParamContextKey{}, chat) |
| 47 | next.ServeHTTP(rw, r.WithContext(ctx)) |
| 48 | }) |
| 49 | } |
| 50 | } |