You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2021/04/08 03:57:28 UTC

[james-project] 02/03: JAMES-3537 Refactoring: use a case class to get rid of a tuple3

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit d2912ece655de976db8287466bf70b7b4d4965a1
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Mon Apr 5 13:26:29 2021 +0700

    JAMES-3537 Refactoring: use a case class to get rid of a tuple3
---
 .../org/apache/james/jmap/mail/EmailSet.scala      | 44 ++++++++++++----------
 1 file changed, 25 insertions(+), 19 deletions(-)

diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala
index bebbb0a..7505f72 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSet.scala
@@ -179,13 +179,13 @@ case class EmailCreationRequest(mailboxIds: MailboxIds,
                                              blobResolvers: BlobResolvers,
                                              htmlTextExtractor: HtmlTextExtractor,
                                              mailboxSession: MailboxSession): Either[Throwable, MultipartBuilder] = {
-    val maybeAttachments: Either[Throwable, List[(Attachment, Blob, Array[Byte])]] =
+    val maybeAttachments: Either[Throwable, List[LoadedAttachment]] =
       attachments
         .map(loadWithMetadata(blobResolvers, mailboxSession))
         .sequence
 
     maybeAttachments.map(list => {
-      (list.filter(_._1.isInline), list.filter(!_._1.isInline)) match {
+      (list.filter(_.isInline), list.filter(!_.isInline)) match {
         case (Nil, normalAttachments) => createMixedBody(maybeHtmlBody, maybeTextBody, normalAttachments, htmlTextExtractor)
         case (inlineAttachments, Nil) => createRelatedBody(maybeHtmlBody, maybeTextBody, inlineAttachments, htmlTextExtractor)
         case (inlineAttachments, normalAttachments) => createMixedRelatedBody(maybeHtmlBody, maybeTextBody, inlineAttachments, normalAttachments, htmlTextExtractor)
@@ -193,9 +193,9 @@ case class EmailCreationRequest(mailboxIds: MailboxIds,
     })
   }
 
-  private def loadWithMetadata(blobResolvers: BlobResolvers, mailboxSession: MailboxSession)(attachment: Attachment): Either[Throwable, (Attachment, Blob, Array[Byte])] =
+  private def loadWithMetadata(blobResolvers: BlobResolvers, mailboxSession: MailboxSession)(attachment: Attachment): Either[Throwable, LoadedAttachment] =
     Try(blobResolvers.resolve(attachment.blobId, mailboxSession).subscribeOn(Schedulers.elastic()).block())
-      .toEither.flatMap(blob => load(blob).map(content => (attachment, blob, content)))
+      .toEither.flatMap(blob => load(blob).map(content => LoadedAttachment(attachment, blob, content)))
 
   private def load(blob: Blob): Either[Throwable, Array[Byte]] =
     Using(blob.content) {
@@ -204,51 +204,53 @@ case class EmailCreationRequest(mailboxIds: MailboxIds,
 
   private def createMixedRelatedBody(maybeHtmlBody: Option[String],
                                      maybeTextBody: Option[String],
-                                     inlineAttachments: List[(Attachment, Blob, Array[Byte])],
-                                     normalAttachments: List[(Attachment, Blob, Array[Byte])],
-                                     htmlTextExtractor: HtmlTextExtractor) = {
+                                     inlineAttachments: List[LoadedAttachment],
+                                     normalAttachments: List[LoadedAttachment],
+                                     htmlTextExtractor: HtmlTextExtractor): MultipartBuilder = {
     val mixedMultipartBuilder = MultipartBuilder.create(SubType.MIXED_SUBTYPE)
     val relatedMultipartBuilder = MultipartBuilder.create(SubType.RELATED_SUBTYPE)
     relatedMultipartBuilder.addBodyPart(BodyPartBuilder.create().setBody(createAlternativeBody(maybeHtmlBody, maybeTextBody, htmlTextExtractor).build))
     inlineAttachments.foldLeft(relatedMultipartBuilder) {
-      case (acc, (attachment, blob, content)) =>
-        acc.addBodyPart(toBodypartBuilder(attachment, blob, content))
+      case (acc, loadedAttachment) =>
+        acc.addBodyPart(toBodypartBuilder(loadedAttachment))
         acc
     }
 
     mixedMultipartBuilder.addBodyPart(BodyPartBuilder.create().setBody(relatedMultipartBuilder.build))
 
     normalAttachments.foldLeft(mixedMultipartBuilder) {
-      case (acc, (attachment, blob, content)) =>
-        acc.addBodyPart(toBodypartBuilder(attachment, blob, content))
+      case (acc, loadedAttachment) =>
+        acc.addBodyPart(toBodypartBuilder(loadedAttachment))
         acc
     }
   }
 
-  private def createMixedBody(maybeHtmlBody: Option[String], maybeTextBody: Option[String], normalAttachments: List[(Attachment, Blob, Array[Byte])], htmlTextExtractor: HtmlTextExtractor) = {
+  private def createMixedBody(maybeHtmlBody: Option[String], maybeTextBody: Option[String], normalAttachments: List[LoadedAttachment], htmlTextExtractor: HtmlTextExtractor) = {
     val mixedMultipartBuilder = MultipartBuilder.create(SubType.MIXED_SUBTYPE)
     mixedMultipartBuilder.addBodyPart(BodyPartBuilder.create().setBody(createAlternativeBody(maybeHtmlBody, maybeTextBody, htmlTextExtractor).build))
     normalAttachments.foldLeft(mixedMultipartBuilder) {
-      case (acc, (attachment, blob, content)) =>
-        acc.addBodyPart(toBodypartBuilder(attachment, blob, content))
+      case (acc, loadedAttachment) =>
+        acc.addBodyPart(toBodypartBuilder(loadedAttachment))
         acc
     }
   }
 
-  private def createRelatedBody(maybeHtmlBody: Option[String], maybeTextBody: Option[String], inlineAttachments: List[(Attachment, Blob, Array[Byte])], htmlTextExtractor: HtmlTextExtractor) = {
+  private def createRelatedBody(maybeHtmlBody: Option[String], maybeTextBody: Option[String], inlineAttachments: List[LoadedAttachment], htmlTextExtractor: HtmlTextExtractor) = {
     val relatedMultipartBuilder = MultipartBuilder.create(SubType.RELATED_SUBTYPE)
     relatedMultipartBuilder.addBodyPart(BodyPartBuilder.create().setBody(createAlternativeBody(maybeHtmlBody, maybeTextBody, htmlTextExtractor).build))
     inlineAttachments.foldLeft(relatedMultipartBuilder) {
-      case (acc, (attachment, blob, content)) =>
-        acc.addBodyPart(toBodypartBuilder(attachment, blob, content))
+      case (acc, loadedAttachment) =>
+        acc.addBodyPart(toBodypartBuilder(loadedAttachment))
         acc
     }
     relatedMultipartBuilder
   }
 
-  private def toBodypartBuilder(attachment: Attachment, blob: Blob, content: Array[Byte]) = {
+  private def toBodypartBuilder(loadedAttachment: LoadedAttachment) = {
     val bodypartBuilder = BodyPartBuilder.create()
-    bodypartBuilder.setBody(content, attachment.`type`.value)
+    val attachment = loadedAttachment.attachment
+    val blob = loadedAttachment.blob
+    bodypartBuilder.setBody(loadedAttachment.content, attachment.`type`.value)
       .setField(contentTypeField(attachment, blob))
       .setContentDisposition(attachment.disposition.getOrElse(Disposition.ATTACHMENT).value)
     attachment.cid.map(_.asField).foreach(bodypartBuilder.addField)
@@ -315,6 +317,10 @@ case class EmailCreationRequest(mailboxIds: MailboxIds,
   }
 }
 
+case class LoadedAttachment(attachment: Attachment, blob: Blob, content: Array[Byte]) {
+  def isInline: Boolean = attachment.isInline
+}
+
 case class DestroyIds(value: Seq[UnparsedMessageId])
 
 case class EmailSetRequest(accountId: AccountId,

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org