chibenwa commented on code in PR #2442: URL: https://github.com/apache/james-project/pull/2442#discussion_r1793202687
########## server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/SearchSnippetGetMethod.scala: ########## @@ -0,0 +1,108 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jmap.method + +import eu.timepit.refined.auto._ +import jakarta.inject.Inject +import org.apache.james.jmap.core.CapabilityIdentifier.{CapabilityIdentifier, JMAP_CORE, JMAP_MAIL} +import org.apache.james.jmap.core.Invocation.{Arguments, MethodName} +import org.apache.james.jmap.core.{CapabilityIdentifier, Invocation, SessionTranslator} +import org.apache.james.jmap.highlight.{SearchSnippetGetRequest, SearchSnippetGetResponse} +import org.apache.james.jmap.json.SearchSnippetSerializer +import org.apache.james.jmap.mail.UnparsedEmailId +import org.apache.james.jmap.routes.SessionSupplier +import org.apache.james.mailbox.MailboxSession +import org.apache.james.mailbox.model.MultimailboxesSearchQuery.{AccessibleNamespace, Namespace, PersonalNamespace} +import org.apache.james.mailbox.model.SearchQuery.ConjunctionCriterion +import org.apache.james.mailbox.model.{MessageId, MultimailboxesSearchQuery, SearchQuery} +import org.apache.james.mailbox.searchhighligt.{SearchHighlighter, SearchSnippet} +import org.apache.james.metrics.api.MetricFactory +import org.reactivestreams.Publisher +import play.api.libs.json.JsObject +import reactor.core.scala.publisher.{SFlux, SMono} + +import scala.jdk.CollectionConverters._ +import scala.util.Try + +class SearchSnippetGetMethod @Inject()(serializer: SearchSnippetSerializer, + searchHighlighter: SearchHighlighter, + messageIdFactory: MessageId.Factory, + val metricFactory: MetricFactory, + val sessionSupplier: SessionSupplier, + val sessionTranslator: SessionTranslator) extends MethodRequiringAccountId[SearchSnippetGetRequest] { + + override val methodName: MethodName = MethodName("SearchSnippet/get") + override val requiredCapabilities: Set[CapabilityIdentifier] = Set(JMAP_CORE, JMAP_MAIL) + + override def getRequest(mailboxSession: MailboxSession, invocation: Invocation): Either[Exception, SearchSnippetGetRequest] = serializer.deserializeSearchSnippetGetRequest(invocation.arguments.value) + .asEitherRequest + + override def doProcess(capabilities: Set[CapabilityIdentifier], invocation: InvocationWithContext, mailboxSession: MailboxSession, request: SearchSnippetGetRequest): Publisher[InvocationWithContext] = + processRequest(mailboxSession, invocation.invocation, request, capabilities) + .map(invocationResult => InvocationWithContext(invocationResult, invocation.processingContext)) + + private def processRequest(mailboxSession: MailboxSession, + invocation: Invocation, + request: SearchSnippetGetRequest, + capabilities: Set[CapabilityIdentifier]): SMono[Invocation] = + getSearchSnippet(request, mailboxSession, capabilities) + .map(searchSnippetList => SearchSnippetGetResponse.from(request.accountId, searchSnippetList.toList, request.emailIds)) + .map(response => Invocation( + methodName = methodName, + arguments = Arguments(serializer.serialize(response).as[JsObject]), + methodCallId = invocation.methodCallId)) + + private def getSearchSnippet(request: SearchSnippetGetRequest, + mailboxSession: MailboxSession, + capabilities: Set[CapabilityIdentifier]): SMono[Seq[SearchSnippet]] = + if (request.emptyRequest) { + SMono.just(Seq.empty) + } else { + SMono.fromCallable(() => request.emailIds.flatMap(asMessageId(_).toOption)) Review Comment: Wrapping here in a mono isn't necessary ########## server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/SearchSnippetGetMethod.scala: ########## @@ -0,0 +1,108 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jmap.method + +import eu.timepit.refined.auto._ +import jakarta.inject.Inject +import org.apache.james.jmap.core.CapabilityIdentifier.{CapabilityIdentifier, JMAP_CORE, JMAP_MAIL} +import org.apache.james.jmap.core.Invocation.{Arguments, MethodName} +import org.apache.james.jmap.core.{CapabilityIdentifier, Invocation, SessionTranslator} +import org.apache.james.jmap.highlight.{SearchSnippetGetRequest, SearchSnippetGetResponse} +import org.apache.james.jmap.json.SearchSnippetSerializer +import org.apache.james.jmap.mail.UnparsedEmailId +import org.apache.james.jmap.routes.SessionSupplier +import org.apache.james.mailbox.MailboxSession +import org.apache.james.mailbox.model.MultimailboxesSearchQuery.{AccessibleNamespace, Namespace, PersonalNamespace} +import org.apache.james.mailbox.model.SearchQuery.ConjunctionCriterion +import org.apache.james.mailbox.model.{MessageId, MultimailboxesSearchQuery, SearchQuery} +import org.apache.james.mailbox.searchhighligt.{SearchHighlighter, SearchSnippet} +import org.apache.james.metrics.api.MetricFactory +import org.reactivestreams.Publisher +import play.api.libs.json.JsObject +import reactor.core.scala.publisher.{SFlux, SMono} + +import scala.jdk.CollectionConverters._ +import scala.util.Try + +class SearchSnippetGetMethod @Inject()(serializer: SearchSnippetSerializer, + searchHighlighter: SearchHighlighter, + messageIdFactory: MessageId.Factory, + val metricFactory: MetricFactory, + val sessionSupplier: SessionSupplier, + val sessionTranslator: SessionTranslator) extends MethodRequiringAccountId[SearchSnippetGetRequest] { + + override val methodName: MethodName = MethodName("SearchSnippet/get") + override val requiredCapabilities: Set[CapabilityIdentifier] = Set(JMAP_CORE, JMAP_MAIL) + + override def getRequest(mailboxSession: MailboxSession, invocation: Invocation): Either[Exception, SearchSnippetGetRequest] = serializer.deserializeSearchSnippetGetRequest(invocation.arguments.value) + .asEitherRequest + + override def doProcess(capabilities: Set[CapabilityIdentifier], invocation: InvocationWithContext, mailboxSession: MailboxSession, request: SearchSnippetGetRequest): Publisher[InvocationWithContext] = + processRequest(mailboxSession, invocation.invocation, request, capabilities) + .map(invocationResult => InvocationWithContext(invocationResult, invocation.processingContext)) + + private def processRequest(mailboxSession: MailboxSession, + invocation: Invocation, + request: SearchSnippetGetRequest, + capabilities: Set[CapabilityIdentifier]): SMono[Invocation] = + getSearchSnippet(request, mailboxSession, capabilities) + .map(searchSnippetList => SearchSnippetGetResponse.from(request.accountId, searchSnippetList.toList, request.emailIds)) + .map(response => Invocation( + methodName = methodName, + arguments = Arguments(serializer.serialize(response).as[JsObject]), + methodCallId = invocation.methodCallId)) + + private def getSearchSnippet(request: SearchSnippetGetRequest, + mailboxSession: MailboxSession, + capabilities: Set[CapabilityIdentifier]): SMono[Seq[SearchSnippet]] = + if (request.emptyRequest) { + SMono.just(Seq.empty) + } else { + SMono.fromCallable(() => request.emailIds.flatMap(asMessageId(_).toOption)) + .filter(messageIds => messageIds.nonEmpty) + .flatMap(messageIds => buildMultiMailboxesSearchQuery(request, messageIds, capabilities, mailboxSession) + .fold(e => SMono.error(e), + multiMailboxesSearchQuery => SFlux(searchHighlighter.highlightSearch(messageIds.asJava, multiMailboxesSearchQuery, mailboxSession)) + .collectSeq())) + .switchIfEmpty(SMono.just(Seq.empty)) + } + + private def asMessageId(id: UnparsedEmailId): Either[(UnparsedEmailId, IllegalArgumentException), MessageId] = + Try(messageIdFactory.fromString(id.id.value)) + .toEither + .left.map(error => (id, new IllegalArgumentException(s"""Can not parse UnparsedEmailId(${id.id.value}) as MessageId""", error))) Review Comment: ```suggestion .left.map(error => (id, new IllegalArgumentException(s"Can not parse UnparsedEmailId(${id.id.value}) as MessageId", error))) ``` ########## mailbox/lucene/src/main/java/org/apache/james/mailbox/lucene/search/LuceneMessageSearchIndex.java: ########## @@ -445,8 +445,9 @@ public org.apache.james.events.Group getDefaultGroup() { @Override public EnumSet<SearchCapabilities> getSupportedCapabilities(EnumSet<MailboxManager.MessageCapabilities> messageCapabilities) { - return EnumSet.of(SearchCapabilities.MultimailboxSearch); - + return EnumSet.of(SearchCapabilities.MultimailboxSearch, + SearchCapabilities.Attachment, Review Comment: Let's adda search capability: search highlights? ########## server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/highlight/SearchSnippetGetRequest.scala: ########## @@ -0,0 +1,113 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jmap.highlight + +import cats.implicits._ +import com.google.common.base.Preconditions +import org.apache.james.jmap.core.AccountId +import org.apache.james.jmap.highlight.SearchSnippetGetRequest.{filterAsCriterionList, validateFilter} +import org.apache.james.jmap.mail.{And, Email, FilterCondition, FilterOperator, FilterQuery, UnparsedEmailId, UnsupportedFilterException, UnsupportedNestingException} +import org.apache.james.jmap.method.{GetRequest, WithAccountId} +import org.apache.james.jmap.utils.search.MailboxFilter.{AndFilter, Body, NotFilter, OrFilter, QueryFilter, Subject, Text} +import org.apache.james.mailbox.model.SearchQuery.Criterion +import org.apache.james.mailbox.model.{MessageId, SearchQuery} +import org.apache.james.mailbox.searchhighligt.{SearchSnippet => JavaSearchSnippet} + +import scala.jdk.CollectionConverters._ +import scala.jdk.OptionConverters._ + +object SearchSnippetGetRequest { + + private val FILTER_SUPPORT_LIST: List[QueryFilter] = List(Text, Subject, Body, NotFilter, OrFilter, AndFilter) + + private def filterAsCriterionList(filterQuery: FilterQuery): Either[UnsupportedFilterException, List[Criterion]] = + FILTER_SUPPORT_LIST.traverse(_.toQuery(filterQuery)).map(_.flatten) + + def validateFilter(filter: FilterQuery): Either[UnsupportedNestingException, FilterQuery] = filter match { + case filterCondition: FilterCondition => Right(filterCondition) + case filterOperator: FilterOperator if filterOperator.operator == And => + val (nestedCount, topLevelCount) = (filter.countNestedMailboxFilter, filter.countMailboxFilter) + (nestedCount, topLevelCount) match { + case (1, 1) | (0, _) => Right(filterOperator) + case _ => rejectMailboxFilters(filterOperator) + } + case operator: FilterOperator => rejectMailboxFilters(operator) + } + + private def rejectMailboxFilters(filter: FilterQuery): Either[UnsupportedNestingException, FilterQuery] = + filter match { + case filterCondition: FilterCondition if filterCondition.inMailbox.isDefined => + scala.Left(UnsupportedNestingException("Nested inMailbox filters are not supported")) + case filterCondition: FilterCondition if filterCondition.inMailboxOtherThan.isDefined => + scala.Left(UnsupportedNestingException("Nested inMailboxOtherThan filter are not supported")) + case filterCondition: FilterCondition => Right(filterCondition) + case filterOperator: FilterOperator => + filterOperator.conditions.toList + .traverse(rejectMailboxFilters) + .map(_ => filterOperator) + } Review Comment: THis smells copy and paste. This code is rather complex and define limitations we might one day try to overcome. Let's please not duplicate it. ########## server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/SearchSnippetGetMethod.scala: ########## @@ -0,0 +1,108 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jmap.method + +import eu.timepit.refined.auto._ +import jakarta.inject.Inject +import org.apache.james.jmap.core.CapabilityIdentifier.{CapabilityIdentifier, JMAP_CORE, JMAP_MAIL} +import org.apache.james.jmap.core.Invocation.{Arguments, MethodName} +import org.apache.james.jmap.core.{CapabilityIdentifier, Invocation, SessionTranslator} +import org.apache.james.jmap.highlight.{SearchSnippetGetRequest, SearchSnippetGetResponse} +import org.apache.james.jmap.json.SearchSnippetSerializer +import org.apache.james.jmap.mail.UnparsedEmailId +import org.apache.james.jmap.routes.SessionSupplier +import org.apache.james.mailbox.MailboxSession +import org.apache.james.mailbox.model.MultimailboxesSearchQuery.{AccessibleNamespace, Namespace, PersonalNamespace} +import org.apache.james.mailbox.model.SearchQuery.ConjunctionCriterion +import org.apache.james.mailbox.model.{MessageId, MultimailboxesSearchQuery, SearchQuery} +import org.apache.james.mailbox.searchhighligt.{SearchHighlighter, SearchSnippet} +import org.apache.james.metrics.api.MetricFactory +import org.reactivestreams.Publisher +import play.api.libs.json.JsObject +import reactor.core.scala.publisher.{SFlux, SMono} + +import scala.jdk.CollectionConverters._ +import scala.util.Try + +class SearchSnippetGetMethod @Inject()(serializer: SearchSnippetSerializer, + searchHighlighter: SearchHighlighter, + messageIdFactory: MessageId.Factory, + val metricFactory: MetricFactory, + val sessionSupplier: SessionSupplier, + val sessionTranslator: SessionTranslator) extends MethodRequiringAccountId[SearchSnippetGetRequest] { + + override val methodName: MethodName = MethodName("SearchSnippet/get") + override val requiredCapabilities: Set[CapabilityIdentifier] = Set(JMAP_CORE, JMAP_MAIL) + + override def getRequest(mailboxSession: MailboxSession, invocation: Invocation): Either[Exception, SearchSnippetGetRequest] = serializer.deserializeSearchSnippetGetRequest(invocation.arguments.value) + .asEitherRequest + + override def doProcess(capabilities: Set[CapabilityIdentifier], invocation: InvocationWithContext, mailboxSession: MailboxSession, request: SearchSnippetGetRequest): Publisher[InvocationWithContext] = + processRequest(mailboxSession, invocation.invocation, request, capabilities) + .map(invocationResult => InvocationWithContext(invocationResult, invocation.processingContext)) + + private def processRequest(mailboxSession: MailboxSession, + invocation: Invocation, + request: SearchSnippetGetRequest, + capabilities: Set[CapabilityIdentifier]): SMono[Invocation] = + getSearchSnippet(request, mailboxSession, capabilities) + .map(searchSnippetList => SearchSnippetGetResponse.from(request.accountId, searchSnippetList.toList, request.emailIds)) + .map(response => Invocation( + methodName = methodName, + arguments = Arguments(serializer.serialize(response).as[JsObject]), + methodCallId = invocation.methodCallId)) + + private def getSearchSnippet(request: SearchSnippetGetRequest, + mailboxSession: MailboxSession, + capabilities: Set[CapabilityIdentifier]): SMono[Seq[SearchSnippet]] = + if (request.emptyRequest) { + SMono.just(Seq.empty) + } else { + SMono.fromCallable(() => request.emailIds.flatMap(asMessageId(_).toOption)) + .filter(messageIds => messageIds.nonEmpty) + .flatMap(messageIds => buildMultiMailboxesSearchQuery(request, messageIds, capabilities, mailboxSession) + .fold(e => SMono.error(e), + multiMailboxesSearchQuery => SFlux(searchHighlighter.highlightSearch(messageIds.asJava, multiMailboxesSearchQuery, mailboxSession)) Review Comment: searchHighlighter do already ensure the user is doing the search on mailboxes he have access to, right? ########## server/protocols/jmap-rfc-8621-integration-tests/memory-jmap-rfc-8621-integration-tests/src/test/java/org/apache/james/jmap/rfc8621/memory/MemorySearchSnippetGetMethodTest.java: ########## @@ -0,0 +1,92 @@ +/**************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + ****************************************************************/ + +package org.apache.james.jmap.rfc8621.memory; + +import static org.apache.james.data.UsersRepositoryModuleChooser.Implementation.DEFAULT; + +import java.io.IOException; + +import org.apache.james.JamesServerBuilder; +import org.apache.james.JamesServerExtension; +import org.apache.james.MemoryJamesConfiguration; +import org.apache.james.MemoryJamesServerMain; +import org.apache.james.events.EventListener; +import org.apache.james.jmap.rfc8621.contract.IdentityProbeModule; +import org.apache.james.jmap.rfc8621.contract.SearchSnippetGetMethodContract; +import org.apache.james.jmap.rfc8621.contract.probe.DelegationProbeModule; +import org.apache.james.mailbox.lucene.search.LuceneMessageSearchIndex; +import org.apache.james.mailbox.lucene.search.LuceneSearchHighlighter; +import org.apache.james.mailbox.searchhighligt.SearchHighlighter; +import org.apache.james.mailbox.searchhighligt.SearchHighlighterConfiguration; +import org.apache.james.mailbox.store.search.ListeningMessageSearchIndex; +import org.apache.james.mailbox.store.search.MessageSearchIndex; +import org.apache.james.modules.TestJMAPServerModule; +import org.apache.lucene.store.ByteBuffersDirectory; +import org.apache.lucene.store.Directory; +import org.junit.jupiter.api.extension.RegisterExtension; + +import com.google.inject.AbstractModule; +import com.google.inject.Provides; +import com.google.inject.Scopes; +import com.google.inject.Singleton; +import com.google.inject.multibindings.Multibinder; + +public class MemorySearchSnippetGetMethodTest implements SearchSnippetGetMethodContract { + + static class LuceneMemorySearchSnippetModule extends AbstractModule { Review Comment: Should be done as part of the memory server! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@james.apache.org For additional commands, e-mail: notifications-h...@james.apache.org