81 lines
2.5 KiB
Go
81 lines
2.5 KiB
Go
package server
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
"strconv"
|
||
"strings"
|
||
)
|
||
|
||
// Each user's look of the panel (Аккаунт › Вид), kept on the server so it
|
||
// is the same on every device: where a tab's icons go, the terminal's font
|
||
// size, a compact file list, the time column, animations, and whether a
|
||
// terminal or file pane opened «Здесь» starts in the active pane's folder;
|
||
// and the assistant's model: which one, and whether the chat always offers
|
||
// the choice.
|
||
type viewPrefs struct {
|
||
TabIcons string `json:"tabIcons,omitempty"` // left (default), right, hide
|
||
TermFont int `json:"termFont,omitempty"` // 11-20; 0: the default
|
||
Compact bool `json:"compact,omitempty"` // denser file lists
|
||
FileTime string `json:"fileTime,omitempty"` // show (default), hide
|
||
Motion string `json:"motion,omitempty"` // on (default), off
|
||
OpenHere string `json:"openHere,omitempty"` // on (default), off
|
||
// AIModelPick: the model menu in the chat - when there are several
|
||
// (default) or always. AIModel: the id of the model chosen.
|
||
AIModelPick string `json:"aiModelPick,omitempty"`
|
||
AIModel string `json:"aiModel,omitempty"`
|
||
}
|
||
|
||
func prefsKey(id int64) string { return "prefs_" + strconv.FormatInt(id, 10) }
|
||
|
||
// clean keeps known values only.
|
||
func (p viewPrefs) clean() viewPrefs {
|
||
if p.TabIcons != "right" && p.TabIcons != "hide" {
|
||
p.TabIcons = ""
|
||
}
|
||
if p.TermFont < 11 || p.TermFont > 20 {
|
||
p.TermFont = 0
|
||
}
|
||
if p.FileTime != "hide" {
|
||
p.FileTime = ""
|
||
}
|
||
if p.Motion != "off" {
|
||
p.Motion = ""
|
||
}
|
||
if p.OpenHere != "off" {
|
||
p.OpenHere = ""
|
||
}
|
||
if p.AIModelPick != "always" {
|
||
p.AIModelPick = ""
|
||
}
|
||
if len(p.AIModel) > 32 || strings.Trim(p.AIModel, "0123456789abcdefghijklmnopqrstuvwxyz") != "" {
|
||
p.AIModel = ""
|
||
}
|
||
return p
|
||
}
|
||
|
||
func (s *Server) prefs(id int64) viewPrefs {
|
||
var p viewPrefs
|
||
if v, _ := s.db.GetConfig(prefsKey(id)); v != "" {
|
||
_ = json.Unmarshal([]byte(v), &p)
|
||
}
|
||
return p.clean()
|
||
}
|
||
|
||
// GET /api/panel/account/prefs
|
||
func (s *Server) handlePrefs(w http.ResponseWriter, r *http.Request) {
|
||
writeJSON(w, s.prefs(s.currentAdmin(r).ID))
|
||
}
|
||
|
||
// POST /api/panel/account/prefs - the whole set.
|
||
func (s *Server) handlePrefsSet(w http.ResponseWriter, r *http.Request) {
|
||
var p viewPrefs
|
||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<10)).Decode(&p); err != nil {
|
||
writeJSON(w, map[string]any{"ok": false, "error": "плохой запрос"})
|
||
return
|
||
}
|
||
b, _ := json.Marshal(p.clean())
|
||
_ = s.db.SetConfig(prefsKey(s.currentAdmin(r).ID), string(b))
|
||
writeJSON(w, map[string]any{"ok": true})
|
||
}
|