Files
stpweb/backend/handlers/handle_spotify.go
T
adamfandClaude Opus 5.5 4530cb8dc4
Test and deploy / test (push) Successful in 28s
Test and deploy / deploy (push) Successful in 31s
Trim comments to the non-obvious why
Drop narration, history and restated-code comments across the repo,
keep short ones for security boundaries, upstream quirks and ordering
constraints, and record the convention in CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 20:49:03 +00:00

114 lines
2.9 KiB
Go

package handlers
import (
"context"
"log"
"net/http"
"adam-french.co.uk/backend/services"
"github.com/gin-gonic/gin"
"github.com/zmb3/spotify/v2"
)
// The callback must be REST (Spotify redirects a browser to it); the rest
// predate the GraphQL equivalents.
// CompleteSpotifyAuth installs the client on the Store, so no restart is needed.
func (store *Store) CompleteSpotifyAuth(ctx *gin.Context) {
state := ctx.Query("state")
// The real CSRF check: the library compares state against the value we
// pass it, which is the one just received.
if !store.ConsumeSpotifyState(state) {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid state"})
return
}
// The client outlives this request.
c := context.Background()
token, err := store.SpotifyAuth.Token(c, state, ctx.Request)
if err != nil {
log.Println(err)
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "authentication failed"})
return
}
if err := services.SaveSpotifyToken(services.SPOTIFY_TOKEN_JSON_PATH, token); err != nil {
log.Println(err)
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "internal error"})
return
}
client := spotify.New(store.SpotifyAuth.Client(c, token))
store.SpotifyClient = client
ctx.JSON(http.StatusOK, gin.H{
"message": "Authentication successful",
})
}
// StartSpotifyAuth returns the authorisation URL with a fresh state nonce.
func (store *Store) StartSpotifyAuth(ctx *gin.Context) {
state, err := store.NewSpotifyState()
if err != nil {
log.Println(err)
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "internal error"})
return
}
ctx.JSON(http.StatusOK, gin.H{"url": store.SpotifyAuth.AuthURL(state)})
}
func (store *Store) ListeningTo(ctx *gin.Context) {
c := ctx.Request.Context()
if store.SpotifyClient == nil {
ctx.JSON(500, gin.H{"error": "Spotify not authenticated"})
return
}
playing, err := store.SpotifyClient.PlayerCurrentlyPlaying(c)
if err != nil {
log.Println(err)
ctx.JSON(500, gin.H{"error": "failed to fetch currently playing"})
return
}
ctx.JSON(200, playing)
}
// RecentlyPlayed shares its cache with the GraphQL spotifyRecent resolver.
func (store *Store) RecentlyPlayed(ctx *gin.Context) {
if store.SpotifyClient == nil {
ctx.JSON(500, gin.H{"error": "Spotify not authenticated"})
return
}
played, err := store.RecentlyPlayedTracks(ctx)
if err != nil {
log.Println(err)
ctx.JSON(500, gin.H{"error": "failed to fetch recently played"})
return
}
ctx.JSON(200, played)
}
// RecentlyPlayedTracks requires a non-nil SpotifyClient.
func (store *Store) RecentlyPlayedTracks(ctx context.Context) ([]spotify.RecentlyPlayedItem, error) {
if cached, ok := store.CachedRecentSongs(); ok {
return cached, nil
}
opts := spotify.RecentlyPlayedOptions{Limit: 3}
played, err := store.SpotifyClient.PlayerRecentlyPlayedOpt(ctx, &opts)
if err != nil {
return nil, err
}
store.SetRecentSongs(played)
return played, nil
}