aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/server/EmailManager.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/server/EmailManager.java')
-rw-r--r--src/main/java/com/juick/server/EmailManager.java84
1 files changed, 41 insertions, 43 deletions
diff --git a/src/main/java/com/juick/server/EmailManager.java b/src/main/java/com/juick/server/EmailManager.java
index d4e521e4..3939d706 100644
--- a/src/main/java/com/juick/server/EmailManager.java
+++ b/src/main/java/com/juick/server/EmailManager.java
@@ -17,20 +17,25 @@
package com.juick.server;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.juick.model.Message;
import com.juick.model.User;
import com.juick.server.api.SystemActivity;
+import com.juick.server.util.HttpBadRequestException;
+import com.juick.server.www.WebApp;
import com.juick.service.EmailService;
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;
-import org.springframework.stereotype.Component;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.event.EventListener;
+import org.springframework.scheduling.annotation.Async;
import javax.annotation.Nonnull;
import javax.inject.Inject;
@@ -42,19 +47,11 @@ 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 java.util.*;
import static com.juick.formatters.PlainTextFormatter.formatPost;
import static com.juick.formatters.PlainTextFormatter.formatUrl;
-@Component
public class EmailManager implements NotificationListener {
public static final String MSGID_PATTERN = "\\.|@|<";
@@ -68,6 +65,12 @@ public class EmailManager implements NotificationListener {
private UserService userService;
@Inject
private PebbleEngine pebbleEngine;
+ @Inject
+ private ObjectMapper jsonMapper;
+ @Inject
+ private WebApp webApp;
+ @Value("${service_email:}")
+ private String serviceEmail;
@Override
public void processSystemEvent(@Nonnull SystemEvent systemEvent) {
@@ -77,6 +80,13 @@ public class EmailManager implements NotificationListener {
if (activity.getType().equals(SystemActivity.ActivityType.message)) {
processMessage(msg, subscribers);
}
+ try {
+ var eventHeader = Collections.singletonMap("X-Event-Version", "1.0");
+ sendEmail("noreply@juick.com", serviceEmail, "New system event",
+ jsonMapper.writeValueAsString(systemEvent.getActivity()), null, eventHeader);
+ } catch (JsonProcessingException e) {
+ logger.warn("JSON exception", e);
+ }
}
private void processMessage(Message msg, List<User> subscribedUsers) {
if (msg.isService()) {
@@ -104,6 +114,24 @@ public class EmailManager implements NotificationListener {
}
+ @Async
+ @EventListener
+ public void processMailVerificationEvent(MailVerificationEvent mailVerificationEvent) {
+ if (!sendEmail("noreply@juick.com", mailVerificationEvent.getEmail(), "Juick authorization link",
+ String.format("Follow link to attach this email to Juick account:\n" +
+ "http://juick.com/settings?page=auth-email&code=%s\n\n" +
+ "If you don't know, what this mean - just ignore this mail.\n", mailVerificationEvent.getCode()),
+ StringUtils.EMPTY, Collections.emptyMap())) {
+ throw new HttpBadRequestException();
+ }
+ }
+ @Async
+ @EventListener
+ public void processAccountVerificationEvent(AccountVerificationEvent accountVerificationEvent) {
+ String signupUrl = String.format("Follow this link to create Juick account: https://juick.com/signup?type=email&hash=%s", accountVerificationEvent.getCode());
+ sendEmail("noreply@juick.com", accountVerificationEvent.getEmail(), "Juick registration", signupUrl, StringUtils.EMPTY, Collections.emptyMap());
+ }
+
private void emailNotify(String email, String subject, Message msg) {
Map<String, String> headers = new HashMap<>();
if (!MessageUtils.isPM(msg)) {
@@ -118,9 +146,9 @@ public class EmailManager implements NotificationListener {
headers.put("In-Reply-To", String.format("<%d.%d@juick.com>", original.getMid(), original.getRid()));
}
}
- String plainText = renderPlaintext(formatPost(msg), formatUrl(msg)).orElseThrow(IllegalStateException::new);
+ String plainText = webApp.renderPlaintext(formatPost(msg), formatUrl(msg)).orElseThrow(IllegalStateException::new);
String hash = userService.getHashByUID(userService.getUserByEmail(email).getUid());
- String htmlText = renderHtml(MessageUtils.formatHtml(msg), formatUrl(msg), msg, hash).orElseThrow(IllegalStateException::new);
+ String htmlText = webApp.renderHtml(MessageUtils.formatHtml(msg), formatUrl(msg), msg, hash).orElseThrow(IllegalStateException::new);
sendEmail(StringUtils.EMPTY, email, subject, plainText, htmlText, headers);
}
public boolean sendEmail(String from, String to, String subject, String textPart, String htmlPart, Map<String, String> messageHeaders) {
@@ -170,34 +198,4 @@ public class EmailManager implements NotificationListener {
return false;
}
}
-
- 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();
- }
- }
}