feat(sources): enforce supported source allowlist

Assign stable keys to the three supported sources, preserve excluded historical rows, and expose history read-only in admin. Add migration, coverage, and responsive browser spec.

Refs #13

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 00:11:26 +01:00
parent 4459a81206
commit ac17d27f51
26 changed files with 924 additions and 61 deletions

View File

@ -130,7 +130,7 @@ func (r *Repository) CountAdmins() (int, error) {
// ── Sources ────────────────────────────────────────────────────────────────
func (r *Repository) ListSources() ([]Source, error) {
rows, err := r.db.Query(`SELECT id, name, type, enabled, created_at, article_limit FROM sources ORDER BY name`)
rows, err := r.db.Query(`SELECT id, name, type, enabled, created_at, article_limit, source_key FROM sources ORDER BY name`)
if err != nil {
return nil, err
}
@ -138,35 +138,44 @@ func (r *Repository) ListSources() ([]Source, error) {
var sources []Source
for rows.Next() {
var s Source
if err := rows.Scan(&s.ID, &s.Name, &s.Type, &s.Enabled, &s.CreatedAt, &s.ArticleLimit); err != nil {
if err := rows.Scan(&s.ID, &s.Name, &s.Type, &s.Enabled, &s.CreatedAt, &s.ArticleLimit, &s.SourceKey); err != nil {
return nil, err
}
sources = append(sources, s)
}
return sources, nil
return sources, rows.Err()
}
func (r *Repository) GetSourceByType(sourceType string) (*Source, error) {
s := &Source{}
err := r.db.QueryRow(`
SELECT id, name, type, enabled, created_at, article_limit FROM sources WHERE type=$1`, sourceType,
).Scan(&s.ID, &s.Name, &s.Type, &s.Enabled, &s.CreatedAt, &s.ArticleLimit)
SELECT id, name, type, enabled, created_at, article_limit, source_key FROM sources WHERE type=$1 AND source_key IN ('yahoo_finance','bloomberg','watcher_guru')`, sourceType,
).Scan(&s.ID, &s.Name, &s.Type, &s.Enabled, &s.CreatedAt, &s.ArticleLimit, &s.SourceKey)
if err == sql.ErrNoRows {
return nil, nil
}
return s, err
}
var ErrSourceNotSupported = errors.New("source_not_supported")
func (r *Repository) UpdateSource(id string, enabled bool, articleLimit *int) error {
result, err := r.db.Exec(`UPDATE sources SET enabled=$1, article_limit=COALESCE($2,article_limit) WHERE id=$3`, enabled, articleLimit, id)
tx, err := r.db.Begin()
if err != nil {
return err
}
count, err := result.RowsAffected()
if err == nil && count == 0 {
return sql.ErrNoRows
defer tx.Rollback()
var key *string
if err = tx.QueryRow(`SELECT source_key FROM sources WHERE id=$1 FOR UPDATE`, id).Scan(&key); err != nil {
return err
}
return err
if key == nil || !SupportedSourceKey(*key) {
return ErrSourceNotSupported
}
if _, err = tx.Exec(`UPDATE sources SET enabled=$1,article_limit=COALESCE($2,article_limit) WHERE id=$3`, enabled, articleLimit, id); err != nil {
return err
}
return tx.Commit()
}
// ── Scrape Credentials ─────────────────────────────────────────────────────