All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 4m19s
Generate self-signed certs for local HTTPS, add port 443 and full SSL server block to dev nginx config, add Spotify redirect URI env var, improve Spotify token error handling, and fix Chat/Steam mobile sizing. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
91 lines
1.9 KiB
Go
91 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"adam-french.co.uk/backend/services"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/zmb3/spotify/v2"
|
|
)
|
|
|
|
func (store *Store) CompleteSpotifyAuth(ctx *gin.Context) {
|
|
state := ctx.Query("state")
|
|
c := context.Background()
|
|
// code := c.Query("code")
|
|
|
|
token, err := store.SpotifyAuth.Token(c, state, ctx.Request)
|
|
if err != nil {
|
|
ctx.String(http.StatusInternalServerError, "Couldn't get token: %v", err)
|
|
return
|
|
}
|
|
|
|
if err := services.SaveSpotifyToken(services.SPOTIFY_TOKEN_JSON_PATH, token); err != nil {
|
|
ctx.String(http.StatusInternalServerError, "Failed to save token: %v", err)
|
|
return
|
|
}
|
|
|
|
client := spotify.New(store.SpotifyAuth.Client(c, token))
|
|
|
|
store.SpotifyClient = client
|
|
|
|
ctx.JSON(http.StatusOK, gin.H{
|
|
"message": "Authentication successful",
|
|
"token": token.AccessToken,
|
|
"type": token.TokenType,
|
|
"expiry": token.Expiry,
|
|
})
|
|
}
|
|
|
|
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 {
|
|
ctx.JSON(500, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
ctx.JSON(200, playing)
|
|
}
|
|
|
|
func (store *Store) RecentlyPlayed(ctx *gin.Context) {
|
|
if store.SpotifyClient == nil {
|
|
ctx.JSON(500, gin.H{"error": "Spotify not authenticated"})
|
|
return
|
|
}
|
|
|
|
opts := spotify.RecentlyPlayedOptions{Limit: 3}
|
|
|
|
if store.RecentSongsFresh() {
|
|
ctx.JSON(200, *store.RecentSongs)
|
|
return
|
|
}
|
|
|
|
played, err := store.SpotifyClient.PlayerRecentlyPlayedOpt(ctx, &opts)
|
|
if err != nil {
|
|
ctx.JSON(500, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
ctx.JSON(200, played)
|
|
}
|
|
|
|
func (s *Store) RecentSongsFresh() bool {
|
|
if s.RecentSongs == nil {
|
|
return false
|
|
}
|
|
|
|
if len(*s.RecentSongs) == 0 {
|
|
return false
|
|
}
|
|
|
|
return time.Since(s.RecentSongsFetchedAt) < time.Minute
|
|
}
|