Skip to content

Commit

Permalink
Merge pull request #544 from jorgeblacio/composer_files_fix
Browse files Browse the repository at this point in the history
Added a fix for adding files in the composer which had a couple of issues.
  • Loading branch information
danieltigse authored Sep 18, 2019
2 parents 66e6c51 + 53c4185 commit 4cddefc
Show file tree
Hide file tree
Showing 27 changed files with 133 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class UploadAttachmentWorkerTest {
private fun newWorker(filepath: String): UploadAttachmentWorker =
UploadAttachmentWorker(filepath = filepath, activeAccount = activeAccount,
httpClient = httpClient, publishFn = {}, fileKey = null,
accountDao = db.accountDao(), filesSize = 0L, storage = storage)
accountDao = db.accountDao(), filesSize = 0L, storage = storage,
uuid = "_UUID_")

@Test
fun should_upload_file_without_errors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class DownloadAttachmentWorkerTest {
private fun newWorker(filepath: String): UploadAttachmentWorker =
UploadAttachmentWorker(filepath = filepath, activeAccount = activeAccount,
httpClient = httpClient, publishFn = {}, fileKey = null, accountDao = db.accountDao(),
storage = storage, filesSize = 0L)
storage = storage, filesSize = 0L, uuid = "_UUID_")

private fun newDownloadWorker(filetoken: String): DownloadAttachmentWorker =
DownloadAttachmentWorker(fileToken = filetoken, emailId = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.whispersystems.libsignal.SignalProtocolAddress
import java.util.*
import kotlin.collections.ArrayList


/**
Expand Down Expand Up @@ -215,6 +217,7 @@ class SendEmailWorkerTest {
val file = folder.newFile(String.format("file_%s.png", i))
attachmentList.add(ComposerAttachment(
id=0,
uuid = UUID.randomUUID().toString(),
filepath=file.name,
uploadProgress=100,
filetoken="__FILE_TOKEN__",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.test.runner.AndroidJUnit4
import com.criptext.mail.androidtest.TestActivity
import com.criptext.mail.androidtest.TestDatabase
import com.criptext.mail.api.HttpClient
import com.criptext.mail.db.EventLocalDB
import com.criptext.mail.db.KeyValueStorage
import com.criptext.mail.db.MailboxLocalDB
import com.criptext.mail.db.SettingsLocalDB
Expand All @@ -13,9 +14,10 @@ import com.criptext.mail.db.models.ActiveAccount
import com.criptext.mail.db.models.Contact
import com.criptext.mail.db.models.Label
import com.criptext.mail.mocks.MockEmailData
import com.criptext.mail.scenes.settings.workers.LogoutWorker
import com.criptext.mail.utils.MockedResponse
import com.criptext.mail.utils.enqueueResponses
import com.criptext.mail.utils.generaldatasource.data.GeneralResult
import com.criptext.mail.utils.generaldatasource.workers.LogoutWorker
import io.mockk.mockk
import okhttp3.mockwebserver.MockWebServer
import org.amshove.kluent.shouldBe
Expand All @@ -33,7 +35,7 @@ class LogoutWorkerTest{

private lateinit var db: TestDatabase
private lateinit var mailboxLocalDB: MailboxLocalDB
private lateinit var settingsLocalDB: SettingsLocalDB
private lateinit var eventLocalDB: EventLocalDB
private lateinit var storage: KeyValueStorage
private val activeAccount = ActiveAccount(name = "Tester", recipientId = "tester",
deviceId = 1, jwt = "__JWTOKEN__", signature = "", refreshToken = "", id = 1,
Expand All @@ -59,7 +61,7 @@ class LogoutWorkerTest{
signature = "", refreshToken = "__REFRESH__", isActive = true, domain = "criptext.com", isLoggedIn = true,
backupPassword = null, autoBackupFrequency = 0, hasCloudBackup = false, wifiOnly = true, lastTimeBackup = null))
mailboxLocalDB = MailboxLocalDB.Default(db, mActivityRule.activity.filesDir)
settingsLocalDB = SettingsLocalDB.Default(db)
eventLocalDB = EventLocalDB(db, mActivityRule.activity.filesDir, mActivityRule.activity.cacheDir)

MockEmailData.insertEmailsNeededForTests(db, listOf(Label.defaultItems.inbox),
mActivityRule.activity.filesDir, activeAccount.recipientId, accountId = activeAccount.id,
Expand All @@ -81,7 +83,7 @@ class LogoutWorkerTest{

val worker = newWorker()

worker.work(mockk()) as SettingsResult.Logout.Success
worker.work(mockk()) as GeneralResult.Logout.Success

mailboxLocalDB.getThreadsIdsFromLabel(
labelName = Label.defaultItems.inbox.text,
Expand All @@ -93,10 +95,12 @@ class LogoutWorkerTest{
private fun newWorker(): LogoutWorker =

LogoutWorker(
db = settingsLocalDB,
db = eventLocalDB,
httpClient = httpClient,
activeAccount = activeAccount,
storage = storage,
accountDao = db.accountDao(),
shouldDeleteAllData = false,
publishFn = {})

@After
Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/com/criptext/mail/db/dao/SignUpDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ interface SignUpDao {
registrationId=:registrationId,
domain=:domain,
isActive=:isActive,
isLoggedIn=:isLoggedIn
isLoggedIn=:isLoggedIn,
hasCloudBackup=:hasCloudBackup,
wifiOnly=:wifiOnly,
autoBackupFrequency=:backupFrequency
WHERE recipientId=:recipientId
""")
fun updateAccount(recipientId: String, name: String, jwt: String, refreshJwt: String,
deviceId: Int, identityKey: String, registrationId: Int, domain: String,
isActive: Int, isLoggedIn: Int)
isActive: Int, isLoggedIn: Int, hasCloudBackup: Boolean, wifiOnly: Boolean, backupFrequency: Int)

@Insert
fun insertPreKeys(preKeys : List<CRPreKey>)
Expand Down Expand Up @@ -68,7 +71,8 @@ interface SignUpDao {
updateAccount(recipientId = account.recipientId, name = account.name, deviceId = account.deviceId,
domain = account.domain, isLoggedIn = if(account.isLoggedIn) 1 else 0, isActive = if(account.isActive) 1 else 0,
identityKey = account.identityKeyPairB64, jwt = account.jwt, refreshJwt = account.refreshToken,
registrationId = account.registrationId)
registrationId = account.registrationId, backupFrequency = account.autoBackupFrequency, hasCloudBackup = account.hasCloudBackup,
wifiOnly = account.wifiOnly)

val savedAccount = accountDao.getLoggedInAccount()!!
preKeyList.forEach { it.accountId = savedAccount.id }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.criptext.mail.scenes.composer


import android.Manifest
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.view.View
import com.criptext.mail.BaseActivity
Expand All @@ -19,20 +18,17 @@ import com.criptext.mail.scenes.ActivityMessage
import com.criptext.mail.scenes.SceneController
import com.criptext.mail.scenes.composer.data.*
import com.criptext.mail.scenes.composer.ui.ComposerUIObserver
import com.criptext.mail.scenes.mailbox.data.EmailThread
import com.criptext.mail.scenes.params.EmailDetailParams
import com.criptext.mail.scenes.params.LinkingParams
import com.criptext.mail.scenes.params.MailboxParams
import com.criptext.mail.scenes.params.SignInParams
import com.criptext.mail.scenes.settings.data.SettingsRequest
import com.criptext.mail.utils.*
import com.criptext.mail.utils.file.FileUtils
import com.criptext.mail.utils.generaldatasource.data.GeneralDataSource
import com.criptext.mail.utils.generaldatasource.data.GeneralRequest
import com.criptext.mail.utils.generaldatasource.data.GeneralResult
import com.criptext.mail.utils.ui.data.DialogResult
import com.criptext.mail.utils.ui.data.DialogType
import com.criptext.mail.websocket.WebSocketSingleton
import java.io.File
import java.util.*

Expand Down Expand Up @@ -244,7 +240,7 @@ class ComposerController(private val storage: KeyValueStorage,
EmailAddressUtils.extractEmailAddressDomain(email) in
checkedData.filter { it.isCriptextDomain }
.map { it.name } }
contacts.forEachIndexed { index, contact ->
contacts.forEachIndexed { _, contact ->
if(contact.email in isCriptext)
contact.isCriptextDomain = true
}
Expand Down Expand Up @@ -272,7 +268,7 @@ class ComposerController(private val storage: KeyValueStorage,
when (result) {
is GeneralResult.GetRemoteFile.Success -> {
scene.dismissPreparingFileDialog()
model.attachments.addAll(result.remoteFiles.map { ComposerAttachment(it.first, it.second, model.fileKey!!) })
model.attachments.addAll(result.remoteFiles.map { ComposerAttachment(UUID.randomUUID().toString(), it.first, it.second, model.fileKey!!) })
scene.notifyAttachmentSetChanged()
handleNextUpload()
}
Expand All @@ -297,15 +293,15 @@ class ComposerController(private val storage: KeyValueStorage,
private fun onUploadFile(result: ComposerResult.UploadFile){
when (result) {
is ComposerResult.UploadFile.Register -> {
val composerAttachment = getAttachmentByPath(result.filepath) ?: return
val composerAttachment = getAttachmentByUUID(result.uuid) ?: return
composerAttachment.filetoken = result.filetoken
}
is ComposerResult.UploadFile.Progress -> {
val composerAttachment = getAttachmentByPath(result.filepath) ?: return
val composerAttachment = getAttachmentByUUID(result.uuid) ?: return
composerAttachment.uploadProgress = result.percentage
}
is ComposerResult.UploadFile.Success -> {
val composerAttachment = getAttachmentByPath(result.filepath)
val composerAttachment = getAttachmentByUUID(result.uuid)
composerAttachment?.uploadProgress = 100
model.isUploadingAttachments = false
model.filesSize = result.filesSize
Expand Down Expand Up @@ -339,8 +335,8 @@ class ComposerController(private val storage: KeyValueStorage,
scene.notifyAttachmentSetChanged()
}

private fun getAttachmentByPath(filepath: String): ComposerAttachment? {
return model.attachments.firstOrNull{it.filepath == filepath}
private fun getAttachmentByUUID(uuid: String): ComposerAttachment? {
return model.attachments.firstOrNull{it.uuid == uuid}
}

private fun removeAttachmentByPath(filepath: String) {
Expand Down Expand Up @@ -469,13 +465,14 @@ class ComposerController(private val storage: KeyValueStorage,

private fun isReadyForSending() = (model.to.isNotEmpty() || model.cc.isNotEmpty() || model.bcc.isNotEmpty())

private fun uploadSelectedFile(filepath: String, fileKey: String){
private fun uploadSelectedFile(filepath: String, fileKey: String, uuid: String){
model.isUploadingAttachments = true
scene.dismissPreparingFileDialog()
dataSource.submitRequest(ComposerRequest.UploadAttachment(
filepath = filepath,
fileKey = fileKey,
filesSize = model.filesSize
filesSize = model.filesSize,
uuid = uuid
))
}

Expand Down Expand Up @@ -540,7 +537,7 @@ class ComposerController(private val storage: KeyValueStorage,
val localAttachments = filesMetadata
.filter(isNewAttachment)
.filter {it.second != -1L}
.map{ComposerAttachment(it.first, it.second, model.fileKey!!)}
.map{ComposerAttachment(UUID.randomUUID().toString(), it.first, it.second, model.fileKey!!)}
val remoteAttachments = filesMetadata
.filter(isNewAttachment)
.filter{ it.second == -1L }
Expand All @@ -563,13 +560,13 @@ class ComposerController(private val storage: KeyValueStorage,
return
}
val attachmentToUpload = model.attachments.firstOrNull { it.uploadProgress == -1 } ?: return
val composerAttachment = getAttachmentByPath(attachmentToUpload.filepath)
val composerAttachment = getAttachmentByUUID(attachmentToUpload.uuid)
if(composerAttachment == null){
scene.showMaxFilesExceedsDialog()
return
}else {
composerAttachment.uploadProgress = 0
uploadSelectedFile(attachmentToUpload.filepath, composerAttachment.fileKey)
uploadSelectedFile(attachmentToUpload.filepath, composerAttachment.fileKey, composerAttachment.uuid)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import com.criptext.mail.db.AttachmentTypes
import com.criptext.mail.utils.file.FileUtils
import org.json.JSONArray
import org.json.JSONObject
import java.util.*

data class ComposerAttachment(val id: Long, val filepath: String, var uploadProgress: Int,
data class ComposerAttachment(val id: Long, val uuid: String, val filepath: String, var uploadProgress: Int,
var filetoken: String, val type: AttachmentTypes, var size: Long,
val fileKey: String, val cid: String?) {
constructor(filepath: String, size: Long, fileKey: String): this (0, filepath, -1, filetoken = "",
constructor(uuid: String, filepath: String, size: Long, fileKey: String): this (0, uuid ,filepath, -1, filetoken = "",
type = FileUtils.getAttachmentTypeFromPath(filepath), size = size, fileKey = fileKey, cid = null)

companion object{
fun fromJSON(jsonString: String): ComposerAttachment{
val json = JSONObject(jsonString)
return ComposerAttachment(
filepath = json.getString("filepath"),
uuid = json.getString("uuid"),
size = json.getLong("size"),
fileKey = json.getString("fileKey")
)
Expand All @@ -35,6 +37,7 @@ data class ComposerAttachment(val id: Long, val filepath: String, var uploadProg
attachments.forEach {
val json = JSONObject()
json.put("id", it.id)
json.put("uuid", it.uuid)
json.put("filepath", it.filepath)
json.put("uploadProgress", it.uploadProgress)
json.put("filetoken", it.filetoken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ComposerDataSource(
filepath = params.filepath,
httpClient = httpClient, activeAccount = activeAccount,
publishFn = { res -> flushResults(res) }, fileKey = params.fileKey,
accountDao = composerLocalDB.accountDao, storage = storage)
accountDao = composerLocalDB.accountDao, storage = storage, uuid = params.uuid)
is ComposerRequest.LoadInitialData -> LoadInitialDataWorker(
httpClient = HttpClient.Default(),
db = composerLocalDB,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sealed class ComposerRequest {
val onlySave: Boolean, val attachments: List<ComposerAttachment>,
val fileKey: String?, val originalId: Long?, val senderAccount: ActiveAccount? = null,
val currentLabel: Label): ComposerRequest()
class UploadAttachment(val filepath: String, val fileKey: String?, val filesSize: Long): ComposerRequest()
class UploadAttachment(val filepath: String, val fileKey: String?, val filesSize: Long, val uuid: String): ComposerRequest()
class LoadInitialData(val composerType: ComposerType, val emailId: Long): ComposerRequest()
data class CheckDomain(val emails: List<String>) : ComposerRequest()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.criptext.mail.db.models.Account
import com.criptext.mail.db.models.Contact
import com.criptext.mail.email_preview.EmailPreview
import com.criptext.mail.utils.UIMessage
import java.util.*

/**
* Created by gabriel on 2/26/18.
Expand Down Expand Up @@ -38,9 +39,9 @@ sealed class ComposerResult {
}

sealed class UploadFile : ComposerResult() {
data class Success(val filepath: String, val filesSize: Long): UploadFile()
data class Register(val filepath: String, val filetoken: String): UploadFile()
data class Progress(val filepath: String, val percentage: Int): UploadFile()
data class Success(val filepath: String, val filesSize: Long, val uuid: String): UploadFile()
data class Register(val filepath: String, val filetoken: String, val uuid: String): UploadFile()
data class Progress(val filepath: String, val percentage: Int, val uuid: String): UploadFile()
data class MaxFilesExceeds(val filepath: String): UploadFile()
data class PayloadTooLarge(val filepath: String, val headers: ResultHeaders): UploadFile()
data class Failure(val filepath: String, val message: UIMessage): UploadFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.criptext.mail.utils.EmailAddressUtils
import com.criptext.mail.utils.UIMessage
import com.criptext.mail.utils.file.FileUtils
import com.github.kittinunf.result.Result
import java.util.*
import kotlin.collections.ArrayList

/**
* Created by gabriel on 7/2/18.
Expand All @@ -38,7 +40,7 @@ class LoadInitialDataWorker(

private fun convertDraftToInputData(fullEmail: FullEmail): ComposerInputData {
val attachments = ArrayList(fullEmail.files.map {
ComposerAttachment(0, it.name, 100,
ComposerAttachment(0, UUID.randomUUID().toString(), it.name, 100,
it.token, FileUtils.getAttachmentTypeFromPath(it.name),
it.size, it.fileKey, it.cid)
})
Expand Down Expand Up @@ -119,7 +121,7 @@ class LoadInitialDataWorker(
val subject = (if(fullEmail.email.subject.matches("^(Fw|FW|Fwd|FWD): .*\$".toRegex())) "" else "FW: ") +
fullEmail.email.subject
val attachments = ArrayList<ComposerAttachment>(fullEmail.files.map {
ComposerAttachment(0, it.name, 100,
ComposerAttachment(0, UUID.randomUUID().toString(), it.name, 100,
it.token, FileUtils.getAttachmentTypeFromPath(it.name),
it.size, it.fileKey, it.cid)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class SaveEmailWorker(
val (newEmailId, savedMailThreadId) = saveEmail()
val attachmentsSaved = dao.findFilesByEmailId(newEmailId).map {
ComposerAttachment(
id = it.id, fileKey = it.fileKey, size = it.size,
id = it.id, uuid = UUID.randomUUID().toString(), fileKey = it.fileKey, size = it.size,
filepath = attachments.find { file -> it.token == file.filetoken }!!.filepath,
filetoken = it.token, type = attachments.find { file -> it.token == file.filetoken }!!.type,
uploadProgress = attachments.find { file -> it.token == file.filetoken }!!.uploadProgress,
Expand Down
Loading

0 comments on commit 4cddefc

Please sign in to comment.