Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show custom user tags on beatmapset #11750

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 0 additions & 16 deletions app/Http/Controllers/BeatmapTagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ public function __construct()
'destroy',
],
]);

$this->middleware('require-scopes:public', ['only' => 'index']);
}

public function index($beatmapId)
{
$topBeatmapTags = cache_remember_mutexed(
"beatmap_tags:{$beatmapId}",
$GLOBALS['cfg']['osu']['tags']['beatmap_tags_cache_duration'],
[],
fn () => Tag::topTags($beatmapId),
);

return [
'beatmap_tags' => $topBeatmapTags,
];
}

public function destroy($beatmapId, $tagId)
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/BeatmapsetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ private function showJson($beatmapset)
'beatmaps.failtimes',
'beatmaps.max_combo',
'beatmaps.owners',
'beatmaps.top_tag_ids',
'converts',
'converts.failtimes',
'converts.owners',
Expand All @@ -415,6 +416,7 @@ private function showJson($beatmapset)
'pack_tags',
'ratings',
'recent_favourites',
'related_tags',
'related_users',
'user',
]);
Expand Down
12 changes: 1 addition & 11 deletions app/Http/Controllers/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

namespace App\Http\Controllers;

use App\Models\Tag;
use App\Transformers\TagTransformer;

class TagsController extends Controller
{
public function __construct()
Expand All @@ -21,15 +18,8 @@ public function __construct()

public function index()
{
$tags = cache_remember_mutexed(
'tags',
$GLOBALS['cfg']['osu']['tags']['tags_cache_duration'],
[],
fn () => Tag::all(),
);

return [
'tags' => json_collection($tags, new TagTransformer()),
'tags' => app('tags')->json(),
];
}
}
10 changes: 10 additions & 0 deletions app/Models/Beatmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@ public function status()
return array_search($this->approved, Beatmapset::STATES, true);
}

public function topTagIds()
{
return cache_remember_mutexed(
"beatmap_top_tag_ids:{$this->getKey()}",
$GLOBALS['cfg']['osu']['tags']['beatmap_tags_cache_duration'],
[],
fn () => Tag::topTagIds($this->getKey())->toArray(),
);
}

private function getDifficultyrating()
{
if ($this->convert) {
Expand Down
6 changes: 3 additions & 3 deletions app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ public function beatmapTags(): HasMany
return $this->hasMany(BeatmapTag::class);
}

public static function topTags($beatmapId)
public static function topTagIds(int $beatmapId, int $limit = 50)
notbakaneko marked this conversation as resolved.
Show resolved Hide resolved
{
return static
::joinRelation(
'beatmapTags',
fn ($q) => $q->where('beatmap_id', $beatmapId)->whereHas('user', fn ($userQuery) => $userQuery->default())
)
->groupBy('id')
->select('id', 'name')
->select('id')
->selectRaw('COUNT(*) as count')
->orderBy('count', 'desc')
->orderBy('id', 'desc')
->limit(50)
->limit($limit)
->get();
}
}
1 change: 1 addition & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class AppServiceProvider extends ServiceProvider
'layout-cache' => Singletons\LayoutCache::class,
'medals' => Singletons\Medals::class,
'smilies' => Singletons\Smilies::class,
'tags' => Singletons\Tags::class,
'user-cover-presets' => Singletons\UserCoverPresets::class,
];

Expand Down
35 changes: 35 additions & 0 deletions app/Singletons/Tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace App\Singletons;

use App\Models\Tag;
use App\Traits\Memoizes;
use App\Transformers\TagTransformer;

