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 2024/02/23 16:07:17 UTC

(james-project) branch master updated: JMAP - EmailSubmission/set - Should not invoke onSuccessDestroyEmail/onSuccessUpdateEmail when not created (#2041)

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


The following commit(s) were added to refs/heads/master by this push:
     new 169ae5b8e8 JMAP - EmailSubmission/set - Should not invoke onSuccessDestroyEmail/onSuccessUpdateEmail when not created (#2041)
169ae5b8e8 is described below

commit 169ae5b8e8287823247905662459e12dadc3026b
Author: vttran <vt...@linagora.com>
AuthorDate: Fri Feb 23 23:07:11 2024 +0700

    JMAP - EmailSubmission/set - Should not invoke onSuccessDestroyEmail/onSuccessUpdateEmail when not created (#2041)
---
 .../EmailSubmissionSetMethodContract.scala         | 122 +++++++++++++++++++++
 .../james/jmap/mail/EmailSubmissionSet.scala       |  30 ++---
 .../jmap/method/EmailSubmissionSetMethod.scala     |  26 ++---
 3 files changed, 151 insertions(+), 27 deletions(-)

diff --git a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailSubmissionSetMethodContract.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailSubmissionSetMethodContract.scala
index d8c5622928..76fa078296 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailSubmissionSetMethodContract.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailSubmissionSetMethodContract.scala
@@ -1989,4 +1989,126 @@ trait EmailSubmissionSetMethodContract {
         .hasSize(1)
     }
   }
+
+  @Test
+  def onSuccessUpdateEmailShouldNotInvokedWhenEmailSubmissionNotCreated(): Unit = {
+    val requestBob =
+      s"""{
+         |  "using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail", "urn:ietf:params:jmap:submission"],
+         |  "methodCalls": [
+         |     ["EmailSubmission/set", {
+         |       "accountId": "$ACCOUNT_ID",
+         |       "create": {
+         |         "k1490": {
+         |           "emailId": "${randomMessageId.serialize}",
+         |           "extra": true,
+         |           "envelope": {
+         |             "mailFrom": {"email": "${BOB.asString}"},
+         |             "rcptTo": [{"email": "${ANDRE.asString}"}]
+         |           }
+         |         }
+         |    },
+         |    "onSuccessUpdateEmail": {
+         |         "#k1490": {
+         |           "keywords": {"$$sent":true}
+         |         }
+         |       }
+         |  }, "c1"]]
+         |}""".stripMargin
+
+    val response = `given`
+      .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+      .body(requestBob)
+    .when
+      .post
+    .`then`
+      .statusCode(SC_OK)
+      .contentType(JSON)
+      .extract
+      .body
+      .asString
+
+    assertThatJson(response)
+      .isEqualTo(s"""{
+                   |  "sessionState": "${SESSION_STATE.value}",
+                   |  "methodResponses": [
+                   |    [
+                   |      "EmailSubmission/set",
+                   |      {
+                   |        "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+                   |        "newState": "2c9f1b12-b35a-43e6-9af2-0106fb53a943",
+                   |        "notCreated": {
+                   |          "k1490": {
+                   |            "type": "invalidArguments",
+                   |            "description": "Some unknown properties were specified",
+                   |            "properties": [
+                   |              "extra"
+                   |            ]
+                   |          }
+                   |        }
+                   |      },
+                   |      "c1"
+                   |    ]
+                   |  ]
+                   |}""".stripMargin)
+  }
+
+  @Test
+  def onSuccessDestroyEmailShouldNotInvokedWhenEmailSubmissionNotCreated(): Unit = {
+    val requestBob =
+      s"""{
+         |  "using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail", "urn:ietf:params:jmap:submission"],
+         |  "methodCalls": [
+         |     ["EmailSubmission/set", {
+         |       "accountId": "$ACCOUNT_ID",
+         |       "create": {
+         |         "k1490": {
+         |           "emailId": "${randomMessageId.serialize}",
+         |           "extra": true,
+         |           "envelope": {
+         |             "mailFrom": {"email": "${BOB.asString}"},
+         |             "rcptTo": [{"email": "${ANDRE.asString}"}]
+         |           }
+         |         }
+         |    },
+         |    "onSuccessDestroyEmail": ["#k1490"]
+         |  }, "c1"]]
+         |}""".stripMargin
+
+    val response = `given`
+      .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+      .body(requestBob)
+    .when
+      .post
+    .`then`
+      .statusCode(SC_OK)
+      .contentType(JSON)
+      .extract
+      .body
+      .asString
+
+    assertThatJson(response)
+      .isEqualTo(s"""{
+                    |  "sessionState": "${SESSION_STATE.value}",
+                    |  "methodResponses": [
+                    |    [
+                    |      "EmailSubmission/set",
+                    |      {
+                    |        "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+                    |        "newState": "2c9f1b12-b35a-43e6-9af2-0106fb53a943",
+                    |        "notCreated": {
+                    |          "k1490": {
+                    |            "type": "invalidArguments",
+                    |            "description": "Some unknown properties were specified",
+                    |            "properties": [
+                    |              "extra"
+                    |            ]
+                    |          }
+                    |        }
+                    |      },
+                    |      "c1"
+                    |    ]
+                    |  ]
+                    |}""".stripMargin)
+  }
 }
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSubmissionSet.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSubmissionSet.scala
index 9864ff341b..95b67e2983 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSubmissionSet.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailSubmissionSet.scala
@@ -47,35 +47,39 @@ case class EmailSubmissionSetRequest(accountId: AccountId,
                                      create: Option[Map[EmailSubmissionCreationId, JsObject]],
                                      onSuccessUpdateEmail: Option[Map[EmailSubmissionCreationId, JsObject]],
                                      onSuccessDestroyEmail: Option[List[EmailSubmissionCreationId]]) extends WithAccountId {
-  def implicitEmailSetRequest(messageIdResolver: EmailSubmissionCreationId => Either[IllegalArgumentException, MessageId]): Either[IllegalArgumentException, Option[EmailSetRequest]] =
+  def implicitEmailSetRequest(messageIdResolver: EmailSubmissionCreationId => Either[IllegalArgumentException, Option[MessageId]]): Either[IllegalArgumentException, Option[EmailSetRequest]] =
     for {
       update <- resolveOnSuccessUpdateEmail(messageIdResolver)
       destroy <- resolveOnSuccessDestroyEmail(messageIdResolver)
     } yield {
-      if (update.isEmpty && destroy.isEmpty) {
-        None
-      } else {
+      if (update.exists(_.nonEmpty) || destroy.exists(_.nonEmpty)) {
         Some(EmailSetRequest(
           accountId = accountId,
           create = None,
           update = update,
           destroy = destroy.map(DestroyIds(_))))
+      } else {
+        None
       }
     }
 
-  def resolveOnSuccessUpdateEmail(messageIdResolver: EmailSubmissionCreationId => Either[IllegalArgumentException, MessageId]): Either[IllegalArgumentException, Option[Map[UnparsedMessageId, JsObject]]]=
+  def resolveOnSuccessUpdateEmail(messageIdResolver: EmailSubmissionCreationId => Either[IllegalArgumentException, Option[MessageId]]): Either[IllegalArgumentException, Option[Map[UnparsedMessageId, JsObject]]]=
     onSuccessUpdateEmail.map(map => map.toList
-      .map {
-        case (creationId, json) => messageIdResolver.apply(creationId).map(messageId => (EmailSet.asUnparsed(messageId), json))
-      }
-      .sequence
-      .map(list => list.toMap))
+        .map {
+          case (creationId, json) => messageIdResolver.apply(creationId).map(msgIdOption => (msgIdOption, json))
+        }
+        .filter(e => e.isLeft || e.toOption.get._1.isDefined)
+        .map(e => e.map { case (msgIdOption, json) => (EmailSet.asUnparsed(msgIdOption.get), json) })
+        .sequence
+        .map(list => list.toMap))
       .sequence
 
-  def resolveOnSuccessDestroyEmail(messageIdResolver: EmailSubmissionCreationId => Either[IllegalArgumentException, MessageId]): Either[IllegalArgumentException, Option[List[UnparsedMessageId]]]=
+  def resolveOnSuccessDestroyEmail(messageIdResolver: EmailSubmissionCreationId => Either[IllegalArgumentException, Option[MessageId]]): Either[IllegalArgumentException, Option[List[UnparsedMessageId]]]=
     onSuccessDestroyEmail.map(list => list
-      .map(creationId => messageIdResolver.apply(creationId).map(messageId => EmailSet.asUnparsed(messageId)))
-      .sequence)
+        .map(creationId => messageIdResolver.apply(creationId))
+        .filter(e => e.isLeft || e.toOption.get.isDefined)
+        .map(e => e.map { case (msgIdOption) => EmailSet.asUnparsed(msgIdOption.get) })
+        .sequence)
       .sequence
 
   def validate: Either[IllegalArgumentException, EmailSubmissionSetRequest] = {
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/EmailSubmissionSetMethod.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/EmailSubmissionSetMethod.scala
index 63749f2bf5..a2be422488 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/EmailSubmissionSetMethod.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/EmailSubmissionSetMethod.scala
@@ -130,28 +130,26 @@ object EmailSubmissionSetMethod {
       }
       .toMap
 
-    def resolveMessageId(creationId: EmailSubmissionCreationId): Either[IllegalArgumentException, MessageId] = {
+    def resolveMessageId(creationId: EmailSubmissionCreationId): Either[IllegalArgumentException, Option[MessageId]] = {
       if (creationId.id.startsWith("#")) {
         val realId = creationId.id.substring(1)
         val validatedId: Either[String, Id] = refineV[IdConstraint](realId)
-        validatedId
+
+       validatedId
           .left.map(s => new IllegalArgumentException(s))
-          .flatMap(id => retrieveMessageId(EmailSubmissionCreationId(id))
+          .flatMap(id => retrieveCreationResult(EmailSubmissionCreationId(id))
             .map(scala.Right(_))
-            .getOrElse(Left(new IllegalArgumentException(s"$creationId cannot be referenced in current method call"))))
+            .getOrElse(Left(new IllegalArgumentException(s"${creationId.id} cannot be referenced in current method call"))))
+          .map {
+            case (success: CreationSuccess) => Some(success.messageId)
+            case (_: CreationFailure) => None
+          }
       } else {
-        Left(new IllegalArgumentException(s"$creationId cannot be retrieved as storage for EmailSubmission is not yet implemented"))
+        Left(new IllegalArgumentException(s"${creationId.id} cannot be retrieved as storage for EmailSubmission is not yet implemented"))
       }
     }
-
-    private def retrieveMessageId(creationId: EmailSubmissionCreationId): Option[MessageId] =
-      created.flatMap {
-        case success: CreationSuccess => Some(success)
-        case _: CreationFailure => None
-      }.filter(_.emailSubmissionCreationId.equals(creationId))
-        .map(_.messageId)
-        .toList
-        .headOption
+    private def retrieveCreationResult(creationId: EmailSubmissionCreationId): Option[CreationResult] =
+      created.find(_.emailSubmissionCreationId.equals(creationId))
   }
 }
 


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