Share user settings update transactions and notifications
This commit is contained in:
@@ -2,50 +2,19 @@ package store
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
|
||||||
|
|
||||||
"g.fc.run/ai/byre/internal/api"
|
"g.fc.run/ai/byre/internal/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Store) UpdateNotificationPrefs(ctx context.Context, login string, prefs api.NotificationPrefs) (api.User, error) {
|
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 {
|
return s.updateUser(ctx, login, `notify_questions = ?, notify_reports = ?, notify_errors = ?, notify_rate_limits = ?, notify_ready = ?, notify_discard = ?, notify_merge_problems = ?, notify_workflows = ?`,
|
||||||
_, 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)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) UpdateDefaultModel(ctx context.Context, login, model string) (api.User, error) {
|
func (s *Store) UpdateDefaultModel(ctx context.Context, login, model string) (api.User, error) {
|
||||||
err := s.write(ctx, func(tx *sql.Tx, emit emitFunc) error {
|
return s.updateUser(ctx, login, `default_model = ?`, model)
|
||||||
_, 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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) UpdateAway(ctx context.Context, login string, away bool) (api.User, error) {
|
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 {
|
return s.updateUser(ctx, login, `away = ?`, away)
|
||||||
_, 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)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {
|
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 := 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user