( ctx context.Context, rw http.ResponseWriter, chat database.Chat, rawTitle string, )
| 2558 | } |
| 2559 | |
| 2560 | func (api *API) applyChatTitleUpdate( |
| 2561 | ctx context.Context, |
| 2562 | rw http.ResponseWriter, |
| 2563 | chat database.Chat, |
| 2564 | rawTitle string, |
| 2565 | ) (database.Chat, bool) { |
| 2566 | trimmedTitle := strings.TrimSpace(rawTitle) |
| 2567 | if trimmedTitle == "" { |
| 2568 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 2569 | Message: "Title cannot be empty.", |
| 2570 | }) |
| 2571 | return chat, true |
| 2572 | } |
| 2573 | const maxChatTitleRunes = 200 |
| 2574 | if utf8.RuneCountInString(trimmedTitle) > maxChatTitleRunes { |
| 2575 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 2576 | Message: fmt.Sprintf("Title must be at most %d characters.", maxChatTitleRunes), |
| 2577 | }) |
| 2578 | return chat, true |
| 2579 | } |
| 2580 | if trimmedTitle == chat.Title { |
| 2581 | return chat, false |
| 2582 | } |
| 2583 | |
| 2584 | var ( |
| 2585 | updatedChat database.Chat |
| 2586 | wrote bool |
| 2587 | err error |
| 2588 | ) |
| 2589 | if api.chatDaemon != nil { |
| 2590 | updatedChat, wrote, err = api.chatDaemon.RenameChatTitle(ctx, chat, trimmedTitle) |
| 2591 | } else { |
| 2592 | err = api.Database.InTx(func(tx database.Store) error { |
| 2593 | currentChat, txErr := tx.GetChatByID(ctx, chat.ID) |
| 2594 | if txErr != nil { |
| 2595 | return txErr |
| 2596 | } |
| 2597 | if trimmedTitle == currentChat.Title { |
| 2598 | updatedChat = currentChat |
| 2599 | wrote = false |
| 2600 | return nil |
| 2601 | } |
| 2602 | updatedChat, txErr = tx.UpdateChatTitleByID(ctx, database.UpdateChatTitleByIDParams{ |
| 2603 | ID: chat.ID, |
| 2604 | Title: trimmedTitle, |
| 2605 | }) |
| 2606 | if txErr != nil { |
| 2607 | return txErr |
| 2608 | } |
| 2609 | wrote = true |
| 2610 | return nil |
| 2611 | }, nil) |
| 2612 | } |
| 2613 | if err != nil { |
| 2614 | if errors.Is(err, chatd.ErrManualTitleRegenerationInProgress) { |
| 2615 | httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ |
| 2616 | Message: "Title regeneration already in progress for this chat.", |
| 2617 | }) |
no test coverage detected