the browser-facing portion of osu!
at master 100 lines 2.9 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\Transformers; 7 8use App\Models\ContestEntry; 9use App\Models\DeletedUser; 10use League\Fractal\Resource\Collection; 11use League\Fractal\Resource\Item; 12 13class ContestEntryTransformer extends TransformerAbstract 14{ 15 protected array $availableIncludes = [ 16 'artMeta', 17 'current_user_judge_vote', 18 'judge_votes', 19 'results', 20 'user', 21 ]; 22 23 public function transform(ContestEntry $entry) 24 { 25 $return = [ 26 'contest_id' => $entry->contest_id, 27 'id' => $entry->getKey(), 28 'title' => $entry->contest->unmasked ? $entry->name : $entry->masked_name, 29 'preview' => $entry->entry_url, 30 ]; 31 32 if ($entry->contest->hasThumbnails()) { 33 $return['thumbnail'] = mini_asset($entry->thumbnail()); 34 } 35 36 return $return; 37 } 38 39 public function includeCurrentUserJudgeVote(ContestEntry $entry): ?Item 40 { 41 $currentUser = auth()->user(); 42 43 if ($currentUser === null) { 44 return null; 45 } 46 47 $judgeVote = $entry->judgeVotes->where('user_id', $currentUser->getKey())->first(); 48 49 if ($judgeVote === null) { 50 return null; 51 } 52 53 return $this->item($judgeVote, new ContestJudgeVoteTransformer()); 54 } 55 56 public function includeJudgeVotes(ContestEntry $entry): Collection 57 { 58 return $this->collection($entry->judgeVotes, new ContestJudgeVoteTransformer()); 59 } 60 61 public function includeResults(ContestEntry $entry) 62 { 63 $votes = $entry->contest->isJudged() 64 ? $entry->scores_sum_value 65 : $entry->votes_count; 66 67 return $this->primitive([ 68 'actual_name' => $entry->name, 69 'votes' => (int) $votes, 70 ]); 71 } 72 73 public function includeUser(ContestEntry $entry) 74 { 75 return $this->primitive([ 76 'id' => $entry->user_id, 77 'username' => ($entry->user ?? (new DeletedUser()))->username, 78 ]); 79 } 80 81 public function includeArtMeta(ContestEntry $entry) 82 { 83 if (!$entry->contest->hasThumbnails() || !presence($entry->entry_url)) { 84 return $this->primitive([]); 85 } 86 87 $thumbnailUrl = $entry->thumbnail(); 88 // suffix urls when contests are made live to ensure image dimensions are forcibly rechecked 89 if ($entry->contest->visible) { 90 $thumbnailUrl .= str_contains($thumbnailUrl, '?') ? '&live' : '?live'; 91 } 92 93 $size = fast_imagesize($thumbnailUrl, "contest_entry:{$entry->getKey()}"); 94 95 return $this->primitive([ 96 'width' => $size[0] ?? 0, 97 'height' => $size[1] ?? 0, 98 ]); 99 } 100}