feat: show last played songs in web ui

This commit is contained in:
Felipe M 2025-05-13 12:49:59 +02:00
parent 7f16452a99
commit bcc1bce743
Signed by: fmartingr
GPG key ID: CCFBC5637D4000A8
6 changed files with 206 additions and 7 deletions

View file

@ -26,13 +26,18 @@ type JukeboxPlayer struct {
playlistMutex sync.Mutex
playingMutex sync.Mutex
currentStreamCancel context.CancelFunc
songHistory []*subsonic.Song
historyMutex sync.RWMutex
maxHistorySize int
}
// NewJukeboxPlayer creates a new jukebox player
func NewJukeboxPlayer(bot *Bot) *JukeboxPlayer {
jukebox := &JukeboxPlayer{
bot: bot,
playlist: make([]subsonic.Song, 0),
bot: bot,
playlist: make([]subsonic.Song, 0),
songHistory: make([]*subsonic.Song, 0),
maxHistorySize: 20, // Store the last 20 songs
}
// Register command handlers
@ -232,7 +237,7 @@ func (j *JukeboxPlayer) startPlaying() {
j.playingMutex.Lock()
j.currentSong = nil
j.playingMutex.Unlock()
return
}
@ -269,6 +274,9 @@ func (j *JukeboxPlayer) startPlaying() {
j.playingMutex.Lock()
j.currentSong = song
j.playingMutex.Unlock()
// Add song to history when it starts playing
j.addToHistory(song)
// Update Discord status with the current song information in format "Artist - Title (Album)"
var statusText string
@ -772,3 +780,33 @@ func (j *JukeboxPlayer) GetCurrentSong() *subsonic.Song {
defer j.playingMutex.Unlock()
return j.currentSong
}
// addToHistory adds a song to the playback history
func (j *JukeboxPlayer) addToHistory(song *subsonic.Song) {
if song == nil {
return
}
j.historyMutex.Lock()
defer j.historyMutex.Unlock()
// Add the song to the beginning of the history list
j.songHistory = append([]*subsonic.Song{song}, j.songHistory...)
// Trim the history if it exceeds the maximum size
if len(j.songHistory) > j.maxHistorySize {
j.songHistory = j.songHistory[:j.maxHistorySize]
}
}
// GetSongHistory returns a copy of the playback history
func (j *JukeboxPlayer) GetSongHistory() []*subsonic.Song {
j.historyMutex.RLock()
defer j.historyMutex.RUnlock()
// Create a copy of the history to return
history := make([]*subsonic.Song, len(j.songHistory))
copy(history, j.songHistory)
return history
}