1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 09:00:10 +00:00

Finished logging api

This commit is contained in:
Harvey Tindall 2020-08-01 12:35:28 +01:00
parent 326b274329
commit 99a643ce9c

34
api.go
View File

@ -267,9 +267,8 @@ func (ctx *appContext) GenerateInvite(gc *gin.Context) {
gc.JSON(200, map[string]bool{"success": true}) gc.JSON(200, map[string]bool{"success": true})
} }
// logged up to here!
func (ctx *appContext) GetInvites(gc *gin.Context) { func (ctx *appContext) GetInvites(gc *gin.Context) {
ctx.debug.Println("Invites requested")
current_time := time.Now() current_time := time.Now()
// checking one checks all of them // checking one checks all of them
ctx.storage.loadInvites() ctx.storage.loadInvites()
@ -333,10 +332,12 @@ func (ctx *appContext) SetNotify(gc *gin.Context) {
gc.BindJSON(&req) gc.BindJSON(&req)
changed := false changed := false
for code, settings := range req { for code, settings := range req {
ctx.debug.Printf("%s: Notification settings change requested", code)
ctx.storage.loadInvites() ctx.storage.loadInvites()
ctx.storage.loadEmails() ctx.storage.loadEmails()
invite, ok := ctx.storage.invites[code] invite, ok := ctx.storage.invites[code]
if !ok { if !ok {
ctx.err.Printf("%s Notification setting change failed: Invalid code", code)
gc.JSON(400, map[string]string{"error": "Invalid invite code"}) gc.JSON(400, map[string]string{"error": "Invalid invite code"})
gc.Abort() gc.Abort()
return return
@ -346,6 +347,8 @@ func (ctx *appContext) SetNotify(gc *gin.Context) {
var ok bool var ok bool
address, ok = ctx.storage.emails[gc.GetString("jfId")].(string) address, ok = ctx.storage.emails[gc.GetString("jfId")].(string)
if !ok { if !ok {
ctx.err.Printf("%s: Couldn't find email address. Make sure it's set", code)
ctx.debug.Printf("%s: User ID \"%s\"", code, gc.GetString("jfId"))
gc.JSON(500, map[string]string{"error": "Missing user email"}) gc.JSON(500, map[string]string{"error": "Missing user email"})
gc.Abort() gc.Abort()
return return
@ -363,10 +366,12 @@ func (ctx *appContext) SetNotify(gc *gin.Context) {
*/ */
if invite.Notify[address]["notify-expiry"] != settings.NotifyExpiry { if invite.Notify[address]["notify-expiry"] != settings.NotifyExpiry {
invite.Notify[address]["notify-expiry"] = settings.NotifyExpiry invite.Notify[address]["notify-expiry"] = settings.NotifyExpiry
ctx.debug.Printf("%s: Set \"notify-expiry\" to %t for %s", code, settings.NotifyExpiry, address)
changed = true changed = true
} }
if invite.Notify[address]["notify-creation"] != settings.NotifyCreation { if invite.Notify[address]["notify-creation"] != settings.NotifyCreation {
invite.Notify[address]["notify-creation"] = settings.NotifyCreation invite.Notify[address]["notify-creation"] = settings.NotifyCreation
ctx.debug.Printf("%s: Set \"notify-creation\" to %t for %s", code, settings.NotifyExpiry, address)
changed = true changed = true
} }
if changed { if changed {
@ -385,17 +390,17 @@ type deleteReq struct {
func (ctx *appContext) DeleteInvite(gc *gin.Context) { func (ctx *appContext) DeleteInvite(gc *gin.Context) {
var req deleteReq var req deleteReq
gc.BindJSON(&req) gc.BindJSON(&req)
ctx.debug.Printf("%s: Deletion requested", req.Code)
var ok bool var ok bool
fmt.Println(req.Code)
fmt.Println(ctx.storage.invites[req.Code])
_, ok = ctx.storage.invites[req.Code] _, ok = ctx.storage.invites[req.Code]
if ok { if ok {
fmt.Println("deleting invite")
delete(ctx.storage.invites, req.Code) delete(ctx.storage.invites, req.Code)
ctx.storage.storeInvites() ctx.storage.storeInvites()
ctx.info.Printf("%s: Invite deleted", req.Code)
gc.JSON(200, map[string]bool{"success": true}) gc.JSON(200, map[string]bool{"success": true})
return return
} }
ctx.err.Printf("%s: Deletion failed: Invalid code", req.Code)
respond(401, "Code doesn't exist", gc) respond(401, "Code doesn't exist", gc)
} }
@ -409,10 +414,13 @@ type respUser struct {
} }
func (ctx *appContext) GetUsers(gc *gin.Context) { func (ctx *appContext) GetUsers(gc *gin.Context) {
ctx.debug.Println("Users requested")
var resp userResp var resp userResp
resp.UserList = []respUser{} resp.UserList = []respUser{}
users, status, err := ctx.jf.getUsers(false) users, status, err := ctx.jf.getUsers(false)
if !(status == 200 || status == 204) || err != nil { if !(status == 200 || status == 204) || err != nil {
ctx.err.Printf("Failed to get users from Jellyfin: Code %d", status)
ctx.debug.Printf("Error: %s", err)
respond(500, "Couldn't get users", gc) respond(500, "Couldn't get users", gc)
return return
} }
@ -430,8 +438,11 @@ func (ctx *appContext) GetUsers(gc *gin.Context) {
func (ctx *appContext) ModifyEmails(gc *gin.Context) { func (ctx *appContext) ModifyEmails(gc *gin.Context) {
var req map[string]string var req map[string]string
gc.BindJSON(&req) gc.BindJSON(&req)
ctx.debug.Println("Email modification requested")
users, status, err := ctx.jf.getUsers(false) users, status, err := ctx.jf.getUsers(false)
if !(status == 200 || status == 204) || err != nil { if !(status == 200 || status == 204) || err != nil {
ctx.err.Printf("Failed to get users from Jellyfin: Code %d", status)
ctx.debug.Printf("Error: %s", err)
respond(500, "Couldn't get users", gc) respond(500, "Couldn't get users", gc)
return return
} }
@ -441,6 +452,7 @@ func (ctx *appContext) ModifyEmails(gc *gin.Context) {
} }
} }
ctx.storage.storeEmails() ctx.storage.storeEmails()
ctx.info.Println("Email list modified")
gc.JSON(200, map[string]bool{"success": true}) gc.JSON(200, map[string]bool{"success": true})
} }
@ -452,8 +464,11 @@ type defaultsReq struct {
func (ctx *appContext) SetDefaults(gc *gin.Context) { func (ctx *appContext) SetDefaults(gc *gin.Context) {
var req defaultsReq var req defaultsReq
gc.BindJSON(&req) gc.BindJSON(&req)
ctx.info.Printf("Getting user defaults from \"%s\"", req.Username)
user, status, err := ctx.jf.userByName(req.Username, false) user, status, err := ctx.jf.userByName(req.Username, false)
if !(status == 200 || status == 204) || err != nil { if !(status == 200 || status == 204) || err != nil {
ctx.err.Printf("Failed to get user from Jellyfin: Code %d", status)
ctx.debug.Printf("Error: %s", err)
respond(500, "Couldn't get user", gc) respond(500, "Couldn't get user", gc)
return return
} }
@ -461,23 +476,29 @@ func (ctx *appContext) SetDefaults(gc *gin.Context) {
policy := user["Policy"].(map[string]interface{}) policy := user["Policy"].(map[string]interface{})
ctx.storage.policy = policy ctx.storage.policy = policy
ctx.storage.storePolicy() ctx.storage.storePolicy()
ctx.debug.Println("User policy template stored")
if req.Homescreen { if req.Homescreen {
configuration := user["Configuration"].(map[string]interface{}) configuration := user["Configuration"].(map[string]interface{})
var displayprefs map[string]interface{} var displayprefs map[string]interface{}
displayprefs, status, err = ctx.jf.getDisplayPreferences(userId) displayprefs, status, err = ctx.jf.getDisplayPreferences(userId)
if !(status == 200 || status == 204) || err != nil { if !(status == 200 || status == 204) || err != nil {
ctx.err.Printf("Failed to get DisplayPrefs: Code %d", status)
ctx.debug.Printf("Error: %s", err)
respond(500, "Couldn't get displayprefs", gc) respond(500, "Couldn't get displayprefs", gc)
return return
} }
ctx.storage.configuration = configuration ctx.storage.configuration = configuration
ctx.storage.displayprefs = displayprefs ctx.storage.displayprefs = displayprefs
ctx.storage.storeConfiguration() ctx.storage.storeConfiguration()
ctx.debug.Println("Configuration template stored")
ctx.storage.storeDisplayprefs() ctx.storage.storeDisplayprefs()
ctx.debug.Println("DisplayPrefs template stored")
} }
gc.JSON(200, map[string]bool{"success": true}) gc.JSON(200, map[string]bool{"success": true})
} }
func (ctx *appContext) GetConfig(gc *gin.Context) { func (ctx *appContext) GetConfig(gc *gin.Context) {
ctx.info.Println("Config requested")
resp := map[string]interface{}{} resp := map[string]interface{}{}
for section, settings := range ctx.configBase { for section, settings := range ctx.configBase {
if section == "order" { if section == "order" {
@ -490,7 +511,6 @@ func (ctx *appContext) GetConfig(gc *gin.Context) {
} else { } else {
resp[section].(map[string]interface{})[key] = values.(map[string]interface{}) resp[section].(map[string]interface{})[key] = values.(map[string]interface{})
if key != "meta" { if key != "meta" {
fmt.Println(resp[section].(map[string]interface{})[key].(map[string]interface{}))
dataType := resp[section].(map[string]interface{})[key].(map[string]interface{})["type"].(string) dataType := resp[section].(map[string]interface{})[key].(map[string]interface{})["type"].(string)
configKey := ctx.config.Section(section).Key(key) configKey := ctx.config.Section(section).Key(key)
if dataType == "number" { if dataType == "number" {
@ -511,6 +531,7 @@ func (ctx *appContext) GetConfig(gc *gin.Context) {
} }
func (ctx *appContext) ModifyConfig(gc *gin.Context) { func (ctx *appContext) ModifyConfig(gc *gin.Context) {
ctx.info.Println("Config modification requested")
var req map[string]interface{} var req map[string]interface{}
gc.BindJSON(&req) gc.BindJSON(&req)
tempConfig, _ := ini.Load(ctx.config_path) tempConfig, _ := ini.Load(ctx.config_path)
@ -523,6 +544,7 @@ func (ctx *appContext) ModifyConfig(gc *gin.Context) {
} }
} }
tempConfig.SaveTo(ctx.config_path) tempConfig.SaveTo(ctx.config_path)
ctx.debug.Println("Config saved")
gc.JSON(200, map[string]bool{"success": true}) gc.JSON(200, map[string]bool{"success": true})
ctx.loadConfig() ctx.loadConfig()
} }