aboutsummaryrefslogtreecommitdiff
path: root/juick-api/src/main/java/com/juick/api/EmailManager.java
blob: a2ddbc3b6a6128ae5b67ddbc73f56549e575a285 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.juick.api;

import com.juick.Message;
import com.juick.server.component.MessageEvent;
import com.juick.server.util.HttpBadRequestException;
import com.juick.service.EmailService;
import com.juick.service.SubscriptionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import javax.inject.Inject;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

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> {
    private static final Logger logger = LoggerFactory.getLogger(EmailManager.class);
    @Inject
    EmailService emailService;
    @Inject
    SubscriptionService subscriptionService;
    @Override
    public void onApplicationEvent(MessageEvent event) {
        Message msg = event.getMessage();
        if (msg.getRid() == 0) {
            subscriptionService.getSubscribedUsers(msg.getUser().getUid(), msg.getMid())
                    .forEach(user -> emailService.getEmails(user.getUid(), true)
                            .forEach(email -> emailNotify(email, msg)));
        } else {
            subscriptionService.getUsersSubscribedToComments(msg.getMid(), msg.getUser().getUid())
                    .forEach(user -> emailService.getEmails(user.getUid(), true)
                        .forEach(email -> emailNotify(email, msg)));
        }
    }

    void emailNotify(String email, 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);
            message.setFrom(new InternetAddress("juick@juick.com"));
            message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(email));
            message.setSubject(String.format("New message from %s", msg.getUser().getName()));
            message.setText(String.format("%s\n%s\n---\nYou are receiving this because you are subscribed to this user " +
                    "or discussion. Configure notifications at https://juick.com/settings",
                    formatPost(msg), formatUrl(msg)));
            transport.send(message);
        } catch (MessagingException ex) {
            logger.error("mail exception", ex);
        }
    }
}