From afb897034311fe7e0e981ec44100d3b3fd3ef39d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 7 Mar 2020 16:01:52 +0300 Subject: Load EmailManager conditionally --- src/main/java/com/juick/server/www/WebApp.java | 41 ++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 3 deletions(-) (limited to 'src/main/java/com/juick/server/www') diff --git a/src/main/java/com/juick/server/www/WebApp.java b/src/main/java/com/juick/server/www/WebApp.java index 8476224d..bf013c3b 100644 --- a/src/main/java/com/juick/server/www/WebApp.java +++ b/src/main/java/com/juick/server/www/WebApp.java @@ -16,9 +16,12 @@ */ package com.juick.server.www; +import com.juick.model.Message; import com.juick.model.Tag; import com.juick.model.User; import com.juick.service.TagService; +import com.mitchellbosecke.pebble.PebbleEngine; +import com.mitchellbosecke.pebble.template.PebbleTemplate; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -27,9 +30,10 @@ import org.springframework.web.util.UriComponentsBuilder; import javax.annotation.PostConstruct; import javax.inject.Inject; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; +import java.io.IOException; +import java.io.StringWriter; +import java.io.Writer; +import java.util.*; import java.util.stream.Stream; /** @@ -42,6 +46,8 @@ public class WebApp { private TagService tagService; @Inject private ResourceUrlProvider resourceUrlProvider; + @Inject + private PebbleEngine pebbleEngine; @Value("${img_url:http://localhost:8080/i/}") private String baseImagesUri; UriComponentsBuilder avatarBuilder; @@ -91,4 +97,33 @@ public class WebApp { avatarBuilder.replacePath(getAvatarWebPath(user)); return avatarBuilder.build().toUriString(); } + public Optional renderPlaintext(String body, String messageUrl) { + PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/plaintext"); + Map context = new HashMap<>(); + context.put("messageBody", body); + context.put("messageUrl", messageUrl); + try { + Writer writer = new StringWriter(); + noteTemplate.evaluate(writer, context); + return Optional.of(writer.toString()); + } catch (IOException e) { + return Optional.empty(); + } + } + + public Optional renderHtml(String body, String messageUrl, Message msg, String hash) { + PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/html"); + Map context = new HashMap<>(); + context.put("messageBody", body); + context.put("messageUrl", messageUrl); + context.put("msg", msg); + context.put("hash", hash); + try { + Writer writer = new StringWriter(); + noteTemplate.evaluate(writer, context); + return Optional.of(writer.toString()); + } catch (IOException e) { + return Optional.empty(); + } + } } -- cgit v1.2.3