From 7d84fdec961635d454009c7274e1618a17e6b685 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Wed, 16 Sep 2020 19:19:04 +0100 Subject: [PATCH] userByName reloads cache if user not found, more things in test --- .gitignore | 1 + jfapi.go | 23 ++++++++++++++++------- main.go | 7 +++++++ pwreset.go | 6 ++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index a1b30c1..4e60be7 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ build/ pkg/ old/ version.go +notes diff --git a/jfapi.go b/jfapi.go index 95b5ee2..00d368b 100644 --- a/jfapi.go +++ b/jfapi.go @@ -240,16 +240,25 @@ func (jf *Jellyfin) getUsers(public bool) ([]map[string]interface{}, int, error) } func (jf *Jellyfin) userByName(username string, public bool) (map[string]interface{}, int, error) { - users, status, err := jf.getUsers(public) - if err != nil || status != 200 { + var match map[string]interface{} + find := func() (map[string]interface{}, int, error) { + users, status, err := jf.getUsers(public) + if err != nil || status != 200 { + return nil, status, err + } + for _, user := range users { + if user["Name"].(string) == username { + return user, status, err + } + } return nil, status, err } - for _, user := range users { - if user["Name"].(string) == username { - return user, status, nil - } + match, status, err := find() + if match == nil { + jf.cacheExpiry = time.Now() + match, status, err = find() } - return nil, status, err + return match, status, err } func (jf *Jellyfin) userById(userId string, public bool) (map[string]interface{}, int, error) { diff --git a/main.go b/main.go index 51d11c7..fdc8cd1 100644 --- a/main.go +++ b/main.go @@ -132,6 +132,13 @@ func test(app *appContext) { out, err := json.MarshalIndent(users, "", " ") fmt.Print(string(out), err) } + fmt.Printf("Enter a user to grab: ") + var username string + fmt.Scanln(&username) + user, status, err := app.jf.userByName(username, false) + fmt.Printf("userByName (%s): code %d err %s", username, status, err) + out, err := json.MarshalIndent(user, "", " ") + fmt.Print(string(out)) } func start(asDaemon, firstCall bool) { diff --git a/pwreset.go b/pwreset.go index 670dd6a..915ea09 100644 --- a/pwreset.go +++ b/pwreset.go @@ -67,6 +67,12 @@ func pwrMonitor(app *appContext, watcher *fsnotify.Watcher) { } app.storage.loadEmails() var address string + uid := user["Id"] + if uid == nil { + app.err.Printf("Couldn't get user ID for user \"%s\"", pwr.Username) + app.debug.Printf("user maplength: %d", len(user)) + return + } addr, ok := app.storage.emails[user["Id"].(string)] if !ok || addr == nil { app.err.Printf("Couldn't find email for user \"%s\". Make sure it's set", pwr.Username)