signal-desktop/ts/util/Username.ts

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-10-18 17:12:02 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2024-02-14 18:18:49 +00:00
import { usernames } from '@signalapp/libsignal-client';
2022-10-18 17:12:02 +00:00
import * as RemoteConfig from '../RemoteConfig';
2024-02-14 18:18:49 +00:00
import { getNickname } from '../types/Username';
2022-10-18 17:12:02 +00:00
import { parseIntWithFallback } from './parseIntWithFallback';
export function getMaxNickname(): number {
return parseIntWithFallback(
RemoteConfig.getValue('global.nicknames.max'),
32
);
}
export function getMinNickname(): number {
return parseIntWithFallback(RemoteConfig.getValue('global.nicknames.min'), 3);
}
2024-02-14 18:18:49 +00:00
export function getUsernameFromSearch(searchTerm: string): string | undefined {
const nickname = getNickname(searchTerm);
if (nickname == null || nickname.length < getMinNickname()) {
return undefined;
}
let modifiedTerm = searchTerm;
if (searchTerm.endsWith('.')) {
// Allow nicknames without full discriminator
modifiedTerm = `${searchTerm}01`;
} else if (!/\.\d*$/.test(searchTerm)) {
// Allow nicknames without discriminator
modifiedTerm = `${searchTerm}.01`;
}
try {
usernames.hash(modifiedTerm);
return searchTerm;
} catch {
return undefined;
}
}