mirror of
https://github.com/koel/koel
synced 2024-11-27 22:40:26 +00:00
Add "Recently Played" playlist
This commit is contained in:
parent
ee6ac687c4
commit
239171f2e6
6 changed files with 56 additions and 1 deletions
|
@ -17,6 +17,8 @@ use Illuminate\Http\Request;
|
|||
|
||||
class DataController extends Controller
|
||||
{
|
||||
private const RECENTLY_PLAYED_EXCERPT_COUNT = 7;
|
||||
|
||||
private $lastfmService;
|
||||
private $youTubeService;
|
||||
private $iTunesService;
|
||||
|
@ -60,6 +62,10 @@ class DataController extends Controller
|
|||
'settings' => $request->user()->is_admin ? $this->settingRepository->getAllAsKeyValueArray() : [],
|
||||
'playlists' => $this->playlistRepository->getAllByCurrentUser(),
|
||||
'interactions' => $this->interactionRepository->getAllByCurrentUser(),
|
||||
'recentlyPlayed' => $this->interactionRepository->getRecentlyPlayed(
|
||||
$request->user(),
|
||||
self::RECENTLY_PLAYED_EXCERPT_COUNT
|
||||
),
|
||||
'users' => $request->user()->is_admin ? $this->userRepository->getAll() : [],
|
||||
'currentUser' => $request->user(),
|
||||
'useLastfm' => $this->lastfmService->used(),
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\API\Interaction;
|
||||
|
||||
use App\Repositories\InteractionRepository;
|
||||
use App\Services\InteractionService;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class RecentlyPlayedController extends Controller
|
||||
{
|
||||
private $interactionRepository;
|
||||
|
||||
public function __construct(InteractionService $interactionService, InteractionRepository $interactionRepository)
|
||||
{
|
||||
parent::__construct($interactionService);
|
||||
$this->interactionRepository = $interactionRepository;
|
||||
}
|
||||
|
||||
public function index(Request $request, ?int $count = null)
|
||||
{
|
||||
return response()->json($this->interactionRepository->getRecentlyPlayed($request->user(), $count));
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ namespace App\Repositories;
|
|||
use App\Models\Interaction;
|
||||
use App\Models\User;
|
||||
use App\Repositories\Traits\ByCurrentUser;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class InteractionRepository extends AbstractRepository
|
||||
|
@ -29,4 +30,24 @@ class InteractionRepository extends AbstractRepository
|
|||
->get()
|
||||
->pluck('song');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Interaction[]
|
||||
*/
|
||||
public function getRecentlyPlayed(User $user, ?int $count = null): array
|
||||
{
|
||||
/** @var Builder $query */
|
||||
$query = $this->model
|
||||
->where('user_id', $user->id)
|
||||
->orderBy('updated_at', 'DESC');
|
||||
|
||||
if ($count) {
|
||||
$query = $query->take($count);
|
||||
}
|
||||
|
||||
return $query
|
||||
->get()
|
||||
->pluck('song_id')
|
||||
->all();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,9 @@ namespace App\Services;
|
|||
|
||||
use App\Events\SongLikeToggled;
|
||||
use App\Models\Interaction;
|
||||
use App\Models\Song;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
|
||||
class InteractionService
|
||||
{
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit 2053ab3f60958838147b1db529aee6d8e3bea5bb
|
||||
Subproject commit e5a80b03133498f3a9c1887ba4853c9e8bd27ec1
|
|
@ -40,6 +40,9 @@ Route::group(['namespace' => 'API'], function () {
|
|||
Route::post('interaction/like', 'Interaction\LikeController@store');
|
||||
Route::post('interaction/batch/like', 'Interaction\BatchLikeController@store');
|
||||
Route::post('interaction/batch/unlike', 'Interaction\BatchLikeController@destroy');
|
||||
Route::get('interaction/recently-played/{count?}', 'Interaction\RecentlyPlayedController@index')->where([
|
||||
'count' => '\d+',
|
||||
]);
|
||||
|
||||
// Playlist routes
|
||||
Route::resource('playlist', 'PlaylistController');
|
||||
|
|
Loading…
Reference in a new issue