class Tags
{
use Memoizes;

public function get(int $id): ?Tag
{
$allById = $this->memoize(
'allById',
fn () => Tag::all()->keyBy('id'),
);

return $allById[$id] ?? null;
}

public function json(): array
{
return $this->memoize(
__FUNCTION__,
fn () => json_collection(Tag::all(), new TagTransformer()),
notbakaneko marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
6 changes: 6 additions & 0 deletions app/Transformers/BeatmapCompactTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class BeatmapCompactTransformer extends TransformerAbstract
'failtimes',
'max_combo',
'owners',
'top_tag_ids',
'user',
];

Expand Down Expand Up @@ -83,6 +84,11 @@ public function includeOwners(Beatmap $beatmap)
]);
}

public function includeTopTagIds(Beatmap $beatmap)
{
return $this->primitive($beatmap->topTagIds());
}

public function includeUser(Beatmap $beatmap)
{
return $this->item(
Expand Down
19 changes: 19 additions & 0 deletions app/Transformers/BeatmapsetCompactTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BeatmapsetCompactTransformer extends TransformerAbstract
'ratings',
'recent_favourites',
'related_users',
'related_tags',
'user',
];

Expand Down Expand Up @@ -299,6 +300,24 @@ public function includeRelatedUsers(Beatmapset $beatmapset)
return $this->collection($users, new UserCompactTransformer());
}

public function includeRelatedTags(Beatmapset $beatmapset)
{
$beatmaps = $this->beatmaps($beatmapset);
$tagIdSet = new Set($beatmaps->flatMap->topTagIds()->pluck('id'));

$cachedTags = app('tags');
$json = [];

foreach ($tagIdSet as $tagId) {
$tag = $cachedTags->get($tagId);
if ($tag !== null) {
$json[] = $tag;
}
}

return $this->primitive($json);
}

