<commit_before><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Http\Controllers;

use App\Exceptions\ScoreRetrievalException;
use App\Models\Beatmap;
use App\Models\Score\Best\Model as BestModel;
use Auth;
use Request;

class BeatmapsController extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->middleware('require-scopes:public');
    }

    public function show($id)
    {
        $beatmap = Beatmap::findOrFail($id);
        $set = $beatmap->beatmapset;

        if ($set === null) {
            abort(404);
        }

        $requestedMode = presence(request('mode'));

        if (Beatmap::isModeValid($requestedMode) && $beatmap->mode === 'osu') {
            $mode = $requestedMode;
        } else {
            $mode = $beatmap->mode;
        }

        return ujs_redirect(route('beatmapsets.show', ['beatmapset' => $set->beatmapset_id]).'#'.$mode.'/'.$id);
    }

    public function scores($id)
    {
        $beatmap = Beatmap::findOrFail($id);
        if ($beatmap->approved <= 0) {
            return ['scores' => []];
        }

        return with_db_fallback('mysql-readonly', function ($connection) use ($beatmap) {
            $mode = presence(Request::input('mode')) ?? $beatmap->mode;
            $mods = get_arr(Request::input('mods'), 'presence') ?? [];
            $type = Request::input('type', 'global');
            $user = Auth::user();

            try {
                if ($type !== 'global' || !empty($mods)) {
                    if ($user === null || !$user->isSupporter()) {
                        throw new ScoreRetrievalException(trans('errors.supporter_only'));
                    }
                }

                $class = BestModel::getClassByString($mode);
                $model = new $class;
                $model->setConnection($connection);

                $query = $model
                    ->default()
                    ->where('beatmap_id', $beatmap->getKey())
                    ->with(['beatmap', 'user.country'])
                    ->withMods($mods)
                    ->withType($type, compact('user'));

                if ($user !== null) {
                    $score = (clone $query)->where('user_id', $user->user_id)->first();
                }

                $results = [
                    'scores' => json_collection($query->visibleUsers()->forListing(), 'Score', ['beatmap', 'user', 'user.country']),
                ];

                if (isset($score)) {
                    $results['userScore'] = [
                        'position' => $score->userRank(compact('type', 'mods')),
                        'score' => json_item($score, 'Score', ['user', 'user.country']),
                    ];
                }

                return $results;
            } catch (ScoreRetrievalException $ex) {
                return error_popup($ex->getMessage());
            }
        });
    }
}
<commit_msg>add user cover to beatmaps response<commit_after><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace App\Http\Controllers;

use App\Exceptions\ScoreRetrievalException;
use App\Models\Beatmap;
use App\Models\Score\Best\Model as BestModel;
use Auth;
use Request;

class BeatmapsController extends Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->middleware('require-scopes:public');
    }

    public function show($id)
    {
        $beatmap = Beatmap::findOrFail($id);
        $set = $beatmap->beatmapset;

        if ($set === null) {
            abort(404);
        }

        $requestedMode = presence(request('mode'));

        if (Beatmap::isModeValid($requestedMode) && $beatmap->mode === 'osu') {
            $mode = $requestedMode;
        } else {
            $mode = $beatmap->mode;
        }

        return ujs_redirect(route('beatmapsets.show', ['beatmapset' => $set->beatmapset_id]).'#'.$mode.'/'.$id);
    }

    public function scores($id)
    {
        $beatmap = Beatmap::findOrFail($id);
        if ($beatmap->approved <= 0) {
            return ['scores' => []];
        }

        return with_db_fallback('mysql-readonly', function ($connection) use ($beatmap) {
            $mode = presence(Request::input('mode')) ?? $beatmap->mode;
            $mods = get_arr(Request::input('mods'), 'presence') ?? [];
            $type = Request::input('type', 'global');
            $user = Auth::user();

            try {
                if ($type !== 'global' || !empty($mods)) {
                    if ($user === null || !$user->isSupporter()) {
                        throw new ScoreRetrievalException(trans('errors.supporter_only'));
                    }
                }

                $class = BestModel::getClassByString($mode);
                $model = new $class;
                $model->setConnection($connection);

                $query = $model
                    ->default()
                    ->where('beatmap_id', $beatmap->getKey())
                    ->with(['beatmap', 'user.country'])
                    ->withMods($mods)
                    ->withType($type, compact('user'));

                if ($user !== null) {
                    $score = (clone $query)->where('user_id', $user->user_id)->first();
                }

                $results = [
                    'scores' => json_collection($query->visibleUsers()->forListing(), 'Score', ['beatmap', 'user', 'user.country']),
                ];

                if (isset($score)) {
                    $results['userScore'] = [
                        'position' => $score->userRank(compact('type', 'mods')),
                        'score' => json_item($score, 'Score', ['user', 'user.country', 'user.cover']),
                    ];
                }

                return $results;
            } catch (ScoreRetrievalException $ex) {
                return error_popup($ex->getMessage());
            }
        });
    }
}

{'fraction_non_alphanumeric': 0.1604260813428018, 'fraction_numerical': 0.001936733376371853, 'mean_word_length': 1.764049955396967, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7139351', 'n_tokens_mistral': 1760, 'n_tokens_neox': 1667, 'n_words': 334}