Allow leading @ in Compose/Find by username

This commit is contained in:
Scott Nonnenberg
2025-03-04 09:29:51 +10:00
committed by GitHub
parent 144d7d33cc
commit ee63cfc277
3 changed files with 24 additions and 9 deletions

View File

@@ -15,8 +15,14 @@ describe('Username', () => {
}); });
it('matches partial username searches without discriminator', () => { it('matches partial username searches without discriminator', () => {
assert.strictEqual(getUsernameFromSearch('use'), 'use'); assert.strictEqual(getUsernameFromSearch('use'), 'use.01');
assert.strictEqual(getUsernameFromSearch('use.'), 'use.'); assert.strictEqual(getUsernameFromSearch('use.'), 'use.01');
});
it('matches and strips leading @', () => {
assert.strictEqual(getUsernameFromSearch('@user'), 'user.01');
assert.strictEqual(getUsernameFromSearch('@user.'), 'user.01');
assert.strictEqual(getUsernameFromSearch('@user.01'), 'user.01');
}); });
it('matches valid username searches', () => { it('matches valid username searches', () => {

View File

@@ -24,17 +24,21 @@ export function getUsernameFromSearch(searchTerm: string): string | undefined {
} }
let modifiedTerm = searchTerm; let modifiedTerm = searchTerm;
if (searchTerm.endsWith('.')) {
if (modifiedTerm.startsWith('@')) {
modifiedTerm = modifiedTerm.slice(1);
}
if (modifiedTerm.endsWith('.')) {
// Allow nicknames without full discriminator // Allow nicknames without full discriminator
modifiedTerm = `${searchTerm}01`; modifiedTerm = `${modifiedTerm}01`;
} else if (!/\.\d*$/.test(searchTerm)) { } else if (!/\.\d*$/.test(modifiedTerm)) {
// Allow nicknames without discriminator // Allow nicknames without discriminator
modifiedTerm = `${searchTerm}.01`; modifiedTerm = `${modifiedTerm}.01`;
} }
try { try {
usernames.hash(modifiedTerm); usernames.hash(modifiedTerm);
return searchTerm; return modifiedTerm;
} catch { } catch {
return undefined; return undefined;
} }

View File

@@ -142,8 +142,13 @@ export async function checkForUsername(
username: string username: string
): Promise<FoundUsernameType | undefined> { ): Promise<FoundUsernameType | undefined> {
let hash: Buffer; let hash: Buffer;
let fixedUsername = username;
if (fixedUsername.startsWith('@')) {
fixedUsername = fixedUsername.slice(1);
}
try { try {
hash = usernames.hash(username); hash = usernames.hash(fixedUsername);
} catch (error) { } catch (error) {
log.error('checkForUsername: invalid username', Errors.toLogFormat(error)); log.error('checkForUsername: invalid username', Errors.toLogFormat(error));
return undefined; return undefined;
@@ -166,7 +171,7 @@ export async function checkForUsername(
return { return {
aci: account.uuid, aci: account.uuid,
username, username: fixedUsername,
}; };
} catch (error) { } catch (error) {
if (error instanceof HTTPError) { if (error instanceof HTTPError) {