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.
|
2021-02-14 03:39:15 +01: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
|
|
|
|
2021-02-14 20:45:51 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2021-02-14 03:34:58 +01:00
|
|
|
use App\Commands\CreateUserCommand;
|
2021-02-14 03:39:15 +01:00
|
|
|
use App\Http\Controllers\ApiControllerBase;
|
2021-02-14 03:34:58 +01:00
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Comment;
|
|
|
|
use App\Models\Favourite;
|
2021-02-14 03:39:15 +01:00
|
|
|
use App\Models\Follower;
|
2021-02-14 03:34:58 +01:00
|
|
|
use App\Models\Image;
|
|
|
|
use App\Models\Track;
|
|
|
|
use App\Models\User;
|
2016-05-19 23:31:04 +02:00
|
|
|
use ColorThief\ColorThief;
|
2021-02-14 20:45:51 +01:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2016-05-19 23:31:04 +02:00
|
|
|
use Helpers;
|
2021-02-14 03:39:15 +01:00
|
|
|
use Illuminate\Support\Facades\Request;
|
2021-02-14 20:45:51 +01:00
|
|
|
use Illuminate\Support\Facades\Response;
|
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)
|
|
|
|
{
|
2016-01-25 07:05:08 +01:00
|
|
|
$user = User::where('slug', $slug)->whereNull('disabled_at')->first();
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! $user) {
|
2021-02-14 20:38:28 +01:00
|
|
|
abort(404);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
2016-01-25 07:05:08 +01:00
|
|
|
$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();
|
2021-02-14 03:39:15 +01:00
|
|
|
},
|
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:39:58 +01:00
|
|
|
if ($fav->type == \App\Models\Track::class) {
|
2015-08-31 16:30:02 +02:00
|
|
|
$tracks[] = Track::mapPublicTrackSummary($fav->track);
|
|
|
|
} else {
|
2021-02-14 03:39:58 +01:00
|
|
|
if ($fav->type == \App\Models\Album::class) {
|
2015-08-31 16:30:02 +02:00
|
|
|
$albums[] = Album::mapPublicAlbumSummary($fav->album);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response::json([
|
|
|
|
'tracks' => $tracks,
|
2021-02-14 03:39:15 +01:00
|
|
|
'albums' => $albums,
|
2015-08-31 16:30:02 +02:00
|
|
|
], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getContent($slug)
|
|
|
|
{
|
2016-01-25 07:05:08 +01:00
|
|
|
$user = User::where('slug', $slug)->whereNull('disabled_at')->first();
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! $user) {
|
2021-02-14 20:38:28 +01:00
|
|
|
abort(404);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2016-01-25 07:05:08 +01:00
|
|
|
$user = User::where('slug', $slug)
|
2015-12-29 17:48:35 +01:00
|
|
|
->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']);
|
2021-02-14 03:39:15 +01:00
|
|
|
},
|
2015-08-31 16:30:02 +02:00
|
|
|
])
|
|
|
|
->first();
|
2021-02-14 03:39:15 +01:00
|
|
|
if (! $user) {
|
2021-02-14 20:38:28 +01:00
|
|
|
abort(404);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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 = [
|
2021-02-14 03:39:15 +01:00
|
|
|
'is_following' => false,
|
2015-08-31 16:30:02 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($user->users->count()) {
|
|
|
|
$userRow = $user->users[0];
|
|
|
|
$userData = [
|
2021-02-14 03:39:15 +01:00
|
|
|
'is_following' => (bool) $userRow->is_followed,
|
2015-08-31 16:30:02 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-11-12 22:38:03 +01:00
|
|
|
$palette = ColorThief::getPalette($user->getAvatarUrlLocal(Image::SMALL), 2);
|
2021-02-14 03:39:15 +01:00
|
|
|
$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' => [
|
2016-03-18 12:22:54 +01:00
|
|
|
'id' => $user->id,
|
2015-08-31 16:30:02 +02:00
|
|
|
'name' => $user->display_name,
|
|
|
|
'slug' => $user->slug,
|
2016-06-06 08:29:37 +02:00
|
|
|
'is_archived' => (bool) $user->is_archived,
|
2015-08-31 16:30:02 +02:00
|
|
|
'avatars' => [
|
|
|
|
'small' => $user->getAvatarUrl(Image::SMALL),
|
2021-02-14 03:39:15 +01:00
|
|
|
'normal' => $user->getAvatarUrl(Image::NORMAL),
|
2015-08-31 16:30:02 +02:00
|
|
|
],
|
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,
|
2015-11-21 03:25:11 +01:00
|
|
|
'mlpforums_username' => $user->username,
|
2015-08-31 16:30:02 +02:00
|
|
|
'message_url' => $user->message_url,
|
2016-03-18 12:22:54 +01:00
|
|
|
'user_data' => $userData,
|
|
|
|
'permissions' => [
|
2021-02-14 03:39:15 +01:00
|
|
|
'edit' => Gate::allows('edit', $user),
|
2016-05-26 00:57:46 +02:00
|
|
|
],
|
2021-02-14 03:39:15 +01:00
|
|
|
'isAdmin' => $user->hasRole('admin'),
|
|
|
|
],
|
2015-08-31 16:30:02 +02:00
|
|
|
], 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIndex()
|
|
|
|
{
|
|
|
|
$page = 1;
|
2016-09-30 00:26:31 +02:00
|
|
|
if (Request::has('page')) {
|
|
|
|
$page = Request::get('page');
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 04:20:10 +02:00
|
|
|
$query = User::where('track_count', '>', 0);
|
2015-08-31 16:30:02 +02:00
|
|
|
$count = $query->count();
|
|
|
|
|
2016-06-27 04:20:10 +02:00
|
|
|
// 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) {
|
2016-01-17 08:35:43 +01:00
|
|
|
$users[] = User::mapPublicUserSummary($user);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 00:26:31 +02:00
|
|
|
return Response::json(
|
2021-02-14 03:39:15 +01:00
|
|
|
['artists' => $users, 'current_page' => $page, 'total_pages' => ceil($count / $perPage)],
|
2016-09-30 00:26:31 +02:00
|
|
|
200
|
|
|
|
);
|
2015-08-31 16:30:02 +02:00
|
|
|
}
|
2016-06-12 07:55:27 +02:00
|
|
|
|
2016-09-30 00:26:31 +02:00
|
|
|
public function postIndex()
|
|
|
|
{
|
|
|
|
$name = Request::json('username');
|
2021-02-14 03:39:15 +01:00
|
|
|
|
2016-06-12 07:55:27 +02:00
|
|
|
return $this->execute(new CreateUserCommand($name, $name, null, true));
|
|
|
|
}
|
2015-09-20 10:47:24 +02:00
|
|
|
}
|