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
6declare(strict_types=1);
7
8namespace App\Http\Controllers\Users;
9
10use App\Http\Controllers\Controller;
11use App\Models\User;
12use App\Transformers\UserCompactTransformer;
13
14class LookupController extends Controller
15{
16 public function __construct()
17 {
18 $this->middleware('throttle:1200,1');
19 $this->middleware('require-scopes:public');
20 }
21
22 public function index()
23 {
24 // TODO: referer check?
25 $params = get_params(\Request::all(), null, [
26 'exclude_bots:bool',
27 'ids:string[]',
28 ]);
29
30 $ids = array_slice(array_reject_null(get_arr($params['ids'] ?? [], presence(...))), 0, 50);
31
32 $numericIds = [];
33 $stringIds = [];
34 foreach ($ids as $id) {
35 if (ctype_digit($id)) {
36 $numericIds[] = $id;
37 } elseif (present($id)) {
38 $stringIds[] = $id[0] === '@' ? substr($id, 1) : $id;
39 }
40 }
41
42 $users = User::where(fn ($q) => $q->whereIn('user_id', $numericIds)->orWhereIn('username', $stringIds))
43 ->default()
44 ->withoutNoProfile()
45 ->with(UserCompactTransformer::CARD_INCLUDES_PRELOAD);
46
47 if ($params['exclude_bots'] ?? false) {
48 $users = $users->withoutBots();
49 }
50
51 return [
52 'users' => json_collection($users->get(), new UserCompactTransformer(), UserCompactTransformer::CARD_INCLUDES),
53 ];
54 }
55}