Skip to content

Commit d245a00

Browse files
Add more breadcrumbs to better understand Attachments issues
1 parent e250d01 commit d245a00

File tree

7 files changed

+67
-33
lines changed

7 files changed

+67
-33
lines changed

app/src/main/java/com/infomaniak/mail/data/cache/mailboxContent/DraftController.kt

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.infomaniak.mail.data.models.draft.Draft
2626
import com.infomaniak.mail.data.models.draft.Draft.DraftMode
2727
import com.infomaniak.mail.data.models.message.Message
2828
import com.infomaniak.mail.utils.AccountUtils
29+
import com.infomaniak.mail.utils.SentryDebug
2930
import io.realm.kotlin.MutableRealm
3031
import io.realm.kotlin.Realm
3132
import io.realm.kotlin.TypedRealm
@@ -100,6 +101,7 @@ class DraftController @Inject constructor(
100101
resource = previousMessage.attachments.find { it.name == name }?.resource
101102
setUploadStatus(UploadStatus.FINISHED)
102103
}
104+
SentryDebug.addAttachmentsBreadcrumb(draft)
103105
}
104106

105107
draft.uiQuote = replyForwardFooterManager.createForwardFooter(previousMessage, draft.attachments)

app/src/main/java/com/infomaniak/mail/data/models/Attachment.kt

+6-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ import android.content.Context
2121
import androidx.core.net.toFile
2222
import androidx.core.net.toUri
2323
import com.infomaniak.lib.core.utils.Utils.enumValueOfOrNull
24+
import com.infomaniak.mail.data.models.draft.Draft
2425
import com.infomaniak.mail.utils.AttachableMimeTypeUtils
2526
import com.infomaniak.mail.utils.LocalStorageUtils
27+
import com.infomaniak.mail.utils.SentryDebug
2628
import io.realm.kotlin.types.EmbeddedRealmObject
2729
import kotlinx.serialization.SerialName
2830
import kotlinx.serialization.Serializable
@@ -80,13 +82,14 @@ class Attachment : EmbeddedRealmObject, Attachable {
8082
* After uploading an Attachment, we replace the local version with the remote one.
8183
* The remote one doesn't know about local data, so we have to backup them.
8284
*/
83-
fun backupLocalData(oldAttachment: Attachment, uploadStatus: UploadStatus) {
85+
fun backupLocalData(oldAttachment: Attachment, uploadStatus: UploadStatus, draft: Draft) {
8486
localUuid = oldAttachment.localUuid
8587
uploadLocalUri = oldAttachment.uploadLocalUri
86-
setUploadStatus(uploadStatus)
88+
setUploadStatus(uploadStatus, draft)
8789
}
8890

89-
fun setUploadStatus(uploadStatus: UploadStatus) {
91+
fun setUploadStatus(uploadStatus: UploadStatus, draft: Draft? = null) {
92+
draft?.let(SentryDebug::addAttachmentsBreadcrumb)
9093
_uploadStatus = uploadStatus.name
9194
}
9295

app/src/main/java/com/infomaniak/mail/ui/newMessage/NewMessageViewModel.kt

+4
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ class NewMessageViewModel @Inject constructor(
317317
}
318318

319319
if (mailToUri != null) handleMailTo(draft, mailToUri)
320+
321+
SentryDebug.addAttachmentsBreadcrumb(draft)
320322
}
321323

322324
private fun Draft.flagRecipientsAsAutomaticallyEntered() {
@@ -857,6 +859,8 @@ class NewMessageViewModel @Inject constructor(
857859
clear()
858860
addAll(updatedAttachments)
859861
}
862+
863+
SentryDebug.addAttachmentsBreadcrumb(draft = this)
860864
}
861865

862866
private fun Draft.getWholeBody(): String = uiBody.textToHtml() + (uiSignature ?: "") + (uiQuote ?: "")

app/src/main/java/com/infomaniak/mail/utils/DraftUtils.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private suspend fun Draft.uploadAttachments(mailbox: Mailbox, draftController: D
5252
fun setUploadStatus(attachment: Attachment, uploadStatus: UploadStatus) {
5353
realm.writeBlocking {
5454
draftController.updateDraft(localUuid, realm = this) {
55-
it.attachments.findSpecificAttachment(attachment)?.setUploadStatus(uploadStatus)
55+
it.attachments.findSpecificAttachment(attachment)?.setUploadStatus(uploadStatus, draft = it)
5656
}
5757
}
5858
}

app/src/main/java/com/infomaniak/mail/utils/SentryDebug.kt

+34-1
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,44 @@ object SentryDebug {
104104
)
105105
}
106106

107+
fun addAttachmentsBreadcrumb(draft: Draft) = with(draft) {
108+
109+
var count = 1
110+
val data = mutableMapOf<String, Any>()
111+
112+
fun String.keyPad(): String = padStart(length = 15)
113+
fun Int.countPad(): String = toString().padStart(length = 2, '0')
114+
fun count(): String = "${count.countPad().also { count++ }}."
115+
fun format(index: Int): String = (index + 1).countPad()
116+
117+
data[count() + "email".keyPad()] = AccountUtils.currentMailboxEmail.toString()
118+
data[count() + "draft".keyPad() + " - localUuid"] = localUuid
119+
data[count() + "draft".keyPad() + " - remoteUuid"] = remoteUuid.toString()
120+
data[count() + "draft".keyPad() + " - action"] = action?.name.toString()
121+
122+
val draftMode = when {
123+
inReplyToUid != null -> "REPLY or REPLY_ALL"
124+
forwardedUid != null -> "FORWARD"
125+
else -> "NEW_MAIL"
126+
}
127+
data[count() + "draft".keyPad() + " - mode"] = draftMode
128+
129+
data[count() + "attachments".keyPad() + " - count"] = attachments.count()
130+
131+
attachments.forEachIndexed { index, it ->
132+
data[count() + "attachment #${format(index)}".keyPad()] =
133+
"localUuid: ${it.localUuid} | uuid: ${it.uuid} | uploadLocalUri: ${it.uploadLocalUri}"
134+
data[count() + "attachment #${format(index)}".keyPad()] = "uploadStatus: ${it.uploadStatus.name} | size: ${it.size}"
135+
}
136+
137+
addInfoBreadcrumb(category = "Attachments_Situation", data = data)
138+
}
139+
107140
private fun addInfoBreadcrumb(category: String, message: String? = null, data: Map<String, Any>? = null) {
108141
Breadcrumb().apply {
109142
this.category = category
110143
this.message = message
111-
data?.let { it.forEach { (key, value) -> this.data[key] = value } }
144+
data?.let { it.forEach { (key, value) -> this.setData(key, value) } }
112145
this.level = SentryLevel.INFO
113146
}.also(Sentry::addBreadcrumb)
114147
}

app/src/main/java/com/infomaniak/mail/utils/extensions/AttachmentExtensions.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import com.infomaniak.mail.data.models.mailbox.Mailbox
3939
import com.infomaniak.mail.ui.main.SnackbarManager
4040
import com.infomaniak.mail.ui.main.thread.actions.DownloadAttachmentProgressDialogArgs
4141
import com.infomaniak.mail.utils.AccountUtils
42+
import com.infomaniak.mail.utils.SentryDebug
4243
import com.infomaniak.mail.utils.WorkerUtils.UploadMissingLocalFileException
4344
import com.infomaniak.mail.utils.extensions.AttachmentExtensions.AttachmentIntentType.OPEN_WITH
4445
import com.infomaniak.mail.utils.extensions.AttachmentExtensions.AttachmentIntentType.SAVE_TO_DRIVE
@@ -189,7 +190,7 @@ object AttachmentExtensions {
189190
SentryLog.d(ATTACHMENT_TAG, "When removing uploaded attachment, we found (uuids to localUris): $uuidToLocalUri")
190191
SentryLog.d(ATTACHMENT_TAG, "Target uploadLocalUri is: $uploadLocalUri")
191192

192-
remoteAttachment.backupLocalData(oldAttachment = this@updateLocalAttachment, UploadStatus.FINISHED)
193+
remoteAttachment.backupLocalData(oldAttachment = this@updateLocalAttachment, UploadStatus.FINISHED, draft)
193194

194195
SentryLog.d(ATTACHMENT_TAG, "Uploaded attachment uuid: ${remoteAttachment.uuid}")
195196
SentryLog.d(ATTACHMENT_TAG, "Uploaded attachment localUuid: ${remoteAttachment.localUuid}")
@@ -199,6 +200,8 @@ object AttachmentExtensions {
199200
delete(findSpecificAttachment(attachment = this@updateLocalAttachment)!!)
200201
add(remoteAttachment)
201202
}
203+
204+
SentryDebug.addAttachmentsBreadcrumb(draft)
202205
}
203206
}
204207
}

