mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2025-10-26 11:27:18 +00:00
Squashed commit of the following: commit 9324a0066202f1677bfd033d40d3a82fa9756ed9 Merge:8a1b5cad4f9da40e39Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Oct 23 17:48:01 2025 +0300 Merge branch 'master' into AGDNS-3224-aghhttp-register-slog commit8a1b5cad4cAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 21 15:51:48 2025 +0300 filtering: imp code commitfe569166efMerge:9a101a2f59be4ca90eAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 21 15:45:42 2025 +0300 Merge branch 'master' into AGDNS-3224-aghhttp-register-slog commit9a101a2f5fAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Oct 15 18:52:22 2025 +0300 home: imp code commit727e1663baAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Oct 15 10:19:56 2025 +0300 all: imp code commit113a9017dfAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 13 23:10:06 2025 +0300 home: fix typo commit6588dd2dadAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Oct 13 22:46:28 2025 +0300 all: imp naming commit44278505a9Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Oct 10 16:20:17 2025 +0300 home: fix typo commit7b4b57628bAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Oct 10 15:58:07 2025 +0300 all: web mw commit93168142cbAuthor: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Oct 8 22:20:07 2025 +0300 all: aghhttp slog commit9155edef67Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Oct 8 15:38:01 2025 +0300 aghhttp: registrar commita356473855Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Oct 7 15:32:30 2025 +0300 all: http registrar
128 lines
2.6 KiB
Go
128 lines
2.6 KiB
Go
package home
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
|
)
|
|
|
|
// Theme is an enum of all allowed UI themes.
|
|
type Theme string
|
|
|
|
// Allowed [Theme] values.
|
|
//
|
|
// Keep in sync with client/src/helpers/constants.ts.
|
|
const (
|
|
ThemeAuto Theme = "auto"
|
|
ThemeLight Theme = "light"
|
|
ThemeDark Theme = "dark"
|
|
)
|
|
|
|
// UnmarshalText implements [encoding.TextUnmarshaler] interface for *Theme.
|
|
func (t *Theme) UnmarshalText(b []byte) (err error) {
|
|
switch string(b) {
|
|
case "auto":
|
|
*t = ThemeAuto
|
|
case "dark":
|
|
*t = ThemeDark
|
|
case "light":
|
|
*t = ThemeLight
|
|
default:
|
|
return fmt.Errorf("invalid theme %q, supported: %q, %q, %q", b, ThemeAuto, ThemeDark, ThemeLight)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// profileJSON is an object for /control/profile and /control/profile/update
|
|
// endpoints.
|
|
type profileJSON struct {
|
|
Name string `json:"name"`
|
|
Language string `json:"language"`
|
|
Theme Theme `json:"theme"`
|
|
}
|
|
|
|
// handleGetProfile is the handler for GET /control/profile endpoint.
|
|
func (web *webAPI) handleGetProfile(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
|
|
var name string
|
|
|
|
if !web.auth.isGLiNet && !web.auth.isUserless {
|
|
u, ok := webUserFromContext(ctx)
|
|
if !ok {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
}
|
|
|
|
name = string(u.Login)
|
|
}
|
|
|
|
var resp profileJSON
|
|
func() {
|
|
config.RLock()
|
|
defer config.RUnlock()
|
|
|
|
resp = profileJSON{
|
|
Name: name,
|
|
Language: config.Language,
|
|
Theme: config.Theme,
|
|
}
|
|
}()
|
|
|
|
aghhttp.WriteJSONResponseOK(ctx, web.logger, w, r, resp)
|
|
}
|
|
|
|
// handlePutProfile is the handler for PUT /control/profile/update endpoint.
|
|
func (web *webAPI) handlePutProfile(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
l := web.logger
|
|
|
|
if aghhttp.WriteTextPlainDeprecated(ctx, l, w, r) {
|
|
return
|
|
}
|
|
|
|
profileReq := &profileJSON{}
|
|
err := json.NewDecoder(r.Body).Decode(profileReq)
|
|
if err != nil {
|
|
aghhttp.ErrorAndLog(ctx, l, r, w, http.StatusBadRequest, "reading req: %s", err)
|
|
|
|
return
|
|
}
|
|
|
|
lang := profileReq.Language
|
|
if !allowedLanguages.Has(lang) {
|
|
aghhttp.ErrorAndLog(ctx, l, r, w, http.StatusBadRequest, "unknown language: %q", lang)
|
|
|
|
return
|
|
}
|
|
|
|
theme := profileReq.Theme
|
|
|
|
changed := false
|
|
func() {
|
|
config.Lock()
|
|
defer config.Unlock()
|
|
|
|
if config.Language == lang && config.Theme == theme {
|
|
l.DebugContext(ctx, "updating profile; no changes")
|
|
|
|
return
|
|
}
|
|
|
|
changed = true
|
|
config.Language = lang
|
|
config.Theme = theme
|
|
l.InfoContext(ctx, "profile updated", "lang", lang, "theme", theme)
|
|
}()
|
|
|
|
if changed {
|
|
web.confModifier.Apply(ctx)
|
|
}
|
|
|
|
aghhttp.OK(ctx, l, w)
|
|
}
|