Encode ws messages as JSON

This commit is contained in:
eikek
2021-11-11 21:55:31 +01:00
parent d0f3d54060
commit cf933b60a7
10 changed files with 99 additions and 46 deletions

View File

@ -8,25 +8,48 @@ package docspell.restserver.ws
import docspell.backend.auth.AuthToken
import docspell.common._
import io.circe._
import io.circe.syntax._
import io.circe.generic.semiauto.deriveEncoder
/** The event that is sent to clients through a websocket connection. All events are
* encoded as JSON.
*/
sealed trait OutputEvent {
def forCollective(token: AuthToken): Boolean
def encode: String
def asJson: Json
def encode: String =
asJson.noSpaces
}
object OutputEvent {
case object KeepAlive extends OutputEvent {
def forCollective(token: AuthToken): Boolean = true
def encode: String = "keep-alive"
def asJson: Json =
Msg("keep-alive", ()).asJson
}
final case class ItemProcessed(collective: Ident) extends OutputEvent {
def forCollective(token: AuthToken): Boolean =
token.account.collective == collective
def encode: String =
"item-processed"
def asJson: Json =
Msg("item-processed", ()).asJson
}
final case class JobsWaiting(group: Ident, count: Int) extends OutputEvent {
def forCollective(token: AuthToken): Boolean =
token.account.collective == group
def asJson: Json =
Msg("jobs-waiting", count).asJson
}
private case class Msg[A](tag: String, content: A)
private object Msg {
@scala.annotation.nowarn
implicit def jsonEncoder[A: Encoder]: Encoder[Msg[A]] =
deriveEncoder
}
}

View File

@ -9,9 +9,7 @@ package docspell.restserver.ws
import cats.effect.Async
import fs2.concurrent.Topic
import fs2.{Pipe, Stream}
import docspell.backend.auth.AuthToken
import org.http4s.HttpRoutes
import org.http4s.dsl.Http4sDsl
import org.http4s.server.websocket.WebSocketBuilder2