From b65120b871abad16effd93acc91f806db15d0069 Mon Sep 17 00:00:00 2001 From: Denys Bondarenko Date: Tue, 1 Feb 2022 20:42:29 +0200 Subject: [PATCH] Included names in from/to/cc/bcc fields when calling FES.| #1662 --- .../flowcrypt/email/api/email/EmailUtil.kt | 6 ++-- .../api/email/model/OutgoingMessageInfo.kt | 19 ++++-------- .../email/database/dao/RecipientDao.kt | 3 ++ .../email/database/entity/RecipientEntity.kt | 5 ++++ .../extensions/OutgoingMessageInfoExt.kt | 30 +++++++++++++++++++ .../javax/mail/internet/MimeMessageExt.kt | 4 +-- .../HandlePasswordProtectedMsgWorker.kt | 16 +++++----- ...PrepareOutgoingMessagesJobIntentService.kt | 12 ++++---- .../fragment/CreateMessageFragment.kt | 2 +- .../com/flowcrypt/email/ParcelableTest.kt | 2 +- .../email/api/email/EmailUtilTest.kt | 4 +-- 11 files changed, 67 insertions(+), 36 deletions(-) create mode 100644 FlowCrypt/src/main/java/com/flowcrypt/email/extensions/OutgoingMessageInfoExt.kt diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/EmailUtil.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/EmailUtil.kt index 2947536dc4..5ac93d1080 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/EmailUtil.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/EmailUtil.kt @@ -645,7 +645,7 @@ class EmailUtil { outgoingMsgInfo: OutgoingMessageInfo ): Message { val session = Session.getInstance(Properties()) - val senderEmail = outgoingMsgInfo.from + val senderEmail = outgoingMsgInfo.from.address var pubKeys: List? = null var prvKeys: List? = null var ringProtector: SecretKeyRingProtector? = null @@ -978,7 +978,7 @@ class EmailUtil { ): MimeMessage { val msg = FlowCryptMimeMessage(session) msg.subject = info.subject - msg.setFrom(InternetAddress(info.from)) + msg.setFrom(info.from) msg.setRecipients(Message.RecipientType.TO, info.toRecipients.toTypedArray()) msg.setRecipients(Message.RecipientType.CC, info.ccRecipients?.toTypedArray()) msg.setRecipients(Message.RecipientType.BCC, info.bccRecipients?.toTypedArray()) @@ -996,7 +996,7 @@ class EmailUtil { protector: SecretKeyRingProtector? = null ): Message { val reply = replyToMsg.reply(false)//we use replyToAll == false to use the own logic - reply.setFrom(InternetAddress(info.from)) + reply.setFrom(info.from) reply.setContent(MimeMultipart().apply { addBodyPart(prepareBodyPart(info, pubKeys, prvKeys, protector)) }) diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/model/OutgoingMessageInfo.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/model/OutgoingMessageInfo.kt index dbf9be1397..5d4fe7030c 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/model/OutgoingMessageInfo.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/api/email/model/OutgoingMessageInfo.kt @@ -27,7 +27,7 @@ data class OutgoingMessageInfo constructor( val toRecipients: List, val ccRecipients: List? = null, val bccRecipients: List? = null, - val from: String, + val from: InternetAddress, val atts: List? = null, val forwardedAtts: List? = null, val encryptionType: MessageEncryptionType, @@ -60,7 +60,7 @@ data class OutgoingMessageInfo constructor( parcel.readValue(InternetAddress::class.java.classLoader) as List, parcel.readValue(InternetAddress::class.java.classLoader) as List?, parcel.readValue(InternetAddress::class.java.classLoader) as List?, - parcel.readString()!!, + parcel.readSerializable() as InternetAddress, parcel.readValue(AttachmentInfo::class.java.classLoader) as List?, parcel.readValue(AttachmentInfo::class.java.classLoader) as List?, parcel.readParcelable(MessageEncryptionType::class.java.classLoader)!!, @@ -82,7 +82,7 @@ data class OutgoingMessageInfo constructor( writeValue(toRecipients) writeValue(ccRecipients) writeValue(bccRecipients) - writeString(from) + writeSerializable(from) writeValue(atts) writeValue(forwardedAtts) writeParcelable(encryptionType, flags) @@ -138,15 +138,8 @@ data class OutgoingMessageInfo constructor( return result } - companion object { - @JvmField - @Suppress("unused") - val CREATOR: Parcelable.Creator = - object : Parcelable.Creator { - override fun createFromParcel(source: Parcel): OutgoingMessageInfo = - OutgoingMessageInfo(source) - - override fun newArray(size: Int): Array = arrayOfNulls(size) - } + companion object CREATOR : Parcelable.Creator { + override fun createFromParcel(parcel: Parcel) = OutgoingMessageInfo(parcel) + override fun newArray(size: Int): Array = arrayOfNulls(size) } } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/RecipientDao.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/RecipientDao.kt index be9586c452..40e458c17c 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/RecipientDao.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/database/dao/RecipientDao.kt @@ -45,6 +45,9 @@ interface RecipientDao : BaseDao { @Query("SELECT * FROM recipients WHERE email = :email") fun getRecipientByEmail(email: String): RecipientEntity? + @Query("SELECT * FROM recipients WHERE email IN (:emails)") + fun getRecipientsByEmails(emails: Collection): List + @Transaction @Query("SELECT * FROM recipients WHERE email = :email") fun getRecipientsWithPubKeysByEmailsLD(email: String): LiveData diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/database/entity/RecipientEntity.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/database/entity/RecipientEntity.kt index 5fb20dc040..8c501195e4 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/database/entity/RecipientEntity.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/database/entity/RecipientEntity.kt @@ -12,6 +12,7 @@ import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey +import javax.mail.internet.InternetAddress /** * @author Denis Bondarenko @@ -52,6 +53,10 @@ data class RecipientEntity( return 0 } + fun toInternetAddress(): InternetAddress { + return InternetAddress(email, name) + } + companion object CREATOR : Parcelable.Creator { override fun createFromParcel(parcel: Parcel): RecipientEntity = RecipientEntity(parcel) override fun newArray(size: Int): Array = arrayOfNulls(size) diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/extensions/OutgoingMessageInfoExt.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/extensions/OutgoingMessageInfoExt.kt new file mode 100644 index 0000000000..8867da77b9 --- /dev/null +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/extensions/OutgoingMessageInfoExt.kt @@ -0,0 +1,30 @@ +/* + * © 2016-present FlowCrypt a.s. Limitations apply. Contact human@flowcrypt.com + * Contributors: denbond7 + */ + +package com.flowcrypt.email.extensions + +import android.content.Context +import com.flowcrypt.email.api.email.model.OutgoingMessageInfo +import com.flowcrypt.email.database.FlowCryptRoomDatabase + +fun OutgoingMessageInfo.replaceWithCachedRecipients(context: Context): OutgoingMessageInfo { + val recipientDao = FlowCryptRoomDatabase.getDatabase(context).recipientDao() + val updatedFrom = recipientDao.getRecipientByEmail(from.address.lowercase()) + ?.toInternetAddress() ?: from + return copy( + from = updatedFrom, + toRecipients = recipientDao.getRecipientsByEmails(toRecipients + .map { it.address.lowercase() }) + .map { it.toInternetAddress() }, + ccRecipients = ccRecipients?.let { list -> + recipientDao.getRecipientsByEmails(list.map { it.address.lowercase() }) + .map { it.toInternetAddress() } + }, + bccRecipients = bccRecipients?.let { list -> + recipientDao.getRecipientsByEmails(list.map { it.address.lowercase() }) + .map { it.toInternetAddress() } + } + ) +} diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/extensions/javax/mail/internet/MimeMessageExt.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/extensions/javax/mail/internet/MimeMessageExt.kt index 6a2cbaf85c..34104d4383 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/extensions/javax/mail/internet/MimeMessageExt.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/extensions/javax/mail/internet/MimeMessageExt.kt @@ -21,8 +21,8 @@ fun MimeMessage.getAddresses(type: Message.RecipientType): List { ?.mapNotNull { (it as? InternetAddress)?.address?.lowercase() } ?: emptyList() } -fun MimeMessage.getFromAddress(): String { - return (from.first() as? InternetAddress)?.address?.lowercase() +fun MimeMessage.getFromAddress(): InternetAddress { + return (from.first() as? InternetAddress) ?: throw IllegalStateException("'from' address is undefined") } diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/HandlePasswordProtectedMsgWorker.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/HandlePasswordProtectedMsgWorker.kt index 4a4e25264a..9dabb93591 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/HandlePasswordProtectedMsgWorker.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/jetpack/workmanager/HandlePasswordProtectedMsgWorker.kt @@ -139,15 +139,15 @@ class HandlePasswordProtectedMsgWorker(context: Context, params: WorkerParameter //start of creating and uploading a password-protected msg to FES val fromAddress = plainMimeMsgWithAttachments.getFromAddress() - val domain = EmailUtil.getDomain(fromAddress) + val domain = EmailUtil.getDomain(fromAddress.address) val idToken = getGoogleIdToken() val replyToken = fetchReplyToken(apiRepository, domain, idToken) val replyInfoData = ReplyInfoData( - sender = fromAddress, + sender = fromAddress.address, recipient = (toCandidates + ccCandidates + bccCandidates) .mapNotNull { (it as? InternetAddress)?.address } .filterNot { - it.equals(fromAddress, true) + it.equals(fromAddress.address, true) }, subject = plainMimeMsgWithAttachments.subject, token = replyToken @@ -211,16 +211,16 @@ class HandlePasswordProtectedMsgWorker(context: Context, params: WorkerParameter private fun genMessageUploadRequest( replyToken: String, - fromAddress: String, + fromAddress: Address, toCandidates: Array
, ccCandidates: Array
, bccCandidates: Array
) = MessageUploadRequest( associateReplyToken = replyToken, - from = fromAddress, - to = toCandidates.map { (it as InternetAddress).address }, - cc = ccCandidates.map { (it as InternetAddress).address }, - bcc = bccCandidates.map { (it as InternetAddress).address } + from = (fromAddress as InternetAddress).toString(), + to = toCandidates.map { (it as InternetAddress).toString() }, + cc = ccCandidates.map { (it as InternetAddress).toString() }, + bcc = bccCandidates.map { (it as InternetAddress).toString() } ) private suspend fun handleExceptionsForMessage( diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/service/PrepareOutgoingMessagesJobIntentService.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/service/PrepareOutgoingMessagesJobIntentService.kt index 510a33c91a..1b72ef8819 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/service/PrepareOutgoingMessagesJobIntentService.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/service/PrepareOutgoingMessagesJobIntentService.kt @@ -22,6 +22,7 @@ import com.flowcrypt.email.database.entity.AccountEntity import com.flowcrypt.email.database.entity.AttachmentEntity import com.flowcrypt.email.database.entity.MessageEntity import com.flowcrypt.email.database.entity.RecipientEntity +import com.flowcrypt.email.extensions.replaceWithCachedRecipients import com.flowcrypt.email.jetpack.workmanager.ForwardedAttachmentsDownloaderWorker import com.flowcrypt.email.jetpack.workmanager.HandlePasswordProtectedMsgWorker import com.flowcrypt.email.jetpack.workmanager.MessagesSenderWorker @@ -44,7 +45,6 @@ import java.io.ByteArrayOutputStream import java.io.File import java.io.IOException import java.io.InputStream -import java.util.ArrayList import java.util.UUID import javax.mail.Message @@ -76,12 +76,12 @@ class PrepareOutgoingMessagesJobIntentService : JobIntentService() { LogsUtil.d(TAG, "onHandleWork") val roomDatabase = FlowCryptRoomDatabase.getDatabase(applicationContext) - val outgoingMsgInfo = + val originalOutgoingMsgInfo = intent.getParcelableExtra(EXTRA_KEY_OUTGOING_MESSAGE_INFO) ?: return - val accountEntity = - roomDatabase.accountDao().getAccount(outgoingMsgInfo.account.lowercase()) - ?: return + val outgoingMsgInfo = originalOutgoingMsgInfo.replaceWithCachedRecipients(applicationContext) + val accountEntity = roomDatabase.accountDao().getAccount(outgoingMsgInfo.account.lowercase()) + ?: return val uid = outgoingMsgInfo.uid val email = accountEntity.email @@ -271,7 +271,7 @@ class PrepareOutgoingMessagesJobIntentService : JobIntentService() { var pubKeys: List? = null if (outgoingMsgInfo.encryptionType === MessageEncryptionType.ENCRYPTED) { - val senderEmail = outgoingMsgInfo.from + val senderEmail = outgoingMsgInfo.from.address val recipients = outgoingMsgInfo.getAllRecipients().toMutableList() pubKeys = mutableListOf() pubKeys.addAll(SecurityUtils.getRecipientsUsablePubKeys(applicationContext, recipients)) diff --git a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt index 446af25f3b..f6654b7512 100644 --- a/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt +++ b/FlowCrypt/src/main/java/com/flowcrypt/email/ui/activity/fragment/CreateMessageFragment.kt @@ -1858,7 +1858,7 @@ class CreateMessageFragment : BaseSyncFragment(), View.OnFocusChangeListener, ?: emptyList(), ccRecipients = binding?.editTextRecipientCc?.chipValues?.map { InternetAddress(it) }, bccRecipients = binding?.editTextRecipientBcc?.chipValues?.map { InternetAddress(it) }, - from = binding?.editTextFrom?.text.toString(), + from = InternetAddress(binding?.editTextFrom?.text.toString()), atts = attachments, forwardedAtts = getForwardedAttachments(), encryptionType = composeMsgViewModel.msgEncryptionType, diff --git a/FlowCrypt/src/test/java/com/flowcrypt/email/ParcelableTest.kt b/FlowCrypt/src/test/java/com/flowcrypt/email/ParcelableTest.kt index a9fafeee9b..62b37c1a00 100644 --- a/FlowCrypt/src/test/java/com/flowcrypt/email/ParcelableTest.kt +++ b/FlowCrypt/src/test/java/com/flowcrypt/email/ParcelableTest.kt @@ -64,7 +64,7 @@ class ParcelableTest(val name: String, private val currentClass: Class