diff options
author | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2022-06-04 09:06:31 +0200 |
---|---|---|
committer | Matthias Andreas Benkard <code@mail.matthias.benkard.de> | 2022-06-04 16:24:24 +0200 |
commit | 1937e6a72594f98d1fa031d8220f10ccc798c2be (patch) | |
tree | 5d78a7976b75f3196e50856577fb4a23d26617ce /src | |
parent | 0351a8fbbac6cfa671128513605288fb940ce94b (diff) |
KB66 Add comment notification email.
Change-Id: I27feecfe2d4309397b116552856227eacb7e9600
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java | 25 | ||||
-rw-r--r-- | src/main/java/eu/mulk/mulkcms2/benki/users/User.java | 6 | ||||
-rw-r--r-- | src/main/resources/templates/PostResource/commentNotificationMail.txt | 9 | ||||
-rw-r--r-- | src/main/resources/templates/PostResource/postList.html (renamed from src/main/resources/templates/benki/posts/postList.html) | 0 |
4 files changed, 38 insertions, 2 deletions
diff --git a/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java b/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java index 5a38262..d1e5775 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/posts/PostResource.java @@ -21,6 +21,7 @@ import eu.mulk.mulkcms2.benki.login.LoginRoles; import eu.mulk.mulkcms2.benki.posts.Post.PostPage; import eu.mulk.mulkcms2.benki.posts.Post.Scope; import eu.mulk.mulkcms2.benki.users.User; +import io.quarkus.mailer.MailTemplate.MailTemplateInstance; import io.quarkus.qute.CheckedTemplate; import io.quarkus.qute.TemplateExtension; import io.quarkus.qute.TemplateInstance; @@ -45,6 +46,9 @@ import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.stream.Collectors; import javax.annotation.CheckForNull; import javax.annotation.Nullable; @@ -96,7 +100,10 @@ public abstract class PostResource { @ConfigProperty(name = "mulkcms.posts.default-max-results") int defaultMaxResults; - @CheckedTemplate(basePath = "benki/posts") + @ConfigProperty(name = "quarkus.mailer.from") + String mailSenderAddress; + + @CheckedTemplate static class Templates { public static native TemplateInstance postList( @@ -112,6 +119,9 @@ public abstract class PostResource { @CheckForNull Integer nextCursor, @CheckForNull Integer pageSize, @CheckForNull String searchQuery); + + public static native MailTemplateInstance commentNotificationMail( + int postId, LazychatMessage comment); } @Inject protected SecurityIdentity identity; @@ -303,7 +313,7 @@ public abstract class PostResource { @PathParam("id") int postId, @FormParam("message") @NotEmpty String message, @FormParam("hashcash-salt") long hashcashSalt) - throws NoSuchAlgorithmException { + throws NoSuchAlgorithmException, ExecutionException, InterruptedException, TimeoutException { var hashcashDigest = MessageDigest.getInstance(hashcashDigestAlgorithm); hashcashDigest.update("Hashcash-Salt: ".getBytes(UTF_8)); hashcashDigest.update(String.valueOf(hashcashSalt).getBytes(UTF_8)); @@ -334,6 +344,17 @@ public abstract class PostResource { assignPostTargets(post.getVisibility(), post.owner, comment); comment.persist(); + var admins = User.findAdmins(); + + var mailText = Templates.commentNotificationMail(postId, comment); + var sendJob = + mailText + .subject(String.format("MulkCMS comment #%d", comment.id)) + .to(mailSenderAddress) + .bcc(admins.stream().map(x -> x.email).toArray(String[]::new)) + .send(); + sendJob.subscribe().asCompletionStage().get(10000, TimeUnit.SECONDS); + return Response.seeOther(UriBuilder.fromUri("/posts/{id}").build(postId)).build(); } diff --git a/src/main/java/eu/mulk/mulkcms2/benki/users/User.java b/src/main/java/eu/mulk/mulkcms2/benki/users/User.java index 04a5cd4..99ab91f 100644 --- a/src/main/java/eu/mulk/mulkcms2/benki/users/User.java +++ b/src/main/java/eu/mulk/mulkcms2/benki/users/User.java @@ -8,6 +8,7 @@ import eu.mulk.mulkcms2.benki.posts.Post; import eu.mulk.mulkcms2.benki.wiki.WikiPageRevision; import io.quarkus.hibernate.orm.panache.PanacheEntityBase; import java.util.Collection; +import java.util.List; import java.util.Objects; import java.util.Set; import javax.annotation.CheckForNull; @@ -162,6 +163,11 @@ public class User extends PanacheEntityBase { .singleResult(); } + public static List<User> findAdmins() { + return find("select distinct u from BenkiUser u left join u.effectiveRoles r left join r.tags t where t = 'admin' or u.status = 'admin'") + .list(); + } + public final boolean canSee(Post message) { return message.isVisibleTo(this); } diff --git a/src/main/resources/templates/PostResource/commentNotificationMail.txt b/src/main/resources/templates/PostResource/commentNotificationMail.txt new file mode 100644 index 0000000..a226106 --- /dev/null +++ b/src/main/resources/templates/PostResource/commentNotificationMail.txt @@ -0,0 +1,9 @@ +{@int postId} +{@eu.mulk.mulkcms2.benki.lazychat.LazychatMessage comment} +New Comment +=========== + +Link: <https://matthias.benkard.de/posts/{postId}#comment-{comment.id}> +{#if comment.owner != null}Author: {comment.owner.firstName}{/if} + +{comment.text.content} diff --git a/src/main/resources/templates/benki/posts/postList.html b/src/main/resources/templates/PostResource/postList.html index 2f932ec..2f932ec 100644 --- a/src/main/resources/templates/benki/posts/postList.html +++ b/src/main/resources/templates/PostResource/postList.html |