From 98a140791b71e66d9d6fc2dd693d7d9a6fd7d738 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Sat, 13 Apr 2024 08:34:14 -0700 Subject: [PATCH] beginning of work to save queue to server --- backend/app.go | 2 +- backend/mediaprovider/mediaprovider.go | 5 +++++ backend/mediaprovider/model.go | 6 ++++++ .../subsonic/subsonicmediaprovider.go | 21 +++++++++++++++++++ backend/savedplayqueue.go | 12 +++++------ 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/backend/app.go b/backend/app.go index 1ea8d41..1f211c5 100644 --- a/backend/app.go +++ b/backend/app.go @@ -305,7 +305,7 @@ func (a *App) Shutdown() { a.MPRISHandler.Shutdown() a.PlaybackManager.DisableCallbacks() if a.Config.Application.SavePlayQueue { - SavePlayQueue(a.ServerManager.ServerID.String(), a.PlaybackManager, configdir.LocalConfig(a.appName, savedQueueFile)) + SavePlayQueue(a.ServerManager.ServerID.String(), a.PlaybackManager, configdir.LocalConfig(a.appName, savedQueueFile), false) } a.PlaybackManager.Stop() // will trigger scrobble check a.Config.LocalPlayback.Volume = a.LocalPlayer.GetVolume() diff --git a/backend/mediaprovider/mediaprovider.go b/backend/mediaprovider/mediaprovider.go index cc04d2c..648a363 100644 --- a/backend/mediaprovider/mediaprovider.go +++ b/backend/mediaprovider/mediaprovider.go @@ -258,6 +258,11 @@ type SupportsSharing interface { CanShareArtists() bool } +type CanSavePlayQueue interface { + SavePlayQueue(trackIDs []string, currentTrackPos int, timeSeconds int) error + GetPlayQueue() (*SavedPlayQueue, error) +} + type LyricsProvider interface { GetLyrics(track *Track) (*Lyrics, error) } diff --git a/backend/mediaprovider/model.go b/backend/mediaprovider/model.go index 33a9d70..c865bce 100644 --- a/backend/mediaprovider/model.go +++ b/backend/mediaprovider/model.go @@ -133,6 +133,12 @@ type LyricLine struct { Start float64 // seconds } +type SavedPlayQueue struct { + Tracks []*Track + TrackPos int + TimePos int // seconds +} + type ContentType int const ( diff --git a/backend/mediaprovider/subsonic/subsonicmediaprovider.go b/backend/mediaprovider/subsonic/subsonicmediaprovider.go index a8316bb..ea7001d 100644 --- a/backend/mediaprovider/subsonic/subsonicmediaprovider.go +++ b/backend/mediaprovider/subsonic/subsonicmediaprovider.go @@ -379,6 +379,27 @@ func (s *subsonicMediaProvider) GetLyrics(track *mediaprovider.Track) (*mediapro return mpLyrics, nil } +// CanSavePlayQueue interface + +func (s *subsonicMediaProvider) SavePlayQueue(trackIDs []string, currentTrackPos int, timeSeconds int) error { + return s.client.SavePlayQueue(trackIDs, map[string]string{ + "current": trackIDs[currentTrackPos], + "position": strconv.Itoa(timeSeconds * 1000), + }) +} + +func (s *subsonicMediaProvider) GetPlayQueue() (*mediaprovider.SavedPlayQueue, error) { + pq, err := s.client.GetPlayQueue() + if err != nil { + return nil, err + } + savedQueue := &mediaprovider.SavedPlayQueue{} + savedQueue.Tracks = sharedutil.MapSlice(pq.Entries, toTrack) + savedQueue.TrackPos = pq.Current + savedQueue.TimePos = int(pq.Position) + return savedQueue, nil +} + func toTrack(ch *subsonic.Child) *mediaprovider.Track { if ch == nil { return nil diff --git a/backend/savedplayqueue.go b/backend/savedplayqueue.go index 1fe8e85..53cdca6 100644 --- a/backend/savedplayqueue.go +++ b/backend/savedplayqueue.go @@ -22,7 +22,7 @@ type serializedSavedPlayQueue struct { } // SavePlayQueue saves the current play queue and playback position to a JSON file. -func SavePlayQueue(serverID string, pm *PlaybackManager, filepath string) error { +func SavePlayQueue(serverID string, pm *PlaybackManager, filepath string, saveToServer bool) error { queue := pm.GetPlayQueue() stats := pm.PlayerStatus() trackIdx := pm.NowPlayingIndex() @@ -38,12 +38,12 @@ func SavePlayQueue(serverID string, pm *PlaybackManager, filepath string) error TrackIndex: trackIdx, TimePos: stats.TimePos, } - b, err := json.Marshal(saved) - if err != nil { - return err - } + b, _ := json.Marshal(saved) + err := os.WriteFile(filepath, b, 0644) - return os.WriteFile(filepath, b, 0644) + if saveToServer { + } + return err } // Loads the saved play queue from the given filepath using the current server.