quantranhong1999 commented on code in PR #1275: URL: https://github.com/apache/james-project/pull/1275#discussion_r1006391734
########## server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/QuotaChangesMethod.scala: ########## @@ -0,0 +1,102 @@ +/**************************************************************** + * 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 org.apache.james.core.Username +import org.apache.james.jmap.api.change.{CanNotCalculateChangesException, State} +import org.apache.james.jmap.core.CapabilityIdentifier.{CapabilityIdentifier, JMAP_CORE, JMAP_QUOTA} +import org.apache.james.jmap.core.Invocation.{Arguments, MethodName} +import org.apache.james.jmap.core.{ErrorCode, Invocation, Properties, UuidState} +import org.apache.james.jmap.json.{QuotaSerializer, ResponseSerializer} +import org.apache.james.jmap.mail.{QuotaChangesRequest, QuotaChangesResponse} +import org.apache.james.jmap.routes.SessionSupplier +import org.apache.james.mailbox.MailboxSession +import org.apache.james.mailbox.quota.{QuotaManager, UserQuotaRootResolver} +import org.apache.james.metrics.api.MetricFactory +import org.reactivestreams.Publisher +import play.api.libs.json.{JsError, JsSuccess} +import reactor.core.scala.publisher.SMono + +import java.nio.charset.StandardCharsets +import java.util.UUID +import javax.inject.Inject + +object QuotaChangesMethod { + val updatedProperties: Properties = Properties("used") +} + +class QuotaChangesMethod @Inject()(val metricFactory: MetricFactory, + val sessionSupplier: SessionSupplier, + val quotaManager: QuotaManager, + val quotaRootResolver: UserQuotaRootResolver) extends MethodRequiringAccountId[QuotaChangesRequest] { + + override val methodName: Invocation.MethodName = MethodName("Quota/changes") + + override val requiredCapabilities: Set[CapabilityIdentifier] = Set(JMAP_QUOTA, JMAP_CORE) + + val quotaChangesResolver: QuotaChangesResolver = QuotaChangesResolver(quotaManager, quotaRootResolver) + + override def doProcess(capabilities: Set[CapabilityIdentifier], invocation: InvocationWithContext, mailboxSession: MailboxSession, request: QuotaChangesRequest): Publisher[InvocationWithContext] = + quotaChangesResolver.getLatestState(mailboxSession.getUser, capabilities) + .map(newState => QuotaChangesResponse.from(request.sinceState, UuidState.fromJava(newState), request.accountId)) + .map(response => InvocationWithContext( + invocation = Invocation( + methodName = methodName, + arguments = Arguments(QuotaSerializer.serializeChanges(response)), + methodCallId = invocation.invocation.methodCallId), + processingContext = invocation.processingContext)) + .onErrorResume { + case e: CanNotCalculateChangesException => SMono.just(InvocationWithContext(Invocation.error(ErrorCode.CannotCalculateChanges, e.getMessage, invocation.invocation.methodCallId), invocation.processingContext)) + case e => SMono.error(e) + } + + override def getRequest(mailboxSession: MailboxSession, invocation: Invocation): Either[Exception, QuotaChangesRequest] = + QuotaSerializer.deserializeQuotaChangesRequest(invocation.arguments.value) match { + case JsSuccess(mailboxGetRequest, _) => Right(mailboxGetRequest) + case errors: JsError => Left(new IllegalArgumentException(ResponseSerializer.serialize(errors).toString)) + } + +} + +case class QuotaChangesResolver(private var quotaManager: QuotaManager, + private var quotaRootResolver: UserQuotaRootResolver) { + + private val jmapQuotaManagerWrapper: JmapQuotaManagerWrapper = JmapQuotaManagerWrapper(quotaManager, quotaRootResolver) + + def getLatestState(username: Username, capabilities: Set[CapabilityIdentifier]): SMono[State] = + jmapQuotaManagerWrapper.retrieveQuotaRoot(username, capabilities) + .flatMap(quotaRoot => SMono(quotaManager.getQuotasReactive(quotaRoot))) + .collectSeq() + .map(quotasList => evaluateStateFromQuotas(username, capabilities, quotasList)) + + + private def evaluateStateFromQuotas(username: Username, capabilities: Set[CapabilityIdentifier], quotasList: Seq[QuotaManager.Quotas]): State = { + if (!quotasList.exists(quotas => quotas.getMessageQuota.getLimit.isLimited || quotas.getStorageQuota.getLimit.isLimited)) { + throw new CanNotCalculateChangesException("Quota is empty, hence we cannot calculate changes") Review Comment: I am not sure we can use `cannotCalculateChanges` like that. ``` `cannotCalculateChanges`: The server cannot calculate the changes from the state string given by the client. Usually, this is due to the client’s state being too old or the server being unable to produce an update to an intermediate state when there are too many updates. The client MUST invalidate its Foo cache. ``` I think the case a user has an unlimited quota unchanged, we should find a way to return noChanges instead of throwing an error. ########## server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/QuotaChangesMethod.scala: ########## @@ -0,0 +1,102 @@ +/**************************************************************** + * 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 org.apache.james.core.Username +import org.apache.james.jmap.api.change.{CanNotCalculateChangesException, State} +import org.apache.james.jmap.core.CapabilityIdentifier.{CapabilityIdentifier, JMAP_CORE, JMAP_QUOTA} +import org.apache.james.jmap.core.Invocation.{Arguments, MethodName} +import org.apache.james.jmap.core.{ErrorCode, Invocation, Properties, UuidState} +import org.apache.james.jmap.json.{QuotaSerializer, ResponseSerializer} +import org.apache.james.jmap.mail.{QuotaChangesRequest, QuotaChangesResponse} +import org.apache.james.jmap.routes.SessionSupplier +import org.apache.james.mailbox.MailboxSession +import org.apache.james.mailbox.quota.{QuotaManager, UserQuotaRootResolver} +import org.apache.james.metrics.api.MetricFactory +import org.reactivestreams.Publisher +import play.api.libs.json.{JsError, JsSuccess} +import reactor.core.scala.publisher.SMono + +import java.nio.charset.StandardCharsets +import java.util.UUID +import javax.inject.Inject + +object QuotaChangesMethod { + val updatedProperties: Properties = Properties("used") +} + +class QuotaChangesMethod @Inject()(val metricFactory: MetricFactory, + val sessionSupplier: SessionSupplier, + val quotaManager: QuotaManager, + val quotaRootResolver: UserQuotaRootResolver) extends MethodRequiringAccountId[QuotaChangesRequest] { + + override val methodName: Invocation.MethodName = MethodName("Quota/changes") + + override val requiredCapabilities: Set[CapabilityIdentifier] = Set(JMAP_QUOTA, JMAP_CORE) + + val quotaChangesResolver: QuotaChangesResolver = QuotaChangesResolver(quotaManager, quotaRootResolver) + + override def doProcess(capabilities: Set[CapabilityIdentifier], invocation: InvocationWithContext, mailboxSession: MailboxSession, request: QuotaChangesRequest): Publisher[InvocationWithContext] = + quotaChangesResolver.getLatestState(mailboxSession.getUser, capabilities) + .map(newState => QuotaChangesResponse.from(request.sinceState, UuidState.fromJava(newState), request.accountId)) + .map(response => InvocationWithContext( + invocation = Invocation( + methodName = methodName, + arguments = Arguments(QuotaSerializer.serializeChanges(response)), + methodCallId = invocation.invocation.methodCallId), + processingContext = invocation.processingContext)) + .onErrorResume { + case e: CanNotCalculateChangesException => SMono.just(InvocationWithContext(Invocation.error(ErrorCode.CannotCalculateChanges, e.getMessage, invocation.invocation.methodCallId), invocation.processingContext)) + case e => SMono.error(e) + } + + override def getRequest(mailboxSession: MailboxSession, invocation: Invocation): Either[Exception, QuotaChangesRequest] = + QuotaSerializer.deserializeQuotaChangesRequest(invocation.arguments.value) match { + case JsSuccess(mailboxGetRequest, _) => Right(mailboxGetRequest) + case errors: JsError => Left(new IllegalArgumentException(ResponseSerializer.serialize(errors).toString)) + } + +} + +case class QuotaChangesResolver(private var quotaManager: QuotaManager, + private var quotaRootResolver: UserQuotaRootResolver) { + + private val jmapQuotaManagerWrapper: JmapQuotaManagerWrapper = JmapQuotaManagerWrapper(quotaManager, quotaRootResolver) + + def getLatestState(username: Username, capabilities: Set[CapabilityIdentifier]): SMono[State] = + jmapQuotaManagerWrapper.retrieveQuotaRoot(username, capabilities) + .flatMap(quotaRoot => SMono(quotaManager.getQuotasReactive(quotaRoot))) + .collectSeq() + .map(quotasList => evaluateStateFromQuotas(username, capabilities, quotasList)) + + + private def evaluateStateFromQuotas(username: Username, capabilities: Set[CapabilityIdentifier], quotasList: Seq[QuotaManager.Quotas]): State = { + if (!quotasList.exists(quotas => quotas.getMessageQuota.getLimit.isLimited || quotas.getStorageQuota.getLimit.isLimited)) { + throw new CanNotCalculateChangesException("Quota is empty, hence we cannot calculate changes") + } + + val usedTotal: Long = quotasList + .map(quotas => quotas.getMessageQuota.getUsed.asLong() + quotas.getStorageQuota.getUsed.asLong()) + .sum + State.of(UUID.nameUUIDFromBytes(s"${username.toString}:${capabilities.map(_.value).mkString("_")}:${usedTotal}".getBytes(StandardCharsets.UTF_8))) + } Review Comment: Should we calculate the change for the limit also? E.g a user limit changed from unlimited to limited and the opposite? ########## server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/method/QuotaChangesMethod.scala: ########## @@ -0,0 +1,102 @@ +/**************************************************************** + * 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 org.apache.james.core.Username +import org.apache.james.jmap.api.change.{CanNotCalculateChangesException, State} +import org.apache.james.jmap.core.CapabilityIdentifier.{CapabilityIdentifier, JMAP_CORE, JMAP_QUOTA} +import org.apache.james.jmap.core.Invocation.{Arguments, MethodName} +import org.apache.james.jmap.core.{ErrorCode, Invocation, Properties, UuidState} +import org.apache.james.jmap.json.{QuotaSerializer, ResponseSerializer} +import org.apache.james.jmap.mail.{QuotaChangesRequest, QuotaChangesResponse} +import org.apache.james.jmap.routes.SessionSupplier +import org.apache.james.mailbox.MailboxSession +import org.apache.james.mailbox.quota.{QuotaManager, UserQuotaRootResolver} +import org.apache.james.metrics.api.MetricFactory +import org.reactivestreams.Publisher +import play.api.libs.json.{JsError, JsSuccess} +import reactor.core.scala.publisher.SMono + +import java.nio.charset.StandardCharsets +import java.util.UUID +import javax.inject.Inject + +object QuotaChangesMethod { + val updatedProperties: Properties = Properties("used") Review Comment: I think we agreed to just return `null` updatedProperties for now? Imagine James admin changes some quota limit, `null` will be better than only `used` IMO. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
