From 7a1bc3abeb1c695a071fcd0557c9d0e95a97bc68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Fri, 13 Dec 2024 15:18:04 +0900 Subject: [PATCH 1/4] Add submission-relevant events to database via LIO endpoints --- .../InterOp/BeatmapsetsController.php | 18 ++++ app/Models/Event.php | 89 ++++++++++++++++--- routes/web.php | 1 + 3 files changed, 95 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/InterOp/BeatmapsetsController.php b/app/Http/Controllers/InterOp/BeatmapsetsController.php index ed8a9de7803..6e30fa63edf 100644 --- a/app/Http/Controllers/InterOp/BeatmapsetsController.php +++ b/app/Http/Controllers/InterOp/BeatmapsetsController.php @@ -6,12 +6,14 @@ namespace App\Http\Controllers\InterOp; use App\Http\Controllers\Controller; +use App\Http\Requests\Request; use App\Jobs\BeatmapsetDelete; use App\Jobs\Notifications\UserBeatmapsetNew; use App\Jobs\Notifications\UserBeatmapsetRevive; use App\Models\BeatmapDiscussion; use App\Models\BeatmapDiscussionPost; use App\Models\Beatmapset; +use App\Models\Event; use App\Models\User; class BeatmapsetsController extends Controller @@ -22,6 +24,9 @@ public function broadcastNew($id) (new UserBeatmapsetNew($beatmapset))->dispatch(); + if (api_version() >= 20241213) + Event::generate('beatmapsetUpload', ['beatmapset' => $beatmapset]); + return response(null, 204); } @@ -31,6 +36,19 @@ public function broadcastRevive($id) (new UserBeatmapsetRevive($beatmapset))->dispatch(); + if (api_version() >= 20241213) + Event::generate('beatmapsetRevive', ['beatmapset' => $beatmapset]); + + return response(null, 204); + } + + public function broadcastUpdate($id) + { + $beatmapset = Beatmapset::findOrFail($id); + $user = User::findOrFail(request()->integer('user_id')); + + Event::generate('beatmapsetUpdate', ['beatmapset' => $beatmapset, 'user' => $user]); + return response(null, 204); } diff --git a/app/Models/Event.php b/app/Models/Event.php index b7e48085928..08d4eb165bd 100644 --- a/app/Models/Event.php +++ b/app/Models/Event.php @@ -88,19 +88,14 @@ public static function generate($type, $options) case 'beatmapsetApprove': $beatmapset = $options['beatmapset']; - - $beatmapsetUrl = e(route('beatmapsets.show', $beatmapset, false)); - $beatmapsetTitle = e($beatmapset->artist.' - '.$beatmapset->title); - $userName = e($beatmapset->user->username); - $userUrl = e(route('users.show', $beatmapset->user, false)); + $beatmapsetParams = static::beatmapsetParams($beatmapset); + $userParams = static::userParams($options['beatmapset']->user); $approval = e($beatmapset->status()); - $textCleanBeatmapsetUrl = $GLOBALS['cfg']['app']['url'].$beatmapsetUrl; - $textCleanUserUrl = $GLOBALS['cfg']['app']['url'].$userUrl; - $textClean = "[{$textCleanBeatmapsetUrl} {$beatmapsetTitle}] by [{$textCleanUserUrl} {$userName}] has just been {$approval}!"; + $textClean = "[{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}] by [{$userParams['url_clean']} {$userParams['username']}] has just been {$approval}!"; $params = [ - 'text' => "{$beatmapsetTitle} by {$userName} has just been {$approval}!", + 'text' => "{$beatmapsetParams['title']} by {$userParams['username']} has just been {$approval}!", 'text_clean' => $textClean, 'beatmap_id' => 0, 'beatmapset_id' => $beatmapset->getKey(), @@ -113,11 +108,10 @@ public static function generate($type, $options) case 'beatmapsetDelete': $beatmapset = $options['beatmapset']; - $beatmapsetUrl = e(route('beatmapsets.show', $beatmapset, false)); - $beatmapsetTitle = e($beatmapset->artist.' - '.$beatmapset->title); + $beatmapsetParams = static::beatmapsetParams($beatmapset); $params = [ - 'text' => "{$beatmapsetTitle} has been deleted.", + 'text' => "{$beatmapsetParams['title']} has been deleted.", 'beatmapset_id' => $beatmapset->getKey(), 'user_id' => $options['user']->getKey(), 'private' => false, @@ -126,6 +120,63 @@ public static function generate($type, $options) break; + case 'beatmapsetRevive': + $beatmapset = $options['beatmapset']; + $beatmapsetParams = static::beatmapsetParams($beatmapset); + $userParams = static::userParams($beatmapset->user); + + $textClean = "[{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}] has been revived from eternal slumber by [{$userParams['url_clean']} {$userParams['username']}]"; + + $params = [ + 'text' => "{$beatmapsetParams['title']} has been revived from eternal slumber by {$userParams['username']}", + 'text_clean' => $textClean, + 'beatmapset_id' => $beatmapset->getKey(), + 'user_id' => $beatmapset->user->getKey(), + 'private' => false, + 'epicfactor' => 5, + ]; + + break; + + case 'beatmapsetUpdate': + $beatmapset = $options['beatmapset']; + $beatmapsetParams = static::beatmapsetParams($beatmapset); + // retrieved separately from options because it doesn't necessarily need to be the same user + // as $beatmapset->user in some cases (see: direct guest difficulty update) + $user = $options['user']; + $userParams = static::userParams($user); + + $textClean = "[{$userParams['url_clean']} {$userParams['username']}] has updated the beatmap [{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}]"; + + $params = [ + 'text' => "{$userParams['username']} has updated the beatmap \"{$beatmapsetParams['title']}\"", + 'text_clean' => $textClean, + 'beatmapset_id' => $beatmapset->getKey(), + 'user_id' => $user->getKey(), + 'private' => false, + 'epicfactor' => 2, + ]; + + break; + + case 'beatmapsetUpload': + $beatmapset = $options['beatmapset']; + $beatmapsetParams = static::beatmapsetParams($beatmapset); + $userParams = static::userParams($beatmapset->user); + + $textClean = "[{$userParams['url_clean']} {$userParams['username']}] has submitted a new beatmap [{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}]"; + + $params = [ + 'text' => "{$userParams['username']} has submitted a new beatmap \"{$beatmapsetParams['title']}\"", + 'text_clean' => $textClean, + 'beatmapset_id' => $beatmapset->getKey(), + 'user_id' => $beatmapset->user->getKey(), + 'private' => false, + 'epicfactor' => 4, + ]; + + break; + case 'usernameChange': $user = static::userParams($options['user']); $oldUsername = e($options['history']->username_last); @@ -430,10 +481,22 @@ public function scopeRecent($query) private static function userParams($user) { + $url = e(route('users.show', $user, false)); return [ 'id' => $user->getKey(), 'username' => e($user->username), - 'url' => e(route('users.show', $user, false)), + 'url' => $url, + 'url_clean' => $GLOBALS['cfg']['app']['url'].$url, + ]; + } + + private static function beatmapsetParams($beatmapset) + { + $url = e(route('beatmapsets.show', $beatmapset, false)); + return [ + 'title' => e($beatmapset->artist.' - '.$beatmapset->title), + 'url' => $url, + 'url_clean' => $GLOBALS['cfg']['app']['url'].$url, ]; } } diff --git a/routes/web.php b/routes/web.php index c65e4547bc1..17dfb39f6c1 100644 --- a/routes/web.php +++ b/routes/web.php @@ -588,6 +588,7 @@ Route::group(['prefix' => '{beatmapset}'], function () { Route::post('broadcast-new', 'BeatmapsetsController@broadcastNew')->name('broadcast-new'); Route::post('broadcast-revive', 'BeatmapsetsController@broadcastRevive')->name('broadcast-revive'); + Route::post('broadcast-update', 'BeatmapsetsController@broadcastUpdate')->name('broadcast-update'); Route::post('disqualify', 'BeatmapsetsController@disqualify')->name('disqualify'); }); }); From 7a03dc5a5cd44c115c42ad914e59d71aa2d9af33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Mon, 16 Dec 2024 16:21:37 +0900 Subject: [PATCH 2/4] Use explicit request param for determining whether to create event --- app/Http/Controllers/InterOp/BeatmapsetsController.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/InterOp/BeatmapsetsController.php b/app/Http/Controllers/InterOp/BeatmapsetsController.php index 6e30fa63edf..64b5f1d8064 100644 --- a/app/Http/Controllers/InterOp/BeatmapsetsController.php +++ b/app/Http/Controllers/InterOp/BeatmapsetsController.php @@ -6,7 +6,6 @@ namespace App\Http\Controllers\InterOp; use App\Http\Controllers\Controller; -use App\Http\Requests\Request; use App\Jobs\BeatmapsetDelete; use App\Jobs\Notifications\UserBeatmapsetNew; use App\Jobs\Notifications\UserBeatmapsetRevive; @@ -24,8 +23,9 @@ public function broadcastNew($id) (new UserBeatmapsetNew($beatmapset))->dispatch(); - if (api_version() >= 20241213) + if (request()->boolean('create_event')) { Event::generate('beatmapsetUpload', ['beatmapset' => $beatmapset]); + } return response(null, 204); } @@ -36,8 +36,9 @@ public function broadcastRevive($id) (new UserBeatmapsetRevive($beatmapset))->dispatch(); - if (api_version() >= 20241213) + if (request()->boolean('create_event')) { Event::generate('beatmapsetRevive', ['beatmapset' => $beatmapset]); + } return response(null, 204); } From d4bad3dc773536b7dc91ea4751ce69f58b9dba14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Fri, 20 Dec 2024 14:21:29 +0100 Subject: [PATCH 3/4] Fix typos --- app/Models/Event.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Models/Event.php b/app/Models/Event.php index 08d4eb165bd..2909f953f50 100644 --- a/app/Models/Event.php +++ b/app/Models/Event.php @@ -125,10 +125,10 @@ public static function generate($type, $options) $beatmapsetParams = static::beatmapsetParams($beatmapset); $userParams = static::userParams($beatmapset->user); - $textClean = "[{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}] has been revived from eternal slumber by [{$userParams['url_clean']} {$userParams['username']}]"; + $textClean = "[{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}] has been revived from eternal slumber by [{$userParams['url_clean']} {$userParams['username']}]."; $params = [ - 'text' => "{$beatmapsetParams['title']} has been revived from eternal slumber by {$userParams['username']}", + 'text' => "{$beatmapsetParams['title']} has been revived from eternal slumber by {$userParams['username']}.", 'text_clean' => $textClean, 'beatmapset_id' => $beatmapset->getKey(), 'user_id' => $beatmapset->user->getKey(), @@ -167,7 +167,7 @@ public static function generate($type, $options) $textClean = "[{$userParams['url_clean']} {$userParams['username']}] has submitted a new beatmap [{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}]"; $params = [ - 'text' => "{$userParams['username']} has submitted a new beatmap \"{$beatmapsetParams['title']}\"", + 'text' => "{$userParams['username']} has submitted a new beatmap \"{$beatmapsetParams['title']}\"", 'text_clean' => $textClean, 'beatmapset_id' => $beatmapset->getKey(), 'user_id' => $beatmapset->user->getKey(), From b2937ec62e178ae807a47484787f6038c9dbe75f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Fri, 20 Dec 2024 14:40:55 +0100 Subject: [PATCH 4/4] Refactor event formatting to be maybe better --- app/Models/Event.php | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/app/Models/Event.php b/app/Models/Event.php index 2909f953f50..1a58e0372f3 100644 --- a/app/Models/Event.php +++ b/app/Models/Event.php @@ -92,11 +92,10 @@ public static function generate($type, $options) $userParams = static::userParams($options['beatmapset']->user); $approval = e($beatmapset->status()); - $textClean = "[{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}] by [{$userParams['url_clean']} {$userParams['username']}] has just been {$approval}!"; - + $template = '%s by %s has just been %s!'; $params = [ - 'text' => "{$beatmapsetParams['title']} by {$userParams['username']} has just been {$approval}!", - 'text_clean' => $textClean, + 'text' => sprintf($template, "{$beatmapsetParams['title']}", "{$userParams['username']}", $approval), + 'text_clean' => sprintf($template, "[{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}]", "[{$userParams['url_clean']} {$userParams['username']}]", $approval), 'beatmap_id' => 0, 'beatmapset_id' => $beatmapset->getKey(), 'user_id' => $beatmapset->user->getKey(), @@ -125,11 +124,10 @@ public static function generate($type, $options) $beatmapsetParams = static::beatmapsetParams($beatmapset); $userParams = static::userParams($beatmapset->user); - $textClean = "[{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}] has been revived from eternal slumber by [{$userParams['url_clean']} {$userParams['username']}]."; - + $template = '%s has been revived from eternal slumber by %s.'; $params = [ - 'text' => "{$beatmapsetParams['title']} has been revived from eternal slumber by {$userParams['username']}.", - 'text_clean' => $textClean, + 'text' => sprintf($template, "{$beatmapsetParams['title']}", "{$userParams['username']}"), + 'text_clean' => sprintf($template, "[{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}]", "[{$userParams['url_clean']} {$userParams['username']}]"), 'beatmapset_id' => $beatmapset->getKey(), 'user_id' => $beatmapset->user->getKey(), 'private' => false, @@ -146,11 +144,10 @@ public static function generate($type, $options) $user = $options['user']; $userParams = static::userParams($user); - $textClean = "[{$userParams['url_clean']} {$userParams['username']}] has updated the beatmap [{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}]"; - + $template = '%s has updated the beatmap "%s"'; $params = [ - 'text' => "{$userParams['username']} has updated the beatmap \"{$beatmapsetParams['title']}\"", - 'text_clean' => $textClean, + 'text' => sprintf($template, "{$userParams['username']}", "{$beatmapsetParams['title']}"), + 'text_clean' => sprintf($template, "[{$userParams['url_clean']} {$userParams['username']}]", "[{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}]"), 'beatmapset_id' => $beatmapset->getKey(), 'user_id' => $user->getKey(), 'private' => false, @@ -164,11 +161,10 @@ public static function generate($type, $options) $beatmapsetParams = static::beatmapsetParams($beatmapset); $userParams = static::userParams($beatmapset->user); - $textClean = "[{$userParams['url_clean']} {$userParams['username']}] has submitted a new beatmap [{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}]"; - + $template = '%s has submitted a new beatmap "%s"'; $params = [ - 'text' => "{$userParams['username']} has submitted a new beatmap \"{$beatmapsetParams['title']}\"", - 'text_clean' => $textClean, + 'text' => sprintf($template, "{$userParams['username']}", "{$beatmapsetParams['title']}"), + 'text_clean' => sprintf($template, "[{$userParams['url_clean']} {$userParams['username']}]", "[{$beatmapsetParams['url_clean']} {$beatmapsetParams['title']}]"), 'beatmapset_id' => $beatmapset->getKey(), 'user_id' => $beatmapset->user->getKey(), 'private' => false,