From d252662630c15aa24d1c8c411ce559516f6c6129 Mon Sep 17 00:00:00 2001 From: William Grant Date: Wed, 17 Jul 2024 13:47:24 +1000 Subject: [PATCH] fix: truncate submitted display names we dont look for libsession throws when setting a display name. libsession now truncates by default --- .../dialog/edit-profile/EditProfileDialog.tsx | 32 ++++++++----------- .../registration/stages/CreateAccount.tsx | 18 ++--------- .../registration/stages/RestoreAccount.tsx | 8 +---- ts/session/profile_manager/ProfileManager.ts | 10 +++--- 4 files changed, 22 insertions(+), 46 deletions(-) diff --git a/ts/components/dialog/edit-profile/EditProfileDialog.tsx b/ts/components/dialog/edit-profile/EditProfileDialog.tsx index fc705bb30..a5e770a30 100644 --- a/ts/components/dialog/edit-profile/EditProfileDialog.tsx +++ b/ts/components/dialog/edit-profile/EditProfileDialog.tsx @@ -194,17 +194,17 @@ export const EditProfileDialog = () => { const backButton = mode === 'edit' || mode === 'qr' ? [ - { - iconType: 'chevron', - iconRotation: 90, - onClick: () => { - if (loading) { - return; - } - setMode('default'); - }, + { + iconType: 'chevron', + iconRotation: 90, + onClick: () => { + if (loading) { + return; + } + setMode('default'); }, - ] + }, + ] : undefined; const onClickOK = async () => { @@ -214,16 +214,12 @@ export const EditProfileDialog = () => { try { setLoading(true); - await ProfileManager.updateOurProfileDisplayName(profileName); - setUpdateProfileName(profileName); + const validName = await ProfileManager.updateOurProfileDisplayName(profileName); + setUpdateProfileName(validName); setMode('default'); } catch (err) { - // Note error substring is taken from libsession-util - if (err.message && err.message.includes('exceeds maximum length')) { - setProfileNameError(window.i18n('displayNameTooLong')); - } else { - setProfileNameError(window.i18n('unknownError')); - } + window.log.error('Profile update error', err); + setProfileNameError(window.i18n('unknownError')); } finally { setLoading(false); } diff --git a/ts/components/registration/stages/CreateAccount.tsx b/ts/components/registration/stages/CreateAccount.tsx index 6a69498ee..4146fb6ba 100644 --- a/ts/components/registration/stages/CreateAccount.tsx +++ b/ts/components/registration/stages/CreateAccount.tsx @@ -3,7 +3,6 @@ import { useDispatch } from 'react-redux'; import useMount from 'react-use/lib/useMount'; import { SettingsKey } from '../../../data/settings-key'; import { mnDecode } from '../../../session/crypto/mnemonic'; -import { ProfileManager } from '../../../session/profile_manager/ProfileManager'; import { StringUtils } from '../../../session/utils'; import { fromHex } from '../../../session/utils/String'; import LIBSESSION_CONSTANTS from '../../../session/utils/libsession/libsession_constants'; @@ -29,7 +28,6 @@ import { sessionGenerateKeyPair, } from '../../../util/accountManager'; import { Storage, setSignWithRecoveryPhrase } from '../../../util/storage'; -import { UserConfigWrapperActions } from '../../../webworker/workers/browser/libsession_worker_interface'; import { Flex } from '../../basic/Flex'; import { SpacerLG, SpacerSM } from '../../basic/Text'; import { SessionInput } from '../../inputs'; @@ -101,32 +99,20 @@ export const CreateAccount = () => { if (!privateKeyBytes) { throw new Error('Private key not found'); } - // validate display name using libsession - // eslint-disable-next-line max-len - // TODO [libsession validation] if we try and use a different display name after entering one that is already too long we get an error because the user config has been initialised. I call .free() in the finally but that doesn't help - await UserConfigWrapperActions.init(privateKeyBytes, null); - - const validName = await ProfileManager.updateOurProfileDisplayName(displayName, true); await signUp({ - displayName: validName, + displayName, recoveryPassword, }); dispatch(setAccountCreationStep(AccountCreation.Done)); } catch (err) { - let errorString = err.message || String(err); - // Note error substring is taken from libsession-util - if (err.message && err.message.includes('exceeds maximum length')) { - errorString = window.i18n('displayNameTooLong'); - } + const errorString = err.message || String(err); window.log.error( `[onboarding] create account: signUpWithDetails failed! Error: ${errorString}` ); dispatch(setAccountCreationStep(AccountCreation.DisplayName)); dispatch(setDisplayNameError(errorString)); - } finally { - await UserConfigWrapperActions.free(); } }; diff --git a/ts/components/registration/stages/RestoreAccount.tsx b/ts/components/registration/stages/RestoreAccount.tsx index 059dec23b..ba4bb3285 100644 --- a/ts/components/registration/stages/RestoreAccount.tsx +++ b/ts/components/registration/stages/RestoreAccount.tsx @@ -177,8 +177,6 @@ export const RestoreAccount = () => { } try { - // validate display name using libsession - // TODO [libsession validation] once you have it working in CreateAccount.tsx you will need to do it here const validName = await ProfileManager.updateOurProfileDisplayName(displayName, true); await signInWithNewDisplayName({ @@ -187,11 +185,7 @@ export const RestoreAccount = () => { dispatch, }); } catch (err) { - let errorString = err.message || String(err); - // Note error substring is taken from libsession-util - if (err.message && err.message.includes('exceeds maximum length')) { - errorString = window.i18n('displayNameTooLong'); - } + const errorString = err.message || String(err); window.log.error( `[onboarding] restore account: Failed with new display name! Error: ${errorString}` ); diff --git a/ts/session/profile_manager/ProfileManager.ts b/ts/session/profile_manager/ProfileManager.ts index fd957f6c1..b252d117f 100644 --- a/ts/session/profile_manager/ProfileManager.ts +++ b/ts/session/profile_manager/ProfileManager.ts @@ -99,8 +99,8 @@ export async function updateOurProfileDisplayName(newName: string, onboarding?: const cleanName = sanitizeSessionUsername(newName).trim(); if (onboarding) { - await UserConfigWrapperActions.setUserInfo(cleanName, CONVERSATION_PRIORITIES.default, null); - return cleanName; + const userInfoName = await UserConfigWrapperActions.setUserInfo(cleanName, CONVERSATION_PRIORITIES.default, null); + return userInfoName; } const ourNumber = UserUtils.getOurPubKeyStrFromCache(); @@ -120,9 +120,9 @@ export async function updateOurProfileDisplayName(newName: string, onboarding?: dbPriority, dbProfileUrl && dbProfileKey ? { - url: dbProfileUrl, - key: dbProfileKey, - } + url: dbProfileUrl, + key: dbProfileKey, + } : null );