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

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

This commit is contained in:
Harvey Tindall 2020-09-16 19:19:04 +01:00
parent 802f957d22
commit 7d84fdec96
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2
4 changed files with 30 additions and 7 deletions

1
.gitignore vendored
View File

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

View File

@ -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) {

View File

@ -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) {

View File

@ -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)