EXPERIMENTAL: this endpoint is experimental and is subject to change. @Summary Get chat by ID @ID get-chat-by-id @Security CoderSessionToken @Tags Chats @Produce json @Param chat path string true "Chat ID" format(uuid) @Success 200 {object} codersdk.Chat @Router /api/experimental/chats/{chat} [get]
(rw http.ResponseWriter, r *http.Request)
| 2004 | // |
| 2005 | //nolint:revive // HTTP handler writes to ResponseWriter. |
| 2006 | func (api *API) getChat(rw http.ResponseWriter, r *http.Request) { |
| 2007 | ctx := r.Context() |
| 2008 | chat := httpmw.ChatParam(r) |
| 2009 | |
| 2010 | // Use the cached diff status from the database rather than |
| 2011 | // resolving it inline. Inline resolution calls out to the |
| 2012 | // git provider API (e.g. GitHub) on every request which |
| 2013 | // blocks the response for 200-800ms. The background gitsync |
| 2014 | // worker keeps the cached status fresh. |
| 2015 | var diffStatus *database.ChatDiffStatus |
| 2016 | status, err := api.Database.GetChatDiffStatusByChatID(ctx, chat.ID) |
| 2017 | switch { |
| 2018 | case err == nil: |
| 2019 | diffStatus = &status |
| 2020 | case !xerrors.Is(err, sql.ErrNoRows): |
| 2021 | api.Logger.Error(ctx, "failed to get cached chat diff status", |
| 2022 | slog.F("chat_id", chat.ID), |
| 2023 | slog.Error(err), |
| 2024 | ) |
| 2025 | } |
| 2026 | |
| 2027 | // Hydrate file metadata for all files linked to this chat. |
| 2028 | chatFiles := api.fetchChatFileMetadata(ctx, chat.ID) |
| 2029 | |
| 2030 | sdkChat := db2sdk.Chat(chat, diffStatus, chatFiles) |
| 2031 | |
| 2032 | // For root chats, embed children so callers get a complete |
| 2033 | // tree in a single response. |
| 2034 | if !chat.ParentChatID.Valid { |
| 2035 | // Embed children matching the parent's archive state. |
| 2036 | childRows, err := api.Database.GetChildChatsByParentIDs(ctx, database.GetChildChatsByParentIDsParams{ |
| 2037 | ParentIds: []uuid.UUID{chat.ID}, |
| 2038 | Archived: sql.NullBool{Bool: chat.Archived, Valid: true}, |
| 2039 | }) |
| 2040 | if err != nil { |
| 2041 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 2042 | Message: "Failed to fetch child chats.", |
| 2043 | Detail: err.Error(), |
| 2044 | }) |
| 2045 | return |
| 2046 | } |
| 2047 | // Look up diff statuses for children. |
| 2048 | childChats := make([]database.Chat, len(childRows)) |
| 2049 | for i, row := range childRows { |
| 2050 | childChats[i] = row.Chat |
| 2051 | } |
| 2052 | childDiffStatuses, err := api.getChatDiffStatusesByChatID(ctx, childChats) |
| 2053 | if err != nil { |
| 2054 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 2055 | Message: "Failed to fetch child chat diff statuses.", |
| 2056 | Detail: err.Error(), |
| 2057 | }) |
| 2058 | return |
| 2059 | } |
| 2060 | |
| 2061 | sdkChat.Children = db2sdk.ChildChatRows(childRows, childDiffStatuses) |
| 2062 | } |
| 2063 |
no test coverage detected