mirror of
https://github.com/riwiwa/muzi.git
synced 2026-02-28 11:56:57 -08:00
add scrobbling through listenbrainz-like endpoint and lastfm-like endpoint
This commit is contained in:
123
web/settings.go
123
web/settings.go
@@ -1,6 +1,22 @@
|
||||
package web
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"muzi/scrobble"
|
||||
)
|
||||
|
||||
type settingsData struct {
|
||||
Title string
|
||||
LoggedInUsername string
|
||||
TemplateName string
|
||||
APIKey string
|
||||
APISecret string
|
||||
SpotifyClientId string
|
||||
SpotifyConnected bool
|
||||
}
|
||||
|
||||
func settingsPageHandler() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -9,19 +25,112 @@ func settingsPageHandler() http.HandlerFunc {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
type data struct {
|
||||
Title string
|
||||
LoggedInUsername string
|
||||
TemplateName string
|
||||
|
||||
userId, err := getUserIdByUsername(r.Context(), username)
|
||||
if err != nil {
|
||||
http.Error(w, "User not found", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
d := data{
|
||||
|
||||
user, err := scrobble.GetUserById(userId)
|
||||
if err != nil {
|
||||
http.Error(w, "Error loading user", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
d := settingsData{
|
||||
Title: "muzi | Settings",
|
||||
LoggedInUsername: username,
|
||||
TemplateName: "settings",
|
||||
APIKey: "",
|
||||
APISecret: "",
|
||||
SpotifyClientId: "",
|
||||
SpotifyConnected: user.IsSpotifyConnected(),
|
||||
}
|
||||
err := templates.ExecuteTemplate(w, "base", d)
|
||||
|
||||
if user.ApiKey != nil {
|
||||
d.APIKey = *user.ApiKey
|
||||
}
|
||||
if user.ApiSecret != nil {
|
||||
d.APISecret = *user.ApiSecret
|
||||
}
|
||||
if user.SpotifyClientId != nil {
|
||||
d.SpotifyClientId = *user.SpotifyClientId
|
||||
}
|
||||
|
||||
err = templates.ExecuteTemplate(w, "base", d)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func generateAPIKeyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
username := getLoggedInUsername(r)
|
||||
if username == "" {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := getUserIdByUsername(r.Context(), username)
|
||||
if err != nil {
|
||||
http.Error(w, "User not found", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
apiKey, err := scrobble.GenerateAPIKey()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error generating API key: %v\n", err)
|
||||
http.Error(w, "Error generating API key", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
apiSecret, err := scrobble.GenerateAPISecret()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error generating API secret: %v\n", err)
|
||||
http.Error(w, "Error generating API secret", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = scrobble.UpdateUserAPIKey(userId, apiKey, apiSecret)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error saving API key: %v\n", err)
|
||||
http.Error(w, "Error saving API key", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/settings", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func updateSpotifyCredentialsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
username := getLoggedInUsername(r)
|
||||
if username == "" {
|
||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := getUserIdByUsername(r.Context(), username)
|
||||
if err != nil {
|
||||
http.Error(w, "User not found", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
clientId := r.FormValue("spotify_client_id")
|
||||
clientSecret := r.FormValue("spotify_client_secret")
|
||||
|
||||
if clientId == "" || clientSecret == "" {
|
||||
err = scrobble.DeleteUserSpotifyCredentials(userId)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error removing Spotify credentials: %v\n", err)
|
||||
}
|
||||
} else {
|
||||
err = scrobble.UpdateUserSpotifyCredentials(userId, clientId, clientSecret)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error saving Spotify credentials: %v\n", err)
|
||||
http.Error(w, "Error saving Spotify credentials", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/settings", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user