Skip to content

Commit

Permalink
Fixed deleting a draft if a message was send quickly.| #74
Browse files Browse the repository at this point in the history
  • Loading branch information
DenBond7 committed Nov 25, 2024
1 parent f4952c5 commit cd774f9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit cd774f9

Please sign in to comment.