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

242 lines
7.2 KiB
PHP
Raw Normal View History

2015-08-31 16:30:02 +02:00
<?php
/**
* Pony.fm - A community for pony fan music.
2018-04-21 06:25:36 +02:00
* 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
use Gate;
2021-02-14 03:34:58 +01:00
use App\Commands\CreateUserCommand;
use App\Models\Album;
use App\Models\Comment;
use App\Models\Favourite;
use App\Http\Controllers\ApiControllerBase;
use App\Models\Image;
use App\Models\Track;
use App\Models\User;
use App\Models\Follower;
use App;
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
use Illuminate\Support\Facades\Request;
use Response;
2016-05-19 23:31:04 +02:00
use ColorThief\ColorThief;
use Helpers;
2015-08-31 16:30:02 +02:00
2015-09-06 19:21:11 +02:00
class ArtistsController extends ApiControllerBase
2015-08-31 16:30:02 +02:00
{
public function getFavourites($slug)
{
$user = User::where('slug', $slug)->whereNull('disabled_at')->first();
2015-08-31 16:30:02 +02:00
if (!$user) {
App::abort(404);
}
$favs = Favourite::where('user_id', $user->id)
->with([
2015-08-31 16:30:02 +02:00
'track.genre',
'track.cover',
'track.user',
2016-05-28 21:29:06 +02:00
'track.user.avatar',
'track.album',
'track.album.cover',
'track.album.user.avatar',
2015-08-31 16:30:02 +02:00
'album.cover',
'album.user',
2016-05-28 21:29:06 +02:00
'album.user.avatar',
2015-08-31 16:30:02 +02:00
'track' => function ($query) {
$query->userDetails();
},
'album' => function ($query) {
$query->userDetails();
}
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
])->get();
2015-08-31 16:30:02 +02:00
$tracks = [];
$albums = [];
foreach ($favs as $fav) {
2021-02-14 03:34:58 +01:00
if ($fav->type == 'App\Models\Track') {
2015-08-31 16:30:02 +02:00
$tracks[] = Track::mapPublicTrackSummary($fav->track);
} else {
2021-02-14 03:34:58 +01:00
if ($fav->type == 'App\Models\Album') {
2015-08-31 16:30:02 +02:00
$albums[] = Album::mapPublicAlbumSummary($fav->album);
}
}
}
return Response::json([
'tracks' => $tracks,
'albums' => $albums
], 200);
}
public function getContent($slug)
{
$user = User::where('slug', $slug)->whereNull('disabled_at')->first();
2015-08-31 16:30:02 +02:00
if (!$user) {
App::abort(404);
}
2016-05-28 21:29:06 +02:00
$query = Track::summary()
->published()
->listed()
->explicitFilter()
->with('genre', 'cover', 'user', 'user.avatar', 'album', 'album.cover')
->userDetails()
->whereUserId($user->id)
->whereNotNull('published_at');
2015-08-31 16:30:02 +02:00
$tracks = [];
$singles = [];
foreach ($query->get() as $track) {
if ($track->album_id != null) {
$tracks[] = Track::mapPublicTrackSummary($track);
} else {
$singles[] = Track::mapPublicTrackSummary($track);
}
}
$query = Album::summary()
->with('user')
->orderBy('created_at', 'desc')
->where('track_count', '>', 0)
->whereUserId($user->id);
$albums = [];
foreach ($query->get() as $album) {
$albums[] = Album::mapPublicAlbumSummary($album);
}
return Response::json(['singles' => $singles, 'albumTracks' => $tracks, 'albums' => $albums], 200);
}
public function getShow($slug)
{
$user = User::where('slug', $slug)
->whereNull('disabled_at')
2015-08-31 16:30:02 +02:00
->userDetails()
->with([
'comments' => function ($query) {
2016-05-28 21:29:06 +02:00
$query->with(['user', 'user.avatar']);
2015-08-31 16:30:02 +02:00
}
])
->first();
if (!$user) {
App::abort(404);
}
$trackQuery = Track::summary()
->published()
->explicitFilter()
->listed()
2016-05-28 21:29:06 +02:00
->with('genre', 'cover', 'user', 'album', 'album.cover')
2015-08-31 16:30:02 +02:00
->userDetails()
->whereUserId($user->id)
->whereNotNull('published_at')
->orderBy('created_at', 'desc')
->take(20);
$latestTracks = [];
foreach ($trackQuery->get() as $track) {
$latestTracks[] = Track::mapPublicTrackSummary($track);
}
$comments = [];
foreach ($user->comments as $comment) {
$comments[] = Comment::mapPublic($comment);
}
$userData = [
'is_following' => false
];
if ($user->users->count()) {
$userRow = $user->users[0];
$userData = [
'is_following' => (bool) $userRow->is_followed
2015-08-31 16:30:02 +02:00
];
}
$palette = ColorThief::getPalette($user->getAvatarUrlLocal(Image::SMALL), 2);
$formatted_palette = array_map("Helpers::rgb2hex", $palette);
2016-05-19 23:31:04 +02:00
2016-06-07 20:31:32 +02:00
$followers = Follower::where('artist_id', $user->id)
->count();
2015-08-31 16:30:02 +02:00
return Response::json([
'artist' => [
'id' => $user->id,
2015-08-31 16:30:02 +02:00
'name' => $user->display_name,
'slug' => $user->slug,
'is_archived' => (bool) $user->is_archived,
2015-08-31 16:30:02 +02:00
'avatars' => [
'small' => $user->getAvatarUrl(Image::SMALL),
'normal' => $user->getAvatarUrl(Image::NORMAL)
],
2016-05-19 23:31:04 +02:00
'avatar_colors' => $formatted_palette,
2015-08-31 16:30:02 +02:00
'created_at' => $user->created_at,
2016-06-07 20:31:32 +02:00
'followers' => $followers,
2015-08-31 16:30:02 +02:00
'following' => [],
'latest_tracks' => $latestTracks,
'comments' => $comments,
'bio' => $user->bio,
'mlpforums_username' => $user->username,
2015-08-31 16:30:02 +02:00
'message_url' => $user->message_url,
'user_data' => $userData,
'permissions' => [
'edit' => Gate::allows('edit', $user)
2016-05-26 00:57:46 +02:00
],
'isAdmin' => $user->hasRole('admin')
2015-08-31 16:30:02 +02:00
]
], 200);
}
public function getIndex()
{
$page = 1;
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
if (Request::has('page')) {
$page = Request::get('page');
2015-08-31 16:30:02 +02:00
}
$query = User::where('track_count', '>', 0);
2015-08-31 16:30:02 +02:00
$count = $query->count();
// The query results are ordered after they're counted
// due to Postgres's behaviour when combining those two operations.
$query->orderBy('display_name', 'asc');
$perPage = 40;
2015-08-31 16:30:02 +02:00
$query->skip(($page - 1) * $perPage)->take($perPage);
$users = [];
foreach ($query->get() as $user) {
$users[] = User::mapPublicUserSummary($user);
2015-08-31 16:30:02 +02:00
}
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
return Response::json(
["artists" => $users, "current_page" => $page, "total_pages" => ceil($count / $perPage)],
200
);
2015-08-31 16:30:02 +02:00
}
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
public function postIndex()
{
$name = Request::json('username');
return $this->execute(new CreateUserCommand($name, $name, null, true));
}
}