diff --git a/ts/ConversationController.ts b/ts/ConversationController.ts index 3f663b87e1b..5d9f028de60 100644 --- a/ts/ConversationController.ts +++ b/ts/ConversationController.ts @@ -1146,7 +1146,9 @@ export class ConversationController { const profileKey = obsolete.get('profileKey'); if (profileKey) { - await current.setProfileKey(profileKey); + await current.setProfileKey(profileKey, { + reason: 'doCombineConversations ', + }); } } diff --git a/ts/background.ts b/ts/background.ts index 82a17647d9f..4ad18e33c2b 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -1744,7 +1744,9 @@ export async function startApp(): Promise { if (firstRun && profileKey) { const me = window.ConversationController.getOurConversation(); strictAssert(me !== undefined, "Didn't find newly created ourselves"); - await me.setProfileKey(Bytes.toBase64(profileKey)); + await me.setProfileKey(Bytes.toBase64(profileKey), { + reason: 'connect/firstRun', + }); } if (isBackupEnabled()) { @@ -2290,7 +2292,9 @@ export async function startApp(): Promise { if (sender) { // Will do the save for us - await sender.setProfileKey(profileKey); + await sender.setProfileKey(profileKey, { + reason: 'handleMessageReceivedProfileUpdate', + }); } return confirm(); @@ -2572,13 +2576,9 @@ export async function startApp(): Promise { return; } - log.info( - `${logId}: updating profileKey for ${idForLogging}`, - data.sourceAci, - data.source - ); - - const hasChanged = await conversation.setProfileKey(data.profileKey); + const hasChanged = await conversation.setProfileKey(data.profileKey, { + reason: `onProfileKey/${reason}`, + }); if (hasChanged) { drop(conversation.getProfiles()); @@ -2615,7 +2615,9 @@ export async function startApp(): Promise { ); // Will do the save for us if needed - await me.setProfileKey(profileKey); + await me.setProfileKey(profileKey, { + reason: 'handleMessageSentProfileUpdate', + }); return confirm(); } diff --git a/ts/groups.ts b/ts/groups.ts index 8797760c8c3..4f901e9a343 100644 --- a/ts/groups.ts +++ b/ts/groups.ts @@ -3191,7 +3191,7 @@ async function updateGroup( contact.get('profileKey') !== profileKey ) { contactsWithoutProfileKey.push(contact); - drop(contact.setProfileKey(profileKey)); + drop(contact.setProfileKey(profileKey, { reason: 'updateGroup' })); } } diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index 21a68ca8e89..f06a7eaee50 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -4913,8 +4913,12 @@ export class ConversationModel extends window.Backbone async setProfileKey( profileKey: string | undefined, - { viaStorageServiceSync = false } = {} + { + viaStorageServiceSync = false, + reason, + }: { viaStorageServiceSync?: boolean; reason: string } ): Promise { + const logId = `setProfileKey(${this.idForLogging()}/${reason})`; const oldProfileKey = this.get('profileKey'); // profileKey is a string so we can compare it directly @@ -4922,9 +4926,7 @@ export class ConversationModel extends window.Backbone return false; } - log.info( - `Setting sealedSender to UNKNOWN for conversation ${this.idForLogging()}` - ); + log.info(`${logId}: Profile key changed. Setting sealedSender to UNKNOWN`); this.set({ profileKeyCredential: null, profileKeyCredentialExpiration: null, @@ -4935,10 +4937,7 @@ export class ConversationModel extends window.Backbone // We messaged the contact when it had either phone number or username // title. if (this.get('needsTitleTransition')) { - log.info( - `setProfileKey(${this.idForLogging()}): adding a ` + - 'title transition notification' - ); + log.info(`${logId}: adding a title transition notification`); const { type, e164, username } = this.attributes; @@ -5040,7 +5039,7 @@ export class ConversationModel extends window.Backbone 'deriveProfileKeyVersion: Failed to derive profile key version, ' + 'clearing profile key.' ); - void this.setProfileKey(undefined); + void this.setProfileKey(undefined, { reason: 'deriveProfileKeyVersion' }); return; } diff --git a/ts/models/messages.ts b/ts/models/messages.ts index ac23dde003b..80d4cd8bff3 100644 --- a/ts/models/messages.ts +++ b/ts/models/messages.ts @@ -2008,14 +2008,22 @@ export class MessageModel extends window.Backbone.Model { ) { conversation.set({ profileSharing: true }); } else if (isDirectConversation(conversation.attributes)) { - void conversation.setProfileKey(profileKey); + drop( + conversation.setProfileKey(profileKey, { + reason: 'handleDataMessage', + }) + ); } else { const local = window.ConversationController.lookupOrCreate({ e164: source, serviceId: sourceServiceId, reason: 'handleDataMessage:setProfileKey', }); - void local?.setProfileKey(profileKey); + drop( + local?.setProfileKey(profileKey, { + reason: 'handleDataMessage', + }) + ); } } diff --git a/ts/services/profiles.ts b/ts/services/profiles.ts index 690a3c6977b..bbcdd11ccfc 100644 --- a/ts/services/profiles.ts +++ b/ts/services/profiles.ts @@ -329,7 +329,12 @@ async function doGetProfile(c: ConversationModel): Promise { throw error; } - await c.setProfileKey(undefined); + log.warn( + `getProfile: Got 401/403 when using accessKey for ${idForLogging}, removing profileKey` + ); + await c.setProfileKey(undefined, { + reason: 'doGetProfile/accessKey/401+403', + }); // Retry fetch using last known profileKeyVersion or fetch // unversioned profile. @@ -555,7 +560,9 @@ async function doGetProfile(c: ConversationModel): Promise { `getProfile: Got 401/403 when using accessKey for ${idForLogging}, removing profileKey` ); if (!isMe(c.attributes)) { - await c.setProfileKey(undefined); + await c.setProfileKey(undefined, { + reason: 'doGetProfile/accessKey/401+403', + }); } } if (c.get('sealedSender') === SEALED_SENDER.UNKNOWN) { diff --git a/ts/services/storageRecordOps.ts b/ts/services/storageRecordOps.ts index fc2b2549780..b894b32ddf2 100644 --- a/ts/services/storageRecordOps.ts +++ b/ts/services/storageRecordOps.ts @@ -1137,7 +1137,7 @@ export async function mergeContactRecord( if (contactRecord.profileKey && contactRecord.profileKey.length > 0) { needsProfileFetch = await conversation.setProfileKey( Bytes.toBase64(contactRecord.profileKey), - { viaStorageServiceSync: true } + { viaStorageServiceSync: true, reason: 'mergeContactRecord' } ); } @@ -1660,7 +1660,7 @@ export async function mergeAccountRecord( if (profileKey && profileKey.length > 0) { needsProfileFetch = await conversation.setProfileKey( Bytes.toBase64(profileKey), - { viaStorageServiceSync: true } + { viaStorageServiceSync: true, reason: 'mergeAccountRecord' } ); const avatarUrl = dropNull(accountRecord.avatarUrl); diff --git a/ts/util/handleMessageSend.ts b/ts/util/handleMessageSend.ts index 5385efc61d5..c00d2ccae83 100644 --- a/ts/util/handleMessageSend.ts +++ b/ts/util/handleMessageSend.ts @@ -115,7 +115,9 @@ function processError(error: unknown): void { `handleMessageSend: Got 401/403 for ${conversation.idForLogging()}, removing profile key` ); - void conversation.setProfileKey(undefined); + void conversation.setProfileKey(undefined, { + reason: 'handleMessageSend/processError', + }); } if (conversation.get('sealedSender') === SEALED_SENDER.UNKNOWN) { log.warn(