diff --git a/models/tautulli.go b/models/tautulli.go index 5fef21d..dfd61ef 100644 --- a/models/tautulli.go +++ b/models/tautulli.go @@ -115,6 +115,11 @@ type TautulliUser struct { FilterPhotos interface{} `json:"filter_photos"` } +type TautulliUserGroup struct { + TautulliUser TautulliUser `json:"tautulli_user"` + TautulliServer string `json:"tautulli_server"` +} + type TautulliStatusReply struct { Response struct { Result string `json:"result"` diff --git a/models/users.go b/models/users.go index 40212ab..37eb2be 100644 --- a/models/users.go +++ b/models/users.go @@ -3,12 +3,13 @@ package models import "time" type WrapperrUser struct { - FriendlyName string `json:"user_friendly_name"` - User string `json:"user_name"` - UserID int `json:"user_id"` - Email string `json:"user_email"` - Active bool `json:"user_active"` - Wrappings []WrapperrHistoryEntry `json:"wrappings"` + FriendlyName string `json:"user_friendly_name"` + User string `json:"user_name"` + UserID int `json:"user_id"` + Email string `json:"user_email"` + Active bool `json:"user_active"` + TautulliServers []string `json:"user_tautulli_servers"` + Wrappings []WrapperrHistoryEntry `json:"wrappings"` } type WrapperrHistoryEntry struct { diff --git a/modules/tautulli.go b/modules/tautulli.go index cde1400..860a5b7 100644 --- a/modules/tautulli.go +++ b/modules/tautulli.go @@ -224,9 +224,9 @@ func TautulliTestEveryServer() (err error) { return } -func TautulliGetUsersFromEveryServer() (tautulliUsers []models.TautulliUser, err error) { +func TautulliGetUsersFromEveryServer() (tautulliUsers []models.TautulliUserGroup, err error) { err = nil - tautulliUsers = []models.TautulliUser{} + tautulliUsers = []models.TautulliUserGroup{} config, err := files.GetConfig() if err != nil { @@ -243,7 +243,11 @@ func TautulliGetUsersFromEveryServer() (tautulliUsers []models.TautulliUser, err } for _, user := range tautulliReply.Response.Data { - tautulliUsers = append(tautulliUsers, user) + tautulliUserGroup := models.TautulliUserGroup{ + TautulliUser: user, + TautulliServer: config.TautulliConfig[i].TautulliName, + } + tautulliUsers = append(tautulliUsers, tautulliUserGroup) } } diff --git a/modules/users.go b/modules/users.go index f4d0f0f..2ebc4b4 100644 --- a/modules/users.go +++ b/modules/users.go @@ -59,17 +59,8 @@ func UsersGetUser(userID int) (user models.WrapperrUser, err error) { return user, errors.New("User not found.") } -func UsersUpdateUser(userID int, FriendlyName string, userName string, Email string, ActiveInt int) (err error) { +func UsersUpdateUser(userID int, FriendlyName string, userName string, Email string, Active bool, TautulliServers []string) (err error) { err = nil - Active := false - - if ActiveInt == 1 { - Active = true - } else if ActiveInt == 0 { - Active = false - } else { - return errors.New("Invalid active state found.") - } users, err := files.GetUsers() if err != nil { @@ -94,6 +85,7 @@ func UsersUpdateUser(userID int, FriendlyName string, userName string, Email str users[userIndex].Email = Email users[userIndex].FriendlyName = FriendlyName users[userIndex].User = userName + users[userIndex].TautulliServers = TautulliServers err = files.SaveUsers(users) if err != nil { diff --git a/routes/admin.go b/routes/admin.go index 9ac0bfb..982992f 100644 --- a/routes/admin.go +++ b/routes/admin.go @@ -441,13 +441,22 @@ func ApiSyncTautulliUsers(context *gin.Context) { return } - for _, user := range users { - wrapperrUser, err := modules.UsersGetUser(user.UserID) - if err != nil { + newUserArray := []models.WrapperrUser{} + for _, userGroup := range users { + var userMatch bool = false + var userIndex int = 0 + for index, wrapperrUser := range newUserArray { + if userGroup.TautulliUser.UserID == wrapperrUser.UserID { + userMatch = true + userIndex = index + } + } + + if !userMatch { var Active = false - if user.IsActive == 1 { + if userGroup.TautulliUser.IsActive == 1 { Active = true - } else if user.IsActive == 0 { + } else if userGroup.TautulliUser.IsActive == 0 { Active = false } else { log.Println("Failed to convert integer to bool. Error: " + err.Error()) @@ -456,16 +465,29 @@ func ApiSyncTautulliUsers(context *gin.Context) { return } - wrapperrUser = models.WrapperrUser{ - FriendlyName: user.FriendlyName, - User: user.Username, - UserID: user.UserID, - Email: user.Email, - Active: Active, - Wrappings: []models.WrapperrHistoryEntry{}, + wrapperrUser := models.WrapperrUser{ + FriendlyName: userGroup.TautulliUser.FriendlyName, + User: userGroup.TautulliUser.Username, + UserID: userGroup.TautulliUser.UserID, + Email: userGroup.TautulliUser.Email, + TautulliServers: []string{userGroup.TautulliServer}, + Active: Active, + Wrappings: []models.WrapperrHistoryEntry{}, + } + + newUserArray = append(newUserArray, wrapperrUser) + } else { + if !newUserArray[userIndex].Active && userGroup.TautulliUser.IsActive == 1 { + newUserArray[userIndex].Active = true } + newUserArray[userIndex].TautulliServers = append(newUserArray[userIndex].TautulliServers, userGroup.TautulliServer) + } + } - err = modules.UsersSaveUserEntry(wrapperrUser) + for _, user := range newUserArray { + _, err := modules.UsersGetUser(user.UserID) + if err != nil { + err = modules.UsersSaveUserEntry(user) if err != nil { log.Println("Failed to save new user. Error: " + err.Error()) context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save new user."}) @@ -473,7 +495,7 @@ func ApiSyncTautulliUsers(context *gin.Context) { return } } else { - err = modules.UsersUpdateUser(user.UserID, user.FriendlyName, user.Username, user.Email, user.IsActive) + err = modules.UsersUpdateUser(user.UserID, user.FriendlyName, user.User, user.Email, user.Active, user.TautulliServers) if err != nil { log.Println("Failed to update user. Error: " + err.Error()) context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update user."})