Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
[#1219](https://github.com/owncloud/music/pull/1219) @mexvod
- Hungarian translation
[#1245](https://github.com/owncloud/music/pull/1245) @rkrisztian
- Subsonic: Support for methods `savePlayQueue` and `getPlayQueue`
[#1241](https://github.com/owncloud/music/pull/1241) @mattwellss

### Changed
- Human-friendly formatting for bitrate and sample rate in the track details pane
Expand Down
84 changes: 79 additions & 5 deletions lib/Controller/SubsonicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use OCP\Files\File;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUserManager;
Expand Down Expand Up @@ -95,6 +96,7 @@ class SubsonicController extends ApiController {
private AmpacheImageService $imageService;
private Random $random;
private Logger $logger;
private IConfig $configManager;
private ?string $userId;
private ?int $keyId;
private array $ignoredArticles;
Expand Down Expand Up @@ -122,7 +124,8 @@ public function __construct(string $appName,
PodcastService $podcastService,
AmpacheImageService $imageService,
Random $random,
Logger $logger) {
Logger $logger,
\OCP\IConfig $configManager) {
parent::__construct($appName, $request, 'POST, GET', 'Authorization, Content-Type, Accept, X-Requested-With');

$this->albumBusinessLayer = $albumBusinessLayer;
Expand All @@ -145,6 +148,7 @@ public function __construct(string $appName,
$this->imageService = $imageService;
$this->random = $random;
$this->logger = $logger;
$this->configManager = $configManager;
$this->userId = null;
$this->keyId = null;
$this->ignoredArticles = [];
Expand Down Expand Up @@ -1068,15 +1072,78 @@ protected function deleteBookmark(string $id) : array {
* @SubsonicAPI
*/
protected function getPlayQueue() : array {
// TODO: not supported yet
return ['playQueue' => []];
/** @var array|false $playQueue */
$playQueue = json_decode($this->configManager->getUserValue($this->user(), $this->appName, 'play_queue', 'false'), true);

if (!$playQueue) {
return [];
}

$parsedEntries = \array_map([self::class, 'parseEntityId'], $playQueue['entry']);

$typeHandlers = [
[
'track',
[$this->trackBusinessLayer, 'findById'],
[$this, 'trackstoApi']
],
[
'podcast_episode',
[$this->podcastEpisodeBusinessLayer, 'findById'],
[$this, 'podcastEpisodestoApi']
]
];

/** @var array{'track': Track[], 'podcast_episode': PodcastEpisode[]} $apiEntries */
$apiEntries = array_merge([], ...array_map(
function ($handlers) use ($parsedEntries) {
[$type, $lookupFn, $toApiFn] = $handlers;
$typeEntryIds = \array_map(
fn ($entry) => $entry[1],
\array_filter($parsedEntries, fn ($parsedEntry) => $parsedEntry[0] === $type)
);

$entryInstances = $lookupFn($typeEntryIds, $this->user());

return [
$type => $toApiFn(ArrayUtil::createIdLookupTable($entryInstances))
];
},
$typeHandlers
));

$playQueue['entry'] = \array_filter(\array_map(
function ($parsedEntry) use ($apiEntries) {
[$type, $id] = $parsedEntry;
return $apiEntries[$type][$id] ?? false;
},
$parsedEntries
));

return ['playQueue' => $playQueue];
}

/**
* @SubsonicAPI
*/
protected function savePlayQueue() : array {
// TODO: not supported yet
protected function savePlayQueue(array $id, string $c, ?string $current = null, ?int $position = null) : array {
$changedDateTime = new \DateTime();
$playQueue = array_filter([
'entry' => array_filter(
$id,
fn (string $entityId) => in_array(self::parseEntityId($entityId)[0], ['track', 'podcast_episode'])
),
'changedBy' => $c,
'position' => $position,
'current' => $current,
/** @see Util::formatZuluDateTime (if only we could pass a datetime!) */
'changed' => $changedDateTime->format('Y-m-d\TH:i:s.v\Z'),
'username' => $this->user()
], fn ($val) => $val !== null);

$playQueueJson = json_encode($playQueue, \JSON_THROW_ON_ERROR);
$this->configManager->setUserValue($this->userId, $this->appName, 'play_queue', $playQueueJson);

return [];
}

Expand Down Expand Up @@ -1457,6 +1524,13 @@ private function trackToApi(Track $track) : array {
return $this->tracksToApi([$track])[0];
}

/**
* @param PodcastEpisode[] $episodes
*/
private function podcastEpisodestoApi(array $episodes) : array {
return \array_map(fn(PodcastEpisode $p) => $p->toSubsonicApi(), $episodes);
}

/**
* Common logic for getAlbumList and getAlbumList2
* @return Album[]
Expand Down