settings.go
| 1 | package service |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "vidarchive/internal/models" |
| 7 | "vidarchive/internal/repository" |
| 8 | ) |
| 9 | |
| 10 | type SettingsService struct { |
| 11 | repo *repository.SettingsRepository |
| 12 | } |
| 13 | |
| 14 | func NewSettingsService(repo *repository.SettingsRepository) *SettingsService { |
| 15 | return &SettingsService{repo: repo} |
| 16 | } |
| 17 | |
| 18 | func (s *SettingsService) GetAll() (*models.Settings, error) { |
| 19 | return s.repo.GetAll() |
| 20 | } |
| 21 | |
| 22 | func (s *SettingsService) SetRefreshInterval(interval string) error { |
| 23 | return s.repo.Set("refresh_interval", interval) |
| 24 | } |
| 25 | |
| 26 | func (s *SettingsService) SetAutoRefreshLibrary(enabled bool) error { |
| 27 | return s.repo.Set("auto_refresh_library", boolValue(enabled)) |
| 28 | } |
| 29 | |
| 30 | func (s *SettingsService) SetAutoRefreshDownloads(enabled bool) error { |
| 31 | return s.repo.Set("auto_refresh_downloads", boolValue(enabled)) |
| 32 | } |
| 33 | |
| 34 | func boolValue(b bool) string { |
| 35 | if b { |
| 36 | return "1" |
| 37 | } |
| 38 | return "0" |
| 39 | } |
| 40 | |
| 41 | func (s *SettingsService) SetToolAutoUpdate(enabled bool) error { |
| 42 | return s.repo.Set("tool_auto_update", boolValue(enabled)) |
| 43 | } |
| 44 | |
| 45 | func (s *SettingsService) SetJSRuntimeEnabled(enabled bool) error { |
| 46 | return s.repo.Set("js_runtime_enabled", boolValue(enabled)) |
| 47 | } |
| 48 | |
| 49 | // SetToolsCheckedAt is written before the update runs, not after: a crash mid |
| 50 | // update must not make the scheduler retry on every tick. |
| 51 | func (s *SettingsService) SetToolsCheckedAt(t time.Time) error { |
| 52 | return s.repo.Set("tools_checked_at", t.Format(time.RFC3339)) |
| 53 | } |
| 54 | |
| 55 | func (s *SettingsService) SetCookies(cookies string) error { |
| 56 | return s.repo.Set("cookies", cookies) |
| 57 | } |
| 58 | |
| 59 | func (s *SettingsService) GetCookies() (string, error) { |
| 60 | return s.repo.Get("cookies") |
| 61 | } |
| 62 | |
| 63 | // GetSessionSecret returns the secret that signs login cookies, "" when none has |
| 64 | // been generated. It lives in the database so sessions survive a restart. |
| 65 | func (s *SettingsService) GetSessionSecret() (string, error) { |
| 66 | return s.repo.Get("session_secret") |
| 67 | } |
| 68 | |
| 69 | func (s *SettingsService) SetSessionSecret(secret string) error { |
| 70 | return s.repo.Set("session_secret", secret) |
| 71 | } |
| 72 |