vttranlina commented on code in PR #1275: URL: https://github.com/apache/james-project/pull/1275#discussion_r1006638894
########## 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 think the case a user has an unlimited quota unchanged, we should find a way to return noChanges instead of throwing an error. ``` I'm waiting for another opinion! -- 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]
