Skip to content

Commit

Permalink
ISSUE-1240 Use JMAPExtensionConfiguration to handle email recovery co…
Browse files Browse the repository at this point in the history
…nfig
  • Loading branch information
quantranhong1999 authored and vttranlina committed Oct 29, 2024
1 parent 97ab5a8 commit c105a6d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.linagora.tmail.james.jmap

import java.time.Duration
import java.time.temporal.ChronoUnit
import java.util.Locale

import com.linagora.tmail.james.jmap.JMAPExtensionConfiguration.{CALENDAR_EVENT_REPLY_SUPPORTED_LANGUAGES_DEFAULT, PUBLIC_ASSET_TOTAL_SIZE_LIMIT_DEFAULT, TICKET_IP_VALIDATION_ENABLED}
Expand All @@ -10,7 +12,7 @@ import org.apache.commons.configuration2.Configuration
import org.apache.james.core.MailAddress
import org.apache.james.jmap.core.UnsignedInt.{UnsignedInt, UnsignedIntConstraint}
import org.apache.james.server.core.MissingArgumentException
import org.apache.james.util.Size
import org.apache.james.util.{DurationParser, Size}

import scala.util.{Failure, Success, Try}

Expand Down Expand Up @@ -46,8 +48,10 @@ object JMAPExtensionConfiguration {
}))
.fold(_ => Set.empty, identity))

val emailRecoveryActionConfiguration: EmailRecoveryActionConfiguration = EmailRecoveryActionConfiguration.from(configuration)

JMAPExtensionConfiguration(publicAssetTotalSizeLimit, supportMailAddressOpt, ticketIpValidationEnable,
calendarEventReplySupportedLanguagesConfig)
calendarEventReplySupportedLanguagesConfig, emailRecoveryActionConfiguration)
}
}

