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.
session-desktop/ts/session/utils/libsession/libsession_utils_user_profi...

43 lines
1.4 KiB
TypeScript

import { isEmpty } from 'lodash';
import { UserUtils } from '..';
import { UserConfigWrapperActions } from '../../../webworker/workers/browser/libsession_worker_interface';
import { getConversationController } from '../../conversations';
import { fromHexToArray } from '../String';
async function insertUserProfileIntoWrapper(convoId: string) {
if (!isUserProfileToStoreInContactsWrapper(convoId)) {
return;
}
const us = UserUtils.getOurPubKeyStrFromCache();
const ourConvo = getConversationController().get(us);
if (!ourConvo) {
throw new Error('insertUserProfileIntoWrapper needs a ourConvo to exist');
}
const dbName = ourConvo.get('displayNameInProfile') || '';
const dbProfileUrl = ourConvo.get('avatarPointer') || '';
const dbProfileKey = fromHexToArray(ourConvo.get('profileKey') || '');
await UserConfigWrapperActions.setName(dbName);
if (dbProfileUrl && !isEmpty(dbProfileKey)) {
await UserConfigWrapperActions.setProfilePicture(dbProfileUrl, dbProfileKey);
} else {
await UserConfigWrapperActions.setProfilePicture('', new Uint8Array());
}
}
function isUserProfileToStoreInContactsWrapper(convoId: string) {
try {
const us = UserUtils.getOurPubKeyStrFromCache();
return convoId === us;
} catch (e) {
return false;
}
}
export const SessionUtilUserProfile = {
insertUserProfileIntoWrapper,
isUserProfileToStoreInContactsWrapper,
};