1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-29 12:30:11 +00:00

Compare commits

..

No commits in common. "91252730366ea34a9feb2a7936b9e0fc14a1beb2" and "72eb51e9c0841cf30baef6aa0868f2d8d24eb8f6" have entirely different histories.

3 changed files with 11 additions and 54 deletions

View File

@ -44,7 +44,6 @@ type Jellyfin struct {
CacheExpiry time.Time
cacheLength int
noFail bool
Hyphens bool
timeoutHandler common.TimeoutHandler
}
@ -209,8 +208,8 @@ func (jf *Jellyfin) post(url string, data map[string]interface{}, response bool)
}
// DeleteUser deletes the user corresponding to the provided ID.
func (jf *Jellyfin) DeleteUser(userID string) (int, error) {
url := fmt.Sprintf("%s/Users/%s", jf.Server, userID)
func (jf *Jellyfin) DeleteUser(id string) (int, error) {
url := fmt.Sprintf("%s/Users/%s", jf.Server, id)
req, _ := http.NewRequest("DELETE", url, nil)
for name, value := range jf.header {
req.Header.Add(name, value)
@ -240,11 +239,6 @@ func (jf *Jellyfin) GetUsers(public bool) ([]map[string]interface{}, int, error)
json.Unmarshal([]byte(data), &result)
jf.userCache = result
jf.CacheExpiry = time.Now().Add(time.Minute * time.Duration(jf.cacheLength))
if id, ok := result[0]["Id"]; ok {
if id.(string)[8] == '-' {
jf.Hyphens = true
}
}
return result, status, nil
}
return jf.userCache, 200, nil

23
main.go
View File

@ -42,7 +42,6 @@ type User struct {
Password string `json:"password"`
}
// contains everything the application needs, essentially. Wouldn't do this in the future.
type appContext struct {
// defaults *Config
config *ini.File
@ -462,7 +461,7 @@ func start(asDaemon, firstCall bool) {
app.err.Fatalf("Failed to authenticate with Jellyfin @ %s: Code %d", server, status)
}
app.info.Printf("Authenticated with %s", server)
// from 10.7.0, jellyfin may hyphenate user IDs. This checks if the version is equal or higher.
// from 10.7.0, jellyfin hyphenates user IDs. This checks if the version is equal or higher.
checkVersion := func(version string) int {
numberStrings := strings.Split(version, ".")
n := 0
@ -475,9 +474,6 @@ func start(asDaemon, firstCall bool) {
return n
}
if checkVersion(app.jf.ServerInfo.Version) >= checkVersion("10.7.0") {
// Get users to check if server uses hyphenated userIDs
app.jf.GetUsers(false)
noHyphens := true
for id := range app.storage.emails {
if strings.Contains(id, "-") {
@ -485,19 +481,10 @@ func start(asDaemon, firstCall bool) {
break
}
}
if noHyphens == app.jf.Hyphens {
var newEmails map[string]interface{}
var status int
var err error
if app.jf.Hyphens {
app.info.Println(aurora.Yellow("Your build of Jellyfin appears to hypenate user IDs. Your emails.json file will be modified to match."))
time.Sleep(time.Second * time.Duration(3))
newEmails, status, err = app.hyphenateEmailStorage(app.storage.emails)
} else {
app.info.Println(aurora.Yellow("Your emails.json file uses hyphens, but the Jellyfin server no longer does. It will be modified."))
time.Sleep(time.Second * time.Duration(3))
newEmails, status, err = app.deHyphenateEmailStorage(app.storage.emails)
}
if noHyphens {
app.info.Println(aurora.Yellow("From Jellyfin 10.7.0 onwards, user IDs are hyphenated.\nYour emails.json file will be modified to match this new format.\nA backup will be placed next to the file.\n"))
time.Sleep(time.Second * time.Duration(3))
newEmails, status, err := app.upgradeEmailStorage(app.storage.emails)
if status != 200 || err != nil {
app.err.Printf("Failed to get users from Jellyfin: Code %d", status)
app.debug.Printf("Error: %s", err)

View File

@ -192,34 +192,10 @@ func storeJSON(path string, obj interface{}) error {
return err
}
// One build of JF 10.7.0 hyphenated to user IDs like this and we need to upgrade email storage to match it:
// One build of JF 10.7.0 hyphenated user IDs while another one later didn't. These functions will hyphenate/de-hyphenate email storage.
func hyphenate(userID string) string {
if userID[8] == '-' {
return userID
}
return userID[:8] + "-" + userID[8:12] + "-" + userID[12:16] + "-" + userID[16:20] + "-" + userID[20:]
}
func (app *appContext) deHyphenateEmailStorage(old map[string]interface{}) (map[string]interface{}, int, error) {
jfUsers, status, err := app.jf.GetUsers(false)
if status != 200 || err != nil {
return nil, status, err
}
newEmails := map[string]interface{}{}
for _, user := range jfUsers {
unHyphenated := user["Id"].(string)
hyphenated := hyphenate(unHyphenated)
email, ok := old[hyphenated]
if ok {
newEmails[unHyphenated] = email
}
}
return newEmails, status, err
}
func (app *appContext) hyphenateEmailStorage(old map[string]interface{}) (map[string]interface{}, int, error) {
// JF 10.7.0 added hyphens to user IDs like this and we need to upgrade email storage to match it:
// [8 chars]-[4]-[4]-[4]-[12]
// This seems consistent, but we'll grab IDs from jellyfin just in case theres some variation.
func (app *appContext) upgradeEmailStorage(old map[string]interface{}) (map[string]interface{}, int, error) {
jfUsers, status, err := app.jf.GetUsers(false)
if status != 200 || err != nil {
return nil, status, err