2015-08-31 16:30:02 +02:00
|
|
|
<?php
|
|
|
|
|
2015-10-25 06:17:45 +01:00
|
|
|
/**
|
|
|
|
* Pony.fm - A community for pony fan music.
|
2018-04-21 06:25:36 +02:00
|
|
|
* Copyright (C) 2015 Feld0
|
2015-10-25 06:17:45 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-02-14 03:34:58 +01:00
|
|
|
namespace App\Http\Controllers\Api\Web;
|
2015-08-31 16:30:02 +02:00
|
|
|
|
2015-11-08 18:46:35 +01:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2021-02-14 03:34:58 +01:00
|
|
|
use App\Commands\AddTrackToPlaylistCommand;
|
|
|
|
use App\Commands\CreatePlaylistCommand;
|
|
|
|
use App\Commands\DeletePlaylistCommand;
|
|
|
|
use App\Commands\EditPlaylistCommand;
|
|
|
|
use App\Commands\RemoveTrackFromPlaylistCommand;
|
|
|
|
use App\Http\Controllers\ApiControllerBase;
|
|
|
|
use App\Models\Image;
|
|
|
|
use App\Models\Playlist;
|
|
|
|
use App\Models\ResourceLogItem;
|
2016-01-01 03:27:21 +01:00
|
|
|
use Auth;
|
2016-09-30 00:26:31 +02:00
|
|
|
use Illuminate\Support\Facades\Request;
|
2021-02-14 03:34:58 +01:00
|
|
|
use App\Models\User;
|
2016-01-01 03:27:21 +01:00
|
|
|
use Response;
|
2021-02-14 03:34:58 +01:00
|
|
|
use App\Models\Track;
|
2015-08-31 16:30:02 +02:00
|
|
|
|
2015-09-06 19:21:11 +02:00
|
|
|
class PlaylistsController extends ApiControllerBase
|
2015-08-31 16:30:02 +02:00
|
|
|
{
|
|
|
|
public function postCreate()
|
|
|
|
{
|
2016-09-30 00:26:31 +02:00
|
|
|
return $this->execute(new CreatePlaylistCommand(Request::all()));
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function postEdit($id)
|
|
|
|
{
|
2016-09-30 00:26:31 +02:00
|
|
|
return $this->execute(new EditPlaylistCommand($id, Request::all()));
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function postDelete($id)
|
|
|
|
{
|
2016-06-06 08:15:56 +02:00
|
|
|
return $this->execute(new DeletePlaylistCommand($id));
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function postAddTrack($id)
|
|
|
|
{
|
2016-09-30 00:26:31 +02:00
|
|
|
return $this->execute(new AddTrackToPlaylistCommand($id, Request::get('track_id')));
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
2015-12-28 15:31:28 +01:00
|
|
|
public function postRemoveTrack($id)
|
|
|
|
{
|
2016-09-30 00:26:31 +02:00
|
|
|
return $this->execute(new RemoveTrackFromPlaylistCommand($id, Request::get('track_id')));
|
2015-12-28 15:31:28 +01:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:30:02 +02:00
|
|
|
public function getIndex()
|
|
|
|
{
|
2016-09-30 00:26:31 +02:00
|
|
|
$page = Request::has('page') ? Request::get('page') : 1;
|
2015-08-31 16:30:02 +02:00
|
|
|
|
|
|
|
$query = Playlist::summary()
|
2016-09-30 00:26:31 +02:00
|
|
|
->with(
|
|
|
|
'user',
|
2016-05-28 21:29:06 +02:00
|
|
|
'user.avatar',
|
|
|
|
'tracks',
|
|
|
|
'tracks.cover',
|
|
|
|
'tracks.user',
|
|
|
|
'tracks.user.avatar',
|
|
|
|
'tracks.album',
|
2016-09-30 00:26:31 +02:00
|
|
|
'tracks.album.user'
|
|
|
|
)
|
2015-08-31 16:30:02 +02:00
|
|
|
->userDetails()
|
2016-06-05 21:27:04 +02:00
|
|
|
// A playlist with only one track is not much of a list.
|
|
|
|
->where('track_count', '>', 1)
|
2015-08-31 16:30:02 +02:00
|
|
|
->whereIsPublic(true);
|
|
|
|
|
|
|
|
$count = $query->count();
|
2016-06-27 04:20:10 +02:00
|
|
|
$this->applyOrdering($query);
|
2015-08-31 16:30:02 +02:00
|
|
|
|
2016-06-27 04:20:10 +02:00
|
|
|
$perPage = 40;
|
2015-08-31 16:30:02 +02:00
|
|
|
$query->skip(($page - 1) * $perPage)->take($perPage);
|
|
|
|
$playlists = [];
|
|
|
|
|
|
|
|
foreach ($query->get() as $playlist) {
|
|
|
|
$playlists[] = Playlist::mapPublicPlaylistSummary($playlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response::json([
|
|
|
|
"playlists" => $playlists,
|
|
|
|
"current_page" => $page,
|
|
|
|
"total_pages" => ceil($count / $perPage)
|
|
|
|
], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getShow($id)
|
|
|
|
{
|
|
|
|
$playlist = Playlist::with([
|
|
|
|
'tracks.user',
|
|
|
|
'tracks.genre',
|
|
|
|
'tracks.cover',
|
|
|
|
'tracks.album',
|
2016-09-30 00:26:31 +02:00
|
|
|
'tracks' => function ($query) {
|
2015-08-31 16:30:02 +02:00
|
|
|
$query->userDetails();
|
|
|
|
},
|
2016-05-28 21:29:06 +02:00
|
|
|
'tracks.trackFiles',
|
2015-08-31 16:30:02 +02:00
|
|
|
'comments',
|
|
|
|
'comments.user'
|
|
|
|
])->userDetails()->find($id);
|
|
|
|
if (!$playlist || !$playlist->canView(Auth::user())) {
|
|
|
|
App::abort('404');
|
|
|
|
}
|
|
|
|
|
2016-09-30 00:26:31 +02:00
|
|
|
if (Request::get('log')) {
|
2015-08-31 16:30:02 +02:00
|
|
|
ResourceLogItem::logItem('playlist', $id, ResourceLogItem::VIEW);
|
|
|
|
$playlist->view_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response::json(Playlist::mapPublicPlaylistShow($playlist), 200);
|
|
|
|
}
|
|
|
|
|
2015-11-08 18:46:35 +01:00
|
|
|
public function getCachedPlaylist($id, $format)
|
|
|
|
{
|
|
|
|
// Validation
|
|
|
|
try {
|
2015-11-10 06:49:12 +01:00
|
|
|
/** @var $playlist Playlist */
|
2015-11-08 18:46:35 +01:00
|
|
|
$playlist = Playlist::with('tracks.trackFiles')->findOrFail($id);
|
|
|
|
} catch (ModelNotFoundException $e) {
|
|
|
|
return $this->notFound('Playlist not found!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!$playlist->is_public && !Auth::check()) || (!$playlist->is_public && ($playlist->user_id !== Auth::user()->id))) {
|
|
|
|
return $this->notFound('Playlist not found!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!in_array($format, Track::$CacheableFormats)) {
|
|
|
|
return $this->notFound('Format not found!');
|
|
|
|
}
|
|
|
|
|
2015-11-10 06:49:12 +01:00
|
|
|
$trackCount = $playlist->countDownloadableTracks($format);
|
|
|
|
$availableFilesCount = $playlist->countAvailableTrackFiles($format);
|
2015-11-08 18:46:35 +01:00
|
|
|
|
2015-11-10 06:49:12 +01:00
|
|
|
if ($trackCount === $availableFilesCount) {
|
2015-11-08 18:46:35 +01:00
|
|
|
$url = $playlist->getDownloadUrl($format);
|
|
|
|
} else {
|
|
|
|
$playlist->encodeCacheableTrackFiles($format);
|
|
|
|
$url = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response::json(['url' => $url], 200);
|
|
|
|
}
|
|
|
|
|
2015-08-31 16:30:02 +02:00
|
|
|
public function getPinned()
|
|
|
|
{
|
|
|
|
$query = Playlist
|
|
|
|
::userDetails()
|
|
|
|
->with('tracks', 'tracks.cover', 'tracks.user', 'user')
|
2016-09-30 00:26:31 +02:00
|
|
|
->join('pinned_playlists', function ($join) {
|
2015-08-31 16:30:02 +02:00
|
|
|
$join->on('playlist_id', '=', 'playlists.id');
|
|
|
|
})
|
|
|
|
->where('pinned_playlists.user_id', '=', Auth::user()->id)
|
|
|
|
->orderBy('title', 'asc')
|
|
|
|
->select('playlists.*')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
$playlists = [];
|
|
|
|
foreach ($query as $playlist) {
|
|
|
|
$mapped = Playlist::mapPublicPlaylistSummary($playlist);
|
|
|
|
$mapped['description'] = $playlist->description;
|
|
|
|
$mapped['is_pinned'] = true;
|
|
|
|
$playlists[] = $mapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response::json($playlists, 200);
|
|
|
|
}
|
|
|
|
|
2016-06-14 10:27:56 +02:00
|
|
|
public function getOwned(User $user)
|
2015-08-31 16:30:02 +02:00
|
|
|
{
|
2016-01-23 13:20:18 +01:00
|
|
|
$query = Playlist::summary()
|
|
|
|
->with('pins', 'tracks', 'tracks.cover')
|
2016-06-14 10:27:56 +02:00
|
|
|
->where('user_id', $user->id)
|
2016-01-23 13:20:18 +01:00
|
|
|
->orderBy('title', 'asc')
|
|
|
|
->get();
|
|
|
|
|
2015-08-31 16:30:02 +02:00
|
|
|
$playlists = [];
|
|
|
|
foreach ($query as $playlist) {
|
|
|
|
$playlists[] = [
|
|
|
|
'id' => $playlist->id,
|
|
|
|
'title' => $playlist->title,
|
|
|
|
'slug' => $playlist->slug,
|
|
|
|
'created_at' => $playlist->created_at,
|
|
|
|
'description' => $playlist->description,
|
|
|
|
'url' => $playlist->url,
|
|
|
|
'covers' => [
|
|
|
|
'small' => $playlist->getCoverUrl(Image::SMALL),
|
|
|
|
'normal' => $playlist->getCoverUrl(Image::NORMAL)
|
|
|
|
],
|
|
|
|
'is_pinned' => $playlist->hasPinFor(Auth::user()->id),
|
2016-01-23 13:20:18 +01:00
|
|
|
'is_public' => $playlist->is_public == 1,
|
|
|
|
'track_ids' => $playlist->tracks->pluck('id')
|
2015-08-31 16:30:02 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response::json($playlists, 200);
|
|
|
|
}
|
2016-06-01 19:40:49 +02:00
|
|
|
|
2016-06-27 04:20:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function should not deal with anything other than applying order,
|
|
|
|
* which is done after the query's total possible results are counted due
|
|
|
|
* to Postgres not allowing "ORDER BY" to be combined with "COUNT()".
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function applyOrdering($query)
|
2016-06-01 19:40:49 +02:00
|
|
|
{
|
2016-09-30 00:26:31 +02:00
|
|
|
if (Request::has('order')) {
|
|
|
|
$order = \Request::get('order');
|
2016-06-01 19:40:49 +02:00
|
|
|
$parts = explode(',', $order);
|
|
|
|
$query->orderBy($parts[0], $parts[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2015-09-29 06:10:35 +02:00
|
|
|
}
|