koel/app/Http/Controllers/API/DataController.php

95 lines
3.7 KiB
PHP
Raw Normal View History

2015-12-13 04:42:28 +00:00
<?php
namespace App\Http\Controllers\API;
2015-12-27 14:06:10 +00:00
use App\Application;
2018-08-29 06:15:11 +00:00
use App\Repositories\InteractionRepository;
use App\Repositories\PlaylistRepository;
use App\Repositories\SettingRepository;
use App\Repositories\UserRepository;
use App\Services\ApplicationInformationService;
2018-08-19 14:40:25 +00:00
use App\Services\iTunesService;
use App\Services\LastfmService;
2018-08-19 14:56:56 +00:00
use App\Services\MediaCacheService;
2018-08-19 14:40:25 +00:00
use App\Services\YouTubeService;
2017-06-04 01:30:45 +00:00
use Illuminate\Http\JsonResponse;
2017-05-02 03:23:10 +00:00
use Illuminate\Http\Request;
2015-12-13 04:42:28 +00:00
/**
* @group 2. Application data
*/
2015-12-13 04:42:28 +00:00
class DataController extends Controller
{
private const RECENTLY_PLAYED_EXCERPT_COUNT = 7;
2018-08-19 14:40:25 +00:00
private $lastfmService;
private $youTubeService;
private $iTunesService;
2018-08-19 14:56:56 +00:00
private $mediaCacheService;
2018-08-29 06:15:11 +00:00
private $settingRepository;
private $playlistRepository;
private $interactionRepository;
private $userRepository;
private $applicationInformationService;
2018-08-19 14:40:25 +00:00
public function __construct(
LastfmService $lastfmService,
YouTubeService $youTubeService,
2018-08-19 14:56:56 +00:00
iTunesService $iTunesService,
2018-08-29 06:15:11 +00:00
MediaCacheService $mediaCacheService,
SettingRepository $settingRepository,
PlaylistRepository $playlistRepository,
InteractionRepository $interactionRepository,
UserRepository $userRepository,
ApplicationInformationService $applicationInformationService
2018-08-19 15:26:52 +00:00
) {
2018-08-19 14:40:25 +00:00
$this->lastfmService = $lastfmService;
$this->youTubeService = $youTubeService;
$this->iTunesService = $iTunesService;
2018-08-19 14:56:56 +00:00
$this->mediaCacheService = $mediaCacheService;
2018-08-29 06:15:11 +00:00
$this->settingRepository = $settingRepository;
$this->playlistRepository = $playlistRepository;
$this->interactionRepository = $interactionRepository;
$this->userRepository = $userRepository;
$this->applicationInformationService = $applicationInformationService;
2018-08-19 14:40:25 +00:00
}
2015-12-13 04:42:28 +00:00
/**
2020-04-07 21:41:30 +00:00
* Get application data.
*
* The big fat call to retrieve a set of application data catered for the current user
* (songs, albums, artists, playlists, interactions, and if the user is an admin, settings as well).
* Naturally, this call should be made right after the user has been logged in, when you need to populate
* the application's interface with useful information.
*
* @responseFile responses/data.index.json
2015-12-13 04:42:28 +00:00
*
2017-06-04 01:30:45 +00:00
* @return JsonResponse
2015-12-13 04:42:28 +00:00
*/
2017-05-02 03:23:10 +00:00
public function index(Request $request)
2015-12-13 04:42:28 +00:00
{
2018-08-19 14:56:56 +00:00
return response()->json($this->mediaCacheService->get() + [
2018-08-29 06:15:11 +00:00
'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
),
2018-08-29 06:15:11 +00:00
'users' => $request->user()->is_admin ? $this->userRepository->getAll() : [],
2017-05-02 03:23:10 +00:00
'currentUser' => $request->user(),
2018-08-19 14:40:25 +00:00
'useLastfm' => $this->lastfmService->used(),
'useYouTube' => $this->youTubeService->enabled(),
'useiTunes' => $this->iTunesService->used(),
2016-08-21 15:19:03 +00:00
'allowDownload' => config('koel.download.allow'),
'supportsTranscoding' => config('koel.streaming.ffmpeg_path')
&& is_executable(config('koel.streaming.ffmpeg_path')),
'cdnUrl' => app()->staticUrl(),
2016-12-09 08:23:40 +00:00
'currentVersion' => Application::KOEL_VERSION,
'latestVersion' => $request->user()->is_admin
? $this->applicationInformationService->getLatestVersionNumber()
: Application::KOEL_VERSION,
2015-12-13 04:42:28 +00:00
]);
}
}