diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 1f748c2..8654964 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -23,12 +23,11 @@ class UserController extends Controller { /** * Display the profile of a user. - * @param int|string $id + * @param int $id * @return string */ - public function profile($id = 0): string + public function profile(int $id = 0): string { - // Get the user's context $profile = User::construct($id); // If the user id is zero check if there was a namechange @@ -48,6 +47,24 @@ class UserController extends Controller return view('user/profile', compact('profile')); } + /** + * Resolve user id from username and redirect to profile. + * @throws HttpRouteNotFoundException + * @param string $id + */ + public function resolve(string $name): string + { + $id = DB::table('users') + ->where('username_clean', clean_string($name, true, true)) + ->value('user_id'); + + if (!$id) { + throw new HttpRouteNotFoundException; + } + + return redirect(route('user.profile', $id)); + } + /** * Last listened to. * @param int $id diff --git a/routes.php b/routes.php index 2808aa3..b3ffc6a 100644 --- a/routes.php +++ b/routes.php @@ -59,8 +59,8 @@ Router::group(['before' => 'maintenance'], function () { Router::delete('/logout', 'AuthController@logout', 'auth.logout'); Router::get('/register', 'AuthController@register', 'auth.register'); Router::post('/register', 'AuthController@register', 'auth.register'); - Router::get('/resetpassword', 'AuthController@resetPassword', 'auth.resetpassword'); - Router::post('/resetpassword', 'AuthController@resetPassword', 'auth.resetpassword'); + Router::get('/reset-password', 'AuthController@resetPassword', 'auth.resetpassword'); + Router::post('/reset-password', 'AuthController@resetPassword', 'auth.resetpassword'); Router::get('/reactivate', 'AuthController@reactivate', 'auth.reactivate'); Router::post('/reactivate', 'AuthController@reactivate', 'auth.reactivate'); Router::get('/activate', 'AuthController@activate', 'auth.activate'); @@ -123,23 +123,28 @@ Router::group(['before' => 'maintenance'], function () { }); // User - Router::group(['prefix' => 'u'], function () { - Router::get('/{id}', 'UserController@profile', 'user.profile'); - Router::get('/{id}/report', 'UserController@report', 'user.report'); + Router::group(['prefix' => 'user'], function () { + Router::get('/{id:i}', 'UserController@profile', 'user.profile'); + Router::get('/{id}', 'UserController@resolve', 'user.profile'); + Router::get('/{id:i}/report', 'UserController@report', 'user.report'); - Router::get('/{id}/nowplaying', 'UserController@nowPlaying', 'user.nowplaying'); + Router::get('/{id:i}/now-playing', 'UserController@nowPlaying', 'user.nowplaying'); - Router::get('/{id}/avatar', 'FileController@avatar', 'user.avatar'); - Router::post('/{id}/avatar', 'FileController@avatar', 'user.avatar'); - Router::delete('/{id}/avatar', 'FileController@avatar', 'user.avatar'); + Router::get('/{id:i}/avatar', 'FileController@avatar', 'user.avatar'); + Router::post('/{id:i}/avatar', 'FileController@avatar', 'user.avatar'); + Router::delete('/{id:i}/avatar', 'FileController@avatar', 'user.avatar'); - Router::get('/{id}/background', 'FileController@background', 'user.background'); - Router::post('/{id}/background', 'FileController@background', 'user.background'); - Router::delete('/{id}/background', 'FileController@background', 'user.background'); + Router::get('/{id:i}/background', 'FileController@background', 'user.background'); + Router::post('/{id:i}/background', 'FileController@background', 'user.background'); + Router::delete('/{id:i}/background', 'FileController@background', 'user.background'); - Router::get('/{id}/header', 'FileController@header', 'user.header'); - Router::post('/{id}/header', 'FileController@header', 'user.header'); - Router::delete('/{id}/header', 'FileController@header', 'user.header'); + Router::get('/{id:i}/header', 'FileController@header', 'user.header'); + Router::post('/{id:i}/header', 'FileController@header', 'user.header'); + Router::delete('/{id:i}/header', 'FileController@header', 'user.header'); + }); + + Router::get('/u/{id}', function ($id) { + return redirect(route('user.profile', $id)); }); // Notifications