diff options
author | Vitaly Takmazov | 2017-12-25 11:44:32 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-12-25 11:44:32 +0300 |
commit | 5f3e74559144da2b659773a29f73211e2347dd16 (patch) | |
tree | 8cccc6c91045762ea66ac76241f4c36ea1a68dc4 | |
parent | 667682533fb1246c4aff07082bf1710851454db7 (diff) |
email notifications
3 files changed, 66 insertions, 0 deletions
diff --git a/juick-api/src/main/java/com/juick/api/EmailManager.java b/juick-api/src/main/java/com/juick/api/EmailManager.java new file mode 100644 index 00000000..8e868ca0 --- /dev/null +++ b/juick-api/src/main/java/com/juick/api/EmailManager.java @@ -0,0 +1,56 @@ +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.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +import static com.juick.formatters.PlainTextFormatter.formatPost; + +@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()) + .forEach(email -> emailNotify(email, msg))); + } else { + subscriptionService.getUsersSubscribedToComments(msg.getMid(), msg.getUser().getUid()) + .forEach(user -> emailService.getEmails(user.getUid()) + .forEach(email -> emailNotify(email, msg))); + } + } + + void emailNotify(String email, Message msg) { + Session session = Session.getDefaultInstance(System.getProperties()); + try { + MimeMessage message = new MimeMessage(session); + message.setFrom(new InternetAddress("noreply@mail.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(formatPost(msg)); + Transport.send(message); + } catch (MessagingException ex) { + logger.error("mail exception", ex); + } + } +} diff --git a/juick-server-core/src/main/java/com/juick/service/EmailService.java b/juick-server-core/src/main/java/com/juick/service/EmailService.java index 704ba2f7..ec43dcad 100644 --- a/juick-server-core/src/main/java/com/juick/service/EmailService.java +++ b/juick-server-core/src/main/java/com/juick/service/EmailService.java @@ -17,6 +17,8 @@ package com.juick.service; +import java.util.List; + /** * Created by vitalyster on 09.12.2016. */ @@ -25,4 +27,5 @@ public interface EmailService { boolean addVerificationCode(Integer userId, String account, String code); boolean deleteEmail(Integer userId, String account); boolean setSubscriptionHour(Integer userId, String account, String hour); + List<String> getEmails(Integer userId); } diff --git a/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java b/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java index 57aa59f7..d9e35a00 100644 --- a/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java +++ b/juick-server-jdbc/src/main/java/com/juick/service/EmailServiceImpl.java @@ -25,6 +25,7 @@ import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; import javax.inject.Inject; +import java.util.List; /** * Created by vitalyster on 09.12.2016. @@ -72,4 +73,10 @@ public class EmailServiceImpl extends BaseJdbcService implements EmailService { return StringUtils.isNotEmpty(account) && getJdbcTemplate().update( "UPDATE emails SET subscr_hour=? WHERE user_id=? AND email=?", hour, userId, account) > 0; } + + @Transactional(readOnly = true) + @Override + public List<String> getEmails(Integer userId) { + return getJdbcTemplate().queryForList("SELECT email FROM emails WHERE user_id=?", String.class, userId); + } } |