Expand All @@ -61,7 +65,8 @@ object PublicAssetTotalSizeLimit {
case class JMAPExtensionConfiguration(publicAssetTotalSizeLimit: PublicAssetTotalSizeLimit = PUBLIC_ASSET_TOTAL_SIZE_LIMIT_DEFAULT,
supportMailAddress: Option[MailAddress] = Option.empty,
ticketIpValidationEnable: TicketIpValidationEnable = TICKET_IP_VALIDATION_ENABLED,
calendarEventReplySupportedLanguagesConfig: CalendarEventReplySupportedLanguagesConfig = CALENDAR_EVENT_REPLY_SUPPORTED_LANGUAGES_DEFAULT) {
calendarEventReplySupportedLanguagesConfig: CalendarEventReplySupportedLanguagesConfig = CALENDAR_EVENT_REPLY_SUPPORTED_LANGUAGES_DEFAULT,
emailRecoveryActionConfiguration: EmailRecoveryActionConfiguration = EmailRecoveryActionConfiguration.DEFAULT) {
def this(publicAssetTotalSizeLimit: PublicAssetTotalSizeLimit) = {
this(publicAssetTotalSizeLimit, Option.empty)
}
Expand All @@ -81,4 +86,22 @@ case class PublicAssetTotalSizeLimit(value: UnsignedInt) {

case class TicketIpValidationEnable(value: Boolean)

case class CalendarEventReplySupportedLanguagesConfig(supportedLanguages: Set[Locale])
case class CalendarEventReplySupportedLanguagesConfig(supportedLanguages: Set[Locale])

object EmailRecoveryActionConfiguration {
val DEFAULT_MAX_EMAIL_RECOVERY_PER_REQUEST: Long = 5
val DEFAULT_RESTORATION_HORIZON: Duration = DurationParser.parse("15", ChronoUnit.DAYS)
val DEFAULT: EmailRecoveryActionConfiguration = EmailRecoveryActionConfiguration(DEFAULT_MAX_EMAIL_RECOVERY_PER_REQUEST, DEFAULT_RESTORATION_HORIZON)

def from(jmapConfiguration: Configuration): EmailRecoveryActionConfiguration = {
val maxEmailRecoveryPerRequest: Long = jmapConfiguration.getLong("emailRecoveryAction.maxEmailRecoveryPerRequest", DEFAULT_MAX_EMAIL_RECOVERY_PER_REQUEST)

val restorationHorizon: Duration = Option(jmapConfiguration.getString("emailRecoveryAction.restorationHorizon", null))
.map(DurationParser.parse)
.getOrElse(DEFAULT_RESTORATION_HORIZON)

EmailRecoveryActionConfiguration(maxEmailRecoveryPerRequest, restorationHorizon)
}
}

case class EmailRecoveryActionConfiguration(maxEmailRecoveryPerRequest: Long, restorationHorizon: Duration)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.linagora.tmail.james.jmap.method

import java.time.temporal.ChronoUnit
import java.time.{Duration, ZonedDateTime}
import java.time.ZonedDateTime

import com.google.common.collect.ImmutableList
import com.google.inject.multibindings.Multibinder
Expand All @@ -10,6 +9,7 @@ import com.linagora.tmail.james.jmap.json.EmailRecoveryActionSerializer
import com.linagora.tmail.james.jmap.method.CapabilityIdentifier.LINAGORA_MESSAGE_VAULT
import com.linagora.tmail.james.jmap.method.EmailRecoveryActionSetCreatePerformer.CreationResults
import com.linagora.tmail.james.jmap.model.{EmailRecoveryActionCreation, EmailRecoveryActionCreationId, EmailRecoveryActionCreationParseException, EmailRecoveryActionCreationRequest, EmailRecoveryActionCreationResponse, EmailRecoveryActionSetRequest, EmailRecoveryActionSetResponse, EmailRecoveryActionUpdateException, EmailRecoveryActionUpdatePatchObject, EmailRecoveryActionUpdateRequest, EmailRecoveryActionUpdateResponse, EmailRecoveryActionUpdateStatus, UnparsedEmailRecoveryActionId}
import com.linagora.tmail.james.jmap.{EmailRecoveryActionConfiguration, JMAPExtensionConfiguration}
import eu.timepit.refined.auto._
import jakarta.inject.Inject
import org.apache.james.core.Username
Expand All @@ -24,8 +24,7 @@ import org.apache.james.lifecycle.api.Startable
import org.apache.james.mailbox.MailboxSession
import org.apache.james.metrics.api.MetricFactory
import org.apache.james.task.{TaskExecutionDetails, TaskId, TaskManager, TaskNotFoundException}
import org.apache.james.util.{DurationParser, ReactorUtils}
import org.apache.james.utils.PropertiesProvider
import org.apache.james.util.ReactorUtils
import org.apache.james.vault.search.{CriterionFactory, Query}
import org.apache.james.webadmin.vault.routes.DeletedMessagesVaultRestoreTask.{AdditionalInformation => DeletedMessagesVaultRestoreTaskAdditionalInformation}
import org.apache.james.webadmin.vault.routes.{DeletedMessagesVaultRestoreTask, RestoreService}
Expand All @@ -35,7 +34,6 @@ import play.api.libs.json.{JsError, JsObject}
import reactor.core.scala.publisher.{SFlux, SMono}

import scala.jdk.OptionConverters._
import scala.util.Try

class EmailRecoveryActionMethodModule extends AbstractModule {
override def configure(): Unit = {
Expand All @@ -50,34 +48,10 @@ class EmailRecoveryActionMethodModule extends AbstractModule {

@Singleton
@Provides
def provideEmailRecoveryActionConfiguration(propertiesProvider: PropertiesProvider): EmailRecoveryActionConfiguration =
EmailRecoveryActionConfiguration.from(propertiesProvider)
def provideEmailRecoveryActionConfiguration(jmapExtensionConfiguration: JMAPExtensionConfiguration): EmailRecoveryActionConfiguration =
jmapExtensionConfiguration.emailRecoveryActionConfiguration
}

object EmailRecoveryActionConfiguration {

val DEFAULT_MAX_EMAIL_RECOVERY_PER_REQUEST: Long = 5
val DEFAULT_RESTORATION_HORIZON: Duration = DurationParser.parse("15", ChronoUnit.DAYS)

def from(propertiesProvider: PropertiesProvider): EmailRecoveryActionConfiguration = {
val config = Try(propertiesProvider.getConfiguration("jmap"))

val maxEmailRecoveryPerRequest: Option[Long] = config
.map(_.getLong("emailRecoveryAction.maxEmailRecoveryPerRequest"))
.toOption
val restorationHorizon: Option[Duration] = config
.map(_.getString("emailRecoveryAction.restorationHorizon"))
.map(DurationParser.parse)
.toOption

EmailRecoveryActionConfiguration(
maxEmailRecoveryPerRequest = maxEmailRecoveryPerRequest.getOrElse(DEFAULT_MAX_EMAIL_RECOVERY_PER_REQUEST),
restorationHorizon = restorationHorizon.getOrElse(DEFAULT_RESTORATION_HORIZON))
}
}

case class EmailRecoveryActionConfiguration(maxEmailRecoveryPerRequest: Long, restorationHorizon: Duration)

class EmailRecoveryActionSetMethod @Inject()(val createPerformer: EmailRecoveryActionSetCreatePerformer,
val updatePerformer: EmailRecoveryActionSetUpdatePerformer,
val metricFactory: MetricFactory,
Expand Down

0 comments on commit c105a6d

Please sign in to comment.