the browser-facing portion of osu!
at master 4.1 kB view raw
1<?php 2 3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4// See the LICENCE file in the repository root for full licence text. 5 6namespace App\Http\Controllers; 7 8use App\Models\Artist; 9use App\Transformers\ArtistAlbumTransformer; 10use App\Transformers\ArtistTrackTransformer; 11use App\Transformers\ArtistTransformer; 12use Auth; 13 14class ArtistsController extends Controller 15{ 16 public function index() 17 { 18 $artists = Artist::with('label')->withMax('tracks', 'created_at')->withCount('tracks')->orderBy('name', 'asc'); 19 $user = Auth::user(); 20 21 if ($user === null || !$user->isAdmin()) { 22 $artists->where('visible', true); 23 } 24 25 return ext_view('artists.index', [ 26 'artists' => $artists->get(), 27 ]); 28 } 29 30 public function show($id) 31 { 32 $artist = Artist::with('label')->findOrFail($id); 33 $user = Auth::user(); 34 35 if (!$artist->visible && ($user === null || !$user->isAdmin())) { 36 abort(404); 37 } 38 39 $albums = $artist->albums() 40 ->where('visible', true) 41 ->orderBy('id', 'desc') 42 ->with(['tracks' => fn ($q) => $q 43 ->orderBy('display_order', 'asc') 44 ->orderBy('exclusive', 'desc') 45 ->orderBy('id', 'asc'), 46 ]) 47 ->get(); 48 49 $tracks = $artist 50 ->tracks() 51 ->whereNull('album_id') 52 ->orderBy('display_order', 'asc') 53 ->orderBy('exclusive', 'desc') 54 ->orderBy('id', 'desc') 55 ->get(); 56 57 foreach ($albums as $album) { 58 foreach ($album->tracks as $track) { 59 $track->setRelation('album', $album); 60 $track->setRelation('artist', $artist); 61 } 62 } 63 foreach ($tracks as $track) { 64 $track->setRelation('artist', $artist); 65 } 66 67 $images = [ 68 'header_url' => $artist->header_url, 69 'cover_url' => $artist->cover_url, 70 ]; 71 72 // should probably move services to a separate model if the number increases further (HA HA HA) 73 $links = []; 74 75 if ($artist->user_id) { 76 $links[] = [ 77 'title' => osu_trans('artist.links.osu'), 78 'url' => route('users.show', $artist->user_id), 79 'icon' => 'fas fa-user', 80 'class' => 'osu', 81 ]; 82 } 83 84 if ($artist->beatmapsets()->exists()) { 85 $links[] = [ 86 'title' => osu_trans('artist.links.beatmaps'), 87 'url' => route('beatmapsets.index', ['q' => "featured_artist={$artist->getKey()}"]), 88 'icon' => 'fas fa-bars', 89 'class' => 'osu', 90 ]; 91 } 92 93 if ($artist->website) { 94 $links[] = [ 95 'title' => osu_trans('artist.links.site'), 96 'url' => $artist->website, 97 'icon' => 'fas fa-link', 98 'class' => 'website', 99 ]; 100 } 101 102 foreach (['Twitter', 'Facebook', 'Spotify', 'Bandcamp', 'Patreon', 'SoundCloud', 'YouTube'] as $service) { 103 $serviceLowercase = strtolower($service); 104 if ($artist->$serviceLowercase) { 105 $links[] = [ 106 'title' => $service, 107 'url' => $artist->$serviceLowercase, 108 'icon' => "fab fa-{$serviceLowercase}", 109 'class' => $serviceLowercase, 110 ]; 111 } 112 } 113 114 set_opengraph($artist); 115 116 return ext_view('artists.show', [ 117 'artist' => $artist, 118 'images' => $images, 119 'json' => [ 120 'albums' => json_collection($albums, new ArtistAlbumTransformer(), ['tracks']), 121 'artist' => json_item($artist, new ArtistTransformer()), 122 'tracks' => json_collection($tracks, new ArtistTrackTransformer()), 123 ], 124 'links' => $links, 125 ]); 126 } 127}