Files
MeshTender/internal/store/console_sessions.go
T

32 lines
1.0 KiB
Go

package store
import (
"context"
"fmt"
)
// StartConsoleSession records the start of a console session and returns its id.
func (s *Store) StartConsoleSession(ctx context.Context, repeaterID, userID int64) (int64, error) {
var id int64
// Snapshot the username as a fallback so the log still identifies the sender if
// they're later deleted; while they exist the log shows their live display name.
err := s.pool.QueryRow(ctx,
`INSERT INTO console_sessions (repeater_id, user_id, sender_username)
VALUES ($1, $2, (SELECT username FROM users WHERE id = $2)) RETURNING id`,
repeaterID, userID).Scan(&id)
if err != nil {
return 0, fmt.Errorf("start console session: %w", err)
}
return id, nil
}
// EndConsoleSession stamps a session's end time. Best-effort.
func (s *Store) EndConsoleSession(ctx context.Context, sessionID int64) error {
_, err := s.pool.Exec(ctx,
`UPDATE console_sessions SET ended_at = now() WHERE id = $1 AND ended_at IS NULL`, sessionID)
if err != nil {
return fmt.Errorf("end console session: %w", err)
}
return nil
}