Send results from processing documents in the event

This commit is contained in:
eikek
2022-02-10 20:28:07 +01:00
parent d6829ea69b
commit 19e040b029
3 changed files with 30 additions and 26 deletions

View File

@ -10,6 +10,9 @@ import docspell.common._
import docspell.joex.process.ItemData.AttachmentDates import docspell.joex.process.ItemData.AttachmentDates
import docspell.store.records.{RAttachment, RAttachmentMeta, RItem} import docspell.store.records.{RAttachment, RAttachmentMeta, RItem}
import io.circe.syntax.EncoderOps
import io.circe.{Encoder, Json}
/** Data that is carried across all processing tasks. /** Data that is carried across all processing tasks.
* *
* @param item * @param item
@ -94,4 +97,28 @@ object ItemData {
dates.map(dl => dl.label.copy(label = dl.date.toString)) dates.map(dl => dl.label.copy(label = dl.date.toString))
} }
// Used to encode the result passed to the job-done event
implicit val jsonEncoder: Encoder[ItemData] =
Encoder.instance { data =>
val metaMap = data.metas.groupMap(_.id)(identity)
Json.obj(
"id" -> data.item.id.asJson,
"name" -> data.item.name.asJson,
"collective" -> data.item.cid.asJson,
"source" -> data.item.source.asJson,
"attachments" -> data.attachments
.map(a =>
Json.obj(
"id" -> a.id.asJson,
"name" -> a.name.asJson,
"content" -> metaMap.get(a.id).flatMap(_.head.content).asJson,
"language" -> metaMap.get(a.id).flatMap(_.head.language).asJson,
"pages" -> metaMap.get(a.id).flatMap(_.head.pages).asJson
)
)
.asJson,
"tags" -> data.tags.asJson,
"assumedTags" -> data.classifyTags.asJson
)
}
} }

View File

@ -40,18 +40,18 @@ object ItemHandler {
fts: FtsClient[F], fts: FtsClient[F],
analyser: TextAnalyser[F], analyser: TextAnalyser[F],
regexNer: RegexNerFile[F] regexNer: RegexNerFile[F]
): Task[F, Args, Unit] = ): Task[F, Args, Option[ItemData]] =
logBeginning.flatMap(_ => logBeginning.flatMap(_ =>
DuplicateCheck[F] DuplicateCheck[F]
.flatMap(args => .flatMap(args =>
if (args.files.isEmpty) logNoFiles if (args.files.isEmpty) logNoFiles.map(_ => None)
else { else {
val create: Task[F, Args, ItemData] = val create: Task[F, Args, ItemData] =
CreateItem[F].contramap(_ => args.pure[F]) CreateItem[F].contramap(_ => args.pure[F])
create create
.flatMap(itemStateTask(ItemState.Processing)) .flatMap(itemStateTask(ItemState.Processing))
.flatMap(safeProcess[F](cfg, itemOps, fts, analyser, regexNer)) .flatMap(safeProcess[F](cfg, itemOps, fts, analyser, regexNer))
.map(_ => ()) .map(_.some)
} }
) )
) )

View File

@ -1,23 +0,0 @@
/*
* Copyright 2020 Eike K. & Contributors
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package docspell.notification.api
import docspell.notification.api.Event._
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
trait EventCodec {
implicit val tagsChangedDecoder: Decoder[TagsChanged] = deriveDecoder
implicit val tagsChangedEncoder: Encoder[TagsChanged] = deriveEncoder
implicit val eventDecoder: Decoder[Event] =
deriveDecoder
implicit val eventEncoder: Encoder[Event] =
deriveEncoder
}