Skip to content

Commit 292e700

Browse files
committed
fix: use reupload and new upload nts profile changes from libsession
1 parent bc8eb93 commit 292e700

File tree

5 files changed

+51
-66
lines changed

5 files changed

+51
-66
lines changed

ts/interactions/avatar-interactions/nts-avatar-interactions.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { encryptProfile } from '../../util/crypto/profileEncrypter';
1212
import { Storage } from '../../util/storage';
1313
import type { ConversationModel } from '../../models/conversation';
1414
import { processAvatarData } from '../../util/avatar/processAvatarData';
15+
import { UserConfigWrapperActions } from '../../webworker/workers/browser/libsession_worker_interface';
1516

1617
/**
1718
* This function can be used for reupload our avatar to the file server.
@@ -61,17 +62,20 @@ export async function reuploadCurrentAvatarUs() {
6162
decryptedAvatarData,
6263
ourConvo,
6364
profileKey,
65+
context: 'reuploadAvatar',
6466
});
6567
}
6668

6769
export async function uploadAndSetOurAvatarShared({
6870
decryptedAvatarData,
6971
ourConvo,
7072
profileKey,
73+
context,
7174
}: {
7275
ourConvo: ConversationModel;
7376
decryptedAvatarData: ArrayBuffer;
7477
profileKey: Uint8Array;
78+
context: 'uploadNewAvatar' | 'reuploadAvatar';
7579
}) {
7680
if (!decryptedAvatarData?.byteLength) {
7781
window.log.warn('uploadAndSetOurAvatarShared: avatar content is empty');
@@ -106,19 +110,28 @@ export async function uploadAndSetOurAvatarShared({
106110
})
107111
: null;
108112

109-
const displayName = ourConvo.getRealSessionUsername();
110-
111113
// Replace our temporary image with the attachment pointer from the server.
112114
// Note: this commits already to the DB.
113115
await ourConvo.setSessionProfile({
114116
avatarPath: savedMainAvatar.path,
115117
fallbackAvatarPath: processedFallbackAvatar?.path || savedMainAvatar.path,
116-
displayName,
118+
displayName: null,
117119
avatarPointer: fileUrl,
118120
type: 'setAvatarDownloadedPrivate',
119121
profileKey,
120122
});
121123
await Storage.put(SettingsKey.ntsAvatarExpiryMs, expiresMs);
124+
if (context === 'uploadNewAvatar') {
125+
await UserConfigWrapperActions.setNewProfilePic({
126+
key: profileKey,
127+
url: fileUrl,
128+
});
129+
} else if (context === 'reuploadAvatar') {
130+
await UserConfigWrapperActions.setReuploadProfilePic({
131+
key: profileKey,
132+
url: fileUrl,
133+
});
134+
}
122135

123136
return {
124137
avatarPointer: ourConvo.getAvatarPointer(),

ts/models/conversation.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ import {
111111
import { ReadReceiptMessage } from '../session/messages/outgoing/controlMessage/receipt/ReadReceiptMessage';
112112
import { PreConditionFailed } from '../session/utils/errors';
113113
import { LibSessionUtil } from '../session/utils/libsession/libsession_utils';
114-
import { SessionUtilUserProfile } from '../session/utils/libsession/libsession_utils_user_profile';
115114
import { ReduxSogsRoomInfos } from '../state/ducks/sogsRoomInfo';
116115
import {
117116
selectLibGroupAdminsOutsideRedux,
@@ -1835,10 +1834,35 @@ export class ConversationModel extends Model<ConversationAttributes> {
18351834
* Note: Currently, we do not have an order in the list of pinned conversation, but the libsession util wrapper can handle the order.
18361835
*/
18371836
public async togglePinned(shouldCommit: boolean = true) {
1838-
this.set({ priority: this.isPinned() ? 0 : 1 });
1837+
const pinnedBefore = this.isPinned();
1838+
const newPriority = pinnedBefore
1839+
? CONVERSATION_PRIORITIES.default
1840+
: CONVERSATION_PRIORITIES.pinned;
1841+
this.set({
1842+
priority: newPriority,
1843+
});
18391844
if (shouldCommit) {
18401845
await this.commit();
18411846
}
1847+
const convoType = this.getConvoType();
1848+
switch (convoType) {
1849+
case ConvoTypeNarrow.blindedAcquaintance:
1850+
case ConvoTypeNarrow.blindedContact:
1851+
case ConvoTypeNarrow.privateAcquaintance:
1852+
case ConvoTypeNarrow.community:
1853+
case ConvoTypeNarrow.legacyGroup:
1854+
case ConvoTypeNarrow.group:
1855+
// all of the above do not support being hidden (only removed)
1856+
break;
1857+
case ConvoTypeNarrow.nts:
1858+
await UserConfigWrapperActions.setPriority(newPriority);
1859+
break;
1860+
case ConvoTypeNarrow.contact:
1861+
// TODO add cases here for contacts to handle the other cases
1862+
break;
1863+
default:
1864+
assertUnreachable(convoType, `convoType ${convoType} is not supported`);
1865+
}
18421866
return true;
18431867
}
18441868

