Allow a custom message part for the periodic query task

This commit is contained in:
eikek
2022-01-11 22:50:19 +01:00
parent dd9937740a
commit 404fb2a37f
19 changed files with 128 additions and 44 deletions

View File

@ -148,7 +148,8 @@ object Event {
account: AccountId,
items: Nel[Ident],
more: Boolean,
baseUrl: Option[LenientUri]
baseUrl: Option[LenientUri],
contentStart: Option[String]
) extends Event {
val eventType = ItemSelection
}
@ -161,7 +162,7 @@ object Event {
for {
id1 <- Ident.randomId[F]
id2 <- Ident.randomId[F]
} yield ItemSelection(account, Nel.of(id1, id2), true, baseUrl)
} yield ItemSelection(account, Nel.of(id1, id2), true, baseUrl, None)
}
/** Event when a new job is added to the queue */

View File

@ -17,7 +17,8 @@ final case class PeriodicQueryArgs(
channel: ChannelOrRef,
query: Option[ItemQueryString],
bookmark: Option[String],
baseUrl: Option[LenientUri]
baseUrl: Option[LenientUri],
contentStart: Option[String]
)
object PeriodicQueryArgs {

View File

@ -1,7 +1,14 @@
/*
* Copyright 2020 Eike K. & Contributors
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package docspell.notification.impl
import docspell.notification.api.EventContext
import docspell.common.Logger
import docspell.notification.api.EventContext
import io.circe.Json
trait EventContextSyntax {
@ -21,7 +28,9 @@ trait EventContextSyntax {
case Left(err) => logError(logger)(err)
}
def withDefaultBoth[F[_]](logger: Logger[F])(f: (String, String) => F[Unit]): F[Unit] =
def withDefaultBoth[F[_]](
logger: Logger[F]
)(f: (String, String) => F[Unit]): F[Unit] =
(for {
md <- self.defaultBoth
html <- self.defaultBothHtml

View File

@ -32,7 +32,9 @@ final case class DeleteFieldValueCtx(
val titleTemplate = Right(mustache"{{eventType}} (by *{{account.user}}*)")
val bodyTemplate =
Right(mustache"""{{#content}}{{#field.label}}*{{field.label}}* {{/field.label}}{{^field.label}}*{{field.name}}* {{/field.label}} was removed from {{#items}}{{^-first}}, {{/-first}}{{#itemUrl}}[`{{name}}`]({{{itemUrl}}}/{{{id}}}){{/itemUrl}}{{^itemUrl}}`{{name}}`{{/itemUrl}}{{/items}}.{{/content}}""")
Right(
mustache"""{{#content}}{{#field.label}}*{{field.label}}* {{/field.label}}{{^field.label}}*{{field.name}}* {{/field.label}} was removed from {{#items}}{{^-first}}, {{/-first}}{{#itemUrl}}[`{{name}}`]({{{itemUrl}}}/{{{id}}}){{/itemUrl}}{{^itemUrl}}`{{name}}`{{/itemUrl}}{{/items}}.{{/content}}"""
)
}

View File

@ -19,39 +19,29 @@ import doobie._
import io.circe.Encoder
import io.circe.syntax._
import yamusca.implicits._
import yamusca.imports._
final case class ItemSelectionCtx(event: Event.ItemSelection, data: ItemSelectionCtx.Data)
extends AbstractEventContext {
val content = data.asJson
val titleTemplate = Right(mustache"Your items")
val bodyTemplate = Right(mustache"""
Hello {{{ content.username }}},
val bodyTemplate = event.contentStart match {
case Some(cnt) =>
mustache
.parse(cnt)
.leftMap { case (in, err) =>
s"Error parsing template: $err! Near ${in.pos}: ${in.raw}."
}
.map(start => start ++ ItemSelectionCtx.basicBody)
this is Docspell informing you about your next items.
case None =>
Right(ItemSelectionCtx.basicBodyStart ++ ItemSelectionCtx.basicBody)
}
{{#content}}
{{#itemUrl}}
{{#items}}
- {{#overDue}}**(OVERDUE)** {{/overDue}}[{{name}}]({{itemUrl}}/{{id}}){{#dueDate}}, {{#overDue}}was {{/overDue}}due {{dueIn}} on *{{dueDate}}*{{/dueDate}}; {{#corrOrg}}from {{corrOrg}}{{/corrOrg}} received on {{date}} via {{source}}
{{/items}}
{{/itemUrl}}
{{^itemUrl}}
{{#items}}
- {{#overDue}}**(OVERDUE)** {{/overDue}}*{{name}}*{{#dueDate}}, {{#overDue}}was {{/overDue}}due {{dueIn}} on *{{dueDate}}*{{/dueDate}}; {{#corrOrg}}from {{corrOrg}}{{/corrOrg}} received on {{date}} via {{source}}
{{/items}}
{{/itemUrl}}
{{#more}}
- … more have been left out for brevity
{{/more}}
{{/content}}
Sincerely yours,
Docspell
""")
implicit final class TemplateOps(self: Template) {
def ++(next: Template) = Template(self.els ++ next.els)
}
}
object ItemSelectionCtx {
@ -113,4 +103,26 @@ object ItemSelectionCtx {
account.user.id
)
}
private val basicBodyStart = mustache"""
Hello {{{ content.username }}},
this is Docspell informing you about your next items."""
private val basicBody = mustache"""
{{#content}}
{{#itemUrl}}
{{#items}}
- {{#overDue}}**(OVERDUE)** {{/overDue}}[{{name}}]({{itemUrl}}/{{id}}){{#dueDate}}, {{#overDue}}was {{/overDue}}due {{dueIn}} on *{{dueDate}}*{{/dueDate}}; {{#corrOrg}}from {{corrOrg}}{{/corrOrg}} received on {{date}} via {{source}}
{{/items}}
{{/itemUrl}}
{{^itemUrl}}
{{#items}}
- {{#overDue}}**(OVERDUE)** {{/overDue}}*{{name}}*{{#dueDate}}, {{#overDue}}was {{/overDue}}due {{dueIn}} on *{{dueDate}}*{{/dueDate}}; {{#corrOrg}}from {{corrOrg}}{{/corrOrg}} received on {{date}} via {{source}}
{{/items}}
{{/itemUrl}}
{{#more}}
- … more have been left out for brevity
{{/more}}
{{/content}}"""
}

View File

@ -23,7 +23,9 @@ final case class JobDoneCtx(event: Event.JobDone, data: JobDoneCtx.Data)
val content = data.asJson
val titleTemplate = Right(mustache"{{eventType}} (by *{{account.user}}*)")
val bodyTemplate = Right(mustache"""{{#content}}_'{{subject}}'_ finished {{/content}}""")
val bodyTemplate = Right(
mustache"""{{#content}}_'{{subject}}'_ finished {{/content}}"""
)
}
object JobDoneCtx {

View File

@ -24,7 +24,9 @@ final case class JobSubmittedCtx(event: Event.JobSubmitted, data: JobSubmittedCt
val titleTemplate = Right(mustache"{{eventType}} (by *{{account.user}}*)")
val bodyTemplate =
Right(mustache"""{{#content}}_'{{subject}}'_ submitted by {{submitter}} {{/content}}""")
Right(
mustache"""{{#content}}_'{{subject}}'_ submitted by {{submitter}} {{/content}}"""
)
}
object JobSubmittedCtx {

View File

@ -30,7 +30,9 @@ final case class SetFieldValueCtx(event: Event.SetFieldValue, data: SetFieldValu
val titleTemplate = Right(mustache"{{eventType}} (by *{{account.user}}*)")
val bodyTemplate =
Right(mustache"""{{#content}}{{#field.label}}*{{field.label}}* {{/field.label}}{{^field.label}}*{{field.name}}* {{/field.label}} was set to '{{value}}' on {{#items}}{{^-first}}, {{/-first}}{{#itemUrl}}[`{{name}}`]({{{itemUrl}}}/{{{id}}}){{/itemUrl}}{{^itemUrl}}`{{name}}`{{/itemUrl}}{{/items}}.{{/content}}""")
Right(
mustache"""{{#content}}{{#field.label}}*{{field.label}}* {{/field.label}}{{^field.label}}*{{field.name}}* {{/field.label}} was set to '{{value}}' on {{#items}}{{^-first}}, {{/-first}}{{#itemUrl}}[`{{name}}`]({{{itemUrl}}}/{{{id}}}){{/itemUrl}}{{^itemUrl}}`{{name}}`{{/itemUrl}}{{/items}}.{{/content}}"""
)
}

View File

@ -28,7 +28,9 @@ final case class TagsChangedCtx(event: Event.TagsChanged, data: TagsChangedCtx.D
val titleTemplate = Right(mustache"{{eventType}} (by *{{account.user}}*)")
val bodyTemplate =
Right(mustache"""{{#content}}{{#added}}{{#-first}}Adding {{/-first}}{{^-first}}, {{/-first}}*{{name}}*{{/added}}{{#removed}}{{#added}}{{#-first}};{{/-first}}{{/added}}{{#-first}} Removing {{/-first}}{{^-first}}, {{/-first}}*{{name}}*{{/removed}} on {{#items}}{{^-first}}, {{/-first}}{{#itemUrl}}[`{{name}}`]({{{itemUrl}}}/{{{id}}}){{/itemUrl}}{{^itemUrl}}`{{name}}`{{/itemUrl}}{{/items}}.{{/content}}""")
Right(
mustache"""{{#content}}{{#added}}{{#-first}}Adding {{/-first}}{{^-first}}, {{/-first}}*{{name}}*{{/added}}{{#removed}}{{#added}}{{#-first}};{{/-first}}{{/added}}{{#-first}} Removing {{/-first}}{{^-first}}, {{/-first}}*{{name}}*{{/removed}} on {{#items}}{{^-first}}, {{/-first}}{{#itemUrl}}[`{{name}}`]({{{itemUrl}}}/{{{id}}}){{/itemUrl}}{{^itemUrl}}`{{name}}`{{/itemUrl}}{{/items}}.{{/content}}"""
)
}
object TagsChangedCtx {

View File

@ -46,9 +46,9 @@ class TagsChangedCtxTest extends FunSuite {
TagsChangedCtx.Data(account, List(item), List(tag), Nil, url.some.map(_.asString))
)
assertEquals(ctx.defaultTitle, "TagsChanged (by *user2*)")
assertEquals(ctx.defaultTitle.toOption.get, "TagsChanged (by *user2*)")
assertEquals(
ctx.defaultBody,
ctx.defaultBody.toOption.get,
"Adding *tag-red* on [`Report 2`](http://test/item-1)."
)
}
@ -65,9 +65,9 @@ class TagsChangedCtxTest extends FunSuite {
)
)
assertEquals(ctx.defaultTitle, "TagsChanged (by *user2*)")
assertEquals(ctx.defaultTitle.toOption.get, "TagsChanged (by *user2*)")
assertEquals(
ctx.defaultBody,
ctx.defaultBody.toOption.get,
"Adding *tag-red*; Removing *tag-blue* on [`Report 2`](http://test/item-1)."
)
}