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

Compare commits

..

No commits in common. "7d84fdec961635d454009c7274e1618a17e6b685" and "1707c8c1f99d24aa6a083652bfec4cdb16c1fa59" have entirely different histories.

5 changed files with 31 additions and 83 deletions

1
.gitignore vendored
View File

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

View File

@ -136,10 +136,6 @@
.settingIcon { .settingIcon {
margin-left: 0.2rem; margin-left: 0.2rem;
} }
body.modal-open {
overflow: hidden;
}
</style> </style>
<title>Admin</title> <title>Admin</title>
</head> </head>
@ -408,23 +404,10 @@
<script> <script>
{{ if .bs5 }} {{ if .bs5 }}
function createModal(id, find = false) { function createModal(id, find = false) {
let modal;
if (find) { if (find) {
modal = bootstrap.Modal.getInstance(document.getElementById(id)); return bootstrap.Modal.getInstance(document.getElementById(id));
} else {
modal = new bootstrap.Modal(document.getElementById(id));
} }
document.getElementById(id).addEventListener('shown.bs.modal', function () { return new bootstrap.Modal(document.getElementById(id));
document.body.classList.add("modal-open");
});
return {
modal: modal,
show: function() {
let temp = this.modal.show();
return temp
},
hide: function() { return this.modal.hide(); }
};
} }
{{ else }} {{ else }}
let send_to_addess_enabled = document.getElementById('send_to_address_enabled'); let send_to_addess_enabled = document.getElementById('send_to_address_enabled');
@ -436,13 +419,9 @@
multiUseEnabled.classList.remove('form-check-input'); multiUseEnabled.classList.remove('form-check-input');
} }
function createModal(id, find = false) { function createModal(id, find = false) {
$('#' + id).on('shown.bs.modal', function () {
document.body.classList.add("modal-open");
});
return { return {
show: function() { show: function() {
let temp = $('#' + id).modal('show'); return $('#' + id).modal('show');
return temp
}, },
hide: function() { hide: function() {
return $('#' + id).modal('hide'); return $('#' + id).modal('hide');

View File

@ -9,7 +9,6 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"strings"
"time" "time"
) )
@ -137,7 +136,7 @@ func (jf *Jellyfin) authenticate(username, password string) (map[string]interfac
return user, resp.StatusCode, nil return user, resp.StatusCode, nil
} }
func (jf *Jellyfin) _get(url string, params map[string]string) (string, int, error) { func (jf *Jellyfin) _getReader(url string, params map[string]string) (io.Reader, int, error) {
var req *http.Request var req *http.Request
if params != nil { if params != nil {
jsonParams, _ := json.Marshal(params) jsonParams, _ := json.Marshal(params)
@ -155,32 +154,26 @@ func (jf *Jellyfin) _get(url string, params map[string]string) (string, int, err
jf.authenticated = false jf.authenticated = false
_, _, authErr := jf.authenticate(jf.username, jf.password) _, _, authErr := jf.authenticate(jf.username, jf.password)
if authErr == nil { if authErr == nil {
v1, v2, v3 := jf._get(url, params) v1, v2, v3 := jf._getReader(url, params)
return v1, v2, v3 return v1, v2, v3
} }
} }
return "", resp.StatusCode, err return nil, resp.StatusCode, err
} }
defer resp.Body.Close() defer resp.Body.Close()
var data io.Reader var data io.Reader
encoding := resp.Header.Get("Content-Encoding") switch resp.Header.Get("Content-Encoding") {
if TEST {
fmt.Println("response encoding:", encoding)
}
switch encoding {
case "gzip": case "gzip":
data, _ = gzip.NewReader(resp.Body) data, _ = gzip.NewReader(resp.Body)
default: default:
data = resp.Body data = resp.Body
} }
buf := new(strings.Builder)
io.Copy(buf, data)
//var respData map[string]interface{} //var respData map[string]interface{}
//json.NewDecoder(data).Decode(&respData) //json.NewDecoder(data).Decode(&respData)
return buf.String(), resp.StatusCode, nil return data, resp.StatusCode, nil
} }
func (jf *Jellyfin) _post(url string, data map[string]interface{}, response bool) (string, int, error) { func (jf *Jellyfin) _post(url string, data map[string]interface{}, response bool) (io.Reader, int, error) {
params, _ := json.Marshal(data) params, _ := json.Marshal(data)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(params)) req, _ := http.NewRequest("POST", url, bytes.NewBuffer(params))
for name, value := range jf.header { for name, value := range jf.header {
@ -197,7 +190,7 @@ func (jf *Jellyfin) _post(url string, data map[string]interface{}, response bool
return v1, v2, v3 return v1, v2, v3
} }
} }
return "", resp.StatusCode, err return nil, resp.StatusCode, err
} }
if response { if response {
defer resp.Body.Close() defer resp.Body.Close()
@ -208,30 +201,29 @@ func (jf *Jellyfin) _post(url string, data map[string]interface{}, response bool
default: default:
outData = resp.Body outData = resp.Body
} }
buf := new(strings.Builder) return outData, resp.StatusCode, nil
io.Copy(buf, outData)
return buf.String(), resp.StatusCode, nil
} }
return "", resp.StatusCode, nil return nil, resp.StatusCode, nil
} }
func (jf *Jellyfin) getUsers(public bool) ([]map[string]interface{}, int, error) { func (jf *Jellyfin) getUsers(public bool) ([]map[string]interface{}, int, error) {
var result []map[string]interface{} var result []map[string]interface{}
var data string var data io.Reader
var status int var status int
var err error var err error
if time.Now().After(jf.cacheExpiry) { if time.Now().After(jf.cacheExpiry) {
if public { if public {
url := fmt.Sprintf("%s/users/public", jf.server) url := fmt.Sprintf("%s/users/public", jf.server)
data, status, err = jf._get(url, nil) data, status, err = jf._getReader(url, nil)
} else { } else {
url := fmt.Sprintf("%s/users", jf.server) url := fmt.Sprintf("%s/users", jf.server)
data, status, err = jf._get(url, jf.loginParams) data, status, err = jf._getReader(url, jf.loginParams)
} }
if err != nil || status != 200 { if err != nil || status != 200 {
return nil, status, err return nil, status, err
} }
json.Unmarshal([]byte(data), &result) json.NewDecoder(data).Decode(&result)
jf.userCache = result jf.userCache = result
jf.cacheExpiry = time.Now().Add(time.Minute * time.Duration(jf.cacheLength)) jf.cacheExpiry = time.Now().Add(time.Minute * time.Duration(jf.cacheLength))
return result, status, nil return result, status, nil
@ -240,25 +232,16 @@ 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) {
var match map[string]interface{} users, status, err := jf.getUsers(public)
find := func() (map[string]interface{}, int, error) { if err != nil || status != 200 {
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
} }
match, status, err := find() for _, user := range users {
if match == nil { if user["Name"].(string) == username {
jf.cacheExpiry = time.Now() return user, status, nil
match, status, err = find() }
} }
return match, status, err return nil, 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) {
@ -282,15 +265,15 @@ func (jf *Jellyfin) userById(userId string, public bool) (map[string]interface{}
return nil, status, err return nil, status, err
} else { } else {
var result map[string]interface{} var result map[string]interface{}
var data string var data io.Reader
var status int var status int
var err error var err error
url := fmt.Sprintf("%s/users/%s", jf.server, userId) url := fmt.Sprintf("%s/users/%s", jf.server, userId)
data, status, err = jf._get(url, jf.loginParams) data, status, err = jf._getReader(url, jf.loginParams)
if err != nil || status != 200 { if err != nil || status != 200 {
return nil, status, err return nil, status, err
} }
json.Unmarshal([]byte(data), &result) json.NewDecoder(data).Decode(&result)
return result, status, nil return result, status, nil
} }
} }
@ -305,9 +288,9 @@ func (jf *Jellyfin) newUser(username, password string) (map[string]interface{},
for key, value := range stringData { for key, value := range stringData {
data[key] = value data[key] = value
} }
response, status, err := jf._post(url, data, true) reader, status, err := jf._post(url, data, true)
var recv map[string]interface{} var recv map[string]interface{}
json.Unmarshal([]byte(response), &recv) json.NewDecoder(reader).Decode(&recv)
if err != nil || !(status == 200 || status == 204) { if err != nil || !(status == 200 || status == 204) {
return nil, status, err return nil, status, err
} }
@ -331,12 +314,12 @@ func (jf *Jellyfin) setConfiguration(userId string, configuration map[string]int
func (jf *Jellyfin) getDisplayPreferences(userId string) (map[string]interface{}, int, error) { func (jf *Jellyfin) getDisplayPreferences(userId string) (map[string]interface{}, int, error) {
url := fmt.Sprintf("%s/DisplayPreferences/usersettings?userId=%s&client=emby", jf.server, userId) url := fmt.Sprintf("%s/DisplayPreferences/usersettings?userId=%s&client=emby", jf.server, userId)
data, status, err := jf._get(url, nil) data, status, err := jf._getReader(url, nil)
if err != nil || !(status == 204 || status == 200) { if err != nil || !(status == 204 || status == 200) {
return nil, status, err return nil, status, err
} }
var displayprefs map[string]interface{} var displayprefs map[string]interface{}
err = json.Unmarshal([]byte(data), &displayprefs) err = json.NewDecoder(data).Decode(&displayprefs)
if err != nil { if err != nil {
return nil, status, err return nil, status, err
} }

View File

@ -132,13 +132,6 @@ 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) {

View File

@ -67,12 +67,6 @@ 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)