aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/com/juick/server/EmailManager.java49
-rw-r--r--src/main/java/com/juick/server/configuration/WwwAppConfiguration.java1
-rw-r--r--src/main/resources/templates/email/html.html10
-rw-r--r--src/main/resources/templates/email/plaintext.html5
4 files changed, 57 insertions, 8 deletions
diff --git a/src/main/java/com/juick/server/EmailManager.java b/src/main/java/com/juick/server/EmailManager.java
index af4ed416..3d17a041 100644
--- a/src/main/java/com/juick/server/EmailManager.java
+++ b/src/main/java/com/juick/server/EmailManager.java
@@ -7,6 +7,8 @@ import com.juick.service.MessagesService;
import com.juick.service.UserService;
import com.juick.service.component.*;
import com.juick.util.MessageUtils;
+import com.mitchellbosecke.pebble.PebbleEngine;
+import com.mitchellbosecke.pebble.template.PebbleTemplate;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -22,9 +24,13 @@ import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Properties;
import static com.juick.formatters.PlainTextFormatter.formatPost;
@@ -42,6 +48,9 @@ public class EmailManager implements NotificationListener {
private MessagesService messagesService;
@Inject
private UserService userService;
+ @Inject
+ private PebbleEngine pebbleEngine;
+
@Override
public void processMessageEvent(@Nonnull MessageEvent event) {
Message msg = event.getMessage();
@@ -107,15 +116,9 @@ public class EmailManager implements NotificationListener {
headers.put("In-Reply-To", String.format("<%d.%d@juick.com>", original.getMid(), original.getRid()));
}
}
- String plainText = String.format("%s\n\n--\nYou are receiving this because you are subscribed to this user," +
- " discussion, tag or mentioned. Reply to this email directly or view it on Juick: %s.",
- formatPost(msg), formatUrl(msg));
+ String plainText = renderPlaintext(formatPost(msg), formatUrl(msg)).orElseThrow(IllegalStateException::new);
String hash = userService.getHashByUID(userService.getUserByEmail(email).getUid());
- String htmlText = String.format("%s<br /><br />--<br />You are receiving this because you are subscribed to this user" +
- ", discussion, tag or mentioned. Reply to this email directly or <a href=\"%s\"><img src=\"https://api.juick.com/thread/mark_read/%d-%d.gif?hash=%s\" />view it</a> on Juick." +
- "<br /><a href=\"https://juick.com/settings?hash=%s\">Configure or disable notifications</a>",
- msg.isHtml() ? msg.getText() : MessageUtils.formatHtml(msg), formatUrl(msg),
- msg.getMid(), msg.getRid(), hash, hash);
+ String htmlText = renderHtml(MessageUtils.formatHtml(msg), formatUrl(msg), msg, hash).orElseThrow(IllegalStateException::new);
sendEmail(email, subject, plainText, htmlText, headers);
}
public void sendEmail(String to, String subject, String textPart, String htmlPart, Map<String, String> messageHeaders) {
@@ -162,4 +165,34 @@ public class EmailManager implements NotificationListener {
logger.error("mail exception", ex);
}
}
+
+ public Optional<String> renderPlaintext(String body, String messageUrl) {
+ PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/plaintext");
+ Map<String, Object> 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<String> renderHtml(String body, String messageUrl, Message msg, String hash) {
+ PebbleTemplate noteTemplate = pebbleEngine.getTemplate("email/html");
+ Map<String, Object> 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();
+ }
+ }
}
diff --git a/src/main/java/com/juick/server/configuration/WwwAppConfiguration.java b/src/main/java/com/juick/server/configuration/WwwAppConfiguration.java
index 93fe7282..635302c7 100644
--- a/src/main/java/com/juick/server/configuration/WwwAppConfiguration.java
+++ b/src/main/java/com/juick/server/configuration/WwwAppConfiguration.java
@@ -113,6 +113,7 @@ public class WwwAppConfiguration implements WebMvcConfigurer {
.cacheActive(!devToolsArePresent)
.extension(springExtension())
.extension(new FormatterExtension())
+ .newLineTrimming(false)
.strictVariables(true)
.build();
}
diff --git a/src/main/resources/templates/email/html.html b/src/main/resources/templates/email/html.html
new file mode 100644
index 00000000..086df532
--- /dev/null
+++ b/src/main/resources/templates/email/html.html
@@ -0,0 +1,10 @@
+{{ messageBody | raw }}
+<br />
+<br />
+--
+<br />
+You are receiving this because you are subscribed to this user, discussion, tag or mentioned.
+Reply to this email directly or <a href="{{ messageUrl }}">
+ {% if msg.mid > 0 %}<img src="https://api.juick.com/thread/mark_read/{{ msg.mid }}-{{ msg.rid }}.gif?hash={{ hash }}" />{% endif %}view it</a> on Juick.
+<br />
+<a href="https://juick.com/settings?hash={{ hash }}">Configure or disable notifications</a> \ No newline at end of file
diff --git a/src/main/resources/templates/email/plaintext.html b/src/main/resources/templates/email/plaintext.html
new file mode 100644
index 00000000..a0df0038
--- /dev/null
+++ b/src/main/resources/templates/email/plaintext.html
@@ -0,0 +1,5 @@
+{{ messageBody }}
+
+--
+You are receiving this because you are subscribed to this user, discussion, tag or mentioned.
+Reply to this email directly or view it on Juick: {{ messageUrl }} \ No newline at end of file