Pony.fm/app/Http/Controllers/Api/Web/PlaylistsController.php

233 lines
7.4 KiB
PHP
Raw Normal View History

2015-08-31 16:30:02 +02:00
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2015 Feld0.
*
* 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
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;
use App\Models\Track;
use App\Models\User;
use Illuminate\Database\Eloquent\ModelNotFoundException;
2021-02-14 20:46:41 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Request as RequestF;
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(Request $request)
2015-08-31 16:30:02 +02:00
{
return $this->execute(new CreatePlaylistCommand($request->all()));
2015-08-31 16:30:02 +02:00
}
public function postEdit(Request $request, $id)
2015-08-31 16:30:02 +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(Request $request, $id)
2015-08-31 16:30:02 +02:00
{
return $this->execute(new AddTrackToPlaylistCommand($id, $request->get('track_id')));
2015-08-31 16:30:02 +02:00
}
public function postRemoveTrack(Request $request, $id)
{
return $this->execute(new RemoveTrackFromPlaylistCommand($id, $request->get('track_id')));
}
public function getIndex(Request $request)
2015-08-31 16:30:02 +02:00
{
$page = $request->has('page') ? $request->get('page') : 1;
2015-08-31 16:30:02 +02:00
$query = Playlist::summary()
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
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',
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
2016-09-30 00:26:31 +02:00
'tracks.album.user'
)
2015-08-31 16:30:02 +02:00
->userDetails()
// 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();
$this->applyOrdering($query);
2015-08-31 16:30:02 +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),
2015-08-31 16:30:02 +02:00
], 200);
}
public function getShow(Request $request, $id)
2015-08-31 16:30:02 +02:00
{
$playlist = Playlist::with([
'tracks.user',
'tracks.genre',
'tracks.cover',
'tracks.album',
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
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',
2015-08-31 16:30:02 +02:00
])->userDetails()->find($id);
if (! $playlist || ! $playlist->canView($request->user())) {
abort('404');
2015-08-31 16:30:02 +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-08-31 16:30:02 +02:00
}
public function getCachedPlaylist(Request $request, $id, $format)
{
// Validation
try {
/** @var $playlist Playlist */
$playlist = Playlist::with('tracks.trackFiles')->findOrFail($id);
} catch (ModelNotFoundException $e) {
return $this->notFound('Playlist not found!');
}
if ((! $playlist->is_public && ! $request->user()) || (! $playlist->is_public && ($playlist->user_id !== $request->user()->id))) {
return $this->notFound('Playlist not found!');
}
if (! in_array($format, Track::$CacheableFormats)) {
return $this->notFound('Format not found!');
}
$trackCount = $playlist->countDownloadableTracks($format);
$availableFilesCount = $playlist->countAvailableTrackFiles($format);
if ($trackCount === $availableFilesCount) {
$url = $playlist->getDownloadUrl($format);
} else {
$playlist->encodeCacheableTrackFiles($format);
$url = null;
}
return response()->json(['url' => $url], 200);
}
public function getPinned(Request $request)
2015-08-31 16:30:02 +02:00
{
$query = Playlist
::userDetails()
->with('tracks', 'tracks.cover', 'tracks.user', 'user')
Laravel 5.2 Update (#106) * Adopt PSR-2 coding style The Laravel framework adopts the PSR-2 coding style in version 5.1. Laravel apps *should* adopt this coding style as well. Read the [PSR-2 coding style guide][1] for more details and check out [PHPCS][2] to use as a code formatting tool. [1]: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md [2]: https://github.com/squizlabs/PHP_CodeSniffer * Adopt PHP short array syntax Laravel 5 adopted the short array syntax which became available in PHP 5.4. * Remove SelfHandling from Jobs Jobs are self handling by default in Laravel 5.2. * Add new exceptions to `$dontReport` property * Shift core files * Shift Middleware Laravel 5.2 adjusts the `Guard` object used within middleware. In addition, new `can` and `throttles` middleware were added. * Shift Input to Request facade Laravel 5.2 no longer registers the `Input` facade by default. Laravel now prefers using the `Request` facade or the `$request` object within *Controllers* instead. Review the [HTTP Requests][1] documentation for more details. [1]: https://laravel.com/docs/5.2/requests * Shift configuration Laravel 5.2 introduces the `env` app configuration option and removes the `pretend` mail configuration option. In addition, a few of the default `providers` and `aliases` bindings were removed. * Shift Laravel dependencies * Shift cleanup * Updated composer.lock * Updated Middleware to 5.2 * Config update for Laravel 5.2 * [Laravel 5.2] Updated validation strings * Updated auth config * Updated to use middleware groups * Added laravel 5.2 sessions migration
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', '=', $request->user()->id)
2021-02-14 20:45:58 +01:00
->orderBy('title')
2015-08-31 16:30:02 +02:00
->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);
2015-08-31 16:30:02 +02:00
}
public function getOwned(Request $request, User $user)
2015-08-31 16:30:02 +02:00
{
$query = Playlist::summary()
->with('pins', 'tracks', 'tracks.cover')
->where('user_id', $user->id)
2021-02-14 20:45:58 +01:00
->orderBy('title')
->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),
2015-08-31 16:30:02 +02:00
],
'is_pinned' => $playlist->hasPinFor($request->user()->id),
'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);
2015-08-31 16:30:02 +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)
{
if (RequestF::has('order')) {
$order = RequestF::get('order');
$parts = explode(',', $order);
$query->orderBy($parts[0], $parts[1]);
}
return $query;
}
}