You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			66 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			66 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			TypeScript
		
	
import { isEmpty } from 'lodash';
 | 
						|
import { getConversationController } from '../conversations';
 | 
						|
import { UserUtils } from '../utils';
 | 
						|
import { AvatarDownload } from '../utils/job_runners/jobs/AvatarDownloadJob';
 | 
						|
import { toHex } from '../utils/String';
 | 
						|
 | 
						|
/**
 | 
						|
 * This can be used to update our conversation display name with the given name right away, and plan an AvatarDownloadJob to retrieve the new avatar if needed to download it
 | 
						|
 */
 | 
						|
async function updateOurProfileSync(
 | 
						|
  displayName: string | undefined,
 | 
						|
  profileUrl: string | null,
 | 
						|
  profileKey: Uint8Array | null,
 | 
						|
  priority: number | null
 | 
						|
) {
 | 
						|
  const us = UserUtils.getOurPubKeyStrFromCache();
 | 
						|
  const ourConvo = getConversationController().get(us);
 | 
						|
  if (!ourConvo?.id) {
 | 
						|
    window?.log?.warn('[profileupdate] Cannot update our profile without convo associated');
 | 
						|
    return;
 | 
						|
  }
 | 
						|
 | 
						|
  await updateProfileOfContact(us, displayName, profileUrl, profileKey);
 | 
						|
  if (priority !== null && ourConvo.get('priority') !== priority) {
 | 
						|
    ourConvo.set('priority', priority);
 | 
						|
    await ourConvo.commit();
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * This can be used to update the display name of the given pubkey right away, and plan an AvatarDownloadJob to retrieve the new avatar if needed to download it.
 | 
						|
 */
 | 
						|
async function updateProfileOfContact(
 | 
						|
  pubkey: string,
 | 
						|
  displayName: string | null | undefined,
 | 
						|
  profileUrl: string | null | undefined,
 | 
						|
  profileKey: Uint8Array | null | undefined
 | 
						|
) {
 | 
						|
  const conversation = getConversationController().get(pubkey);
 | 
						|
 | 
						|
  if (!conversation || !conversation.isPrivate()) {
 | 
						|
    window.log.warn('updateProfileOfContact can only be used for existing and private convos');
 | 
						|
    return;
 | 
						|
  }
 | 
						|
 | 
						|
  const existingDisplayName = conversation.get('displayNameInProfile');
 | 
						|
 | 
						|
  // avoid setting the display name to an invalid value
 | 
						|
  if (existingDisplayName !== displayName && !isEmpty(displayName)) {
 | 
						|
    conversation.set('displayNameInProfile', displayName || undefined);
 | 
						|
    await conversation.commit();
 | 
						|
  }
 | 
						|
  // add an avatar download job only if needed
 | 
						|
  const profileKeyHex = !profileKey || isEmpty(profileKey) ? null : toHex(profileKey);
 | 
						|
  await AvatarDownload.addAvatarDownloadJobIfNeeded({
 | 
						|
    profileKeyHex,
 | 
						|
    profileUrl,
 | 
						|
    pubkey,
 | 
						|
  });
 | 
						|
}
 | 
						|
 | 
						|
export const ProfileManager = {
 | 
						|
  updateOurProfileSync,
 | 
						|
  updateProfileOfContact,
 | 
						|
};
 |