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 2021/01/12 11:02:10 UTC
[james-project] 11/20: JAMES-3473 Email/get should return latest
state
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 2bbd5b2579fa7fe44086e8c7e699a0f34736bfb4
Author: LanKhuat <dl...@linagora.com>
AuthorDate: Tue Jan 5 10:45:57 2021 +0700
JAMES-3473 Email/get should return latest state
---
.../rfc8621/contract/EmailGetMethodContract.scala | 77 ++++++++++++++++++++++
.../apache/james/jmap/method/EmailGetMethod.scala | 16 +++--
2 files changed, 87 insertions(+), 6 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/EmailGetMethodContract.scala b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailGetMethodContract.scala
index 41ca8ec..7ddd52e 100644
--- a/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailGetMethodContract.scala
+++ b/server/protocols/jmap-rfc-8621-integration-tests/jmap-rfc-8621-integration-tests-common/src/main/scala/org/apache/james/jmap/rfc8621/contract/EmailGetMethodContract.scala
@@ -28,12 +28,16 @@ import io.restassured.RestAssured.{`given`, requestSpecification}
import io.restassured.http.ContentType.JSON
import javax.mail.Flags
import net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson
+import net.javacrumbs.jsonunit.core.Option
import net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER
import net.javacrumbs.jsonunit.core.internal.Options
import org.apache.http.HttpStatus.SC_OK
import org.apache.james.GuiceJamesServer
+import org.apache.james.jmap.api.change.{EmailChange, State}
+import org.apache.james.jmap.api.model.AccountId
import org.apache.james.jmap.core.ResponseObject.SESSION_STATE
import org.apache.james.jmap.core.State.INSTANCE
+import org.apache.james.jmap.draft.JmapGuiceProbe
import org.apache.james.jmap.http.UserCredential
import org.apache.james.jmap.rfc8621.contract.EmailGetMethodContract.createTestMessage
import org.apache.james.jmap.rfc8621.contract.Fixture.{ACCEPT_RFC8621_VERSION_HEADER, ALICE, ANDRE, BOB, BOB_PASSWORD, DOMAIN, authScheme, baseRequestSpecBuilder}
@@ -7046,4 +7050,77 @@ trait EmailGetMethodContract {
| "header:List-Help:asURLs": null
}""".stripMargin)
}
+
+ @Test
+ def emailStateShouldBeTheLatestOne(server: GuiceJamesServer): Unit = {
+ val mailboxProbe: MailboxProbeImpl = server.getProbe(classOf[MailboxProbeImpl])
+ val accountId: AccountId = AccountId.fromUsername(BOB)
+ val path: MailboxPath = MailboxPath.inbox(BOB)
+ mailboxProbe.createMailbox(path)
+ val message: Message = Message.Builder
+ .of
+ .setSubject("test")
+ .setSender(BOB.asString())
+ .setFrom(ANDRE.asString())
+ .setBody("testmail", StandardCharsets.UTF_8)
+ .build
+ val messageId: String = server.getProbe(classOf[MailboxProbeImpl])
+ .appendMessage(BOB.asString, path, AppendCommand.from(message))
+ .getMessageId
+ .serialize()
+
+ val state: State = storeReferenceState(server, accountId)
+
+ val response = `given`
+ .header(ACCEPT.toString, ACCEPT_RFC8621_VERSION_HEADER)
+ .body(
+ s"""{
+ | "using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
+ | "methodCalls": [[
+ | "Email/get",
+ | {
+ | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+ | "ids": ["$messageId"],
+ | "properties": ["id"]
+ | },
+ | "c1"]
+ | ]
+ |}""".stripMargin)
+ .when
+ .post
+ .`then`
+ .statusCode(SC_OK)
+ .extract()
+ .body()
+ .asString()
+
+ assertThatJson(response)
+ .withOptions(new Options(Option.IGNORING_ARRAY_ORDER))
+ .inPath("methodResponses[0][1]")
+ .isEqualTo(
+ s"""{
+ | "accountId": "29883977c13473ae7cb7678ef767cbfbaffc8a44a6e463d971d23a65c1dc4af6",
+ | "state": "${state.getValue}",
+ | "list":[
+ | {
+ | "id":"$messageId"
+ | }
+ | ],
+ | "notFound": []
+ |}""".stripMargin)
+ }
+
+ private def storeReferenceState(server: GuiceJamesServer, accountId: AccountId) = {
+ val jmapGuiceProbe: JmapGuiceProbe = server.getProbe(classOf[JmapGuiceProbe])
+ jmapGuiceProbe.saveEmailChange(
+ EmailChange.builder()
+ .accountId(accountId)
+ .state(State.Factory.DEFAULT.generate())
+ .date(ZonedDateTime.now())
+ .isDelegated(false)
+ .build())
+
+ val state: State = jmapGuiceProbe.getLatestEmailState(accountId)
+ state
+ }
}
\ No newline at end of file
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/EmailGetMethod.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/EmailGetMethod.scala
index e95d711..3ee9579 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/EmailGetMethod.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/EmailGetMethod.scala
@@ -23,10 +23,12 @@ import java.time.ZoneId
import eu.timepit.refined.auto._
import eu.timepit.refined.types.string.NonEmptyString
import javax.inject.Inject
+import org.apache.james.jmap.api.change.{EmailChangeRepository, State => JavaState}
+import org.apache.james.jmap.api.model.{AccountId => JavaAccountId}
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.State.INSTANCE
-import org.apache.james.jmap.core.{AccountId, ErrorCode, Invocation, Properties}
+import org.apache.james.jmap.core.{AccountId, ErrorCode, Invocation, Properties, State}
import org.apache.james.jmap.json.{EmailGetSerializer, ResponseSerializer}
import org.apache.james.jmap.mail.Email.UnparsedEmailId
import org.apache.james.jmap.mail.{Email, EmailBodyPart, EmailGetRequest, EmailGetResponse, EmailIds, EmailNotFound, EmailView, EmailViewReaderFactory, SpecificHeaderRequest}
@@ -78,6 +80,7 @@ class SystemZoneIdProvider extends ZoneIdProvider {
class EmailGetMethod @Inject() (readerFactory: EmailViewReaderFactory,
messageIdFactory: MessageId.Factory,
val metricFactory: MetricFactory,
+ val emailchangeRepository: EmailChangeRepository,
val sessionSupplier: SessionSupplier) extends MethodRequiringAccountId[EmailGetRequest] {
override val methodName: MethodName = MethodName("Email/get")
override val requiredCapabilities: Set[CapabilityIdentifier] = Set(JMAP_CORE, JMAP_MAIL)
@@ -141,11 +144,12 @@ class EmailGetMethod @Inject() (readerFactory: EmailViewReaderFactory,
request.ids match {
case None => SMono.raiseError(new IllegalArgumentException("ids can not be ommited for email/get"))
case Some(ids) => getEmails(ids, mailboxSession, request)
- .map(result => EmailGetResponse(
- accountId = request.accountId,
- state = INSTANCE,
- list = result.emails.toList,
- notFound = result.notFound))
+ .flatMap(result => SMono[JavaState](emailchangeRepository.getLatestState(JavaAccountId.fromUsername(mailboxSession.getUser)))
+ .map(state => EmailGetResponse(
+ accountId = request.accountId,
+ state = State.fromJava(state),
+ list = result.emails.toList,
+ notFound = result.notFound)))
}
private def getEmails(ids: EmailIds, mailboxSession: MailboxSession, request: EmailGetRequest): SMono[EmailGetResults] = {
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org