Prefs managing mostly done
This commit is contained in:
parent
81d02b82dc
commit
74851b9c4d
6 changed files with 344 additions and 51 deletions
48
main.go
48
main.go
|
|
@ -135,7 +135,8 @@ func main() { // {{{
|
|||
http.HandleFunc("/offline", pageOffline)
|
||||
|
||||
http.HandleFunc("/user/authenticate", AuthManager.AuthenticationHandler)
|
||||
http.HandleFunc("/user/preferences", authenticated(actionUserPreferences))
|
||||
http.HandleFunc("GET /user/preferences", authenticated(actionUserGetPreferences))
|
||||
http.HandleFunc("POST /user/preferences", authenticated(actionUserSetPreferences))
|
||||
|
||||
http.HandleFunc("/sync/from_server/count/{sequence}", authenticated(actionSyncFromServerCount))
|
||||
http.HandleFunc("/sync/from_server/{sequence}/{offset}", authenticated(actionSyncFromServer))
|
||||
|
|
@ -268,7 +269,7 @@ func actionSyncFromServer(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
// The purpose of the Client UUID is to avoid
|
||||
// sending nodes back once again to a client that
|
||||
// just created or modified it.
|
||||
user := getUser(r)
|
||||
user := getUserSession(r)
|
||||
changedFrom, _ := strconv.Atoi(r.PathValue("sequence"))
|
||||
offset, _ := strconv.Atoi(r.PathValue("offset"))
|
||||
|
||||
|
|
@ -291,7 +292,7 @@ func actionSyncFromServerCount(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
// The purpose of the Client UUID is to avoid
|
||||
// sending nodes back once again to a client that
|
||||
// just created or modified it.
|
||||
user := getUser(r)
|
||||
user := getUserSession(r)
|
||||
changedFrom, _ := strconv.Atoi(r.PathValue("sequence"))
|
||||
|
||||
count, err := NodesCount(user.UserID, uint64(changedFrom), user.ClientUUID)
|
||||
|
|
@ -311,7 +312,7 @@ func actionSyncFromServerCount(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
w.Write(j)
|
||||
} // }}}
|
||||
func actionNodeRetrieve(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
user := getUser(r)
|
||||
user := getUserSession(r)
|
||||
var err error
|
||||
|
||||
uuid := r.PathValue("uuid")
|
||||
|
|
@ -327,7 +328,7 @@ func actionNodeRetrieve(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
})
|
||||
} // }}}
|
||||
func actionNodeHistoryRetrieve(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
user := getUser(r)
|
||||
user := getUserSession(r)
|
||||
var err error
|
||||
|
||||
uuid := r.PathValue("uuid")
|
||||
|
|
@ -350,7 +351,7 @@ func actionNodeHistoryRetrieve(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
})
|
||||
} // }}}
|
||||
func actionNodeHistoryCount(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
user := getUser(r)
|
||||
user := getUserSession(r)
|
||||
var err error
|
||||
|
||||
uuid := r.PathValue("uuid")
|
||||
|
|
@ -367,7 +368,7 @@ func actionNodeHistoryCount(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
})
|
||||
} // }}}
|
||||
func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
user := getUser(r)
|
||||
user := getUserSession(r)
|
||||
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
var request struct {
|
||||
|
|
@ -391,8 +392,8 @@ func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
})
|
||||
} // }}}
|
||||
|
||||
func actionUserPreferences(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
user := getUser(r)
|
||||
func actionUserGetPreferences(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
user := getUserSession(r)
|
||||
prefs, err := user.Preferences()
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
|
|
@ -404,6 +405,33 @@ func actionUserPreferences(w http.ResponseWriter, r *http.Request) { // {{{
|
|||
"Preferences": prefs,
|
||||
})
|
||||
} // }}}
|
||||
func actionUserSetPreferences(w http.ResponseWriter, r *http.Request) { // {{{
|
||||
session := getUserSession(r)
|
||||
|
||||
// Verify the "default" profile is still there.
|
||||
var newPrefs map[string]appUser.UserPreferences
|
||||
body, _ := io.ReadAll(r.Body)
|
||||
err := json.Unmarshal(body, &newPrefs)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if _, found := newPrefs["default"]; !found {
|
||||
httpError(w, fmt.Errorf("'default' profile missing."))
|
||||
return
|
||||
}
|
||||
|
||||
err = session.SetPreferences(newPrefs)
|
||||
if err != nil {
|
||||
httpError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
responseData(w, map[string]any{
|
||||
"OK": true,
|
||||
})
|
||||
} // }}}
|
||||
|
||||
func createNewUser(username string) { // {{{
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
|
@ -447,7 +475,7 @@ func changePassword(username string) { // {{{
|
|||
|
||||
fmt.Printf("\nPassword changed\n")
|
||||
} // }}}
|
||||
func getUser(r *http.Request) appUser.UserSession { // {{{
|
||||
func getUserSession(r *http.Request) appUser.UserSession { // {{{
|
||||
user, _ := r.Context().Value(CONTEXT_USER).(appUser.UserSession)
|
||||
user.Db = db
|
||||
return user
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue