Add default conversation title

This commit is contained in:
dwrz
2026-10-31 16:56:08 +00:00
parent 47badbb0d6
commit d9ce81af9c
7 changed files with 439 additions and 41 deletions
+59 -10
View File
@@ -288,7 +288,7 @@ func (q *Queries) SelectConversationsUserId(ctx context.Context, userId string)
const updateConversationTitle = `
UPDATE conversations
SET title = ?
SET title = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE id = ?
RETURNING created_at, id, title, updated_at, user_id
`
@@ -300,15 +300,9 @@ type UpdateConversationTitleParams struct {
Title string
}
// UpdateConversationTitle sets the title of one conversation and
// returns the updated row.
//
// The update keeps updated_at. That timestamp is the
// conversation's synchronization token for its message set, and a
// title is not a message: a rename must not move the conversation
// in the picker's ordering, which sorts by updated_at.
//
// A conversation that is missing returns sql.ErrNoRows.
// UpdateConversationTitle sets the title for a conversation, bumps
// updated_at, and returns the updated row. It returns sql.ErrNoRows
// if no conversation has that id.
func (q *Queries) UpdateConversationTitle(
ctx context.Context,
arg UpdateConversationTitleParams,
@@ -344,6 +338,61 @@ func (q *Queries) UpdateConversationTitle(
return &c, nil
}
const updateConversationTitleIfUnset = `
UPDATE conversations
SET title = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
WHERE id = ? AND title IS NULL
RETURNING created_at, id, title, updated_at, user_id
`
// UpdateConversationTitleIfUnsetParams holds arguments for
// UpdateConversationTitleIfUnset.
type UpdateConversationTitleIfUnsetParams struct {
Id string
Title string
}
// UpdateConversationTitleIfUnset sets the first title for a
// conversation when title is NULL, bumps updated_at, and returns
// the updated row. It prevents a caller from overwriting a title
// set by a user or a scheduled job. It returns sql.ErrNoRows if
// the conversation does not exist or already has a title; callers
// treat both cases as no-op.
func (q *Queries) UpdateConversationTitleIfUnset(
ctx context.Context,
arg UpdateConversationTitleIfUnsetParams,
) (*Conversation, error) {
row := q.db.QueryRowContext(
ctx,
updateConversationTitleIfUnset,
arg.Title,
arg.Id,
)
var (
c Conversation
createdAt string
updatedAt string
err error
)
if err := row.Scan(
&createdAt,
&c.Id,
&c.Title,
&updatedAt,
&c.UserId,
); err != nil {
return nil, fmt.Errorf("UpdateConversationTitleIfUnset: %w", err)
}
if c.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
return nil, fmt.Errorf("UpdateConversationTitleIfUnset: parse created_at: %w", err)
}
if c.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
return nil, fmt.Errorf("UpdateConversationTitleIfUnset: parse updated_at: %w", err)
}
return &c, nil
}
const deleteConversationId = `
DELETE FROM conversations
WHERE id = ?