diff --git a/ts/test-electron/util/Username_test.ts b/ts/test-electron/util/Username_test.ts index 43bc9f5fb..6f471230d 100644 --- a/ts/test-electron/util/Username_test.ts +++ b/ts/test-electron/util/Username_test.ts @@ -15,8 +15,14 @@ describe('Username', () => { }); it('matches partial username searches without discriminator', () => { - assert.strictEqual(getUsernameFromSearch('use'), 'use'); - assert.strictEqual(getUsernameFromSearch('use.'), 'use.'); + assert.strictEqual(getUsernameFromSearch('use'), 'use.01'); + 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', () => { diff --git a/ts/util/Username.ts b/ts/util/Username.ts index f6d19c5d0..225576cd5 100644 --- a/ts/util/Username.ts +++ b/ts/util/Username.ts @@ -24,17 +24,21 @@ export function getUsernameFromSearch(searchTerm: string): string | undefined { } let modifiedTerm = searchTerm; - if (searchTerm.endsWith('.')) { + + if (modifiedTerm.startsWith('@')) { + modifiedTerm = modifiedTerm.slice(1); + } + if (modifiedTerm.endsWith('.')) { // Allow nicknames without full discriminator - modifiedTerm = `${searchTerm}01`; - } else if (!/\.\d*$/.test(searchTerm)) { + modifiedTerm = `${modifiedTerm}01`; + } else if (!/\.\d*$/.test(modifiedTerm)) { // Allow nicknames without discriminator - modifiedTerm = `${searchTerm}.01`; + modifiedTerm = `${modifiedTerm}.01`; } try { usernames.hash(modifiedTerm); - return searchTerm; + return modifiedTerm; } catch { return undefined; } diff --git a/ts/util/lookupConversationWithoutServiceId.ts b/ts/util/lookupConversationWithoutServiceId.ts index 58cd7679b..1953aef49 100644 --- a/ts/util/lookupConversationWithoutServiceId.ts +++ b/ts/util/lookupConversationWithoutServiceId.ts @@ -142,8 +142,13 @@ export async function checkForUsername( username: string ): Promise { let hash: Buffer; + let fixedUsername = username; + if (fixedUsername.startsWith('@')) { + fixedUsername = fixedUsername.slice(1); + } + try { - hash = usernames.hash(username); + hash = usernames.hash(fixedUsername); } catch (error) { log.error('checkForUsername: invalid username', Errors.toLogFormat(error)); return undefined; @@ -166,7 +171,7 @@ export async function checkForUsername( return { aci: account.uuid, - username, + username: fixedUsername, }; } catch (error) { if (error instanceof HTTPError) {