-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into proxy-media-controller
- Loading branch information
Showing
26 changed files
with
583 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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\Http\Controllers; | ||
|
||
use App\Models\Beatmap; | ||
use App\Models\BeatmapTag; | ||
use App\Models\Tag; | ||
|
||
class BeatmapTagsController extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
$this->middleware('auth', [ | ||
'only' => [ | ||
'store', | ||
'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) | ||
{ | ||
BeatmapTag::where('tag_id', $tagId) | ||
->where('beatmap_id', $beatmapId) | ||
->where('user_id', \Auth::user()->getKey()) | ||
->delete(); | ||
|
||
return response()->noContent(); | ||
} | ||
|
||
public function store($beatmapId) | ||
{ | ||
$tagId = get_int(request('tag_id')); | ||
|
||
$beatmap = Beatmap::findOrFail($beatmapId); | ||
priv_check('BeatmapTagStore', $beatmap)->ensureCan(); | ||
|
||
$tag = Tag::findOrFail($tagId); | ||
|
||
$user = \Auth::user(); | ||
|
||
$tag | ||
->beatmapTags() | ||
->firstOrCreate(['beatmap_id' => $beatmapId, 'user_id' => $user->getKey()]); | ||
|
||
return response()->noContent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\Http\Controllers; | ||
|
||
use App\Models\Tag; | ||
use App\Transformers\TagTransformer; | ||
|
||
class TagsController extends Controller | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
$this->middleware('require-scopes:public'); | ||
} | ||
|
||
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()), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?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\Models; | ||
|
||
/** | ||
* @property-read Beatmap $beatmap | ||
* @property int $beatmap_id | ||
* @property int $tag_id | ||
* @property-read User $user | ||
* @property int $user_id | ||
*/ | ||
class BeatmapTag extends Model | ||
{ | ||
protected $primaryKey = ':composite'; | ||
protected $primaryKeys = ['beatmap_id', 'tag_id', 'user_id']; | ||
|
||
public function beatmap() | ||
{ | ||
return $this->belongsTo(Beatmap::class, 'beatmap_id'); | ||
} | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class, 'user_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?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\Models; | ||
|
||
use Illuminate\Database\Eloquent\Collection; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
|
||
/** | ||
* @property int $id | ||
* @property string $name | ||
* @property string $description | ||
* @property-read Collection<BeatmapTag> $beatmapTags | ||
*/ | ||
class Tag extends Model | ||
{ | ||
public function beatmapTags(): HasMany | ||
{ | ||
return $this->hasMany(BeatmapTag::class); | ||
} | ||
|
||
public static function topTags($beatmapId) | ||
{ | ||
return static | ||
::joinRelation( | ||
'beatmapTags', | ||
fn ($q) => $q->where('beatmap_id', $beatmapId)->whereHas('user', fn ($userQuery) => $userQuery->default()) | ||
) | ||
->groupBy('id') | ||
->select('id', 'name') | ||
->selectRaw('COUNT(*) as count') | ||
->orderBy('count', 'desc') | ||
->orderBy('id', 'desc') | ||
->limit(50) | ||
->get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?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\Transformers; | ||
|
||
use App\Models\Tag; | ||
|
||
class TagTransformer extends TransformerAbstract | ||
{ | ||
public function transform(Tag $item) | ||
{ | ||
return [ | ||
'id' => $item->getKey(), | ||
'name' => $item->name, | ||
'description' => $item->description, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?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 Database\Factories; | ||
|
||
use App\Models\Beatmap; | ||
use App\Models\BeatmapTag; | ||
use App\Models\Tag; | ||
use App\Models\User; | ||
|
||
class BeatmapTagFactory extends Factory | ||
{ | ||
protected $model = BeatmapTag::class; | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'beatmap_id' => Beatmap::factory(), | ||
'tag_id' => Tag::factory(), | ||
'user_id' => User::factory(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?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 Database\Factories; | ||
|
||
use App\Models\Tag; | ||
|
||
class TagFactory extends Factory | ||
{ | ||
protected $model = Tag::class; | ||
|
||
public function definition(): array | ||
{ | ||
return [ | ||
'name' => fn () => "Tag {$this->faker->word}", | ||
'description' => fn () => $this->faker->sentence, | ||
]; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
database/migrations/2024_11_26_093900_create_tags_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?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); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
|
||
return new class extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::create('tags', function (Blueprint $table) { | ||
$table->id(); | ||
$table->string('name')->unique(); | ||
$table->string('description'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('tags'); | ||
} | ||
}; |
Oops, something went wrong.