Skip to content

Commit

Permalink
all: control/profile API
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Jan 12, 2023
1 parent 68bc996 commit bb9989b
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 4 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,24 @@ and this project adheres to



<!--
## [v0.107.22] - 2222-12-28 (APPROX.)

See also the [v0.107.22 GitHub milestone][ms-v0.107.22].

### Added

- Dark UI theme ([#613]).

### Changed

- The new HTTP API `PUT /control/profile/update`, that updates current user
language and UI theme.
- The HTTP API `GET /control/profile/update` now returns enhanced object with
current user's name, language and UI theme.

[#613]: https://github.com/AdguardTeam/AdGuardHome/issues/613

[ms-v0.107.22]: https://github.com/AdguardTeam/AdGuardHome/milestone/58?closed=1
-->



Expand Down
3 changes: 3 additions & 0 deletions internal/home/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ type configuration struct {
ProxyURL string `yaml:"http_proxy"`
// Language is a two-letter ISO 639-1 language code.
Language string `yaml:"language"`
// Theme is a UI theme for current user.
Theme Theme `yaml:"theme"`
// DebugPProf defines if the profiling HTTP handler will listen on :6060.
DebugPProf bool `yaml:"debug_pprof"`

Expand Down Expand Up @@ -322,6 +324,7 @@ var config = &configuration{
},
OSConfig: &osConfig{},
SchemaVersion: currentSchemaVersion,
Theme: Auto,
}

// getConfigFilename returns path to the current config file
Expand Down
58 changes: 56 additions & 2 deletions internal/home/control.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package home

import (
"encoding/json"
"fmt"
"net/http"
"net/netip"
Expand Down Expand Up @@ -149,19 +150,71 @@ func handleStatus(w http.ResponseWriter, r *http.Request) {
_ = aghhttp.WriteJSONResponse(w, r, resp)
}

// Theme is an enum of all allowed UI themes.
type Theme string

// Allowed themes.
const (
Auto Theme = "auto"
Light Theme = "light"
Dark Theme = "dark"
)

// profileJSON is an object for /control/profile endpoints.
type profileJSON struct {
Name string `json:"name"`
Name string `json:"name"`
Language string `json:"language"`
Theme Theme `json:"theme"`
}

func handleGetProfile(w http.ResponseWriter, r *http.Request) {
u := Context.auth.getCurrentUser(r)

resp := &profileJSON{
Name: u.Name,
Name: u.Name,
Language: config.Language,
Theme: config.Theme,
}

_ = aghhttp.WriteJSONResponse(w, r, resp)
}

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.StatusInternalServerError, "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)
}

// ------------------------
// registration of handlers
// ------------------------
Expand All @@ -172,6 +225,7 @@ func registerControlHandlers() {
Context.mux.HandleFunc("/control/version.json", postInstall(optionalAuth(handleGetVersionJSON)))
httpRegister(http.MethodPost, "/control/update", handleUpdate)
httpRegister(http.MethodGet, "/control/profile", handleGetProfile)
httpRegister(http.MethodPut, "/control/profile/update", handlePutProfile)

// No auth is necessary for DoH/DoT configurations
Context.mux.HandleFunc("/apple/doh.mobileconfig", postInstall(handleMobileConfigDoH))
Expand Down
2 changes: 2 additions & 0 deletions internal/home/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type languageJSON struct {
Language string `json:"language"`
}

// TODO(d.kolyshev): Deprecated, remove it later.
func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
log.Printf("home: language is %s", config.Language)

Expand All @@ -62,6 +63,7 @@ func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
})
}

// TODO(d.kolyshev): Deprecated, remove it later.
func handleI18nChangeLanguage(w http.ResponseWriter, r *http.Request) {
if aghhttp.WriteTextPlainDeprecated(w, r) {
return
Expand Down
16 changes: 16 additions & 0 deletions openapi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@



## v0.107.22: API changes

### `POST /control/i18n/change_language` is deprecated

Use `PUT /control/profile/update`.

### `GET /control/i18n/current_language` is deprecated

Use `GET /control/profile`.

* The `/control/profile` HTTP API has been changed.

* The new `PUT /control/profile/update` HTTP API allows user info updates.



## v0.107.20: API Changes

### `POST /control/cache_clear`
Expand Down
24 changes: 24 additions & 0 deletions openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,9 @@
'description': 'OK.'
'/i18n/change_language':
'post':
'deprecated': true
'description': >
Deprecated: Use `PUT /control/profile` instead.
'tags':
- 'i18n'
'operationId': 'changeLanguage'
Expand All @@ -980,6 +983,9 @@
'description': 'OK.'
'/i18n/current_language':
'get':
'deprecated': true
'description': >
Deprecated: Use `GET /control/profile` instead.
'tags':
- 'i18n'
'operationId': 'currentLanguage'
Expand Down Expand Up @@ -1145,6 +1151,20 @@
'responses':
'302':
'description': 'OK.'
'/profile/update':
'put':
'tags':
- 'global'
'operationId': 'updateProfile'
'summary': 'Updates current user info'
'requestBody':
'content':
'application/json':
'schema':
'$ref': '#/components/schemas/ProfileInfo'
'responses':
'200':
'description': 'OK'
'/profile':
'get':
'tags':
Expand Down Expand Up @@ -2335,6 +2355,10 @@
'properties':
'name':
'type': 'string'
'language':
'type': 'string'
'theme':
'type': 'string'
'Client':
'type': 'object'
'description': 'Client information.'
Expand Down

0 comments on commit bb9989b

Please sign in to comment.