You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2020/10/02 08:48:40 UTC

[james-project] 04/08: JAMES-3390 Implement FilterOperator OR

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

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

commit 085c105bff22144ccf0d691c4eb054bdc263f9f3
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Wed Sep 30 10:20:28 2020 +0700

    JAMES-3390 Implement FilterOperator OR
---
 .../contract/EmailQueryMethodContract.scala        | 65 ++++++++++++++++++++++
 .../james/jmap/json/EmailQuerySerializer.scala     |  3 +-
 .../org/apache/james/jmap/mail/EmailQuery.scala    |  1 +
 .../james/jmap/utils/search/MailboxFilter.scala    | 37 ++++++++----
 4 files changed, 93 insertions(+), 13 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/EmailQueryMethodContract.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailQueryMethodContract.scala
index 8cc4b2a..b269e11 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailQueryMethodContract.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailQueryMethodContract.scala
@@ -5039,6 +5039,71 @@ trait EmailQueryMethodContract {
   }
 
   @Test
+  def emailQueryShouldSupportOrOperator(server: GuiceJamesServer): Unit = {
+    val message: Message = buildTestMessage
+    server.getProbe(classOf[MailboxProbeImpl]).createMailbox(MailboxPath.inbox(BOB))
+    val messageId1 = server.getProbe(classOf[MailboxProbeImpl])
+      .appendMessage(BOB.asString, MailboxPath.inbox(BOB), AppendCommand.builder()
+        .withFlags(new Flags("custom"))
+        .build(message))
+      .getMessageId
+
+    val messageId2 = server.getProbe(classOf[MailboxProbeImpl])
+      .appendMessage(BOB.asString, MailboxPath.inbox(BOB), AppendCommand.builder()
+        .withFlags(new Flags("another_custom"))
+        .build(message))
+      .getMessageId
+
+    val messageId3 = server.getProbe(classOf[MailboxProbeImpl])
+      .appendMessage(BOB.asString, MailboxPath.inbox(BOB), AppendCommand.builder()
+        .withFlags(new FlagsBuilder().add("custom", "another_custom").build())
+        .build(message))
+      .getMessageId
+
+    val messageId4 = server.getProbe(classOf[MailboxProbeImpl])
+      .appendMessage(BOB.asString, MailboxPath.inbox(BOB), AppendCommand.builder().build(message))
+      .getMessageId
+
+    val request =
+      s"""{
+         |  "using": [
+         |    "urn:ietf:params:jmap:core",
+         |    "urn:ietf:params:jmap:mail"],
+         |  "methodCalls": [[
+         |    "Email/query",
+         |    {
+         |      "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+         |      "filter" : {
+         |        "operator": "OR",
+         |        "conditions": [
+         |          { "hasKeyword": "custom" }, { "hasKeyword": "another_custom" }
+         |        ]
+         |      }
+         |    },
+         |    "c1"]]
+         |}""".stripMargin
+
+    awaitAtMostTenSeconds.untilAsserted { () =>
+      val response = `given`
+        .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+        .body(request)
+      .when
+        .post
+      .`then`
+        .statusCode(SC_OK)
+        .contentType(JSON)
+        .extract
+        .body
+        .asString
+
+      assertThatJson(response)
+        .withOptions(new Options(IGNORING_ARRAY_ORDER))
+        .inPath("$.methodResponses[0][1].ids")
+        .isEqualTo(s"""["${messageId3.serialize}", "${messageId2.serialize}", "${messageId1.serialize}"]""")
+    }
+  }
+
+  @Test
   def inMailboxShouldBeRejectedWhenInOperator(server: GuiceJamesServer): Unit = {
     val message: Message = buildTestMessage
     val mailboxId = server.getProbe(classOf[MailboxProbeImpl]).createMailbox(MailboxPath.inbox(BOB))
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailQuerySerializer.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailQuerySerializer.scala
index de6f5f3..cb666d3 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailQuerySerializer.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/json/EmailQuerySerializer.scala
@@ -20,7 +20,7 @@
 package org.apache.james.jmap.json
 
 import javax.inject.Inject
-import org.apache.james.jmap.mail.{AllInThreadHaveKeywordSortProperty, Anchor, AnchorOffset, And, Bcc, Body, Cc, CollapseThreads, Collation, Comparator, EmailQueryRequest, EmailQueryResponse, FilterCondition, FilterOperator, FilterQuery, From, FromSortProperty, HasAttachment, HasKeywordSortProperty, Header, HeaderContains, HeaderExist, IsAscending, Operator, ReceivedAtSortProperty, SentAtSortProperty, SizeSortProperty, SomeInThreadHaveKeywordSortProperty, SortProperty, Subject, SubjectSo [...]
+import org.apache.james.jmap.mail.{AllInThreadHaveKeywordSortProperty, Anchor, AnchorOffset, And, Bcc, Body, Cc, CollapseThreads, Collation, Comparator, EmailQueryRequest, EmailQueryResponse, FilterCondition, FilterOperator, FilterQuery, From, FromSortProperty, HasAttachment, HasKeywordSortProperty, Header, HeaderContains, HeaderExist, IsAscending, Operator, Or, ReceivedAtSortProperty, SentAtSortProperty, SizeSortProperty, SomeInThreadHaveKeywordSortProperty, SortProperty, Subject, Subje [...]
 import org.apache.james.jmap.model.{AccountId, CanCalculateChanges, Keyword, LimitUnparsed, PositionUnparsed, QueryState}
 import org.apache.james.mailbox.model.{MailboxId, MessageId}
 import play.api.libs.json._
@@ -76,6 +76,7 @@ class EmailQuerySerializer @Inject()(mailboxIdFactory: MailboxId.Factory) {
 
   private implicit val operatorReads: Reads[Operator] = {
     case JsString("AND") => JsSuccess(And)
+    case JsString("OR") => JsSuccess(Or)
     case _ => JsError(s"Expecting a JsString to represent a known operator")
   }
 
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailQuery.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailQuery.scala
index 2badc57..d6f9c54 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailQuery.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/mail/EmailQuery.scala
@@ -39,6 +39,7 @@ sealed trait FilterQuery
 
 sealed trait Operator
 case object And extends Operator
+case object Or extends Operator
 
 case class FilterOperator(operator: Operator,
                           conditions: Seq[FilterQuery]) extends FilterQuery
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/utils/search/MailboxFilter.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/utils/search/MailboxFilter.scala
index 754e590..e111671 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/utils/search/MailboxFilter.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/utils/search/MailboxFilter.scala
@@ -21,7 +21,7 @@ package org.apache.james.jmap.utils.search
 import java.util.Date
 
 import cats.implicits._
-import org.apache.james.jmap.mail.{And, EmailQueryRequest, FilterCondition, FilterOperator, FilterQuery, HeaderContains, HeaderExist, UnsupportedFilterException}
+import org.apache.james.jmap.mail.{And, EmailQueryRequest, FilterCondition, FilterOperator, FilterQuery, HeaderContains, HeaderExist, Operator, Or, UnsupportedFilterException}
 import org.apache.james.jmap.model.CapabilityIdentifier
 import org.apache.james.jmap.model.CapabilityIdentifier.CapabilityIdentifier
 import org.apache.james.mailbox.MailboxSession
@@ -86,17 +86,30 @@ object MailboxFilter {
     def toQuery(filterCondition: FilterCondition): Either[UnsupportedFilterException, List[Criterion]]
   }
 
+  object OperatorQueryFilter {
+    def toQuery(filterQuery: FilterQuery,
+                converter: List[Criterion] => Criterion,
+                operator: Operator): Either[UnsupportedFilterException, List[Criterion]] =
+      filterQuery match {
+        case filterOperator: FilterOperator if filterOperator.operator.equals(operator) =>
+          filterOperator.conditions
+            .map(QueryFilter.toCriterion)
+            .toList
+            .sequence
+            .map(_.flatten)
+            .map(criteria => List(converter.apply(criteria)))
+        case _ => Right(Nil)
+      }
+  }
+
   case object AndFilter extends QueryFilter {
-    override def toQuery(filterQuery: FilterQuery): Either[UnsupportedFilterException, List[Criterion]] = filterQuery match {
-      case filterOperator: FilterOperator if filterOperator.operator.equals(And) =>
-        filterOperator.conditions
-          .map(QueryFilter.toCriterion)
-          .toList
-          .sequence
-          .map(_.flatten)
-          .map(criteria => List(SearchQuery.and(criteria.asJava)))
-      case _ => Right(Nil)
-    }
+    override def toQuery(filterQuery: FilterQuery): Either[UnsupportedFilterException, List[Criterion]] =
+      OperatorQueryFilter.toQuery(filterQuery, criteria => SearchQuery.and(criteria.asJava), And)
+  }
+
+  case object OrFilter extends QueryFilter {
+    override def toQuery(filterQuery: FilterQuery): Either[UnsupportedFilterException, List[Criterion]] =
+      OperatorQueryFilter.toQuery(filterQuery, criteria => SearchQuery.or(criteria.asJava), Or)
   }
 
   object QueryFilter {
@@ -109,7 +122,7 @@ object MailboxFilter {
     def toCriterion(filterQuery: FilterQuery): Either[UnsupportedFilterException, List[Criterion]] =
       List(ReceivedBefore, ReceivedAfter, HasAttachment, HasKeyWord, NotKeyWord, MinSize, MaxSize,
            AllInThreadHaveKeyword, NoneInThreadHaveKeyword, SomeInThreadHaveKeyword, Text, From,
-           To, Cc, Bcc, Subject, Header, Body, AndFilter)
+           To, Cc, Bcc, Subject, Header, Body, AndFilter, OrFilter)
         .map(filter => filter.toQuery(filterQuery))
         .sequence
         .map(list => list.flatten)


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