@ -1,4 +1,4 @@
import { isEmpty } from 'lodash' ;
import { isEmpty , isNil } from 'lodash' ;
import { setLastProfileUpdateTimestamp } from '../../util/storage' ;
import { setLastProfileUpdateTimestamp } from '../../util/storage' ;
import { UserConfigWrapperActions } from '../../webworker/workers/browser/libsession_worker_interface' ;
import { UserConfigWrapperActions } from '../../webworker/workers/browser/libsession_worker_interface' ;
import { getConversationController } from '../conversations' ;
import { getConversationController } from '../conversations' ;
@ -95,26 +95,38 @@ async function updateProfileOfContact(
}
}
}
}
export async function updateOurProfileDisplayName ( newName : string , onboarding? : true ) {
/ * *
* This will throw if the display name given is too long .
* When registering a user / linking a device , we want to enforce a limit on the displayName length .
* That limit is enforced by libsession when calling ` setName ` on the ` UserConfigWrapper ` .
* ` updateOurProfileDisplayNameOnboarding ` is used to create a temporary ` UserConfigWrapper ` , call ` setName ` on it and release the memory used by the wrapper .
* @returns the set displayName set if no error where thrown .
* /
async function updateOurProfileDisplayNameOnboarding ( newName : string ) {
const cleanName = sanitizeSessionUsername ( newName ) . trim ( ) ;
const cleanName = sanitizeSessionUsername ( newName ) . trim ( ) ;
if ( onboarding ) {
try {
try {
// create a temp user config wrapper to test the display name with libsession
// create a temp user config wrapper to test the display name with libsession
const privKey = new Uint8Array ( 64 ) ;
const privKey = new Uint8Array ( 64 ) ;
crypto . getRandomValues ( privKey ) ;
crypto . getRandomValues ( privKey ) ;
await UserConfigWrapperActions . init ( privKey , null ) ;
await UserConfigWrapperActions . init ( privKey , null ) ;
// this throws if the name is too long
const userInfoName = await UserConfigWrapperActions . setUserInfo (
await UserConfigWrapperActions . setName ( cleanName ) ;
cleanName ,
const appliedName = await UserConfigWrapperActions . getName ( ) ;
CONVERSATION_PRIORITIES . default ,
null
if ( isNil ( appliedName ) ) {
throw new Error (
'updateOurProfileDisplayNameOnboarding failed to retrieve name after setting it'
) ;
) ;
return userInfoName ;
} finally {
await UserConfigWrapperActions . free ( ) ;
}
}
return appliedName ;
} finally {
await UserConfigWrapperActions . free ( ) ;
}
}
}
async function updateOurProfileDisplayName ( newName : string ) {
const ourNumber = UserUtils . getOurPubKeyStrFromCache ( ) ;
const ourNumber = UserUtils . getOurPubKeyStrFromCache ( ) ;
const conversation = await getConversationController ( ) . getOrCreateAndWait (
const conversation = await getConversationController ( ) . getOrCreateAndWait (
ourNumber ,
ourNumber ,
@ -127,29 +139,32 @@ export async function updateOurProfileDisplayName(newName: string, onboarding?:
: null ;
: null ;
const dbPriority = conversation . get ( 'priority' ) || CONVERSATION_PRIORITIES . default ;
const dbPriority = conversation . get ( 'priority' ) || CONVERSATION_PRIORITIES . default ;
await UserConfigWrapperActions . setUserInfo (
// we don't want to throw if somehow our display name in the DB is too long here, so we use the truncated version.
cleanName ,
await UserConfigWrapperActions . setNameTruncated ( sanitizeSessionUsername ( newName ) . trim ( ) ) ;
dbPriority ,
const truncatedName = await UserConfigWrapperActions . getName ( ) ;
dbProfileUrl && dbProfileKey
if ( isNil ( truncatedName ) ) {
? {
throw new Error ( 'updateOurProfileDisplayName: failed to get truncated displayName back' ) ;
url : dbProfileUrl ,
}
key : dbProfileKey ,
await UserConfigWrapperActions . setPriority ( dbPriority ) ;
}
if ( dbProfileUrl && ! isEmpty ( dbProfileKey ) ) {
: null
await UserConfigWrapperActions . setProfilePic ( { key : dbProfileKey , url : dbProfileUrl } ) ;
) ;
} else {
await UserConfigWrapperActions . setProfilePic ( { key : null , url : null } ) ;
}
conversation . setSessionDisplayNameNoCommit ( newName ) ;
conversation . setSessionDisplayNameNoCommit ( truncated Name) ;
// might be good to not trigger a sync if the name did not change
// might be good to not trigger a sync if the name did not change
await conversation . commit ( ) ;
await conversation . commit ( ) ;
await setLastProfileUpdateTimestamp ( Date . now ( ) ) ;
await setLastProfileUpdateTimestamp ( Date . now ( ) ) ;
await SyncUtils . forceSyncConfigurationNowIfNeeded ( true ) ;
await SyncUtils . forceSyncConfigurationNowIfNeeded ( true ) ;
return clean Name;
return truncated Name;
}
}
export const ProfileManager = {
export const ProfileManager = {
updateOurProfileSync ,
updateOurProfileSync ,
updateProfileOfContact ,
updateProfileOfContact ,
updateOurProfileDisplayName ,
updateOurProfileDisplayName ,
updateOurProfileDisplayNameOnboarding ,
} ;
} ;