From 565d0acb183a15e92e8823df6882dcaac8433433 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Litza Date: Fri, 13 Oct 2023 23:48:01 +0200 Subject: [PATCH] Subsonic API: Implement queue sync via "Queue" playlist The extra attributes "position" and "current" (track) are saved as JSON comment of the playlist. --- lib/Controller/SubsonicController.php | 41 +++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/Controller/SubsonicController.php b/lib/Controller/SubsonicController.php index 20849cdc7..bc2fdbaa3 100644 --- a/lib/Controller/SubsonicController.php +++ b/lib/Controller/SubsonicController.php @@ -996,16 +996,47 @@ protected function deleteBookmark(string $id) { * @SubsonicAPI */ protected function getPlayQueue() { - // TODO: not supported yet - return $this->subsonicErrorResponse(30, 'Not implemented'); + $playQueue = ['songs' => []]; + $playlists = $this->playlistBusinessLayer->findAllByName('Queue', $this->userId); + if (\count($playlists) > 1) { + return $this->subsonicErrorResponse(10, 'Found multiple play queues'); + } elseif (\count($playlists) == 1) { + $playlist = $playlists[0]; + $tracks = $this->playlistBusinessLayer->getPlaylistTracks($playlist->id, $this->userId); + $playQueue['songs'] = $this->tracksToApi($tracks); + $comment = $playlist->getComment(); + if (!empty($comment) && $additional = \json_decode($comment, true)) { + $playQueue = \array_merge($playQueue, $additional); + } + } + return $this->subsonicResponse(['playQueue' => $playQueue]); } /** * @SubsonicAPI */ - protected function savePlayQueue() { - // TODO: not supported yet - return $this->subsonicErrorResponse(30, 'Not implemented'); + protected function savePlayQueue(array $id, ?string $current = null, ?int $position = null) { + $playlists = $this->playlistBusinessLayer->findAllByName('Queue', $this->userId); + if (\count($playlists) == 0) { + $playlist = $this->playlistBusinessLayer->create('Queue', $this->userId); + } elseif (\count($playlists) > 1) { + return $this->subsonicErrorResponse(10, 'Found multiple play queues'); + } else { + $playlist = $playlists[0]; + } + + $this->playlistBusinessLayer->setTracks( + \array_map('self::ripIdPrefix', $id), + $playlist->id, + $this->userId + ); + $additional = []; + if (isset($current)) + $additional['current'] = $current; + if (isset($position)) + $additional['position'] = $position; + $this->playlistBusinessLayer->setComment(\json_encode($additional), $playlist->id, $this->userId); + return $this->subsonicResponse([]); } /**