mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
@ -138,7 +138,7 @@ object RestServer {
|
||||
token: AuthToken
|
||||
): HttpRoutes[F] =
|
||||
Router(
|
||||
"ws" -> WebSocketRoutes(token, topic, wsB),
|
||||
"ws" -> WebSocketRoutes(token, restApp.backend, topic, wsB),
|
||||
"auth" -> LoginRoutes.session(restApp.backend.login, cfg, token),
|
||||
"tag" -> TagRoutes(restApp.backend, token),
|
||||
"equipment" -> EquipmentRoutes(restApp.backend, token),
|
||||
|
@ -6,11 +6,11 @@
|
||||
|
||||
package docspell.restserver
|
||||
|
||||
import cats.effect.Async
|
||||
import fs2.Stream
|
||||
import fs2.concurrent.Topic
|
||||
|
||||
import docspell.backend.msg.JobDone
|
||||
import docspell.common.ProcessItemArgs
|
||||
import docspell.backend.msg.{JobDone, JobSubmitted}
|
||||
import docspell.pubsub.api.PubSubT
|
||||
import docspell.restserver.ws.OutputEvent
|
||||
|
||||
@ -18,15 +18,20 @@ import docspell.restserver.ws.OutputEvent
|
||||
*/
|
||||
object Subscriptions {
|
||||
|
||||
def apply[F[_]](
|
||||
def apply[F[_]: Async](
|
||||
wsTopic: Topic[F, OutputEvent],
|
||||
pubSub: PubSubT[F]
|
||||
): Stream[F, Nothing] =
|
||||
jobDone(pubSub).through(wsTopic.publish)
|
||||
jobDone(pubSub).merge(jobSubmitted(pubSub)).through(wsTopic.publish)
|
||||
|
||||
def jobDone[F[_]](pubSub: PubSubT[F]): Stream[F, OutputEvent] =
|
||||
pubSub
|
||||
.subscribe(JobDone.topic)
|
||||
.filter(m => m.body.task == ProcessItemArgs.taskName)
|
||||
.map(m => OutputEvent.ItemProcessed(m.body.group))
|
||||
.map(m => OutputEvent.JobDone(m.body.group, m.body.task))
|
||||
|
||||
def jobSubmitted[F[_]](pubSub: PubSubT[F]): Stream[F, OutputEvent] =
|
||||
pubSub
|
||||
.subscribe(JobSubmitted.topic)
|
||||
.map(m => OutputEvent.JobSubmitted(m.body.group, m.body.task))
|
||||
|
||||
}
|
||||
|
@ -31,18 +31,26 @@ object OutputEvent {
|
||||
Msg("keep-alive", ()).asJson
|
||||
}
|
||||
|
||||
final case class ItemProcessed(collective: Ident) extends OutputEvent {
|
||||
def forCollective(token: AuthToken): Boolean =
|
||||
token.account.collective == collective
|
||||
|
||||
def asJson: Json =
|
||||
Msg("item-processed", ()).asJson
|
||||
}
|
||||
|
||||
final case class JobsWaiting(group: Ident, count: Int) extends OutputEvent {
|
||||
final case class JobSubmitted(group: Ident, task: Ident) extends OutputEvent {
|
||||
def forCollective(token: AuthToken): Boolean =
|
||||
token.account.collective == group
|
||||
|
||||
def asJson: Json =
|
||||
Msg("job-submitted", task).asJson
|
||||
}
|
||||
|
||||
final case class JobDone(group: Ident, task: Ident) extends OutputEvent {
|
||||
def forCollective(token: AuthToken): Boolean =
|
||||
token.account.collective == group
|
||||
|
||||
def asJson: Json =
|
||||
Msg("job-done", task).asJson
|
||||
}
|
||||
|
||||
final case class JobsWaiting(collective: Ident, count: Int) extends OutputEvent {
|
||||
def forCollective(token: AuthToken): Boolean =
|
||||
token.account.collective == collective
|
||||
|
||||
def asJson: Json =
|
||||
Msg("jobs-waiting", count).asJson
|
||||
}
|
||||
|
@ -7,9 +7,11 @@
|
||||
package docspell.restserver.ws
|
||||
|
||||
import cats.effect.Async
|
||||
import cats.implicits._
|
||||
import fs2.concurrent.Topic
|
||||
import fs2.{Pipe, Stream}
|
||||
|
||||
import docspell.backend.BackendApp
|
||||
import docspell.backend.auth.AuthToken
|
||||
|
||||
import org.http4s.HttpRoutes
|
||||
@ -22,6 +24,7 @@ object WebSocketRoutes {
|
||||
|
||||
def apply[F[_]: Async](
|
||||
user: AuthToken,
|
||||
backend: BackendApp[F],
|
||||
topic: Topic[F, OutputEvent],
|
||||
wsb: WebSocketBuilder2[F]
|
||||
): HttpRoutes[F] = {
|
||||
@ -29,11 +32,18 @@ object WebSocketRoutes {
|
||||
import dsl._
|
||||
|
||||
HttpRoutes.of { case GET -> Root =>
|
||||
val init =
|
||||
for {
|
||||
jc <- backend.job.getUnfinishedJobCount(user.account.collective)
|
||||
msg = OutputEvent.JobsWaiting(user.account.collective, jc)
|
||||
} yield Text(msg.encode)
|
||||
|
||||
val toClient: Stream[F, WebSocketFrame.Text] =
|
||||
topic
|
||||
.subscribe(500)
|
||||
.filter(_.forCollective(user))
|
||||
.map(msg => Text(msg.encode))
|
||||
Stream.eval(init) ++
|
||||
topic
|
||||
.subscribe(500)
|
||||
.filter(_.forCollective(user))
|
||||
.map(msg => Text(msg.encode))
|
||||
|
||||
val toServer: Pipe[F, WebSocketFrame, Unit] =
|
||||
_.map(_ => ())
|
||||
|
Reference in New Issue
Block a user