Skip to content

Commit

Permalink
Tautulli users from every server
Browse files Browse the repository at this point in the history
  • Loading branch information
oeysteinaune committed Nov 16, 2023
1 parent adcea09 commit 2ec6d68
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 33 deletions.
5 changes: 5 additions & 0 deletions models/tautulli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
13 changes: 7 additions & 6 deletions models/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions modules/tautulli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
}

Expand Down
12 changes: 2 additions & 10 deletions modules/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
50 changes: 36 additions & 14 deletions routes/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -456,24 +465,37 @@ 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."})
context.Abort()
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."})
Expand Down

0 comments on commit 2ec6d68

Please sign in to comment.