From f287f9a3a45d11a55c55ecc2a99c078a42667eef Mon Sep 17 00:00:00 2001 From: William Grant Date: Fri, 16 Jun 2023 13:54:51 +1000 Subject: [PATCH] fix: clean up and typing --- .../conversations/ConversationController.ts | 138 +++++++++--------- 1 file changed, 70 insertions(+), 68 deletions(-) diff --git a/ts/session/conversations/ConversationController.ts b/ts/session/conversations/ConversationController.ts index 2f93256b8..93e944b99 100644 --- a/ts/session/conversations/ConversationController.ts +++ b/ts/session/conversations/ConversationController.ts @@ -42,6 +42,8 @@ export const getConversationController = () => { return instance; }; +type DeleteOptions = { fromSyncMessage: boolean }; + export class ConversationController { private readonly conversations: ConversationCollection; private _initialFetchComplete: boolean = false; @@ -176,38 +178,58 @@ export class ConversationController { }); } - /** - * Usually, we want to mark private contact deleted as inactive (active_at = undefined). - * That way we can still have the username and avatar for them, but they won't appear in search results etc. - * For the blinded contact deletion though, we want to delete it completely because we merged it to an unblinded convo. - */ - public async deleteBlindedContact(blindedId: string) { - if (!this._initialFetchComplete) { - throw new Error( - 'getConversationController().deleteBlindedContact() needs complete initial fetch' - ); - } - if (!PubKey.hasBlindedPrefix(blindedId)) { - throw new Error('deleteBlindedContact allow accepts blinded id'); - } - window.log.info(`deleteBlindedContact with ${blindedId}`); - const conversation = this.conversations.get(blindedId); - if (!conversation) { - window.log.warn(`deleteBlindedContact no such convo ${blindedId}`); + public async delete1o1( + id: string, + options: DeleteOptions & { justHidePrivate?: boolean; keepMessages?: boolean } + ) { + const conversation = await this.deleteConvoInitialChecks(id, '1o1', options?.keepMessages); + + if (!conversation || !conversation.isPrivate()) { return; } - // we remove the messages left in this convo. The caller has to merge them if needed - await deleteAllMessagesByConvoIdNoConfirmation(conversation.id); + window.log.debug(`WIP: delete1o1 keepMessages ${id} ${options.keepMessages}`); - await conversation.setIsApproved(false, false); - await conversation.setDidApproveMe(false, false); - await conversation.commit(); + if (options.justHidePrivate || isNil(options.justHidePrivate) || conversation.isMe()) { + window.log.debug(`WIP: delete1o1 justHide ${id}`); + // we just set the hidden field to true + // so the conversation still exists (needed for that user's profile in groups) but is not shown on the list of conversation. + window.log.info(`deleteContact isPrivate, marking as hidden: ${id}`); + conversation.set({ + priority: CONVERSATION_PRIORITIES.hidden, + }); + // We don't remove entries from the contacts wrapper, so better keep corresponding convo volatile info for now (it will be pruned if needed) + await conversation.commit(); // this updates the wrappers content to reflect the hidden state + } else { + window.log.debug(`WIP: delete1o1 delete ${id}`); + window.log.info(`deleteContact isPrivate, reset fields and removing from wrapper: ${id}`); + + await conversation.setIsApproved(false, false); + await conversation.setDidApproveMe(false, false); + conversation.set('active_at', 0); + await BlockedNumberController.unblockAll([conversation.id]); + await conversation.commit(); // first commit to DB so the DB knows about the changes + if (SessionUtilContact.isContactToStoreInWrapper(conversation)) { + window.log.warn('isContactToStoreInWrapper still true for ', conversation.attributes); + } + if (conversation.id.startsWith('05')) { + // make sure to filter blinded contacts as it will throw otherwise + await SessionUtilContact.removeContactFromWrapper(conversation.id); // then remove the entry alltogether from the wrapper + await SessionUtilConvoInfoVolatile.removeContactFromWrapper(conversation.id); + } + if (getCurrentlySelectedConversationOutsideRedux() === conversation.id) { + window.inboxStore?.dispatch(resetConversationExternal()); + } + } + + if (!options.fromSyncMessage) { + await ConfigurationSync.queueNewJobIfNeeded(); + } } public async deleteClosedGroup( groupId: string, - options: { fromSyncMessage: boolean; sendLeaveMessage: boolean } + options: DeleteOptions & { sendLeaveMessage: boolean } ) { const conversation = await this.deleteConvoInitialChecks(groupId, 'LegacyGroup'); if (!conversation || !conversation.isClosedGroup()) { @@ -230,7 +252,7 @@ export class ConversationController { } } - public async deleteCommunity(convoId: string, options: { fromSyncMessage: boolean }) { + public async deleteCommunity(convoId: string, options: DeleteOptions) { const conversation = await this.deleteConvoInitialChecks(convoId, 'Community'); if (!conversation || !conversation.isPublic()) { return; @@ -249,53 +271,33 @@ export class ConversationController { } } - public async delete1o1( - id: string, - options: { fromSyncMessage: boolean; justHidePrivate?: boolean; keepMessages?: boolean } - ) { - const conversation = await this.deleteConvoInitialChecks(id, '1o1', options?.keepMessages); - - if (!conversation || !conversation.isPrivate()) { + /** + * Usually, we want to mark private contact deleted as inactive (active_at = undefined). + * That way we can still have the username and avatar for them, but they won't appear in search results etc. + * For the blinded contact deletion though, we want to delete it completely because we merged it to an unblinded convo. + */ + public async deleteBlindedContact(blindedId: string) { + if (!this._initialFetchComplete) { + throw new Error( + 'getConversationController().deleteBlindedContact() needs complete initial fetch' + ); + } + if (!PubKey.hasBlindedPrefix(blindedId)) { + throw new Error('deleteBlindedContact allow accepts blinded id'); + } + window.log.info(`deleteBlindedContact with ${blindedId}`); + const conversation = this.conversations.get(blindedId); + if (!conversation) { + window.log.warn(`deleteBlindedContact no such convo ${blindedId}`); return; } - window.log.debug(`WIP: delete1o1 keepMessages ${id} ${options.keepMessages}`); - - if (options.justHidePrivate || isNil(options.justHidePrivate) || conversation.isMe()) { - window.log.debug(`WIP: delete1o1 justHide ${id}`); - // we just set the hidden field to true - // so the conversation still exists (needed for that user's profile in groups) but is not shown on the list of conversation. - window.log.info(`deleteContact isPrivate, marking as hidden: ${id}`); - conversation.set({ - priority: CONVERSATION_PRIORITIES.hidden, - }); - // We don't remove entries from the contacts wrapper, so better keep corresponding convo volatile info for now (it will be pruned if needed) - await conversation.commit(); // this updates the wrappers content to reflect the hidden state - } else { - window.log.debug(`WIP: delete1o1 delete ${id}`); - window.log.info(`deleteContact isPrivate, reset fields and removing from wrapper: ${id}`); - - await conversation.setIsApproved(false, false); - await conversation.setDidApproveMe(false, false); - conversation.set('active_at', 0); - await BlockedNumberController.unblockAll([conversation.id]); - await conversation.commit(); // first commit to DB so the DB knows about the changes - if (SessionUtilContact.isContactToStoreInWrapper(conversation)) { - window.log.warn('isContactToStoreInWrapper still true for ', conversation.attributes); - } - if (conversation.id.startsWith('05')) { - // make sure to filter blinded contacts as it will throw otherwise - await SessionUtilContact.removeContactFromWrapper(conversation.id); // then remove the entry alltogether from the wrapper - await SessionUtilConvoInfoVolatile.removeContactFromWrapper(conversation.id); - } - if (getCurrentlySelectedConversationOutsideRedux() === conversation.id) { - window.inboxStore?.dispatch(resetConversationExternal()); - } - } + // we remove the messages left in this convo. The caller has to merge them if needed + await deleteAllMessagesByConvoIdNoConfirmation(conversation.id); - if (!options.fromSyncMessage) { - await ConfigurationSync.queueNewJobIfNeeded(); - } + await conversation.setIsApproved(false, false); + await conversation.setDidApproveMe(false, false); + await conversation.commit(); } /**