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

Combine lookup username array generation logic #10571

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
30 changes: 30 additions & 0 deletions app/Libraries/User/UsernamesForDbLookup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\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] === '@') {
Copy link
Member

@cl8n cl8n Sep 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at usages of findByUsernameForInactive(), I think $trimPrefix can just always be true.

$searchUsername = substr($searchUsername, 1);
}

return [
$searchUsername,
strtr($searchUsername, ' ', '_'),
strtr($searchUsername, '_', ' '),
];
}
}
24 changes: 4 additions & 20 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();

Expand Down