Skip to content

Commit 8568550

Browse files
authored
Merge pull request #11951 from notbakaneko/feature/beatmaps-include-own-tags
Include custom beatmap tags set by own user in response
2 parents 0eecb06 + bcfe6b8 commit 8568550

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

app/Http/Controllers/BeatmapsetsController.php

+2
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ private function showJson($beatmapset)
392392
? 'allBeatmaps'
393393
: 'beatmaps';
394394
$beatmapset->load([
395+
"{$beatmapRelation}" => fn ($q) => $q->withUserTagIds(\Auth::id()),
395396
"{$beatmapRelation}.baseDifficultyRatings",
396397
"{$beatmapRelation}.baseMaxCombo",
397398
"{$beatmapRelation}.failtimes",
@@ -409,6 +410,7 @@ private function showJson($beatmapset)
409410
'beatmaps.failtimes',
410411
'beatmaps.max_combo',
411412
'beatmaps.owners',
413+
'beatmaps.current_user_tag_ids',
412414
'beatmaps.top_tag_ids',
413415
'converts',
414416
'converts.failtimes',

app/Models/Beatmap.php

+24
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ public function scopeWithMaxCombo($query)
183183
) AS attrib_max_combo"));
184184
}
185185

186+
public function scopeWithUserTagIds($query, ?int $userId)
187+
{
188+
if ($userId === null) {
189+
$tagQuery = \DB::query()->selectRaw('null');
190+
} else {
191+
$tagQuery = BeatmapTag
192+
::where('user_id', $userId)
193+
->whereColumn('beatmap_id', $this->qualifyColumn('beatmap_id'));
194+
$tagQuery->selectRaw("json_arrayagg({$tagQuery->qualifyColumn('tag_id')})");
195+
}
196+
197+
return $query->addSelect(['user_tag_ids' => $tagQuery]);
198+
}
199+
186200
public function failtimes()
187201
{
188202
return $this->hasMany(BeatmapFailtimes::class);
@@ -290,6 +304,16 @@ public function getAttribute($key)
290304
};
291305
}
292306

307+
/**
308+
* Requires calling withUserTagIds scope to populate user_tag_ids
309+
*
310+
* @return int[]
311+
*/
312+
public function getUserTagIds(): array
313+
{
314+
return json_decode($this->attributes['user_tag_ids'] ?? '', true) ?? [];
315+
}
316+
293317
/**
294318
* @return Collection<User>
295319
*/

app/Transformers/BeatmapCompactTransformer.php

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class BeatmapCompactTransformer extends TransformerAbstract
1515
protected array $availableIncludes = [
1616
'beatmapset',
1717
'checksum',
18+
'current_user_tag_ids',
1819
'failtimes',
1920
'max_combo',
2021
'owners',
@@ -52,6 +53,11 @@ public function includeChecksum(Beatmap $beatmap)
5253
return $this->primitive($beatmap->checksum);
5354
}
5455

56+
public function includeCurrentUserTagIds(Beatmap $beatmap)
57+
{
58+
return $this->primitive($beatmap->getUserTagIds());
59+
}
60+
5561
public function includeFailtimes(Beatmap $beatmap)
5662
{
5763
$failtimes = $beatmap->failtimes;

app/Transformers/BeatmapsetCompactTransformer.php

-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ public function includeAvailability(Beatmapset $beatmapset)
9292

9393
public function includeBeatmaps(Beatmapset $beatmapset, Fractal\ParamBag $params)
9494
{
95-
9695
return $this->collection($this->beatmaps($beatmapset, $params), new $this->beatmapTransformer());
9796
}
9897

0 commit comments

Comments
 (0)