Small refactor for user preferences

This commit is contained in:
Magnus Åhall 2026-06-18 09:21:23 +02:00
parent 1a712fb7a9
commit 81d02b82dc
13 changed files with 202 additions and 112 deletions

27
main.go
View file

@ -4,6 +4,7 @@ import (
// Internal
"notes2/authentication"
"notes2/html_template"
appUser "notes2/user"
"os"
// Standard
@ -134,6 +135,7 @@ func main() { // {{{
http.HandleFunc("/offline", pageOffline)
http.HandleFunc("/user/authenticate", AuthManager.AuthenticationHandler)
http.HandleFunc("/user/preferences", authenticated(actionUserPreferences))
http.HandleFunc("/sync/from_server/count/{sequence}", authenticated(actionSyncFromServerCount))
http.HandleFunc("/sync/from_server/{sequence}/{offset}", authenticated(actionSyncFromServer))
@ -178,7 +180,7 @@ func authenticated(fn func(http.ResponseWriter, *http.Request)) func(http.Respon
}
// User object is added to the context for the next handler.
user := NewUser(claims)
user := appUser.NewUser(claims)
r = r.WithContext(context.WithValue(r.Context(), CONTEXT_USER, user))
Log.Debug("webserver", "op", "request", "method", r.Method, "url", r.URL.String(), "username", user.Username, "client", user.ClientUUID)
@ -360,8 +362,8 @@ func actionNodeHistoryCount(w http.ResponseWriter, r *http.Request) { // {{{
}
responseData(w, map[string]any{
"OK": true,
"Count": count,
"OK": true,
"Count": count,
})
} // }}}
func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{
@ -389,6 +391,20 @@ func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{
})
} // }}}
func actionUserPreferences(w http.ResponseWriter, r *http.Request) { // {{{
user := getUser(r)
prefs, err := user.Preferences()
if err != nil {
httpError(w, err)
return
}
responseData(w, map[string]any{
"OK": true,
"Preferences": prefs,
})
} // }}}
func createNewUser(username string) { // {{{
reader := bufio.NewReader(os.Stdin)
@ -431,7 +447,8 @@ func changePassword(username string) { // {{{
fmt.Printf("\nPassword changed\n")
} // }}}
func getUser(r *http.Request) UserSession { // {{{
user, _ := r.Context().Value(CONTEXT_USER).(UserSession)
func getUser(r *http.Request) appUser.UserSession { // {{{
user, _ := r.Context().Value(CONTEXT_USER).(appUser.UserSession)
user.Db = db
return user
} // }}}