AdGuardHome/internal/home/profilehttp.go

103 lines
2.1 KiB
Go
Raw Normal View History

Pull request: 613-dark-theme vol.1 Merge in DNS/adguard-home from 613-dark-theme to master Squashed commit of the following: commit 1a286e9677c0bc9203ca0249edf0dd6f64db1609 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 22:37:36 2023 +0700 home: imp docs commit 45c4fac401788a1c5f0ab56869b13cce33690145 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 18:35:58 2023 +0700 home: imp docs commit e23b375611deb9562dfccaf45632e5235d06f96c Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 15:05:41 2023 +0700 home: imp docs commit 85d2cd6250c4bb558c9a9bd5066044f07827c263 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 15:04:28 2023 +0700 home: imp code commit bb00bfda90a809929639b241d7bbc31e7e6dfcb8 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 09:37:41 2023 +0700 home: imp docs commit 140fd0768ea2a0da7618a1f5ef6c53ff415753aa Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 09:34:27 2023 +0700 home: imp code commit 4e866b7c5b4d2ff777e4917cb91b34a4bbb5e995 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 19:11:33 2023 +0700 home: imp docs commit 0f13248165ef920d1dadb2e057b65cc3189a4b31 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 18:53:57 2023 +0700 home: imp code commit e04181f5fcd67cdd08fbf8a7c56933195999af31 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 18:48:44 2023 +0700 home: imp code commit bb9989bb9ab54bfb734880dcb999804c6f2ccda8 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 12:09:20 2023 +0700 all: control/profile API
2023-01-13 19:24:17 +03:00
package home
import (
"encoding/json"
"fmt"
"net/http"
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
"github.com/AdguardTeam/golibs/log"
)
// Theme is an enum of all allowed UI themes.
type Theme string
// Allowed [Theme] values.
//
Pull request #2231: ADG-8368 Frontend rewritten in TypeScript, added Node 18 support Merge in DNS/adguard-home from ADG-8368-typescript-node-18 to master Squashed commit of the following: commit daa288ae0d76178af24595cc807055902e6f09ab Merge: 4c89cf720 1085d59a6 Author: Igor Lobanov <bniwredyc@gmail.com> Date: Mon Jun 10 17:22:20 2024 +0200 merge commit 4c89cf720998f981241d521ec355c73755a63206 Author: Ildar Kamalov <ik@adguard.com> Date: Thu Jun 6 13:27:18 2024 +0300 remove install from initial state commit b943f2011f1526f5387b4df476a1c53115f7420e Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 23:10:55 2024 +0200 frontend production build fix commit cd1be2d66db0a37c853a8336f19e1d5ad03d4803 Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 20:23:14 2024 +0200 production build quickfix commit 7b8ac01fc2f8b5969642ec36a68de3ca5c94d9d0 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Jun 5 19:57:31 2024 +0300 all: upd node docker commit 02afed66d55cea7499e7d1c6494a17ba5fa36f16 Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 18:23:12 2024 +0200 changelog fixes commit 9c0f736f0c853c14e5c3f4fd0c989b3197440068 Merge: 62c4fbf1e e04775c4f Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 18:18:29 2024 +0200 merge commit 62c4fbf1e354e3f41ae9db278f6383df756e318a Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 16:22:22 2024 +0200 empty line in changelog commit 76b1e44a93eb36d7c1004c81c9e1e5f36e03973b Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 16:20:37 2024 +0200 changelog commit f783e90040b15ebdc86810e1d814982fc5e398d2 Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 16:19:13 2024 +0200 filters.js -> filters.ts commit 3d4ce6554c64209a4fe397b8dac52bb8af7893b1 Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 16:18:03 2024 +0200 generated file removed commit e35ba58f2af155d67887581ac003ac79211e00e8 Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 15:45:21 2024 +0200 rollback unwanted changes commit 1f30d4216dadc57b860a46c98a3a92df86616530 Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 15:27:36 2024 +0200 review fix commit 6cd4e44f07308a33521a3772d79f093785e7ede0 Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 11:55:39 2024 +0200 missing generated file restoresd commit 2ab738b3036b3b1c31a63dcf68f785699020022e Author: Igor Lobanov <bniwredyc@gmail.com> Date: Wed Jun 5 11:40:32 2024 +0200 Frontend rewritten in TypeScript, added Node 18 support
2024-06-10 18:42:23 +03:00
// Keep in sync with client/src/helpers/constants.ts.
Pull request: 613-dark-theme vol.1 Merge in DNS/adguard-home from 613-dark-theme to master Squashed commit of the following: commit 1a286e9677c0bc9203ca0249edf0dd6f64db1609 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 22:37:36 2023 +0700 home: imp docs commit 45c4fac401788a1c5f0ab56869b13cce33690145 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 18:35:58 2023 +0700 home: imp docs commit e23b375611deb9562dfccaf45632e5235d06f96c Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 15:05:41 2023 +0700 home: imp docs commit 85d2cd6250c4bb558c9a9bd5066044f07827c263 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 15:04:28 2023 +0700 home: imp code commit bb00bfda90a809929639b241d7bbc31e7e6dfcb8 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 09:37:41 2023 +0700 home: imp docs commit 140fd0768ea2a0da7618a1f5ef6c53ff415753aa Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 09:34:27 2023 +0700 home: imp code commit 4e866b7c5b4d2ff777e4917cb91b34a4bbb5e995 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 19:11:33 2023 +0700 home: imp docs commit 0f13248165ef920d1dadb2e057b65cc3189a4b31 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 18:53:57 2023 +0700 home: imp code commit e04181f5fcd67cdd08fbf8a7c56933195999af31 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 18:48:44 2023 +0700 home: imp code commit bb9989bb9ab54bfb734880dcb999804c6f2ccda8 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 12:09:20 2023 +0700 all: control/profile API
2023-01-13 19:24:17 +03:00
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 handleGetProfile(w http.ResponseWriter, r *http.Request) {
u := Context.auth.getCurrentUser(r)
var resp profileJSON
func() {
config.RLock()
defer config.RUnlock()
resp = profileJSON{
Name: u.Name,
Language: config.Language,
Theme: config.Theme,
}
}()
Pull request 1907: 951-blocked-services-schedule-api Updates #951. Squashed commit of the following: commit 6b840fd516f5a87fde0420e3aceb9c239b22c974 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 29 19:53:03 2023 +0300 client: imp docs more commit 7fc8f0363fbe4c4266cb0f67428fe4d18c351d2d Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Aug 29 19:40:00 2023 +0300 client: imp docs commit 00bc14d5760614f2797714cdc2c4c19b1a94b86e Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 18:43:49 2023 +0300 try to fix lock file commit d749df74b576091e0b58928d86ea8b3b49f919da Merge: c69f9230b e1f6229e5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Mon Aug 28 18:14:02 2023 +0300 Merge branch 'master' into 951-blocked-services-schedule-api commit c69f9230b12f7c983db06b74324b3df77d74b32b Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 17:16:20 2023 +0300 revert eslintrc commit b37916c2dff0ddea5293d87570bb58e3443d2d21 Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 12:02:39 2023 +0300 fix translations commit f5bb67d81506c687d0abd580049a3eee0af808e0 Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 11:43:57 2023 +0300 fix helpers commit 13ec6a8b3a0acfb62762ae7e46c6e98eb7c82212 Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 11:24:57 2023 +0300 remove todo commit 23724ec2fd683ed17b9f1cee841ad9aaf4c9d04f Author: Ildar Kamalov <ik@adguard.com> Date: Mon Aug 28 09:56:56 2023 +0300 add clients schedule form commit 84d29e558a329068e64e7a95ee183946aa4515b5 Author: Ildar Kamalov <ik@adguard.com> Date: Fri Aug 25 17:44:40 2023 +0300 fix schedule form commit 83e4017688082e9eb670091d5a24d98157050502 Author: Ildar Kamalov <ik@adguard.com> Date: Fri Aug 18 12:58:16 2023 +0300 remove unused commit ef2b68e138da382e3cf42586ae604e12d9493504 Author: Ildar Kamalov <ik@adguard.com> Date: Fri Aug 18 12:57:37 2023 +0300 client: fix translation string commit 32ea80c968f52f18adbc811b2f06874644cdfe20 Author: Ildar Kamalov <ik@adguard.com> Date: Fri Aug 18 12:26:26 2023 +0300 wip schedule commit 9b770873859186c9424c8d108812e32ddff33bad Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jul 21 14:29:50 2023 +0300 all: imp naming commit ea4e9514ea3b264bcce7f2a301db817de4e87059 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jul 19 18:09:27 2023 +0300 all: imp code commit 98a705bdaa5c1e79394c73e5d75af2416fe9f297 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Jul 18 18:23:26 2023 +0300 all: imp naming commit 4f84b55c7bfc9f7b680feac0ec45f5ea9189299a Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jul 14 15:01:17 2023 +0300 all: add global schedule api commit 87cf1646869ee9138964b47a27b7493674c8854a Merge: cabb80ac1 2adc8624c Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jul 14 12:09:29 2023 +0300 Merge branch 'master' into 951-blocked-services-schedule-api commit cabb80ac16de437a8118bb0166479574379c97a3 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 13 13:37:23 2023 +0300 openapi: fix typo commit 2279b03acbcfc3d76216f8aaf30ae1c7894127bc Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 13 12:26:19 2023 +0300 all: imp docs ... and 3 more commits
2023-08-29 20:03:40 +03:00
aghhttp.WriteJSONResponseOK(w, r, resp)
Pull request: 613-dark-theme vol.1 Merge in DNS/adguard-home from 613-dark-theme to master Squashed commit of the following: commit 1a286e9677c0bc9203ca0249edf0dd6f64db1609 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 22:37:36 2023 +0700 home: imp docs commit 45c4fac401788a1c5f0ab56869b13cce33690145 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 18:35:58 2023 +0700 home: imp docs commit e23b375611deb9562dfccaf45632e5235d06f96c Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 15:05:41 2023 +0700 home: imp docs commit 85d2cd6250c4bb558c9a9bd5066044f07827c263 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 15:04:28 2023 +0700 home: imp code commit bb00bfda90a809929639b241d7bbc31e7e6dfcb8 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 09:37:41 2023 +0700 home: imp docs commit 140fd0768ea2a0da7618a1f5ef6c53ff415753aa Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 13 09:34:27 2023 +0700 home: imp code commit 4e866b7c5b4d2ff777e4917cb91b34a4bbb5e995 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 19:11:33 2023 +0700 home: imp docs commit 0f13248165ef920d1dadb2e057b65cc3189a4b31 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 18:53:57 2023 +0700 home: imp code commit e04181f5fcd67cdd08fbf8a7c56933195999af31 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 18:48:44 2023 +0700 home: imp code commit bb9989bb9ab54bfb734880dcb999804c6f2ccda8 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Jan 12 12:09:20 2023 +0700 all: control/profile API
2023-01-13 19:24:17 +03:00
}
// handlePutProfile is the handler for PUT /control/profile/update endpoint.
func handlePutProfile(w http.ResponseWriter, r *http.Request) {
if aghhttp.WriteTextPlainDeprecated(w, r) {
return
}
profileReq := &profileJSON{}
err := json.NewDecoder(r.Body).Decode(profileReq)
if err != nil {
aghhttp.Error(r, w, http.StatusBadRequest, "reading req: %s", err)
return
}
lang := profileReq.Language
if !allowedLanguages.Has(lang) {
aghhttp.Error(r, w, http.StatusBadRequest, "unknown language: %q", lang)
return
}
theme := profileReq.Theme
func() {
config.Lock()
defer config.Unlock()
config.Language = lang
config.Theme = theme
log.Printf("home: language is set to %s", lang)
log.Printf("home: theme is set to %s", theme)
}()
onConfigModified()
aghhttp.OK(w)
}