Refine Telegram thread commands
This commit is contained in:
@@ -339,6 +339,13 @@ WHERE telegram_user_id = ?`, threadID, telegramUserID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) ClearActiveThread(ctx context.Context, telegramUserID, threadID int64) error {
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
UPDATE sessions SET active_thread_id = NULL, active_turn_id = '', updated_at = datetime('now')
|
||||
WHERE telegram_user_id = ? AND active_thread_id = ?`, telegramUserID, threadID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) SetActiveTurn(ctx context.Context, telegramUserID int64, turnID string) error {
|
||||
_, err := s.db.ExecContext(ctx, "UPDATE sessions SET active_turn_id = ?, updated_at = datetime('now') WHERE telegram_user_id = ?", turnID, telegramUserID)
|
||||
return err
|
||||
@@ -413,6 +420,18 @@ func (s *Store) ListThreads(ctx context.Context, telegramUserID int64, includeAr
|
||||
}
|
||||
|
||||
func (s *Store) ListThreadsPage(ctx context.Context, telegramUserID int64, includeArchived bool, limit, offset int) ([]Thread, error) {
|
||||
archivedFilter := ""
|
||||
if !includeArchived {
|
||||
archivedFilter = "archived = 0"
|
||||
}
|
||||
return s.listThreadsPage(ctx, telegramUserID, archivedFilter, limit, offset)
|
||||
}
|
||||
|
||||
func (s *Store) ListArchivedThreadsPage(ctx context.Context, telegramUserID int64, limit, offset int) ([]Thread, error) {
|
||||
return s.listThreadsPage(ctx, telegramUserID, "archived = 1", limit, offset)
|
||||
}
|
||||
|
||||
func (s *Store) listThreadsPage(ctx context.Context, telegramUserID int64, archivedFilter string, limit, offset int) ([]Thread, error) {
|
||||
if limit <= 0 {
|
||||
limit = 20
|
||||
}
|
||||
@@ -423,8 +442,8 @@ func (s *Store) ListThreadsPage(ctx context.Context, telegramUserID int64, inclu
|
||||
SELECT id, telegram_user_id, codex_thread_id, workspace_id, title, archived, created_at, updated_at
|
||||
FROM threads WHERE telegram_user_id = ?`
|
||||
args := []any{telegramUserID}
|
||||
if !includeArchived {
|
||||
query += " AND archived = 0"
|
||||
if archivedFilter != "" {
|
||||
query += " AND " + archivedFilter
|
||||
}
|
||||
query += " ORDER BY updated_at DESC, id DESC LIMIT ? OFFSET ?"
|
||||
args = append(args, limit, offset)
|
||||
@@ -453,6 +472,52 @@ WHERE telegram_user_id = ? AND id = ?`, telegramUserID, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) UnarchiveThread(ctx context.Context, telegramUserID, id int64) error {
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
UPDATE threads SET archived = 0, updated_at = datetime('now')
|
||||
WHERE telegram_user_id = ? AND id = ?`, telegramUserID, id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) DeleteThread(ctx context.Context, telegramUserID, id int64) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
UPDATE sessions SET active_thread_id = NULL, active_turn_id = '', updated_at = datetime('now')
|
||||
WHERE telegram_user_id = ? AND active_thread_id = ?`, telegramUserID, id); err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
DELETE FROM threads
|
||||
WHERE telegram_user_id = ? AND id = ?`, telegramUserID, id); err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Store) DeleteThreadByCodexID(ctx context.Context, codexThreadID string) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
UPDATE sessions
|
||||
SET active_thread_id = NULL, active_turn_id = '', updated_at = datetime('now')
|
||||
WHERE active_thread_id IN (SELECT id FROM threads WHERE codex_thread_id = ?)`, codexThreadID); err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, "DELETE FROM threads WHERE codex_thread_id = ?", codexThreadID); err != nil {
|
||||
_ = tx.Rollback()
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Store) TouchThread(ctx context.Context, codexThreadID string) error {
|
||||
_, err := s.db.ExecContext(ctx, "UPDATE threads SET updated_at = datetime('now') WHERE codex_thread_id = ?", codexThreadID)
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user