mirror of
https://github.com/riwiwa/muzi.git
synced 2026-02-28 03:46:57 -08:00
137 lines
3.4 KiB
Go
137 lines
3.4 KiB
Go
package web
|
|
|
|
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) {
|
|
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
|
|
}
|
|
|
|
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(),
|
|
}
|
|
|
|
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)
|
|
}
|