From 79a9f1509c4430f6e65c67e41098ef7aaa227c3f Mon Sep 17 00:00:00 2001
From: Vitaly Takmazov
Date: Thu, 4 Jul 2019 14:31:22 +0300
Subject: Render email using templates
* do not include tracking pixel in pm
* reorganize snapshots layout
* reconfigure pebble to not trim anything
---
src/main/java/com/juick/server/EmailManager.java | 49 ++++++++++++++++++----
.../server/configuration/WwwAppConfiguration.java | 1 +
src/main/resources/templates/email/html.html | 10 +++++
src/main/resources/templates/email/plaintext.html | 5 +++
.../java/com/juick/server/tests/ServerTests.java | 36 +++++++++++++++-
src/test/resources/mocks/activity/testfollow.json | 15 -------
src/test/resources/mocks/activity/testuser.json | 27 ------------
.../resources/snapshots/activity/testfollow.json | 15 +++++++
.../resources/snapshots/activity/testuser.json | 27 ++++++++++++
src/test/resources/snapshots/email/private.html | 10 +++++
.../resources/snapshots/email/subscription.html | 10 +++++
.../resources/snapshots/email/subscription.txt | 5 +++
12 files changed, 158 insertions(+), 52 deletions(-)
create mode 100644 src/main/resources/templates/email/html.html
create mode 100644 src/main/resources/templates/email/plaintext.html
delete mode 100644 src/test/resources/mocks/activity/testfollow.json
delete mode 100644 src/test/resources/mocks/activity/testuser.json
create mode 100644 src/test/resources/snapshots/activity/testfollow.json
create mode 100644 src/test/resources/snapshots/activity/testuser.json
create mode 100644 src/test/resources/snapshots/email/private.html
create mode 100644 src/test/resources/snapshots/email/subscription.html
create mode 100644 src/test/resources/snapshots/email/subscription.txt
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
--
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." +
- "
Configure or disable notifications",
- 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 messageHeaders) {
@@ -162,4 +165,34 @@ public class EmailManager implements NotificationListener {
logger.error("mail exception", ex);
}
}
+
+ 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();
+ }
+ }
}
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 }}
+
+
+--
+
+You are receiving this because you are subscribed to this user, discussion, tag or mentioned.
+Reply to this email directly or
+ {% if msg.mid > 0 %}{% endif %}view it on Juick.
+
+Configure or disable notifications
\ 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
diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java
index 4c72913e..de742447 100644
--- a/src/test/java/com/juick/server/tests/ServerTests.java
+++ b/src/test/java/com/juick/server/tests/ServerTests.java
@@ -28,6 +28,7 @@ import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.jayway.jsonpath.JsonPath;
import com.juick.*;
+import com.juick.formatters.PlainTextFormatter;
import com.juick.model.AnonymousUser;
import com.juick.model.CommandResult;
import com.juick.model.PrivateChats;
@@ -218,10 +219,16 @@ public class ServerTests {
@Inject
private Profile profileController;
- @Value("classpath:mocks/activity/testuser.json")
+ @Value("classpath:snapshots/activity/testuser.json")
private Resource testuserResponse;
- @Value("classpath:mocks/activity/testfollow.json")
+ @Value("classpath:snapshots/activity/testfollow.json")
private Resource testfollowRequest;
+ @Value("classpath:snapshots/email/subscription.html")
+ private Resource testSubscriptionHtmlEmail;
+ @Value("classpath:snapshots/email/private.html")
+ private Resource testPrivateHtmlEmail;
+ @Value("classpath:snapshots/email/subscription.txt")
+ private Resource testSubscriptionTextEmail;
@Value("classpath:static/av-96.png")
private Resource defaultAvatar;
@Value("classpath:cmyk.jpg")
@@ -241,6 +248,8 @@ public class ServerTests {
@Inject
private KeystoreManager testKeystoreManager;
+ @Inject
+ private EmailManager emailManager;
@Inject
private ApplicationEventPublisher applicationEventPublisher;
@@ -2121,4 +2130,27 @@ public class ServerTests {
.findFirst().orElseThrow(IllegalStateException::new);
assertThat(rareTagStats.getUsageCount(), is(1));
}
+ private String getSnapshot(Resource resource) throws IOException {
+ return IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8);
+ }
+ @Test
+ public void emailTemplatesTest() throws IOException {
+ String plainText = emailManager.renderPlaintext("yo", "https://localhost/m/1").orElseThrow();
+ assertThat(plainText, is(getSnapshot(testSubscriptionTextEmail)));
+ User demo = MockUtils.mockUser(45, ugnichName, ugnichPassword);
+ Message html = MockUtils.mockMessage(56, demo, "yo");
+ String htmlText = emailManager.renderHtml(
+ MessageUtils.formatHtml(html),
+ PlainTextFormatter.formatUrl(html),
+ html, "12345")
+ .orElseThrow();
+ assertThat(htmlText, is(getSnapshot(testSubscriptionHtmlEmail)));
+ html.setMid(0);
+ String htmlPM = emailManager.renderHtml(
+ MessageUtils.formatHtml(html),
+ PlainTextFormatter.formatUrl(html),
+ html, "12345")
+ .orElseThrow();
+ assertThat(htmlPM, is(getSnapshot(testPrivateHtmlEmail)));
+ }
}
diff --git a/src/test/resources/mocks/activity/testfollow.json b/src/test/resources/mocks/activity/testfollow.json
deleted file mode 100644
index e308e52e..00000000
--- a/src/test/resources/mocks/activity/testfollow.json
+++ /dev/null
@@ -1,15 +0,0 @@
-{
- "@context": [
- "https://www.w3.org/ns/activitystreams",
- "https://w3id.org/security/v1",
- {
- "schema": "http://schema.org#",
- "PropertyValue": "schema:PropertyValue",
- "value": "schema:value"
- }
- ],
- "id": "https://example.com/12345678",
- "type": "Follow",
- "actor": "https://example.com/u/testuser",
- "object": "http://localhost:8080/u/ugnich"
-}
\ No newline at end of file
diff --git a/src/test/resources/mocks/activity/testuser.json b/src/test/resources/mocks/activity/testuser.json
deleted file mode 100644
index 95fc2aa9..00000000
--- a/src/test/resources/mocks/activity/testuser.json
+++ /dev/null
@@ -1,27 +0,0 @@
-{
- "@context": [
- "https://www.w3.org/ns/activitystreams",
- "https://w3id.org/security/v1",
- {
- "schema": "http://schema.org#",
- "PropertyValue": "schema:PropertyValue",
- "value": "schema:value"
- }
- ],
- "id": "https://example.com/u/testuser",
- "type": "Person",
- "following": "https://example.com/u/testuser/following",
- "followers": "https://example.com/u/testuser/followers",
- "inbox": "https://example.com/u/testuser/inbox",
- "outbox": "https://example.com/u/testuser/outbox",
- "preferredUsername": "testuser",
- "url": "https://example.com/@testuser",
- "publicKey": {
- "id": "https://example.com/u/testuser#main-key",
- "owner": "https://example.com/u/testuser",
- "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiHKRdKFFeT4P/MVlNbxC\nbbgXOkEdeQzvJB/wAJgSYbUwm9SzNFzttePQXk3/MWoK2awWUInZTduVHsWt8zU7\nO3d9PAW6YH6L1oDkjgMLAb9aUWV2ClQWMwsn88WKK9Rb1WOmd8BrXjPfmeFK2ypQ\n9eg8aKpH36WAXiiaTDfBupBZ0Ki2+E87BrWxpbUeDC1dkV+zbl8BMm7X0rp+reoC\nYUWMcjQMzhMmQOXUd4zwJIDPZDMdF4beq/y6WPSUTVgjs4kPDS1HT60ATnsUqyPE\n6tuGxG4j0msb4TTre87PKxMU5YPOxSiqNL0O/3u9/2shVPpjDa/uy9W+VaeBHbFm\nSQIDAQAB\n-----END PUBLIC KEY-----\n"
- },
- "endpoints": {
- "sharedInbox": "https://example.com/inbox"
- }
-}
\ No newline at end of file
diff --git a/src/test/resources/snapshots/activity/testfollow.json b/src/test/resources/snapshots/activity/testfollow.json
new file mode 100644
index 00000000..e308e52e
--- /dev/null
+++ b/src/test/resources/snapshots/activity/testfollow.json
@@ -0,0 +1,15 @@
+{
+ "@context": [
+ "https://www.w3.org/ns/activitystreams",
+ "https://w3id.org/security/v1",
+ {
+ "schema": "http://schema.org#",
+ "PropertyValue": "schema:PropertyValue",
+ "value": "schema:value"
+ }
+ ],
+ "id": "https://example.com/12345678",
+ "type": "Follow",
+ "actor": "https://example.com/u/testuser",
+ "object": "http://localhost:8080/u/ugnich"
+}
\ No newline at end of file
diff --git a/src/test/resources/snapshots/activity/testuser.json b/src/test/resources/snapshots/activity/testuser.json
new file mode 100644
index 00000000..95fc2aa9
--- /dev/null
+++ b/src/test/resources/snapshots/activity/testuser.json
@@ -0,0 +1,27 @@
+{
+ "@context": [
+ "https://www.w3.org/ns/activitystreams",
+ "https://w3id.org/security/v1",
+ {
+ "schema": "http://schema.org#",
+ "PropertyValue": "schema:PropertyValue",
+ "value": "schema:value"
+ }
+ ],
+ "id": "https://example.com/u/testuser",
+ "type": "Person",
+ "following": "https://example.com/u/testuser/following",
+ "followers": "https://example.com/u/testuser/followers",
+ "inbox": "https://example.com/u/testuser/inbox",
+ "outbox": "https://example.com/u/testuser/outbox",
+ "preferredUsername": "testuser",
+ "url": "https://example.com/@testuser",
+ "publicKey": {
+ "id": "https://example.com/u/testuser#main-key",
+ "owner": "https://example.com/u/testuser",
+ "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiHKRdKFFeT4P/MVlNbxC\nbbgXOkEdeQzvJB/wAJgSYbUwm9SzNFzttePQXk3/MWoK2awWUInZTduVHsWt8zU7\nO3d9PAW6YH6L1oDkjgMLAb9aUWV2ClQWMwsn88WKK9Rb1WOmd8BrXjPfmeFK2ypQ\n9eg8aKpH36WAXiiaTDfBupBZ0Ki2+E87BrWxpbUeDC1dkV+zbl8BMm7X0rp+reoC\nYUWMcjQMzhMmQOXUd4zwJIDPZDMdF4beq/y6WPSUTVgjs4kPDS1HT60ATnsUqyPE\n6tuGxG4j0msb4TTre87PKxMU5YPOxSiqNL0O/3u9/2shVPpjDa/uy9W+VaeBHbFm\nSQIDAQAB\n-----END PUBLIC KEY-----\n"
+ },
+ "endpoints": {
+ "sharedInbox": "https://example.com/inbox"
+ }
+}
\ No newline at end of file
diff --git a/src/test/resources/snapshots/email/private.html b/src/test/resources/snapshots/email/private.html
new file mode 100644
index 00000000..fee506c7
--- /dev/null
+++ b/src/test/resources/snapshots/email/private.html
@@ -0,0 +1,10 @@
+@ugnich:
yo
+
+
+--
+
+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.
+
+Configure or disable notifications
\ No newline at end of file
diff --git a/src/test/resources/snapshots/email/subscription.html b/src/test/resources/snapshots/email/subscription.html
new file mode 100644
index 00000000..c75360ed
--- /dev/null
+++ b/src/test/resources/snapshots/email/subscription.html
@@ -0,0 +1,10 @@
+@ugnich:
yo
+
+
+--
+
+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.
+
+Configure or disable notifications
\ No newline at end of file
diff --git a/src/test/resources/snapshots/email/subscription.txt b/src/test/resources/snapshots/email/subscription.txt
new file mode 100644
index 00000000..57967c4b
--- /dev/null
+++ b/src/test/resources/snapshots/email/subscription.txt
@@ -0,0 +1,5 @@
+yo
+
+--
+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: https://localhost/m/1
\ No newline at end of file
--
cgit v1.2.3