Skip to content

Commit

Permalink
Add filters "Recently added" and "Not recently added" for the smart p…
Browse files Browse the repository at this point in the history
…laylist

refs #1098
  • Loading branch information
paulijar committed Jan 14, 2024
1 parent 2e8b367 commit b4bb53b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
+ Properties `openSubsonic`, `type`, and `serverVersion` to all responses
+ Allow getting the whole library with an empty `query` argument in `search3` method
- MusicBrainz link from Last.fm to the artist/album/track details pane, when available
- Filters "Recently added" and "Not recently added" for the smart playlist
[#1098](https://github.com/owncloud/music/issues/1098)

### Changed
- Ampache API:
Expand Down
24 changes: 14 additions & 10 deletions lib/BusinessLayer/PlaylistBusinessLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* later. See the COPYING file.
*
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright Pauli Järvinen 2016 - 2023
* @copyright Pauli Järvinen 2016 - 2024
*/

namespace OCA\Music\BusinessLayer;
Expand Down Expand Up @@ -186,15 +186,15 @@ public function getDuration(int $playlistId, string $userId) : int {
/**
* Generate and return a playlist matching the given criteria. The playlist is not persisted.
*
* @param string|null $playRate One of: 'recent', 'not-recent', 'often', 'rarely'
* @param string|null $history One of: 'recently-played', 'not-recently-played', 'often-played', 'rarely-played', 'recently-added', 'not-recently-added'
* @param int[] $genres Array of genre IDs
* @param int[] $artists Array of artist IDs
* @param int|null $fromYear Earliest release year to include
* @param int|null $toYear Latest release year to include
* @param int $size Size of the playlist to generate, provided that there are enough matching tracks
* @param string $userId the name of the user
*/
public function generate(?string $playRate, array $genres, array $artists, ?int $fromYear, ?int $toYear, int $size, string $userId) : Playlist {
public function generate(?string $history, array $genres, array $artists, ?int $fromYear, ?int $toYear, int $size, string $userId) : Playlist {
$now = new \DateTime();
$nowStr = $now->format(PlaylistMapper::SQL_DATE_FORMAT);

Expand All @@ -204,7 +204,7 @@ public function generate(?string $playRate, array $genres, array $artists, ?int
$playlist->setName('Generated ' . $nowStr);
$playlist->setUserId($userId);

list('sortBy' => $sortBy, 'invert' => $invertSort) = self::sortRulesForPlayRate($playRate);
list('sortBy' => $sortBy, 'invert' => $invertSort) = self::sortRulesForHistory($history);
$limit = ($sortBy === SortBy::None) ? null : $size * 4;

$tracks = $this->trackMapper->findAllByCriteria($genres, $artists, $fromYear, $toYear, $sortBy, $invertSort, $userId, $limit);
Expand All @@ -224,16 +224,20 @@ public function generate(?string $playRate, array $genres, array $artists, ?int
return $playlist;
}

private static function sortRulesForPlayRate(?string $playRate) : array {
switch ($playRate) {
case 'recently':
private static function sortRulesForHistory(?string $history) : array {
switch ($history) {
case 'recently-played':
return ['sortBy' => SortBy::LastPlayed, 'invert' => true];
case 'not-recently':
case 'not-recently-played':
return ['sortBy' => SortBy::LastPlayed, 'invert' => false];
case 'often':
case 'often-played':
return ['sortBy' => SortBy::PlayCount, 'invert' => true];
case 'rarely':
case 'rarely-played':
return ['sortBy' => SortBy::PlayCount, 'invert' => false];
case 'recently-added':
return ['sortBy' => SortBy::Newest, 'invert' => false];
case 'not-recently-added':
return ['sortBy' => SortBy::Newest, 'invert' => true];
default:
return ['sortBy' => SortBy::None, 'invert' => false];
}
Expand Down
12 changes: 6 additions & 6 deletions lib/Controller/PlaylistApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author Morris Jobke <hey@morrisjobke.de>
* @author Pauli Järvinen <pauli.jarvinen@gmail.com>
* @copyright Morris Jobke 2013, 2014
* @copyright Pauli Järvinen 2017 - 2023
* @copyright Pauli Järvinen 2017 - 2024
*/

namespace OCA\Music\Controller;
Expand Down Expand Up @@ -163,16 +163,16 @@ private function toFullTree($playlist) {
* @NoAdminRequired
* @NoCSRFRequired
*/
public function generate(?bool $useLatestParams, ?string $playRate, ?string $genres, ?string $artists, ?int $fromYear, ?int $toYear, int $size=100) {
public function generate(?bool $useLatestParams, ?string $history, ?string $genres, ?string $artists, ?int $fromYear, ?int $toYear, int $size=100) {
if ($useLatestParams) {
$playRate = $this->configManager->getUserValue($this->userId, $this->appName, 'smartlist_play_rate') ?: null;
$history = $this->configManager->getUserValue($this->userId, $this->appName, 'smartlist_history') ?: null;
$genres = $this->configManager->getUserValue($this->userId, $this->appName, 'smartlist_genres') ?: null;
$artists = $this->configManager->getUserValue($this->userId, $this->appName, 'smartlist_artists') ?: null;
$fromYear = (int)$this->configManager->getUserValue($this->userId, $this->appName, 'smartlist_from_year') ?: null;
$toYear = (int)$this->configManager->getUserValue($this->userId, $this->appName, 'smartlist_to_year') ?: null;
$size = (int)$this->configManager->getUserValue($this->userId, $this->appName, 'smartlist_size', 100);
} else {
$this->configManager->setUserValue($this->userId, $this->appName, 'smartlist_play_rate', $playRate ?? '');
$this->configManager->setUserValue($this->userId, $this->appName, 'smartlist_history', $history ?? '');
$this->configManager->setUserValue($this->userId, $this->appName, 'smartlist_genres', $genres ?? '');
$this->configManager->setUserValue($this->userId, $this->appName, 'smartlist_artists', $artists ?? '');
$this->configManager->setUserValue($this->userId, $this->appName, 'smartlist_from_year', (string)$fromYear);
Expand All @@ -185,11 +185,11 @@ public function generate(?bool $useLatestParams, ?string $playRate, ?string $gen
$artists = $this->artistBusinessLayer->findAllIds($this->userId, self::toIntArray($artists));

$playlist = $this->playlistBusinessLayer->generate(
$playRate, $genres, $artists, $fromYear, $toYear, $size, $this->userId);
$history, $genres, $artists, $fromYear, $toYear, $size, $this->userId);
$result = $playlist->toAPI();

$result['params'] = [
'playRate' => $playRate ?: null,
'history' => $history ?: null,
'genres' => \implode(',', $genres) ?: null,
'artists' => \implode(',', $artists) ?: null,
'fromYear' => $fromYear ?: null,
Expand Down
14 changes: 8 additions & 6 deletions templates/partials/sidebar/smartlistfilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
</div>
<div title="{{ 'Note that this selection makes any difference only when the library has more than requested number of matches' | translate }}">
<label for="filter-play-rate" translate>Play history</label>
<select id="filter-play-rate" ng-model="smartListParams.playRate">
<label for="filter-history" translate>History</label>
<select id="filter-history" ng-model="smartListParams.history">
<option value=""></option>
<option value="recently" translate>Recently played</option>
<option value="not-recently" translate>Not recently played</option>
<option value="often" translate>Often played</option>
<option value="rarely" translate>Rarely played</option>
<option value="recently-played" translate>Recently played</option>
<option value="not-recently-played" translate>Not recently played</option>
<option value="often-played" translate>Often played</option>
<option value="rarely-played" translate>Rarely played</option>
<option value="recently-added" translate>Recently added</option>
<option value="not-recently-added" translate>Not recently added</option>
</select>
</div>
Expand Down

0 comments on commit b4bb53b

Please sign in to comment.