From 903a61d0f2866ba2e024364662c41c7d97e27459 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Wed, 23 Sep 2020 00:01:07 +0100 Subject: [PATCH] Profiles replace user templates Profile functionality is essentially complete, and they can be created in settings. Only thing currently missing is a way to set a default profile. --- api.go | 91 +++++++++++++++++++--- data/templates/admin.html | 39 ++++++++-- main.go | 5 +- storage.go | 39 +++++++++- ts/accounts.ts | 19 ++++- ts/admin.ts | 22 +++--- ts/settings.ts | 159 +++++++++++++++++++++++++++++--------- 7 files changed, 301 insertions(+), 73 deletions(-) diff --git a/api.go b/api.go index ffb7548..c444b8d 100644 --- a/api.go +++ b/api.go @@ -469,6 +469,71 @@ func (app *appContext) SetProfile(gc *gin.Context) { gc.JSON(200, map[string]bool{"success": true}) } +func (app *appContext) GetProfiles(gc *gin.Context) { + app.storage.loadProfiles() + app.debug.Println("Profiles requested") + out := map[string]map[string]interface{}{} + for name, p := range app.storage.profiles { + out[name] = map[string]interface{}{ + "admin": p.Admin, + "libraries": p.LibraryAccess, + "fromUser": p.FromUser, + } + } + fmt.Println(out) + gc.JSON(200, out) +} + +type newProfileReq struct { + Name string `json:"name"` + ID string `json:"id"` + Homescreen bool `json:"homescreen"` +} + +func (app *appContext) CreateProfile(gc *gin.Context) { + fmt.Println("Profile creation requested") + var req newProfileReq + gc.BindJSON(&req) + user, status, err := app.jf.userById(req.ID, false) + if !(status == 200 || status == 204) || err != nil { + app.err.Printf("Failed to get user from Jellyfin: Code %d", status) + app.debug.Printf("Error: %s", err) + respond(500, "Couldn't get user", gc) + return + } + profile := Profile{ + FromUser: user["Name"].(string), + Policy: user["Policy"].(map[string]interface{}), + } + app.debug.Printf("Creating profile from user \"%s\"", user["Name"].(string)) + if req.Homescreen { + profile.Configuration = user["Configuration"].(map[string]interface{}) + profile.Displayprefs, status, err = app.jf.getDisplayPreferences(req.ID) + if !(status == 200 || status == 204) || err != nil { + app.err.Printf("Failed to get DisplayPrefs: Code %d", status) + app.debug.Printf("Error: %s", err) + respond(500, "Couldn't get displayprefs", gc) + return + } + } + app.storage.loadProfiles() + app.storage.profiles[req.Name] = profile + app.storage.storeProfiles() + app.storage.loadProfiles() + gc.JSON(200, map[string]bool{"success": true}) +} + +func (app *appContext) DeleteProfile(gc *gin.Context) { + req := map[string]string{} + gc.BindJSON(&req) + name := req["name"] + if _, ok := app.storage.profiles[name]; ok { + delete(app.storage.profiles, name) + } + app.storage.storeProfiles() + gc.JSON(200, map[string]bool{"success": true}) +} + func (app *appContext) GetInvites(gc *gin.Context) { app.debug.Println("Invites requested") current_time := time.Now() @@ -726,12 +791,13 @@ func (app *appContext) SetOmbiDefaults(gc *gin.Context) { type defaultsReq struct { From string `json:"from"` + Profile string `json:"profile"` ApplyTo []string `json:"apply_to"` ID string `json:"id"` Homescreen bool `json:"homescreen"` } -func (app *appContext) SetDefaults(gc *gin.Context) { +/*func (app *appContext) SetDefaults(gc *gin.Context) { var req defaultsReq gc.BindJSON(&req) userID := req.ID @@ -765,28 +831,31 @@ func (app *appContext) SetDefaults(gc *gin.Context) { app.debug.Println("DisplayPrefs template stored") } gc.JSON(200, map[string]bool{"success": true}) -} +}*/ func (app *appContext) ApplySettings(gc *gin.Context) { + app.info.Println("User settings change requested") var req defaultsReq gc.BindJSON(&req) - applyingFrom := "template" + applyingFrom := "profile" var policy, configuration, displayprefs map[string]interface{} - if req.From == "template" { - if len(app.storage.policy) == 0 { - respond(500, "No policy template available", gc) + if req.From == "profile" { + app.storage.loadProfiles() + if _, ok := app.storage.profiles[req.Profile]; !ok || len(app.storage.profiles[req.Profile].Policy) == 0 { + app.err.Printf("Couldn't find profile \"%s\" or profile was empty", req.Profile) + respond(500, "Couldn't find profile", gc) return } - app.storage.loadPolicy() - policy = app.storage.policy if req.Homescreen { - if len(app.storage.configuration) == 0 || len(app.storage.displayprefs) == 0 { + if len(app.storage.profiles[req.Profile].Configuration) == 0 || len(app.storage.profiles[req.Profile].Displayprefs) == 0 { + app.err.Printf("No homescreen saved in profile \"%s\"", req.Profile) respond(500, "No homescreen template available", gc) return } - configuration = app.storage.configuration - displayprefs = app.storage.displayprefs + configuration = app.storage.profiles[req.Profile].Configuration + displayprefs = app.storage.profiles[req.Profile].Displayprefs } + policy = app.storage.profiles[req.Profile].Policy } else if req.From == "user" { applyingFrom = "user" user, status, err := app.jf.userById(req.ID, false) diff --git a/data/templates/admin.html b/data/templates/admin.html index f091ef2..b5df83c 100644 --- a/data/templates/admin.html +++ b/data/templates/admin.html @@ -119,12 +119,21 @@