Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Delete Attachment in Realm when we delete it while writing a mail #2149

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -780,13 +780,22 @@ class NewMessageViewModel @Inject constructor(
if (recipient.isDisplayedAsExternal) appContext.trackExternalEvent("deleteRecipient")
}

fun deleteAttachment(position: Int) = runCatching {
val attachments = attachmentsLiveData.valueOrEmpty().toMutableList()
val attachment = attachments[position]
attachment.getUploadLocalFile()?.delete()
LocalStorageUtils.deleteAttachmentUploadDir(appContext, draftLocalUuid!!, attachment.localUuid)
attachments.removeAt(position)
attachmentsLiveData.value = attachments
fun deleteAttachment(position: Int) = viewModelScope.launch(ioCoroutineContext) {
runCatching {
val attachments = attachmentsLiveData.valueOrEmpty().toMutableList()
val attachment = attachments[position]
attachment.getUploadLocalFile()?.delete()
LocalStorageUtils.deleteAttachmentUploadDir(appContext, draftLocalUuid!!, attachment.localUuid)

mailboxContentRealm().write {
draftController.updateDraft(draftLocalUuid!!, realm = this) {
it.attachments.findSpecificAttachment(attachment)?.let(::delete)
}
}

attachments.removeAt(position)
attachmentsLiveData.postValue(attachments)
}
}

fun updateIsSendingAllowed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,7 @@ object AttachmentExtensions {
}
}

fun List<Attachment>.findSpecificAttachment(attachment: Attachment) = this
.filter { it.localUuid == attachment.localUuid }
.also {
// TODO: If this Sentry never triggers, remove it and replace the
// `filter { … }.also { … }.firstOrNull()` with `singleOrNull { … }`
if (it.count() > 1) Sentry.captureMessage("Found several Attachments with the same localUuid")
}
.firstOrNull()
fun List<Attachment>.findSpecificAttachment(attachment: Attachment) = singleOrNull { it.localUuid == attachment.localUuid }

enum class AttachmentIntentType {
OPEN_WITH,
Expand Down