Skip to content

Commit

Permalink
Subsonic API: Implement queue sync via "Queue" playlist
Browse files Browse the repository at this point in the history
The extra attributes "position" and "current" (track) are saved as JSON
comment of the playlist.
  • Loading branch information
jplitza committed Oct 13, 2023
1 parent 1c2c6dc commit d95e1b0
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions lib/Controller/SubsonicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -996,16 +996,52 @@ protected function deleteBookmark(string $id) {
* @SubsonicAPI
*/
protected function getPlayQueue() {
// TODO: not supported yet
return $this->subsonicErrorResponse(30, 'Not implemented');
$playQueue = ['songs' => [], 'position' => 0];
$playlists = $this->playlistBusinessLayer->findAllByName('Queue', $this->userId);
if (\count($playlists) == 0) {
return $this->subsonicResponse(['playQueue' => $playQueue]);
}
else if (\count($playlists) > 1) {
return $this->subsonicErrorResponse(10, 'Found multiple play queues');
}
else {
$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);
else if (\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
);
if (isset($current) || isset($position)) {
$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([]);
}

/**
Expand Down

0 comments on commit d95e1b0

Please sign in to comment.