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;
9
10use App\Models\Event;
11
12/**
13 * @group Events
14 */
15class EventsController extends Controller
16{
17 public function __construct()
18 {
19 $this->middleware('require-scopes:public', ['only' => ['index']]);
20
21 parent::__construct();
22 }
23
24 /**
25 * Get Events
26 *
27 * Returns a collection of [Event](#event)s in order of creation time.
28 *
29 * ---
30 *
31 * ### Response Format
32 *
33 * Field | Type
34 * ------------- | ----
35 * cursor_string | [CursorString](#cursorstring)
36 * events | [Event](#event)[]
37 *
38 * @usesCursor
39 * @queryParam sort Sorting option. Valid values are `id_desc` (default) and `id_asc`. No-example
40 *
41 * @response {
42 * events: [
43 * {
44 * created_at: "2022-12-08T02:02:51+00:00",
45 * id: 57,
46 * type: "achievement",
47 * achievement: { ... },
48 * user: { ... }
49 * },
50 * ...
51 * ],
52 * cursor_string: "eyJldmVudF9pZCI6OH0"
53 * }
54 */
55 public function index()
56 {
57 $params = request()->all();
58 $cursorHelper = Event::makeDbCursorHelper(get_string($params['sort'] ?? null));
59
60 [$events, $hasMore] = Event
61 ::cursorSort($cursorHelper, cursor_from_params($params))
62 ->limit(50)
63 ->getWithHasMore();
64
65 return [
66 'events' => json_collection($events, 'Event'),
67 ...cursor_for_response($cursorHelper->next($events, $hasMore)),
68 ];
69 }
70}