add basic lastfm hook

This commit is contained in:
sentriz
2019-04-17 21:36:40 +01:00
parent 9f6cd20f5a
commit 4cd9c0c39c
17 changed files with 199 additions and 33 deletions

View File

@@ -74,6 +74,15 @@ func (c *Controller) ServeChangeOwnPasswordDo(w http.ResponseWriter, r *http.Req
http.Redirect(w, r, "/admin/home", 303)
}
func (c *Controller) ServeLinkLastFMCallback(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("token")
if token == "" {
http.Error(w, "please provide a token", 400)
return
}
_ = token
}
func (c *Controller) ServeChangePassword(w http.ResponseWriter, r *http.Request) {
username := r.URL.Query().Get("user")
if username == "" {
@@ -148,3 +157,36 @@ func (c *Controller) ServeCreateUserDo(w http.ResponseWriter, r *http.Request) {
}
http.Redirect(w, r, "/admin/home", 303)
}
func (c *Controller) ServeUpdateLastFMAPIKey(w http.ResponseWriter, r *http.Request) {
var data templateData
var apiKey db.Setting
var secret db.Setting
c.DB.Where("key = ?", "lastfm_api_key").First(&apiKey)
c.DB.Where("key = ?", "lastfm_secret").First(&secret)
data.CurrentLastFMAPIKey = apiKey.Value
data.CurrentLastFMAPISecret = secret.Value
renderTemplate(w, r, "update_lastfm_api_key", &data)
}
func (c *Controller) ServeUpdateLastFMAPIKeyDo(w http.ResponseWriter, r *http.Request) {
session := r.Context().Value("session").(*sessions.Session)
apiKey := r.FormValue("api_key")
secret := r.FormValue("secret")
err := utilities.ValidateAPIKey(apiKey, secret)
if err != nil {
session.AddFlash(err.Error())
session.Save(r, w)
http.Redirect(w, r, r.Header.Get("Referer"), 302)
return
}
c.DB.
Where(db.Setting{Key: "lastfm_api_key"}).
Assign(db.Setting{Value: apiKey}).
FirstOrCreate(&db.Setting{})
c.DB.
Where(db.Setting{Key: "lastfm_secret"}).
Assign(db.Setting{Value: secret}).
FirstOrCreate(&db.Setting{})
http.Redirect(w, r, "/admin/home", 303)
}

View File

@@ -47,6 +47,11 @@ func init() {
filepath.Join("templates", "user.tmpl"),
filepath.Join("templates", "pages", "create_user.tmpl"),
))
templates["update_lastfm_api_key"] = template.Must(template.ParseFiles(
filepath.Join("templates", "layout.tmpl"),
filepath.Join("templates", "user.tmpl"),
filepath.Join("templates", "pages", "update_lastfm_api_key.tmpl"),
))
}
type Controller struct {
@@ -55,13 +60,16 @@ type Controller struct {
}
type templateData struct {
Flashes []interface{}
User *db.User
SelectedUser *db.User
AllUsers []*db.User
ArtistCount uint
AlbumCount uint
TrackCount uint
Flashes []interface{}
User *db.User
SelectedUser *db.User
AllUsers []*db.User
ArtistCount uint
AlbumCount uint
TrackCount uint
CurrentLastFMAPIKey string
CurrentLastFMAPISecret string
RequestRoot string
}
func getStrParam(r *http.Request, key string) string {
@@ -146,6 +154,11 @@ func renderTemplate(w http.ResponseWriter, r *http.Request,
if ok {
data.User = user
}
if r.URL.Host == "" {
data.RequestRoot = "http://localhost"
} else {
data.RequestRoot = fmt.Sprintf("%s://%s", r.URL.Scheme, r.URL.Host)
}
err := templates[name].ExecuteTemplate(w, "layout", data)
if err != nil {
http.Error(w, fmt.Sprintf("500 when executing: %v", err), 500)

View File

@@ -18,3 +18,10 @@ func ValidatePasswords(pOne, pTwo string) error {
}
return nil
}
func ValidateAPIKey(apiKey, secret string) error {
if apiKey == "" || secret == "" {
return fmt.Errorf("please enter both the api key and secret")
}
return nil
}