app/src/main/java/com/infomaniak/mail/workers/DraftsActionsWorker.kt

+16-27
Original file line numberDiff line numberDiff line change
@@ -286,34 +286,23 @@ class DraftsActionsWorker @AssistedInject constructor(
286286
var scheduledDate: String? = null
287287
var savedDraftUuid: String? = null
288288

289-
// TODO: Remove this whole `draft.attachments.forEach { … }` when the Attachment issue is fixed.
290-
draft.attachments.forEach { attachment ->
291-
if (attachment.uploadStatus != UploadStatus.FINISHED) {
292-
293-
Sentry.withScope { scope ->
294-
scope.setExtra("attachmentUuid", attachment.uuid)
295-
scope.setExtra("attachmentsCount", "${draft.attachments.count()}")
296-
scope.setExtra(
297-
"attachmentsUuids to attachmentsLocalUuid",
298-
"${draft.attachments.map { it.uuid to it.localUuid }}",
299-
)
300-
scope.setExtra("draftUuid", "${draft.remoteUuid}")
301-
scope.setExtra("draftLocalUuid", draft.localUuid)
302-
scope.setExtra("email", AccountUtils.currentMailboxEmail.toString())
303-
Sentry.captureMessage(
304-
"We tried to [${draft.action?.name}] a Draft, but an Attachment wasn't uploaded.",
305-
SentryLevel.ERROR,
306-
)
307-
}
289+
SentryDebug.addAttachmentsBreadcrumb(draft)
308290

309-
return DraftActionResult(
310-
realmActionOnDraft = null,
311-
scheduledDate = null,
312-
errorMessageResId = R.string.errorCorruptAttachment,
313-
savedDraftUuid = null,
314-
isSuccess = false,
315-
)
316-
}
291+
// TODO: Remove this whole `draft.attachments.any { … }` + `addAttachmentsBreadcrumb()` when the Attachments issue is fixed.
292+
if (draft.attachments.any { it.uploadStatus != UploadStatus.FINISHED }) {
293+
294+
Sentry.captureMessage(
295+
"We tried to [${draft.action?.name}] a Draft, but an Attachment wasn't uploaded.",
296+
SentryLevel.ERROR,
297+
)
298+
299+
return DraftActionResult(
300+
realmActionOnDraft = null,
301+
scheduledDate = null,
302+
errorMessageResId = R.string.errorCorruptAttachment,
303+
savedDraftUuid = null,
304+
isSuccess = false,
305+
)
317306
}
318307

319308
fun executeSaveAction() = with(ApiRepository.saveDraft(mailboxUuid, draft, okHttpClient)) {

0 commit comments

Comments
 (0)