Skip to content

Commit a3a5979

Browse files
authored
Merge branch 'ppy:master' into capitalization-change
2 parents 086c3ac + 54af1f3 commit a3a5979

32 files changed

+512
-28
lines changed

app/Http/Controllers/BeatmapsetsController.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Carbon\Carbon;
2525
use DB;
2626
use Request;
27+
use Symfony\Component\HttpFoundation\Response;
2728

2829
/**
2930
* @group Beatmapsets
@@ -354,6 +355,23 @@ public function update($id)
354355
return $this->showJson($beatmapset);
355356
}
356357

358+
public function versions(string $id): Response
359+
{
360+
$beatmapset = Beatmapset::findOrFail($id)->load([
361+
'versions' => fn ($q) => $q->orderByDesc('version_id'),
362+
'versions.versionFiles',
363+
]);
364+
$versions = $beatmapset->versions->keyBy('version_id');
365+
foreach ($versions as $version) {
366+
$version->setRelation('previousVersion', $versions[$version->previous_version_id] ?? null);
367+
}
368+
369+
return ext_view('beatmapsets.versions', [
370+
'beatmapset' => $beatmapset,
371+
'versions' => $versions,
372+
]);
373+
}
374+
357375
private function getSearchResponse(?array $params = null)
358376
{
359377
$params = new BeatmapsetSearchRequestParams($params ?? request()->all(), auth()->user());
@@ -435,6 +453,7 @@ private function showJson($beatmapset)
435453
'related_tags',
436454
'related_users',
437455
'user',
456+
'version_count',
438457
]);
439458
}
440459
}

app/Http/Controllers/LegacyMatchesController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ public function __construct()
3636
* matches | [Match](#match)[] | |
3737
* params.limit | integer | |
3838
* params.sort | string | |
39+
* params.active | boolean? | |
3940
*
4041
* @usesCursor
4142
* @queryParam limit integer Maximum number of matches (50 default, 1 minimum, 50 maximum). No-example
4243
* @queryParam sort string `id_desc` for newest first; `id_asc` for oldest first. Defaults to `id_desc`. No-example
44+
* @queryParam active boolean `true` for active matches only; `false` for inactive matches only. Defaults to not specified, returning both. No-example
4345
* @response {
4446
* "matches": [
4547
* {
@@ -52,7 +54,8 @@ public function __construct()
5254
* ],
5355
* "params": {
5456
* "limit": 50,
55-
* "sort": "id_desc"
57+
* "sort": "id_desc",
58+
* "active": null
5659
* },
5760
* "cursor": {
5861
* "match_id": 114428685

app/Http/Middleware/AuthApi.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ private function validTokenFromRequest($psr)
8383
$token->setRelation('client', $client);
8484
$token->validate();
8585

86+
// increment hit count for about every 10 hits
87+
if (rand(0, 9) === 0) {
88+
$token->incrementInstance('hit_count', 10);
89+
}
90+
8691
$user = $token->getResourceOwner();
8792

8893
if ($token->isClientCredentials()) {

app/Models/Beatmapset.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,7 @@ public function defaultDiscussionJson()
13161316
'nominations',
13171317
'related_users',
13181318
'related_users.groups',
1319+
'version_count',
13191320
]
13201321
);
13211322
}

app/Models/BeatmapsetVersion.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,38 @@ public function versionFiles(): HasMany
3939
{
4040
return $this->hasMany(BeatmapsetVersionFile::class);
4141
}
42+
43+
public function changes(): array
44+
{
45+
$previous = $this->previousVersion;
46+
if ($previous === null) {
47+
$added = $this->versionFiles->all();
48+
} else {
49+
$previousVersionFilesByFilename = $previous->versionFiles->keyBy('filename');
50+
$currentVersionFiles = $this->versionFiles;
51+
52+
$added = [];
53+
$updated = [];
54+
foreach ($currentVersionFiles as $versionFile) {
55+
$previousVersionFile = $previousVersionFilesByFilename[$versionFile->filename] ?? null;
56+
if ($previousVersionFile === null) {
57+
$added[] = $versionFile;
58+
} else {
59+
// no update otherwise
60+
if ($previousVersionFile->file_id !== $versionFile->file_id) {
61+
$updated[] = $versionFile;
62+
}
63+
$previousVersionFilesByFilename->forget($versionFile->filename);
64+
}
65+
}
66+
67+
$removed = $previousVersionFilesByFilename->values()->all();
68+
}
69+
70+
return [
71+
'added' => $added ?? [],
72+
'removed' => $removed ?? [],
73+
'updated' => $updated ?? [],
74+
];
75+
}
4276
}

app/Models/Model.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
abstract class Model extends BaseModel
2121
{
22-
use HasFactory, Traits\FasterAttributes, Validatable;
22+
use HasFactory, Traits\FasterAttributes, Traits\IncrementInstance, Validatable;
2323

2424
const MAX_FIELD_LENGTHS = [];
2525
const int PER_PAGE = 50;
@@ -201,18 +201,6 @@ public function delete()
201201
});
202202
}
203203

204-
/**
205-
* Just like increment but only works on saved instance instead of falling back to entire model
206-
*/
207-
public function incrementInstance()
208-
{
209-
if (!$this->exists) {
210-
return false;
211-
}
212-
213-
return $this->increment(...func_get_args());
214-
}
215-
216204
public function save(array $options = [])
217205
{
218206
return $this->runAfterCommitWrapper(function () use ($options) {

app/Models/OAuth/Token.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Exceptions\InvalidScopeException;
1010
use App\Interfaces\SessionVerificationInterface;
1111
use App\Models\Traits\FasterAttributes;
12+
use App\Models\Traits\IncrementInstance;
1213
use App\Models\User;
1314
use Ds\Set;
1415
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -18,7 +19,7 @@
1819
class Token extends PassportToken implements SessionVerificationInterface
1920
{
2021
// PassportToken doesn't have factory
21-
use HasFactory, FasterAttributes;
22+
use HasFactory, FasterAttributes, IncrementInstance;
2223

2324
const SCOPES_CLIENT_CREDENTIALS_ONLY = ['delegate', 'forum.write_manage', 'group_permissions'];
2425
const SCOPES_EXCLUDE_FROM_ALL = ['delegate', 'group_permissions'];
@@ -76,6 +77,7 @@ public function getAttribute($key)
7677
{
7778
return match ($key) {
7879
'client_id',
80+
'hit_count',
7981
'id',
8082
'name',
8183
'user_id',
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
6+
declare(strict_types=1);
7+
8+
namespace App\Models\Traits;
9+
10+
trait IncrementInstance
11+
{
12+
/**
13+
* Just like increment but only works on saved instance instead of falling back to entire model
14+
*/
15+
public function incrementInstance()
16+
{
17+
if (!$this->exists) {
18+
return false;
19+
}
20+
21+
return $this->increment(...func_get_args());
22+
}
23+
}

app/Transformers/BeatmapsetCompactTransformer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
1919
use League\Fractal;
2020
use League\Fractal\Resource\Collection;
21+
use League\Fractal\Resource\Primitive;
2122

2223
class BeatmapsetCompactTransformer extends TransformerAbstract
2324
{
@@ -42,6 +43,7 @@ class BeatmapsetCompactTransformer extends TransformerAbstract
4243
'related_users',
4344
'related_tags',
4445
'user',
46+
'version_count',
4547
];
4648

4749
// TODO: switch to enum after php 8.1
@@ -319,6 +321,11 @@ public function includeRelatedTags(Beatmapset $beatmapset)
319321
return $this->primitive($json);
320322
}
321323

324+
public function includeVersionCount(Beatmapset $beatmapset): Primitive
325+
{
326+
return $this->primitive($beatmapset->versions()->count());
327+
}
328+
322329
private function beatmaps(Beatmapset $beatmapset, ?Fractal\ParamBag $params = null): EloquentCollection
323330
{
324331
$rel = $beatmapset->trashed() || ($params !== null && $params->get('with_trashed')) ? 'allBeatmaps' : 'beatmaps';

database/factories/BeatmapsetFileFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function configure(): static
2828
public function definition(): array
2929
{
3030
return [
31-
'file_size' => rand(10, 100),
31+
'file_size' => rand(10, 100000),
3232
'sha2_hash' => fn (array $attrs): string => hash(
3333
'sha256',
3434
static::generateContent($attrs['file_size']),

0 commit comments

Comments
 (0)