private function beatmaps(Beatmapset $beatmapset, ?Fractal\ParamBag $params = null): EloquentCollection
{
$rel = $beatmapset->trashed() || ($params !== null && $params->get('with_trashed')) ? 'allBeatmaps' : 'beatmaps';
Expand Down
48 changes: 48 additions & 0 deletions resources/js/beatmapsets-show/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

import { BeatmapsetJsonForShow } from 'interfaces/beatmapset-extended-json';
import { TagJsonWithCount } from 'interfaces/tag-json';
import UserJson from 'interfaces/user-json';
import { keyBy } from 'lodash';
import { action, computed, makeObservable, observable, runInAction } from 'mobx';
Expand All @@ -10,6 +11,7 @@ import core from 'osu-core-singleton';
import { find, findDefault, group } from 'utils/beatmap-helper';
import { parse } from 'utils/beatmapset-page-hash';
import { parseJson } from 'utils/json';
import { present } from 'utils/string';
import { currentUrl } from 'utils/turbolinks';

export type ScoreLoadingState = null | 'error' | 'loading' | 'supporter_only' | 'unranked';
Expand Down Expand Up @@ -70,6 +72,52 @@ export default class Controller {
return this.beatmaps.get(this.currentBeatmap.mode) ?? [];
}

get relatedTags() {
notbakaneko marked this conversation as resolved.
Show resolved Hide resolved
const map = new Map();

for (const tag of this.beatmapset.related_tags) {
map.set(tag.id, tag);
}

return map;
}

@computed
get tags() {
const mapperTagSet = new Set(this.beatmapset.tags.split(' ').filter(present));
notbakaneko marked this conversation as resolved.
Show resolved Hide resolved
const tagMap = new Map<number, TagJsonWithCount>();

for (const tag of this.beatmapset.related_tags) {
tag.count = 0; // assign 0 and cast
tagMap.set(tag.id, tag as TagJsonWithCount);
}

for (const beatmap of this.beatmapset.beatmaps) {
notbakaneko marked this conversation as resolved.
Show resolved Hide resolved
if (beatmap.top_tag_ids == null) continue;

for (const tagId of beatmap.top_tag_ids) {
const tag = tagMap.get(tagId.id);
if (tag == null) continue;

tag.count += tagId.count;

// TODO: case insensitivity
if (mapperTagSet.has(tag.name)) {
tag.count++;
mapperTagSet.delete(tag.name);
}
}
}

return {
mapperTags: [...mapperTagSet.values()],
userTags: [...tagMap.values()].sort((a, b) => {
const diff = b.count - a.count;
return diff !== 0 ? diff : a.id - b.id;
notbakaneko marked this conversation as resolved.
Show resolved Hide resolved
}),
};
}

@computed
get usersById() {
return keyBy(this.beatmapset.related_users, 'id') as Partial<Record<number, UserJson>>;
Expand Down
19 changes: 12 additions & 7 deletions resources/js/beatmapsets-show/info.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ export default class Info extends React.Component<Props> {
return ret;
}

private get tags() {
const tags = this.controller.tags;

return [
...tags.userTags.map((tag) => tag.name),
...tags.mapperTags,
];
}

private get withEditDescription() {
return this.controller.beatmapset.description.bbcode != null;
}
Expand All @@ -84,10 +93,6 @@ export default class Info extends React.Component<Props> {
}

render() {
const tags = this.controller.beatmapset.tags
.split(' ')
.filter(present);

return (
<div className='beatmapset-info u-fancy-scrollbar'>
{this.isEditingDescription &&
Expand Down Expand Up @@ -191,14 +196,14 @@ export default class Info extends React.Component<Props> {
</div>
</div>

{tags.length > 0 &&
{this.tags.length > 0 &&
<div className='beatmapset-info__row beatmapset-info__row--value-overflow'>
<h3 className='beatmapset-info__header'>
{trans('beatmapsets.show.info.tags')}
</h3>
<div className='beatmapset-info__value-overflow'>
{tags.map((tag, i) => (
<React.Fragment key={`${tag}-${i}`}>
{this.tags.map((tag) => (
<React.Fragment key={`${tag}`}>
<a
className='beatmapset-info__link'
href={route('beatmapsets.index', { q: tag })}
Expand Down
1 change: 1 addition & 0 deletions resources/js/interfaces/beatmap-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ interface BeatmapJsonAvailableIncludes {
failtimes: BeatmapFailTimesArray;
max_combo: number;
owners: BeatmapOwnerJson[];
top_tag_ids: { count: number; id: number }[];
user: UserJson;
}

Expand Down
1 change: 1 addition & 0 deletions resources/js/interfaces/beatmapset-extended-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type BeatmapsetJsonForShowIncludes = Required<Pick<BeatmapsetExtendedJson,
| 'language'
| 'ratings'
| 'recent_favourites'
| 'related_tags'
| 'related_users'
| 'user'
>>;
Expand Down
3 changes: 3 additions & 0 deletions resources/js/interfaces/beatmapset-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import BeatmapsetNominationJson from './beatmapset-nomination-json';
import GenreJson from './genre-json';
import LanguageJson from './language-json';
import Ruleset from './ruleset';
import TagJson from './tag-json';
import UserJson, { UserJsonDeleted } from './user-json';

export interface Availability {
Expand Down Expand Up @@ -92,8 +93,10 @@ interface BeatmapsetJsonAvailableIncludes {
nominations: BeatmapsetNominationsInterface;
ratings: number[];
recent_favourites: UserJson[];
related_tags: TagJson[];
related_users: UserJson[];
user: UserJson | UserJsonDeleted;
user_tags: TagJson[];
notbakaneko marked this conversation as resolved.
Show resolved Hide resolved
}

interface HypeData {
Expand Down
11 changes: 11 additions & 0 deletions resources/js/interfaces/tag-json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

export default interface TagJson {
count?: number;
description: string;
id: number;
name: string;
}

export type TagJsonWithCount = TagJson & Required<Pick<TagJson, 'count'>>;
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@
});
});

Route::apiResource('tags', 'BeatmapTagsController', ['only' => ['index', 'store', 'destroy']]);
Route::apiResource('tags', 'BeatmapTagsController', ['only' => ['store', 'destroy']]);
});
});

Expand Down
Loading
Loading