Files
lpra/internal/server/prefs.go
T

68 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package server
import (
"encoding/json"
"net/http"
"strconv"
)
// 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.
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
}
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 = ""
}
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})
}