mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-06 07:05:59 +00:00
Change notify-due-item routes to allow multiple tasks per user
This commit is contained in:
parent
d41ddd9729
commit
e51e84408b
@ -3,7 +3,6 @@ package docspell.backend.ops
|
|||||||
import cats.implicits._
|
import cats.implicits._
|
||||||
import cats.effect._
|
import cats.effect._
|
||||||
import cats.data.OptionT
|
import cats.data.OptionT
|
||||||
import com.github.eikek.calev.CalEvent
|
|
||||||
import io.circe.Encoder
|
import io.circe.Encoder
|
||||||
import fs2.Stream
|
import fs2.Stream
|
||||||
|
|
||||||
@ -30,12 +29,16 @@ trait OUserTask[F[_]] {
|
|||||||
task: UserTask[ScanMailboxArgs]
|
task: UserTask[ScanMailboxArgs]
|
||||||
): F[Unit]
|
): F[Unit]
|
||||||
|
|
||||||
/** Return the settings for the notify-due-items task of the current
|
/** Return the settings for all the notify-due-items task of the
|
||||||
* user. There is at most one such task per user. If no task has
|
* current user.
|
||||||
* been created/submitted a new one with default values is
|
|
||||||
* returned.
|
|
||||||
*/
|
*/
|
||||||
def getNotifyDueItems(account: AccountId): F[UserTask[NotifyDueItemsArgs]]
|
def getNotifyDueItems(account: AccountId): Stream[F, UserTask[NotifyDueItemsArgs]]
|
||||||
|
|
||||||
|
/** Find a notify-due-items task by the given id. */
|
||||||
|
def findNotifyDueItems(
|
||||||
|
id: Ident,
|
||||||
|
account: AccountId
|
||||||
|
): OptionT[F, UserTask[NotifyDueItemsArgs]]
|
||||||
|
|
||||||
/** Updates the notify-due-items tasks and notifies the joex nodes.
|
/** Updates the notify-due-items tasks and notifies the joex nodes.
|
||||||
*/
|
*/
|
||||||
@ -100,62 +103,24 @@ object OUserTask {
|
|||||||
_ <- joex.notifyAllNodes
|
_ <- joex.notifyAllNodes
|
||||||
} yield ()
|
} yield ()
|
||||||
|
|
||||||
def getNotifyDueItems(account: AccountId): F[UserTask[NotifyDueItemsArgs]] =
|
def getNotifyDueItems(account: AccountId): Stream[F, UserTask[NotifyDueItemsArgs]] =
|
||||||
store
|
store
|
||||||
.getOneByName[NotifyDueItemsArgs](account, NotifyDueItemsArgs.taskName)
|
.getByName[NotifyDueItemsArgs](account, NotifyDueItemsArgs.taskName)
|
||||||
.getOrElseF(notifyDueItemsDefault(account))
|
|
||||||
|
def findNotifyDueItems(
|
||||||
|
id: Ident,
|
||||||
|
account: AccountId
|
||||||
|
): OptionT[F, UserTask[NotifyDueItemsArgs]] =
|
||||||
|
OptionT(getNotifyDueItems(account).find(_.id == id).compile.last)
|
||||||
|
|
||||||
def submitNotifyDueItems(
|
def submitNotifyDueItems(
|
||||||
account: AccountId,
|
account: AccountId,
|
||||||
task: UserTask[NotifyDueItemsArgs]
|
task: UserTask[NotifyDueItemsArgs]
|
||||||
): F[Unit] =
|
): F[Unit] =
|
||||||
for {
|
for {
|
||||||
_ <- store.updateOneTask[NotifyDueItemsArgs](account, task)
|
_ <- store.updateTask[NotifyDueItemsArgs](account, task)
|
||||||
_ <- joex.notifyAllNodes
|
_ <- joex.notifyAllNodes
|
||||||
} yield ()
|
} yield ()
|
||||||
|
|
||||||
private def notifyDueItemsDefault(
|
|
||||||
account: AccountId
|
|
||||||
): F[UserTask[NotifyDueItemsArgs]] =
|
|
||||||
for {
|
|
||||||
id <- Ident.randomId[F]
|
|
||||||
} yield UserTask(
|
|
||||||
id,
|
|
||||||
NotifyDueItemsArgs.taskName,
|
|
||||||
false,
|
|
||||||
CalEvent.unsafe("*-*-1/7 12:00"),
|
|
||||||
NotifyDueItemsArgs(
|
|
||||||
account,
|
|
||||||
Ident.unsafe(""),
|
|
||||||
Nil,
|
|
||||||
None,
|
|
||||||
5,
|
|
||||||
None,
|
|
||||||
Nil,
|
|
||||||
Nil
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
// private def scanMailboxDefault(
|
|
||||||
// account: AccountId
|
|
||||||
// ): F[UserTask[ScanMailboxArgs]] =
|
|
||||||
// for {
|
|
||||||
// id <- Ident.randomId[F]
|
|
||||||
// } yield UserTask(
|
|
||||||
// id,
|
|
||||||
// ScanMailboxArgs.taskName,
|
|
||||||
// false,
|
|
||||||
// CalEvent.unsafe("*-*-* 0,12:00"),
|
|
||||||
// ScanMailboxArgs(
|
|
||||||
// account,
|
|
||||||
// Ident.unsafe(""),
|
|
||||||
// Nil,
|
|
||||||
// Some(Duration.hours(12)),
|
|
||||||
// None,
|
|
||||||
// false,
|
|
||||||
// None
|
|
||||||
// )
|
|
||||||
// )
|
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2008,7 +2008,8 @@ paths:
|
|||||||
description: |
|
description: |
|
||||||
Return the current notification settings of the authenticated
|
Return the current notification settings of the authenticated
|
||||||
user. Users can be notified on due items via e-mail. This is
|
user. Users can be notified on due items via e-mail. This is
|
||||||
done by periodically querying items.
|
done by periodically querying items. It is possible to have
|
||||||
|
multiple tasks.
|
||||||
security:
|
security:
|
||||||
- authTokenHeader: []
|
- authTokenHeader: []
|
||||||
responses:
|
responses:
|
||||||
@ -2017,13 +2018,13 @@ paths:
|
|||||||
content:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: "#/components/schemas/NotificationSettings"
|
$ref: "#/components/schemas/NotificationSettingsList"
|
||||||
post:
|
post:
|
||||||
tags: [ User Tasks ]
|
tags: [ User Tasks ]
|
||||||
summary: Change current settings for "Notify Due Items" task
|
summary: Create settings for "Notify Due Items" task
|
||||||
description: |
|
description: |
|
||||||
Change the current notification settings of the authenticated
|
Create a new notification settings task of the authenticated
|
||||||
user.
|
user. The id field in the input is ignored.
|
||||||
security:
|
security:
|
||||||
- authTokenHeader: []
|
- authTokenHeader: []
|
||||||
requestBody:
|
requestBody:
|
||||||
@ -2038,6 +2039,58 @@ paths:
|
|||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: "#/components/schemas/BasicResult"
|
$ref: "#/components/schemas/BasicResult"
|
||||||
|
put:
|
||||||
|
tags: [ User Tasks ]
|
||||||
|
summary: Change settings for "Notify Due Items" task
|
||||||
|
description: |
|
||||||
|
Change the settings for a notify-due-items task. The task is
|
||||||
|
looked up by its id.
|
||||||
|
security:
|
||||||
|
- authTokenHeader: []
|
||||||
|
requestBody:
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/NotificationSettings"
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: Ok
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/BasicResult"
|
||||||
|
/sec/usertask/notifydueitems/{id}:
|
||||||
|
parameters:
|
||||||
|
- $ref: "#/components/parameters/id"
|
||||||
|
get:
|
||||||
|
tags: [ User Tasks ]
|
||||||
|
description: |
|
||||||
|
Return the current settings for a single notify-due-items task
|
||||||
|
of the authenticated user.
|
||||||
|
security:
|
||||||
|
- authTokenHeader: []
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: Ok
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/NotificationSettings"
|
||||||
|
delete:
|
||||||
|
tags: [ User Tasks ]
|
||||||
|
description: |
|
||||||
|
Delete the settings to a notify-due-items task of the
|
||||||
|
authenticated user.
|
||||||
|
security:
|
||||||
|
- authTokenHeader: []
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: Ok
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: "#/components/schemas/BasicResult"
|
||||||
|
|
||||||
/sec/usertask/notifydueitems/startonce:
|
/sec/usertask/notifydueitems/startonce:
|
||||||
post:
|
post:
|
||||||
tags: [ User Tasks ]
|
tags: [ User Tasks ]
|
||||||
@ -2100,7 +2153,7 @@ paths:
|
|||||||
$ref: "#/components/schemas/BasicResult"
|
$ref: "#/components/schemas/BasicResult"
|
||||||
put:
|
put:
|
||||||
tags: [ User Tasks ]
|
tags: [ User Tasks ]
|
||||||
summary: Change current settings for "Scan Mailbox" task
|
summary: Change settings for a "Scan Mailbox" task
|
||||||
description: |
|
description: |
|
||||||
Change the settings for a scan-mailbox task. The task is
|
Change the settings for a scan-mailbox task. The task is
|
||||||
looked up by its id.
|
looked up by its id.
|
||||||
@ -2312,6 +2365,16 @@ components:
|
|||||||
properties:
|
properties:
|
||||||
event:
|
event:
|
||||||
type: string
|
type: string
|
||||||
|
NotificationSettingsList:
|
||||||
|
description: |
|
||||||
|
A list of notification settings.
|
||||||
|
required:
|
||||||
|
- items
|
||||||
|
properties:
|
||||||
|
items:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
$ref: "#/components/schemas/NotificationSettings"
|
||||||
NotificationSettings:
|
NotificationSettings:
|
||||||
description: |
|
description: |
|
||||||
Settings for notifying about due items.
|
Settings for notifying about due items.
|
||||||
|
@ -2,6 +2,7 @@ package docspell.restserver.routes
|
|||||||
|
|
||||||
import cats.effect._
|
import cats.effect._
|
||||||
import cats.implicits._
|
import cats.implicits._
|
||||||
|
import cats.data.OptionT
|
||||||
import org.http4s._
|
import org.http4s._
|
||||||
import org.http4s.dsl.Http4sDsl
|
import org.http4s.dsl.Http4sDsl
|
||||||
import org.http4s.circe.CirceEntityEncoder._
|
import org.http4s.circe.CirceEntityEncoder._
|
||||||
@ -27,10 +28,18 @@ object NotifyDueItemsRoutes {
|
|||||||
import dsl._
|
import dsl._
|
||||||
|
|
||||||
HttpRoutes.of {
|
HttpRoutes.of {
|
||||||
|
case GET -> Root / Ident(id) =>
|
||||||
|
(for {
|
||||||
|
task <- ut.findNotifyDueItems(id, user.account)
|
||||||
|
res <- OptionT.liftF(taskToSettings(user.account, backend, task))
|
||||||
|
resp <- OptionT.liftF(Ok(res))
|
||||||
|
} yield resp).getOrElseF(NotFound())
|
||||||
|
|
||||||
case req @ POST -> Root / "startonce" =>
|
case req @ POST -> Root / "startonce" =>
|
||||||
for {
|
for {
|
||||||
data <- req.as[NotificationSettings]
|
data <- req.as[NotificationSettings]
|
||||||
task = makeTask(cfg, user.account, data)
|
newId <- Ident.randomId[F]
|
||||||
|
task <- makeTask(newId, cfg, user.account, data)
|
||||||
res <-
|
res <-
|
||||||
ut.executeNow(user.account, task)
|
ut.executeNow(user.account, task)
|
||||||
.attempt
|
.attempt
|
||||||
@ -38,46 +47,77 @@ object NotifyDueItemsRoutes {
|
|||||||
resp <- Ok(res)
|
resp <- Ok(res)
|
||||||
} yield resp
|
} yield resp
|
||||||
|
|
||||||
case GET -> Root =>
|
case DELETE -> Root / Ident(id) =>
|
||||||
for {
|
for {
|
||||||
task <- ut.getNotifyDueItems(user.account)
|
res <-
|
||||||
res <- taskToSettings(user.account, backend, task)
|
ut.deleteTask(user.account, id)
|
||||||
|
.attempt
|
||||||
|
.map(Conversions.basicResult(_, "Deleted successfully"))
|
||||||
resp <- Ok(res)
|
resp <- Ok(res)
|
||||||
} yield resp
|
} yield resp
|
||||||
|
|
||||||
|
case req @ PUT -> Root =>
|
||||||
|
def run(data: NotificationSettings) =
|
||||||
|
for {
|
||||||
|
task <- makeTask(data.id, cfg, user.account, data)
|
||||||
|
res <-
|
||||||
|
ut.submitNotifyDueItems(user.account, task)
|
||||||
|
.attempt
|
||||||
|
.map(Conversions.basicResult(_, "Saved successfully"))
|
||||||
|
resp <- Ok(res)
|
||||||
|
} yield resp
|
||||||
|
for {
|
||||||
|
data <- req.as[NotificationSettings]
|
||||||
|
resp <-
|
||||||
|
if (data.id.isEmpty) Ok(BasicResult(false, "Empty id is not allowed"))
|
||||||
|
else run(data)
|
||||||
|
} yield resp
|
||||||
|
|
||||||
case req @ POST -> Root =>
|
case req @ POST -> Root =>
|
||||||
for {
|
for {
|
||||||
data <- req.as[NotificationSettings]
|
data <- req.as[NotificationSettings]
|
||||||
task = makeTask(cfg, user.account, data)
|
newId <- Ident.randomId[F]
|
||||||
|
task <- makeTask(newId, cfg, user.account, data)
|
||||||
res <-
|
res <-
|
||||||
ut.submitNotifyDueItems(user.account, task)
|
ut.submitNotifyDueItems(user.account, task)
|
||||||
.attempt
|
.attempt
|
||||||
.map(Conversions.basicResult(_, "Saved successfully."))
|
.map(Conversions.basicResult(_, "Saved successfully."))
|
||||||
resp <- Ok(res)
|
resp <- Ok(res)
|
||||||
} yield resp
|
} yield resp
|
||||||
|
|
||||||
|
case GET -> Root =>
|
||||||
|
ut.getNotifyDueItems(user.account)
|
||||||
|
.evalMap(task => taskToSettings(user.account, backend, task))
|
||||||
|
.compile
|
||||||
|
.toVector
|
||||||
|
.map(v => NotificationSettingsList(v.toList))
|
||||||
|
.flatMap(Ok(_))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def makeTask(
|
def makeTask[F[_]: Sync](
|
||||||
|
id: Ident,
|
||||||
cfg: Config,
|
cfg: Config,
|
||||||
user: AccountId,
|
user: AccountId,
|
||||||
settings: NotificationSettings
|
settings: NotificationSettings
|
||||||
): UserTask[NotifyDueItemsArgs] =
|
): F[UserTask[NotifyDueItemsArgs]] =
|
||||||
UserTask(
|
Sync[F].pure(
|
||||||
settings.id,
|
UserTask(
|
||||||
NotifyDueItemsArgs.taskName,
|
id,
|
||||||
settings.enabled,
|
NotifyDueItemsArgs.taskName,
|
||||||
settings.schedule,
|
settings.enabled,
|
||||||
NotifyDueItemsArgs(
|
settings.schedule,
|
||||||
user,
|
NotifyDueItemsArgs(
|
||||||
settings.smtpConnection,
|
user,
|
||||||
settings.recipients,
|
settings.smtpConnection,
|
||||||
Some(cfg.baseUrl / "app" / "item"),
|
settings.recipients,
|
||||||
settings.remindDays,
|
Some(cfg.baseUrl / "app" / "item"),
|
||||||
if (settings.capOverdue) Some(settings.remindDays)
|
settings.remindDays,
|
||||||
else None,
|
if (settings.capOverdue) Some(settings.remindDays)
|
||||||
settings.tagsInclude.map(_.id),
|
else None,
|
||||||
settings.tagsExclude.map(_.id)
|
settings.tagsInclude.map(_.id),
|
||||||
|
settings.tagsExclude.map(_.id)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user