From 717c90c58bf3da35c6905268f0aea990ea1fcb32 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Fri, 7 Apr 2023 17:41:07 +0400 Subject: [PATCH] mobile: remove cancelled files (#2154) --- .../java/chat/simplex/app/model/SimpleXAPI.kt | 48 +++++++++++-------- apps/ios/Shared/Model/SimpleXAPI.swift | 18 ++----- .../ios/SimpleX NSE/NotificationService.swift | 12 +++++ apps/ios/SimpleXChat/FileUtils.swift | 15 ++++++ 4 files changed, 59 insertions(+), 34 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt index 90ff215dd3..38927e4ea3 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt @@ -1002,6 +1002,7 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a val chatItem = apiCancelFile(fileId) if (chatItem != null) { chatItemSimpleUpdate(user, chatItem) + cleanupFile(chatItem) } } @@ -1417,40 +1418,27 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a chatItemSimpleUpdate(r.user, r.chatItem) is CR.RcvFileComplete -> chatItemSimpleUpdate(r.user, r.chatItem) - is CR.RcvFileSndCancelled -> + is CR.RcvFileSndCancelled -> { chatItemSimpleUpdate(r.user, r.chatItem) + cleanupFile(r.chatItem) + } is CR.RcvFileProgressXFTP -> chatItemSimpleUpdate(r.user, r.chatItem) is CR.SndFileStart -> chatItemSimpleUpdate(r.user, r.chatItem) is CR.SndFileComplete -> { chatItemSimpleUpdate(r.user, r.chatItem) - val cItem = r.chatItem.chatItem - val mc = cItem.content.msgContent - val fileName = cItem.file?.fileName - if ( - r.chatItem.chatInfo.chatType == ChatType.Direct - && mc is MsgContent.MCFile - && fileName != null - ) { - removeFile(appContext, fileName) - } + cleanupDirectFile(r.chatItem) } - is CR.SndFileRcvCancelled -> + is CR.SndFileRcvCancelled -> { chatItemSimpleUpdate(r.user, r.chatItem) + cleanupDirectFile(r.chatItem) + } is CR.SndFileProgressXFTP -> chatItemSimpleUpdate(r.user, r.chatItem) is CR.SndFileCompleteXFTP -> { chatItemSimpleUpdate(r.user, r.chatItem) - val cItem = r.chatItem.chatItem - val mc = cItem.content.msgContent - val fileName = cItem.file?.fileName - if ( - mc is MsgContent.MCFile - && fileName != null - ) { - removeFile(appContext, fileName) - } + cleanupFile(r.chatItem) } is CR.CallInvitation -> { chatModel.callManager.reportNewIncomingCall(r.callInvitation) @@ -1502,6 +1490,24 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a } } + private fun cleanupDirectFile(aChatItem: AChatItem) { + if (aChatItem.chatInfo.chatType == ChatType.Direct) { + cleanupFile(aChatItem) + } + } + + private fun cleanupFile(aChatItem: AChatItem) { + val cItem = aChatItem.chatItem + val mc = cItem.content.msgContent + val fileName = cItem.file?.fileName + if ( + mc is MsgContent.MCFile + && fileName != null + ) { + removeFile(appContext, fileName) + } + } + private fun active(user: User): Boolean = user.userId == chatModel.currentUser.value?.userId private fun withCall(r: CR, contact: Contact, perform: (Call) -> Unit) { diff --git a/apps/ios/Shared/Model/SimpleXAPI.swift b/apps/ios/Shared/Model/SimpleXAPI.swift index ffaed725b6..f4e8fe8c02 100644 --- a/apps/ios/Shared/Model/SimpleXAPI.swift +++ b/apps/ios/Shared/Model/SimpleXAPI.swift @@ -745,6 +745,7 @@ func apiReceiveFile(fileId: Int64, inline: Bool? = nil) async -> AChatItem? { func cancelFile(user: User, fileId: Int64) async { if let chatItem = await apiCancelFile(fileId: fileId) { DispatchQueue.main.async { chatItemSimpleUpdate(user, chatItem) } + cleanupFile(chatItem) } } @@ -1329,31 +1330,22 @@ func processReceivedMsg(_ res: ChatResponse) async { chatItemSimpleUpdate(user, aChatItem) case let .rcvFileSndCancelled(user, aChatItem, _): chatItemSimpleUpdate(user, aChatItem) + cleanupFile(aChatItem) case let .rcvFileProgressXFTP(user, aChatItem, _, _): chatItemSimpleUpdate(user, aChatItem) case let .sndFileStart(user, aChatItem, _): chatItemSimpleUpdate(user, aChatItem) case let .sndFileComplete(user, aChatItem, _): chatItemSimpleUpdate(user, aChatItem) - let cItem = aChatItem.chatItem - let mc = cItem.content.msgContent - if aChatItem.chatInfo.chatType == .direct, - case .file = mc, - let fileName = cItem.file?.filePath { - removeFile(fileName) - } + cleanupDirectFile(aChatItem) case let .sndFileRcvCancelled(user, aChatItem, _): chatItemSimpleUpdate(user, aChatItem) + cleanupDirectFile(aChatItem) case let .sndFileProgressXFTP(user, aChatItem, _, _, _): chatItemSimpleUpdate(user, aChatItem) case let .sndFileCompleteXFTP(user, aChatItem, _): chatItemSimpleUpdate(user, aChatItem) - let cItem = aChatItem.chatItem - let mc = cItem.content.msgContent - if case .file = mc, - let fileName = cItem.file?.filePath { - removeFile(fileName) - } + cleanupFile(aChatItem) case let .callInvitation(invitation): m.callInvitations[invitation.contact.id] = invitation activateCall(invitation) diff --git a/apps/ios/SimpleX NSE/NotificationService.swift b/apps/ios/SimpleX NSE/NotificationService.swift index 52a44ef25d..10c9a5945d 100644 --- a/apps/ios/SimpleX NSE/NotificationService.swift +++ b/apps/ios/SimpleX NSE/NotificationService.swift @@ -292,6 +292,18 @@ func receivedMsgNtf(_ res: ChatResponse) async -> (String, NSENotification)? { } } return cItem.showMutableNotification ? (aChatItem.chatId, .nse(notification: createMessageReceivedNtf(user, cInfo, cItem))) : nil + case let .rcvFileSndCancelled(_, aChatItem, _): + cleanupFile(aChatItem) + return nil + case let .sndFileComplete(_, aChatItem, _): + cleanupDirectFile(aChatItem) + return nil + case let .sndFileRcvCancelled(_, aChatItem, _): + cleanupDirectFile(aChatItem) + return nil + case let .sndFileCompleteXFTP(_, aChatItem, _): + cleanupFile(aChatItem) + return nil case let .callInvitation(invitation): // Do not post it without CallKit support, iOS will stop launching the app without showing CallKit return ( diff --git a/apps/ios/SimpleXChat/FileUtils.swift b/apps/ios/SimpleXChat/FileUtils.swift index dc5e628428..dd6cdbf178 100644 --- a/apps/ios/SimpleXChat/FileUtils.swift +++ b/apps/ios/SimpleXChat/FileUtils.swift @@ -200,6 +200,21 @@ public func removeFile(_ fileName: String) { } } +public func cleanupDirectFile(_ aChatItem: AChatItem) { + if aChatItem.chatInfo.chatType == .direct { + cleanupFile(aChatItem) + } +} + +public func cleanupFile(_ aChatItem: AChatItem) { + let cItem = aChatItem.chatItem + let mc = cItem.content.msgContent + if case .file = mc, + let fileName = cItem.file?.filePath { + removeFile(fileName) + } +} + public func getMaxFileSize(_ fileProtocol: FileProtocol) -> Int64 { switch fileProtocol { case .xftp: return MAX_FILE_SIZE_XFTP