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

Remove associated inline attachements when removing quote #1992

Merged
merged 2 commits into from
Aug 7, 2024
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 @@ -22,7 +22,9 @@ import com.infomaniak.mail.R
import com.infomaniak.mail.data.models.Attachment
import com.infomaniak.mail.data.models.correspondent.Recipient
import com.infomaniak.mail.data.models.message.Message
import com.infomaniak.mail.ui.main.thread.MessageWebViewClient
import com.infomaniak.mail.utils.HtmlUtils.CID_PROTOCOL
import com.infomaniak.mail.utils.HtmlUtils.SRC_ATTRIBUTE
import com.infomaniak.mail.utils.HtmlUtils.processCids
import com.infomaniak.mail.utils.JsoupParserUtil.jsoupParseBodyFragmentWithLog
import com.infomaniak.mail.utils.JsoupParserUtil.jsoupParseWithLog
import com.infomaniak.mail.utils.MailDateFormatUtils.formatForHeader
Expand All @@ -42,12 +44,12 @@ class ReplyForwardFooterManager @Inject constructor(private val appContext: Cont
fun createForwardFooter(message: Message, attachmentsToForward: List<Attachment>): String {
val previousBody = getHtmlDocument(message)?.let { document ->
document.processCids(
message = message,
attachments = message.attachments,
associateDataToCid = { oldAttachment ->
val newAttachment = attachmentsToForward.find { it.originalContentId == oldAttachment.contentId }
newAttachment?.contentId
},
applyAssociatedDataToImage = { newContentId, imageElement ->
onCidImageFound = { newContentId, imageElement ->
imageElement.attr(SRC_ATTRIBUTE, "${CID_PROTOCOL}$newContentId")
},
)
Expand All @@ -66,9 +68,9 @@ class ReplyForwardFooterManager @Inject constructor(private val appContext: Cont

val previousBody = getHtmlDocument(message)?.let { document ->
document.processCids(
message = message,
attachments = message.attachments,
associateDataToCid = Attachment::name,
applyAssociatedDataToImage = { name, imageElement ->
onCidImageFound = { name, imageElement ->
imageElement.replaceWith(TextNode("<$name>"))
},
)
Expand All @@ -80,22 +82,6 @@ class ReplyForwardFooterManager @Inject constructor(private val appContext: Cont
return assembleReplyHtmlFooter(messageReplyHeader, previousFullBody)
}

private fun Document.processCids(
message: Message,
associateDataToCid: (Attachment) -> String?,
applyAssociatedDataToImage: (String, Element) -> Unit
) {
val attachmentsMap = message.attachments.associate {
it.contentId to associateDataToCid(it)
}

doOnHtmlImage { imageElement ->
attachmentsMap[getCid(imageElement)]?.let { associatedData ->
applyAssociatedDataToImage(associatedData, imageElement)
}
}
}

private fun Message.fromName(): String = sender?.quotedDisplayName() ?: appContext.getString(R.string.unknownRecipientTitle)

private fun getHtmlDocument(message: Message): Document? {
Expand All @@ -109,11 +95,6 @@ class ReplyForwardFooterManager @Inject constructor(private val appContext: Cont
return html?.let(::jsoupParseWithLog)
}

private fun Document.doOnHtmlImage(actionOnImage: (Element) -> Unit) {
select(CID_IMAGE_CSS_QUERY).forEach { imageElement -> actionOnImage(imageElement) }
}

private fun getCid(imageElement: Element) = imageElement.attr(SRC_ATTRIBUTE).removePrefix(CID_PROTOCOL)

private fun computePreviousFullBody(previousBody: String, message: Message): String {
return message.body?.let { body ->
Expand Down Expand Up @@ -187,10 +168,4 @@ class ReplyForwardFooterManager @Inject constructor(private val appContext: Cont
private fun formatRecipientList(recipientList: List<Recipient>): String? {
return if (recipientList.isNotEmpty()) recipientList.joinToString { it.quotedDisplayName() } else null
}

companion object {
private const val CID_PROTOCOL = "${MessageWebViewClient.CID_SCHEME}:"
private const val SRC_ATTRIBUTE = "src"
private const val CID_IMAGE_CSS_QUERY = "img[${SRC_ATTRIBUTE}^='${CID_PROTOCOL}']"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ import com.infomaniak.mail.ui.main.thread.AttachmentAdapter
import com.infomaniak.mail.ui.newMessage.NewMessageRecipientFieldsManager.FieldType
import com.infomaniak.mail.ui.newMessage.NewMessageViewModel.ImportationResult
import com.infomaniak.mail.ui.newMessage.NewMessageViewModel.UiFrom
import com.infomaniak.mail.utils.HtmlUtils.processCids
import com.infomaniak.mail.utils.JsoupParserUtil.jsoupParseWithLog
import com.infomaniak.mail.utils.SentryDebug
import com.infomaniak.mail.utils.SignatureUtils
import com.infomaniak.mail.utils.UiUtils.PRIMARY_COLOR_CODE
Expand Down Expand Up @@ -423,10 +425,33 @@ class NewMessageFragment : Fragment() {
}
removeQuote.setOnClickListener {
trackNewMessageEvent("deleteQuote")
removeInlineAttachmentsUsedInQuote()
newMessageViewModel.uiQuoteLiveData.value = null
}
}

private fun removeInlineAttachmentsUsedInQuote() {
newMessageViewModel.uiQuoteLiveData.value?.let { html ->
newMessageViewModel.attachmentsLiveData.value?.let { attachments ->
newMessageViewModel.attachmentsLiveData.value = attachments.filterOutHtmlCids(html)
}
}
}

private fun List<Attachment>.filterOutHtmlCids(html: String): List<Attachment> {
return buildList {
addAll(this@filterOutHtmlCids)

jsoupParseWithLog(html).processCids(
attachments = this@filterOutHtmlCids,
associateDataToCid = { it },
onCidImageFound = { attachment, _ ->
remove(attachment)
}
)
}
}

private fun WebView.loadSignatureContent(html: String, webViewGroup: Group) {
val processedHtml = webViewUtils.processSignatureHtmlForDisplay(html, context.isNightModeEnabled())
loadProcessedContent(processedHtml, webViewGroup)
Expand Down
52 changes: 52 additions & 0 deletions app/src/main/java/com/infomaniak/mail/utils/HtmlUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Infomaniak Mail - Android
* Copyright (C) 2024 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.mail.utils

import com.infomaniak.mail.data.models.Attachment
import com.infomaniak.mail.ui.main.thread.MessageWebViewClient
import org.jsoup.nodes.Document
import org.jsoup.nodes.Element

object HtmlUtils {

const val CID_PROTOCOL = "${MessageWebViewClient.CID_SCHEME}:"
const val SRC_ATTRIBUTE = "src"
private const val CID_IMAGE_CSS_QUERY = "img[$SRC_ATTRIBUTE^='$CID_PROTOCOL']"

fun <T> Document.processCids(
attachments: List<Attachment>,
associateDataToCid: (Attachment) -> T?,
onCidImageFound: (T, Element) -> Unit,
) {
val attachmentsMap = attachments.associate {
it.contentId to associateDataToCid(it)
}

doOnHtmlImage { imageElement ->
attachmentsMap[getCid(imageElement)]?.let { associatedData ->
onCidImageFound(associatedData, imageElement)
}
}
}

private fun Document.doOnHtmlImage(actionOnImage: (Element) -> Unit) {
select(CID_IMAGE_CSS_QUERY).forEach { imageElement -> actionOnImage(imageElement) }
}

private fun getCid(imageElement: Element) = imageElement.attr(SRC_ATTRIBUTE).removePrefix(CID_PROTOCOL)
}
Loading