diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/model/OutgoingMessageInfo.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/model/OutgoingMessageInfo.kt index 6d3aa27d6..f42421d64 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/model/OutgoingMessageInfo.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/model/OutgoingMessageInfo.kt @@ -37,7 +37,7 @@ data class OutgoingMessageInfo( @Expose val encryptionType: MessageEncryptionType? = null, @Expose @MessageType val messageType: Int = MessageType.NEW, @Expose val replyToMessageEntityId: Long? = null, - @Expose val uid: Long = 0, + @Expose val uid: Long = System.currentTimeMillis(), @Expose val password: CharArray? = null, @Expose val timestamp: Long = System.currentTimeMillis(), @Expose val signature: String? = null, diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/CreateOutgoingMessageViewModel.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/CreateOutgoingMessageViewModel.kt index b3db572fe..24318829e 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/CreateOutgoingMessageViewModel.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/CreateOutgoingMessageViewModel.kt @@ -85,14 +85,13 @@ class CreateOutgoingMessageViewModel( } val messageId = roomDatabase.msgDao().insertSuspend(messageEntity) - messageEntity = messageEntity.copy(id = messageId, uid = messageId) + messageEntity = messageEntity.copy(id = messageId, uid = outgoingMessageInfo.uid) roomDatabase.msgDao().updateSuspend(messageEntity) updateOutgoingMsgCount(activeAccount.email, activeAccount.accountType) ProcessingOutgoingMessageInfoHelper.process( context = context, originalOutgoingMessageInfo = outgoingMessageInfo.copy( - uid = messageId, atts = outgoingMessageInfo.atts?.map { it.copy( email = outgoingMessageInfo.account, diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/DraftViewModel.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/DraftViewModel.kt index 5016886cf..c1ab8d3eb 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/DraftViewModel.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/viewmodel/DraftViewModel.kt @@ -296,7 +296,7 @@ class DraftViewModel( account = accountEntity.email, accountType = accountEntity.accountType, label = folderDrafts.fullName, - uid = System.currentTimeMillis(), + uid = outgoingMessageInfo.uid, info = outgoingMessageInfo, flags = listOf(MessageFlag.DRAFT, MessageFlag.SEEN) ).copy( @@ -319,6 +319,14 @@ class DraftViewModel( draftEntity.copy(state = MessageState.PENDING_DELETING_DRAFT.value) ) + val draftsDir = CacheManager.getDraftDirectory(getApplication()) + + draftsDir.walkTopDown().firstOrNull { + it.name == draftEntity.id.toString() + }?.let { currentMsgDraftDir -> + FileAndDirectoryUtils.deleteDir(currentMsgDraftDir) + } + DeleteDraftsWorker.enqueue(getApplication()) } } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt index 76abf5d46..772e2b0a6 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/sync/UploadDraftsWorker.kt @@ -122,10 +122,22 @@ class UploadDraftsWorker(context: Context, params: WorkerParameters) : for (directory in directories) { val directoryName = directory.name val existingDraftEntity = roomDatabase.msgDao().getMsgById(directoryName.toLong()) + if (existingDraftEntity == null) { FileAndDirectoryUtils.deleteDir(directory) continue } + + val activeAccount = roomDatabase.accountDao().getActiveAccountSuspend() + if (activeAccount != null) { + val outgoingMessages = roomDatabase.msgDao().getOutboxMsgsSuspend(activeAccount.email) + if (outgoingMessages.any { it.uid == existingDraftEntity.uid }) { + //it looks like a user sent a message for this draft + FileAndDirectoryUtils.deleteDir(directory) + continue + } + } + val drafts = directory.listFiles(FileFilter { it.isFile }) ?: emptyArray() try { setProgress(