aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/EmailManager.java
blob: 6958d970eb2ad9af8320c3b8a022c82e7a97a873 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (C) 2008-2024, Juick
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.juick;

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.www.api.SystemActivity;

import jakarta.mail.MessagingException;
import jakarta.mail.Multipart;
import jakarta.mail.Session;
import jakarta.mail.Transport;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.internet.MimeMessage;
import jakarta.mail.internet.MimeMultipart;

import com.juick.util.HttpBadRequestException;
import com.juick.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 org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.*;

import static com.juick.util.formatters.PlainTextFormatter.formatPost;
import static com.juick.util.formatters.PlainTextFormatter.formatUrl;

public class EmailManager implements NotificationListener {

    public static final String MSGID_PATTERN = "\\.|@|<";

    private static final Logger logger = LoggerFactory.getLogger(EmailManager.class);
    @Inject
    private EmailService emailService;
    @Inject
    private MessagesService messagesService;
    @Inject
    private UserService userService;
    @Inject
    private ObjectMapper jsonMapper;
    @Inject
    private WebApp webApp;
    @Value("${web_domain:localhost}")
    private String webDomain;
    @Value("${service_email:}")
    private String serviceEmail;

    @Override
    public void processSystemEvent(@Nonnull SystemEvent systemEvent) {
        var activity = systemEvent.getActivity();
        var msg = activity.getMessage();
        var subscribers = activity.getTo();
        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()) {
            return;
        }
        if (MessageUtils.isPM(msg)) {
            String subject = String.format("Private message from %s", msg.getUser().getName());
            emailService.getEmails(msg.getTo().getUid(), true).forEach(email -> emailNotify(email, subject, msg));
        } else if (MessageUtils.isReply(msg)) {
            Message originalMessage = messagesService.getMessage(msg.getMid()).orElseThrow(IllegalStateException::new);
            String subject = String.format("New reply to %s", originalMessage.getUser().getName());
            subscribedUsers.stream()
                    .flatMap(user -> emailService.getEmails(user.getUid(), true).stream())
                    .forEach(email -> emailNotify(email, subject, msg));
        } else {
            String subject = String.format("New message from %s", msg.getUser().getName());
            subscribedUsers
                    .forEach(user -> emailService.getEmails(user.getUid(), true)
                            .forEach(email -> emailNotify(email, subject, msg)));
        }
    }

    @Override
    public void processPingEvent(PingEvent pingEvent) {

    }

    @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)) {
            headers.put("Message-ID", String.format("<%d.%d@juick.com>", msg.getMid(), msg.getRid()));
        }
        if (MessageUtils.isReply(msg)) {
            if (msg.getReplyto() > 0) {
                Message replyto = messagesService.getReply(msg.getMid(), msg.getReplyto());
                headers.put("In-Reply-To", String.format("<%d.%d@juick.com>", replyto.getMid(), replyto.getRid()));
            } else {
                Message original = messagesService.getMessage(msg.getMid()).orElseThrow(IllegalStateException::new);
                headers.put("In-Reply-To", String.format("<%d.%d@juick.com>", original.getMid(), original.getRid()));
            }
        }
        String plainText = messagesService.renderPlaintext(formatPost(msg, webDomain), formatUrl(msg)).orElseThrow(IllegalStateException::new);
        String hash = userService.getHashByUID(userService.getUserByEmail(email).getUid());
        String htmlText = messagesService.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) {
        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 {
                    for (Map.Entry<String, String> entry: messageHeaders.entrySet()) {
                        setHeader(entry.getKey(), entry.getValue());
                    }
                }
            };
            String fromAddress = StringUtils.isNotEmpty(from) ? from : "juick@juick.com";
            message.setFrom(fromAddress);
            message.addRecipient(jakarta.mail.Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(subject);
            MimeBodyPart textBodyPart = new MimeBodyPart();
            textBodyPart.setContent(textPart, "text/plain; charset=UTF-8");

            Multipart multipart = new MimeMultipart("alternative");
            multipart.addBodyPart(textBodyPart);
            if (StringUtils.isNotBlank(htmlPart)) {
                MimeBodyPart htmlBodyPart = new MimeBodyPart();
                htmlBodyPart.setContent(htmlPart, "text/html; charset=UTF-8");
                multipart.addBodyPart(htmlBodyPart);
            }
            message.setContent(multipart);
            User emailUser = userService.getUserByEmail(to);
            if (!emailUser.isAnonymous()) {
                message.setHeader("List-Id", "<mail-notifications.juick.com>");
                message.setHeader("List-Unsubscribe","<mailto:unsubscribe@juick.com>");
                message.setHeader("Feedback-ID", String.format("%d:juick", emailUser.getUid()));
                message.setHeader("Precedence", "bulk");
            }
            message.saveChanges();
            transport.connect();
            transport.sendMessage(message, message.getAllRecipients());
            return true;
        } catch (MessagingException ex) {
            logger.error("mail exception", ex);
            return false;
        }
    }
    @Scheduled(fixedRate = 3600000)
    public void cleanupAuth() {
        var rowsDeleted = emailService.cleanupAuthCodes();
        logger.debug("{} auth codes deleted", rowsDeleted);
    }
}