From 502c25720294561a964d3c4c2e1e596bccde79bb Mon Sep 17 00:00:00 2001 From: nanaya Date: Wed, 20 Sep 2023 20:33:07 +0900 Subject: [PATCH] Combine lookup username array generation logic --- app/Libraries/User/UsernamesForDbLookup.php | 30 +++++++++++++++++++++ app/Models/User.php | 24 +++-------------- 2 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 app/Libraries/User/UsernamesForDbLookup.php diff --git a/app/Libraries/User/UsernamesForDbLookup.php b/app/Libraries/User/UsernamesForDbLookup.php new file mode 100644 index 00000000000..9bf565e7111 --- /dev/null +++ b/app/Libraries/User/UsernamesForDbLookup.php @@ -0,0 +1,30 @@ +. 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\Libraries\User; + +class UsernamesForDbLookup +{ + public static function make($username, bool $trimPrefix = true): array + { + $searchUsername = presence(get_string($username)); + + if ($searchUsername === null) { + return []; + } + + if ($trimPrefix && $searchUsername[0] === '@') { + $searchUsername = substr($searchUsername, 1); + } + + return [ + $searchUsername, + strtr($searchUsername, ' ', '_'), + strtr($searchUsername, '_', ' '), + ]; + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 60c346520f5..77b2695a940 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -16,6 +16,7 @@ use App\Libraries\Transactions\AfterCommit; use App\Libraries\User\DatadogLoginAttempt; use App\Libraries\User\ProfileBeatmapset; +use App\Libraries\User\UsernamesForDbLookup; use App\Libraries\UsernameValidation; use App\Models\Forum\TopicWatch; use App\Models\OAuth\Client; @@ -417,7 +418,7 @@ public static function findByUsernameForInactive($username): ?self { return static::whereIn( 'username', - [str_replace(' ', '_', $username), str_replace('_', ' ', $username)] + UsernamesForDbLookup::make($username, trimPrefix: false), )->first(); } @@ -494,15 +495,7 @@ public static function lookup($usernameOrId, $type = null, $findAll = false): ?s switch ($type) { case 'username': - $searchUsername = (string) $usernameOrId; - if ($searchUsername[0] === '@') { - $searchUsername = substr($searchUsername, 1); - } - $searchUsernames = [ - $searchUsername, - strtr($searchUsername, ' ', '_'), - strtr($searchUsername, '_', ' '), - ]; + $searchUsernames = UsernamesForDbLookup::make($usernameOrId); $user = static::where(fn ($query) => $query ->whereIn('username', $searchUsernames) @@ -550,17 +543,8 @@ public static function lookupWithHistory($usernameOrId, $type = null, $findAll = return null; } - $searchUsername = $usernameOrId[0] === '@' - ? substr($usernameOrId, 1) - : $usernameOrId; - $searchUsernames = [ - $searchUsername, - strtr($searchUsername, ' ', '_'), - strtr($searchUsername, '_', ' '), - ]; - $change = UsernameChangeHistory::visible() - ->whereIn('username_last', $searchUsernames) + ->whereIn('username_last', UsernamesForDbLookup::make($usernameOrId)) ->orderBy('change_id', 'desc') ->first();