userByName reloads cache if user not found, more things in test

pull/20/head
Harvey Tindall 4 years ago
parent 802f957d22
commit 7d84fdec96
No known key found for this signature in database
GPG Key ID: BBC65952848FB1A2

1
.gitignore vendored

@ -14,3 +14,4 @@ build/
pkg/ pkg/
old/ old/
version.go version.go
notes

@ -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) { func (jf *Jellyfin) userByName(username string, public bool) (map[string]interface{}, int, error) {
users, status, err := jf.getUsers(public) var match map[string]interface{}
if err != nil || status != 200 { 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 return nil, status, err
} }
for _, user := range users { match, status, err := find()
if user["Name"].(string) == username { if match == nil {
return user, status, 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) { func (jf *Jellyfin) userById(userId string, public bool) (map[string]interface{}, int, error) {

@ -132,6 +132,13 @@ func test(app *appContext) {
out, err := json.MarshalIndent(users, "", " ") out, err := json.MarshalIndent(users, "", " ")
fmt.Print(string(out), err) 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) { func start(asDaemon, firstCall bool) {

@ -67,6 +67,12 @@ func pwrMonitor(app *appContext, watcher *fsnotify.Watcher) {
} }
app.storage.loadEmails() app.storage.loadEmails()
var address string 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)] addr, ok := app.storage.emails[user["Id"].(string)]
if !ok || addr == nil { if !ok || addr == nil {
app.err.Printf("Couldn't find email for user \"%s\". Make sure it's set", pwr.Username) app.err.Printf("Couldn't find email for user \"%s\". Make sure it's set", pwr.Username)

Loading…
Cancel
Save