Add routes to check calendar events

This commit is contained in:
Eike Kettner 2020-04-20 01:02:45 +02:00
parent 9b30542974
commit 3524904faf
4 changed files with 91 additions and 1 deletions

View File

@ -50,6 +50,9 @@ object Timestamp {
def from(zd: ZonedDateTime): Timestamp =
Timestamp(zd.toInstant)
def atUtc(ldt: LocalDateTime): Timestamp =
from(ldt.atZone(UTC))
implicit val encodeTimestamp: Encoder[Timestamp] =
BaseJsonCodecs.encodeInstantEpoch.contramap(_.value)

View File

@ -1560,6 +1560,27 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/BasicResult"
/sec/calevent/check:
post:
tags: [ Utility ]
summary: Check a calendar event string
description: |
For ui purposes, this route checks a calendar event string and
either returns the normal form or an error message.
security:
- authTokenHeader: []
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/CalEventCheck"
responses:
200:
description: Ok
content:
application/json:
schema:
$ref: "#/components/schemas/CalEventCheckResult"
/sec/usertask/notifydueitems:
get:
tags: [ Notification ]
@ -1600,6 +1621,31 @@ paths:
components:
schemas:
CalEventCheckResult:
description: |
The result of checking a calendar event string.
required:
- success
- message
properties:
success:
type: boolean
message:
type: string
event:
type: string
format: calevent
next:
type: integer
format: date-time
CalEventCheck:
description: |
A calendar event.
required:
- event
properties:
event:
type: string
NotificationSettings:
description: |
Settings for notifying about due items.

View File

@ -76,7 +76,8 @@ object RestServer {
"email/send" -> MailSendRoutes(restApp.backend, token),
"email/settings" -> MailSettingsRoutes(restApp.backend, token),
"email/sent" -> SentMailRoutes(restApp.backend, token),
"usertask/notifydueitems" -> NotifyDueItemsRoutes(restApp.backend, token)
"usertask/notifydueitems" -> NotifyDueItemsRoutes(restApp.backend, token),
"calevent/check" -> CalEventCheckRoutes()
)
def openRoutes[F[_]: Effect](cfg: Config, restApp: RestApp[F]): HttpRoutes[F] =

View File

@ -0,0 +1,40 @@
package docspell.restserver.routes
import cats.effect._
import cats.implicits._
import org.http4s._
import org.http4s.dsl.Http4sDsl
import org.http4s.circe.CirceEntityEncoder._
import org.http4s.circe.CirceEntityDecoder._
import com.github.eikek.calev.CalEvent
import docspell.common._
import docspell.restapi.model._
object CalEventCheckRoutes {
def apply[F[_]: Effect](): HttpRoutes[F] = {
val dsl = new Http4sDsl[F] {}
import dsl._
HttpRoutes.of {
case req @ POST -> Root =>
for {
data <- req.as[CalEventCheck]
res <- testEvent(data.event)
resp <- Ok(res)
} yield resp
}
}
def testEvent[F[_]: Sync](str: String): F[CalEventCheckResult] =
Timestamp.current[F].map { now =>
CalEvent.parse(str) match {
case Right(ev) =>
val next = ev.nextElapse(now.toUtcDateTime).map(Timestamp.atUtc)
CalEventCheckResult(true, "Valid.", ev.some, next)
case Left(err) =>
CalEventCheckResult(false, err, None, None)
}
}
}