diff --git a/app/Http/Controllers/API/DataController.php b/app/Http/Controllers/API/DataController.php index 4e7ab962c4..635c0ef38b 100644 --- a/app/Http/Controllers/API/DataController.php +++ b/app/Http/Controllers/API/DataController.php @@ -17,6 +17,8 @@ class DataController extends Controller { + private const RECENTLY_PLAYED_EXCERPT_COUNT = 7; + private $lastfmService; private $youTubeService; private $iTunesService; @@ -60,6 +62,10 @@ public function index(Request $request) '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(), diff --git a/app/Http/Controllers/API/Interaction/RecentlyPlayedController.php b/app/Http/Controllers/API/Interaction/RecentlyPlayedController.php new file mode 100644 index 0000000000..5089fd72db --- /dev/null +++ b/app/Http/Controllers/API/Interaction/RecentlyPlayedController.php @@ -0,0 +1,23 @@ +interactionRepository = $interactionRepository; + } + + public function index(Request $request, ?int $count = null) + { + return response()->json($this->interactionRepository->getRecentlyPlayed($request->user(), $count)); + } +} diff --git a/app/Repositories/InteractionRepository.php b/app/Repositories/InteractionRepository.php index 491c2a40f9..7a96bc14e5 100644 --- a/app/Repositories/InteractionRepository.php +++ b/app/Repositories/InteractionRepository.php @@ -5,6 +5,7 @@ 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 @@ public function getUserFavorites(User $user): Collection ->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(); + } } diff --git a/app/Services/InteractionService.php b/app/Services/InteractionService.php index 83827a9439..687fdf6150 100644 --- a/app/Services/InteractionService.php +++ b/app/Services/InteractionService.php @@ -4,6 +4,7 @@ use App\Events\SongLikeToggled; use App\Models\Interaction; +use App\Models\Song; use App\Models\User; class InteractionService diff --git a/resources/assets b/resources/assets index 2053ab3f60..e5a80b0313 160000 --- a/resources/assets +++ b/resources/assets @@ -1 +1 @@ -Subproject commit 2053ab3f60958838147b1db529aee6d8e3bea5bb +Subproject commit e5a80b03133498f3a9c1887ba4853c9e8bd27ec1 diff --git a/routes/api.php b/routes/api.php index 139672312d..dee19cf1a6 100644 --- a/routes/api.php +++ b/routes/api.php @@ -40,6 +40,9 @@ 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');