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\User;
11use App\Models\UserGroupEvent;
12
13class GroupHistoryController extends Controller
14{
15 public function index()
16 {
17 $rawParams = request()->all();
18 $params = get_params($rawParams, null, [
19 'group:string',
20 'max_date:time',
21 'min_date:time',
22 'sort:string',
23 'user:string',
24 ], ['null_missing' => true]);
25 $query = UserGroupEvent::visibleForUser(auth()->user());
26
27 if ($params['group'] !== null) {
28 $groupId = app('groups')->byIdentifier($params['group'])?->getKey();
29
30 if ($groupId !== null) {
31 $query->where('group_id', $groupId);
32 } else {
33 $query->none();
34 }
35 }
36
37 if ($params['max_date'] !== null) {
38 $query->where('created_at', '<=', $params['max_date']);
39 }
40
41 if ($params['min_date'] !== null) {
42 $query->where('created_at', '>=', $params['min_date']);
43 }
44
45 if ($params['user'] !== null) {
46 $userId = User::lookupWithHistory($params['user'], null, true)?->getKey();
47
48 if ($userId !== null) {
49 $query->where('user_id', $userId);
50 } else {
51 $query->none();
52 }
53 }
54
55 $cursorHelper = UserGroupEvent::makeDbCursorHelper($params['sort']);
56 [$events, $hasMore] = $query
57 ->cursorSort($cursorHelper, cursor_from_params($rawParams))
58 ->limit(50)
59 ->getWithHasMore();
60 $cursor = $cursorHelper->next($events, $hasMore);
61
62 return [
63 'events' => json_collection($events, 'UserGroupEvent'),
64 ...cursor_for_response($cursor),
65 ];
66 }
67}