@@ -3093,10 +3117,8 @@ async function commitConversationAndRefreshWrapper(id: string) {
30933117

30943118
switch (variant) {
30953119
case 'UserConfig':
3096-
if (SessionUtilUserProfile.isUserProfileToStoreInWrapper(convo.id)) {
3097-
// eslint-disable-next-line no-await-in-loop
3098-
await SessionUtilUserProfile.insertUserProfileIntoWrapper(convo.id);
3099-
}
3120+
// Note: nothing to do here, the NTS changes in libsession are now done
3121+
// when done through the UI
31003122
break;
31013123
case 'ContactsConfig':
31023124
if (SessionUtilContact.isContactToStoreInWrapper(convo)) {
Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,4 @@
1-
import { isEmpty } from 'lodash';
21
import { UserUtils } from '..';
3-
import { SettingsKey } from '../../../data/settings-key';
4-
import { CONVERSATION_PRIORITIES } from '../../../models/types';
5-
import { stringify } from '../../../types/sqlSharedTypes';
6-
import { Storage } from '../../../util/storage';
7-
import { UserConfigWrapperActions } from '../../../webworker/workers/browser/libsession_worker_interface';
8-
import { ConvoHub } from '../../conversations';
9-
import { fromHexToArray } from '../String';
10-
11-
async function insertUserProfileIntoWrapper(convoId: string) {
12-
if (!SessionUtilUserProfile.isUserProfileToStoreInWrapper(convoId)) {
13-
return null;
14-
}
15-
const us = UserUtils.getOurPubKeyStrFromCache();
16-
const ourConvo = ConvoHub.use().get(us);
17-
18-
if (!ourConvo) {
19-
throw new Error('insertUserProfileIntoWrapper needs a ourConvo to exist');
20-
}
21-
22-
const dbName = ourConvo.getRealSessionUsername() || '';
23-
const dbProfileUrl = ourConvo.getAvatarPointer() || '';
24-
const dbProfileKey = fromHexToArray(ourConvo.getProfileKey() || '');
25-
const priority = ourConvo.get('priority') || CONVERSATION_PRIORITIES.default; // this has to be a direct call to .get() and not getPriority()
26-
27-
const areBlindedMsgRequestEnabled = !!Storage.get(SettingsKey.hasBlindedMsgRequestsEnabled);
28-
29-
const expirySeconds = ourConvo.getExpireTimer() || 0;
30-
window.log.debug(
31-
`inserting into userprofile wrapper: username:"${dbName}", priority:${priority} image:${JSON.stringify(
32-
{ url: dbProfileUrl, key: stringify(dbProfileKey) }
33-
)}, settings: ${JSON.stringify({ areBlindedMsgRequestEnabled, expirySeconds })}`
34-
);
35-
36-
// we don't want to throw if somehow our display name in the DB is too long here, so we use the truncated version.
37-
await UserConfigWrapperActions.setNameTruncated(dbName);
38-
await UserConfigWrapperActions.setPriority(priority);
39-
if (dbProfileUrl && !isEmpty(dbProfileKey) && dbProfileKey.length === 32) {
40-
await UserConfigWrapperActions.setProfilePic({ key: dbProfileKey, url: dbProfileUrl });
41-
} else {
42-
await UserConfigWrapperActions.setProfilePic({ key: null, url: null });
43-
}
44-
45-
await UserConfigWrapperActions.setEnableBlindedMsgRequest(areBlindedMsgRequestEnabled);
46-
await UserConfigWrapperActions.setNoteToSelfExpiry(expirySeconds);
47-
48-
// returned for testing purposes only
49-
return {
50-
id: convoId,
51-
name: dbName,
52-
priority,
53-
avatarPointer: dbProfileUrl,
54-
expirySeconds,
55-
};
56-
}
572

583
function isUserProfileToStoreInWrapper(convoId: string) {
594
try {
@@ -64,6 +9,5 @@ function isUserProfileToStoreInWrapper(convoId: string) {
649
}
6510

6611
export const SessionUtilUserProfile = {
67-
insertUserProfileIntoWrapper,
6812
isUserProfileToStoreInWrapper,
6913
};

ts/state/ducks/user.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { uploadAndSetOurAvatarShared } from '../../interactions/avatar-interacti
77
import { ed25519Str } from '../../session/utils/String';
88
import { userSettingsModal, updateEditProfilePictureModal } from './modalDialog';
99
import { NetworkTime } from '../../util/NetworkTime';
10+
import { UserConfigWrapperActions } from '../../webworker/workers/browser/libsession_worker_interface';
1011

1112
export type UserStateType = {
1213
ourDisplayNameInProfile: string;
@@ -44,6 +45,7 @@ const updateOurAvatar = createAsyncThunk(
4445
decryptedAvatarData: mainAvatarDecrypted,
4546
ourConvo,
4647
profileKey,
48+
context: 'uploadNewAvatar',
4749
});
4850

4951
if (res) {
@@ -83,6 +85,10 @@ const clearOurAvatar = createAsyncThunk('user/clearOurAvatar', async () => {
8385
displayName: null,
8486
profileUpdatedAtSeconds: NetworkTime.nowSeconds(),
8587
});
88+
await UserConfigWrapperActions.setNewProfilePic({
89+
url: null,
90+
key: null,
91+
});
8692

8793
await SyncUtils.forceSyncConfigurationNowIfNeeded(true);
8894
window.inboxStore?.dispatch(updateEditProfilePictureModal(null));

ts/state/selectors/modal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,5 @@ export function useModalStack() {
122122

123123
export function useIsTopModal(modalId: ModalId) {
124124
const modalStack = useModalStack();
125-
return modalStack.length === 0 || modalStack[modalStack.length - 1] === modalId;
125+
return !modalStack?.length || modalStack[modalStack.length - 1] === modalId;
126126
}

0 commit comments

Comments
 (0)