From 4bb6f1fa119d23c01d90cc8fb70ee62490ab841f Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 21 Oct 2026 11:45:37 -0700 Subject: [PATCH] Share user settings update transactions and notifications --- internal/store/notifications.go | 39 ++++----------------------------- internal/store/store.go | 7 +++++- 2 files changed, 10 insertions(+), 36 deletions(-) diff --git a/internal/store/notifications.go b/internal/store/notifications.go index aa47e09..220c0c3 100644 --- a/internal/store/notifications.go +++ b/internal/store/notifications.go @@ -2,50 +2,19 @@ package store import ( "context" - "database/sql" "g.fc.run/ai/byre/internal/api" ) func (s *Store) UpdateNotificationPrefs(ctx context.Context, login string, prefs api.NotificationPrefs) (api.User, error) { - err := s.write(ctx, func(tx *sql.Tx, emit emitFunc) error { - _, err := tx.ExecContext(ctx, `UPDATE users SET notify_questions = ?, notify_reports = ?, notify_errors = ?, notify_rate_limits = ?, notify_ready = ?, notify_discard = ?, notify_merge_problems = ?, notify_workflows = ? WHERE login = ?`, - prefs.Questions, prefs.Reports, prefs.Errors, prefs.RateLimits, prefs.Ready, prefs.Discard, prefs.MergeProblems, prefs.Workflows, login) - if err != nil { - return err - } - return emit(api.Event{Kind: "user.updated", Actor: login, Payload: map[string]any{"login": login}}) - }) - if err != nil { - return api.User{}, err - } - return s.GetUser(ctx, login) + return s.updateUser(ctx, login, `notify_questions = ?, notify_reports = ?, notify_errors = ?, notify_rate_limits = ?, notify_ready = ?, notify_discard = ?, notify_merge_problems = ?, notify_workflows = ?`, + prefs.Questions, prefs.Reports, prefs.Errors, prefs.RateLimits, prefs.Ready, prefs.Discard, prefs.MergeProblems, prefs.Workflows) } func (s *Store) UpdateDefaultModel(ctx context.Context, login, model string) (api.User, error) { - err := s.write(ctx, func(tx *sql.Tx, emit emitFunc) error { - _, err := tx.ExecContext(ctx, `UPDATE users SET default_model = ? WHERE login = ?`, model, login) - if err != nil { - return err - } - return emit(api.Event{Kind: "user.updated", Actor: login, Payload: map[string]any{"login": login}}) - }) - if err != nil { - return api.User{}, err - } - return s.GetUser(ctx, login) + return s.updateUser(ctx, login, `default_model = ?`, model) } func (s *Store) UpdateAway(ctx context.Context, login string, away bool) (api.User, error) { - err := s.write(ctx, func(tx *sql.Tx, emit emitFunc) error { - _, err := tx.ExecContext(ctx, `UPDATE users SET away = ? WHERE login = ?`, away, login) - if err != nil { - return err - } - return emit(api.Event{Kind: "user.updated", Actor: login, Payload: map[string]any{"login": login}}) - }) - if err != nil { - return api.User{}, err - } - return s.GetUser(ctx, login) + return s.updateUser(ctx, login, `away = ?`, away) } diff --git a/internal/store/store.go b/internal/store/store.go index 05f8cb5..3c3c259 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -242,8 +242,13 @@ func (s *Store) GetUser(ctx context.Context, login string) (api.User, error) { } func (s *Store) UpdateUserGit(ctx context.Context, login, gitName, gitEmail string) (api.User, error) { + return s.updateUser(ctx, login, `git_name = ?, git_email = ?`, gitName, gitEmail) +} + +func (s *Store) updateUser(ctx context.Context, login, fields string, args ...any) (api.User, error) { + args = append(args, login) err := s.write(ctx, func(tx *sql.Tx, emit emitFunc) error { - _, err := tx.ExecContext(ctx, `UPDATE users SET git_name = ?, git_email = ? WHERE login = ?`, gitName, gitEmail, login) + _, err := tx.ExecContext(ctx, `UPDATE users SET `+fields+` WHERE login = ?`, args...) if err != nil { return err }