Skip to content

Commit

Permalink
refactor: Revert some useless changes
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasBourdin88 committed Feb 26, 2025
1 parent d0d0155 commit 8d88ca6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
10 changes: 8 additions & 2 deletions app/src/main/java/com/infomaniak/mail/data/api/ApiRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import com.infomaniak.mail.data.models.signature.SignaturesResult
import com.infomaniak.mail.data.models.thread.ThreadResult
import com.infomaniak.mail.ui.newMessage.AiViewModel.Shortcut
import com.infomaniak.mail.utils.Utils
import com.infomaniak.mail.utils.Utils.EML_CONTENT_TYPE
import io.realm.kotlin.ext.copyFromRealm
import kotlinx.serialization.json.Json
import okhttp3.MultipartBody
Expand Down Expand Up @@ -451,8 +452,13 @@ object ApiRepository : ApiRepositoryCore() {
return callApi(url = ApiRoutes.shareLink(mailboxUuid, folderId, mailId), method = POST)
}

fun getDownloadedMessage(mailboxUuid: String, folderId: String, shortUid: Int): ApiResponse<ByteArray> {
return ApiController.callApi(url = ApiRoutes.downloadMessage(mailboxUuid, folderId, shortUid), method = GET)
fun getDownloadedMessage(mailboxUuid: String, folderId: String, shortUid: Int): Response {
val request = Request.Builder().url(ApiRoutes.downloadMessage(mailboxUuid, folderId, shortUid))
.headers(HttpUtils.getHeaders(EML_CONTENT_TYPE))
.get()
.build()

return HttpClient.okHttpClient.newCall(request).execute()
}

fun getMyKSuiteData(okHttpClient: OkHttpClient): ApiResponse<MyKSuiteData> {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1021,15 +1021,15 @@ class MainViewModel @Inject constructor(

val apiResponse = ApiRepository.getDownloadedMessage(mailbox.uuid, message.folderId, message.shortUid)

if (apiResponse.data == null || !apiResponse.isSuccess()) {
if (apiResponse.body == null || !apiResponse.isSuccessful) {
reportDisplayProblemTrigger.postValue(Unit)
snackbarManager.postValue(appContext.getString(RCore.string.anErrorHasOccurred))

return@launch
}

val filename = UUID.randomUUID().toString()
val emlAttachment = Attachment(apiResponse.data, filename, EML_CONTENT_TYPE)
val emlAttachment = Attachment(apiResponse.body?.bytes(), filename, EML_CONTENT_TYPE)
Sentry.captureMessage("Message display problem reported", SentryLevel.ERROR) { scope ->
scope.addAttachment(emlAttachment)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/
package com.infomaniak.mail.ui.main.thread.actions

import android.accounts.NetworkErrorException
import android.app.Application
import android.content.Context
import android.net.Uri
Expand All @@ -26,13 +25,11 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.infomaniak.lib.core.api.ApiController
import com.infomaniak.lib.core.utils.DownloadManagerUtils
import com.infomaniak.lib.core.utils.SentryLog
import com.infomaniak.mail.R
import com.infomaniak.mail.data.api.ApiRepository
import com.infomaniak.mail.data.cache.mailboxContent.MessageController
import com.infomaniak.mail.data.cache.mailboxContent.ThreadController
import com.infomaniak.mail.data.models.mailbox.Mailbox
import com.infomaniak.mail.data.models.message.Message
import com.infomaniak.mail.di.IoDispatcher
Expand Down Expand Up @@ -107,21 +104,23 @@ class DownloadMessagesViewModel @Inject constructor(
shortUid = message.shortUid,
)

if (apiResponse.data == null || !apiResponse.isSuccess()) throw apiResponse.error?.exception!!
if (apiResponse.body == null || !apiResponse.isSuccessful) {
throw ByteArrayNetworkException(apiResponse.body.toString(), apiResponse.code)
}

val messageSubject = message.subject ?: NO_SUBJECT_FILE
val truncatedSubject = messageSubject.take(MAX_FILE_NAME_LENGTH)
val fileName = createUniqueFileName(listFileName, truncatedSubject)

saveEmlToFile(appContext, apiResponse.data!!, fileName)
saveEmlToFile(appContext, apiResponse.body?.bytes()!!, fileName)
}
}

deferredResponses.awaitAll()
}.onSuccess { downloadedThreadUris ->
downloadMessagesLiveData.postValue(downloadedThreadUris)
}.onFailure {
if (it is ApiController.ByteArrayException){
if (it is ByteArrayNetworkException) {
SentryLog.e(TAG, "Error while sharing messages to kDrive:", it)
}
clearEmlDir()
Expand Down Expand Up @@ -150,5 +149,8 @@ class DownloadMessagesViewModel @Inject constructor(
private const val NO_SUBJECT_FILE = "message"
private const val MAX_FILE_NAME_LENGTH = 256
private val TAG = DownloadMessagesViewModel::class.simpleName.toString()

private data class ByteArrayNetworkException(val responseBody: String, val responseCode: Int) :
Exception("Failed to get EML $responseCode: $responseBody")
}
}

0 comments on commit 8d88ca6

Please sign in to comment.