diff options
Diffstat (limited to 'juick-server/src/main/java/com/juick/server/EmailManager.java')
-rw-r--r-- | juick-server/src/main/java/com/juick/server/EmailManager.java | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/juick-server/src/main/java/com/juick/server/EmailManager.java b/juick-server/src/main/java/com/juick/server/EmailManager.java new file mode 100644 index 00000000..4c9bf3da --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/EmailManager.java @@ -0,0 +1,110 @@ +package com.juick.server; + +import com.juick.Message; +import com.juick.server.component.MessageEvent; +import com.juick.service.EmailService; +import com.juick.service.MessagesService; +import com.juick.service.SubscriptionService; +import com.juick.service.UserService; +import com.juick.util.MessageUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Component; + +import javax.annotation.Nonnull; +import javax.inject.Inject; +import javax.mail.MessagingException; +import javax.mail.Multipart; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; +import java.util.Properties; + +import static com.juick.formatters.PlainTextFormatter.formatPost; +import static com.juick.formatters.PlainTextFormatter.formatUrl; + +@Component +public class EmailManager implements ApplicationListener<MessageEvent> { + + public static final String MSGID_PATTERN = "\\.|@|<"; + + private static final Logger logger = LoggerFactory.getLogger(EmailManager.class); + @Inject + private EmailService emailService; + @Inject + private SubscriptionService subscriptionService; + @Inject + private MessagesService messagesService; + @Inject + private UserService userService; + @Override + public void onApplicationEvent(@Nonnull MessageEvent event) { + Message msg = event.getMessage(); + if (msg.getMid() > 0 && msg.getRid() == 0) { + String subject = String.format("New message from %s", msg.getUser().getName()); + subscriptionService.getSubscribedUsers(msg.getUser().getUid(), msg.getMid()) + .forEach(user -> emailService.getEmails(user.getUid(), true) + .forEach(email -> emailNotify(email, subject, msg))); + } else if (msg.getRid() > 0) { + Message originalMessage = messagesService.getMessage(msg.getMid()); + String subject = String.format("New reply to %s", originalMessage.getUser().getName()); + subscriptionService.getUsersSubscribedToComments(msg.getMid(), msg.getUser().getUid()) + .forEach(user -> emailService.getEmails(user.getUid(), true) + .forEach(email -> emailNotify(email, subject, msg))); + } + } + + private void emailNotify(String email, String subject, Message msg) { + Properties prop = System.getProperties(); + prop.put("mail.smtp.starttls.enable", "true"); + Session session = Session.getDefaultInstance(prop); + try { + Transport transport = session.getTransport("smtp"); + MimeMessage message = new MimeMessage(session) { + protected void updateMessageID() throws MessagingException { + setHeader("Message-ID", String.format("<%d.%d@juick.com>", msg.getMid(), msg.getRid())); + if (msg.getRid() > 0) { + if (msg.getReplyto() > 0) { + Message replyto = messagesService.getReply(msg.getMid(), msg.getReplyto()); + setHeader("In-Reply-To", String.format("<%d.%d@juick.com>", replyto.getMid(), replyto.getRid())); + } else { + Message original = messagesService.getMessage(msg.getMid()); + setHeader("In-Reply-To", String.format("<%d.%d@juick.com>", original.getMid(), original.getRid())); + } + } + } + }; + message.setFrom(new InternetAddress("juick@juick.com")); + message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(email)); + message.setSubject(subject); + String plainText = String.format("%s\n\n--\nYou are receiving this because you are subscribed to this user " + + ", discussion or tag. Reply to this email directly or view it on Juick: %s.", + formatPost(msg), formatUrl(msg)); + MimeBodyPart textBodyPart = new MimeBodyPart(); + textBodyPart.setContent(plainText, "text/plain; charset=UTF-8"); + String htmlText = String.format("%s<br /><br />--<br />You are receiving this because you are subscribed to this user" + + ", discussion or tag. Reply to this email directly or <a href=\"%s\">view it</a> on Juick." + + "<br /><a href=\"https://juick.com/settings?hash=%s\">Configure or disable notifications</a>", + MessageUtils.formatHtml(msg), formatUrl(msg), + userService.getHashByUID(userService.getUserByEmail(email).getUid())); + MimeBodyPart htmlBodyPart = new MimeBodyPart(); + htmlBodyPart.setContent(htmlText, "text/html; charset=UTF-8"); + Multipart multipart = new MimeMultipart("alternative"); + multipart.addBodyPart(textBodyPart); + multipart.addBodyPart(htmlBodyPart); + message.setContent(multipart); + message.setHeader("List-Unsubscribe", String.format("https://juick.com/settings?hash=%s", + userService.getHashByUID(userService.getUserByEmail(email).getUid()))); + message.saveChanges(); + transport.connect(); + transport.sendMessage(message, message.getAllRecipients()); + } catch (MessagingException ex) { + logger.error("mail exception", ex); + } + } +} |