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

217 lines
6.5 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 Peter Deltchev
*
* 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/>.
*/
namespace Poniverse\Ponyfm\Http\Controllers\Api\Web;
2015-08-31 16:30:02 +02:00
use Poniverse\Ponyfm\Album;
use Poniverse\Ponyfm\Models\Comment;
use Poniverse\Ponyfm\Models\Favourite;
use Poniverse\Ponyfm\Http\Controllers\ApiControllerBase;
use Poniverse\Ponyfm\Models\Image;
use Poniverse\Ponyfm\Models\Track;
use Poniverse\Ponyfm\Models\User;
2015-08-31 16:30:02 +02:00
use Cover;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
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::whereSlug($slug)->first();
if (!$user) {
App::abort(404);
}
$favs = Favourite::whereUserId($user->id)->with([
'track.genre',
'track.cover',
'track.user',
'album.cover',
'album.user',
'track' => function ($query) {
$query->userDetails();
},
'album' => function ($query) {
$query->userDetails();
}
])->get();
$tracks = [];
$albums = [];
foreach ($favs as $fav) {
if ($fav->type == 'Poniverse\Ponyfm\Models\Track') {
2015-08-31 16:30:02 +02:00
$tracks[] = Track::mapPublicTrackSummary($fav->track);
} else {
if ($fav->type == 'Poniverse\Ponyfm\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::whereSlug($slug)->first();
if (!$user) {
App::abort(404);
}
$query = Track::summary()->published()->listed()->explicitFilter()->with('genre', 'cover',
'user')->userDetails()->whereUserId($user->id)->whereNotNull('published_at');
$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::whereSlug($slug)
->whereNull('disabled_at')
2015-08-31 16:30:02 +02:00
->userDetails()
->with([
'comments' => function ($query) {
$query->with('user');
}
])
->first();
if (!$user) {
App::abort(404);
}
$trackQuery = Track::summary()
->published()
->explicitFilter()
->listed()
->with('genre', 'cover', 'user')
->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
];
}
return Response::json([
'artist' => [
'id' => (int)$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)
],
'created_at' => $user->created_at,
'followers' => [],
'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
]
], 200);
}
public function getIndex()
{
$page = 1;
if (Input::has('page')) {
$page = Input::get('page');
}
$query = User::orderBy('display_name', 'asc')
2015-08-31 16:30:02 +02:00
->where('track_count', '>', 0);
$count = $query->count();
$perPage = 40;
$query->skip(($page - 1) * $perPage)->take($perPage);
$users = [];
foreach ($query->get() as $user) {
$users[] = [
'id' => $user->id,
'name' => $user->display_name,
'slug' => $user->slug,
'url' => $user->url,
'is_archived' => $user->is_archived,
'avatars' => [
'small' => $user->getAvatarUrl(Image::SMALL),
'normal' => $user->getAvatarUrl(Image::NORMAL)
],
'created_at' => $user->created_at
];
}
return Response::json(["artists" => $users, "current_page" => $page, "total_pages" => ceil($count / $perPage)],
200);
}
}