diff --git a/src/main/java/com/nonononoki/alovoa/service/MailService.java b/src/main/java/com/nonononoki/alovoa/service/MailService.java index 500ac8ef..cdef448c 100644 --- a/src/main/java/com/nonononoki/alovoa/service/MailService.java +++ b/src/main/java/com/nonononoki/alovoa/service/MailService.java @@ -1,13 +1,10 @@ package com.nonononoki.alovoa.service; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.Locale; - +import com.nonononoki.alovoa.Tools; +import com.nonononoki.alovoa.entity.User; +import com.nonononoki.alovoa.entity.user.Conversation; import jakarta.mail.MessagingException; import jakarta.mail.internet.MimeMessage; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -17,8 +14,11 @@ import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Service; -import com.nonononoki.alovoa.Tools; -import com.nonononoki.alovoa.entity.User; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Locale; +import java.util.Objects; @Service public class MailService { @@ -140,24 +140,30 @@ public void sendAccountConfirmed(User user) throws MessagingException, IOExcepti sendMail(user.getEmail(), defaultFrom, subject, body); } - public void sendLikeNotificationMail(User user){ + public void sendLikeNotificationMail(User user) { Locale locale = Tools.getUserLocale(user); String subject = messageSource.getMessage("backend.mail.like.subject", new String[]{}, locale); String body = messageSource.getMessage("backend.mail.like.body", new String[]{user.getFirstName()}, locale); sendMail(user.getEmail(), defaultFrom, subject, body); } - public void sendMatchNotificationMail(User user){ + public void sendMatchNotificationMail(User user) { Locale locale = Tools.getUserLocale(user); String subject = messageSource.getMessage("backend.mail.match.subject", new String[]{}, locale); String body = messageSource.getMessage("backend.mail.match.body", new String[]{user.getFirstName()}, locale); sendMail(user.getEmail(), defaultFrom, subject, body); } - public void sendChatNotificationMail(User user, User currUser, String message){ - Locale locale = Tools.getUserLocale(user); - String subject = messageSource.getMessage("backend.mail.chat.subject", new String[]{}, locale); - String body = messageSource.getMessage("backend.mail.chat.body", new String[]{currUser.getFirstName(), user.getFirstName(), message},locale); - sendMail(defaultFrom, user.getEmail(), subject, body); + public boolean sendChatNotificationMail(User user, User currUser, String message, Conversation conversation) { + //only send mail notification if the previous message was NOT from current user in order to avoid spam + if (conversation.getMessages().size() <= 1 || + !Objects.equals(conversation.getMessages().get(conversation.getMessages().size() - 2).getUserFrom().getId(), currUser.getId())) { + Locale locale = Tools.getUserLocale(user); + String subject = messageSource.getMessage("backend.mail.chat.subject", new String[]{}, locale); + String body = messageSource.getMessage("backend.mail.chat.body", new String[]{currUser.getFirstName(), user.getFirstName(), message}, locale); + sendMail(defaultFrom, user.getEmail(), subject, body); + return true; + } + return false; } } diff --git a/src/main/java/com/nonononoki/alovoa/service/MessageService.java b/src/main/java/com/nonononoki/alovoa/service/MessageService.java index 6aefe484..31b3f61a 100644 --- a/src/main/java/com/nonononoki/alovoa/service/MessageService.java +++ b/src/main/java/com/nonononoki/alovoa/service/MessageService.java @@ -90,7 +90,7 @@ public void send(Long convoId, String message) conversationRepo.saveAndFlush(c); if(user.getUserSettings().isEmailChat()){ - mailService.sendChatNotificationMail(user, currUser, message); + mailService.sendChatNotificationMail(user, currUser, message, c); } } diff --git a/src/test/java/com/nonononoki/alovoa/service/MailServiceTest.java b/src/test/java/com/nonononoki/alovoa/service/MailServiceTest.java index a791aff2..1f19a3d5 100644 --- a/src/test/java/com/nonononoki/alovoa/service/MailServiceTest.java +++ b/src/test/java/com/nonononoki/alovoa/service/MailServiceTest.java @@ -2,15 +2,13 @@ import com.nonononoki.alovoa.component.TextEncryptorConverter; import com.nonononoki.alovoa.entity.User; -import com.nonononoki.alovoa.entity.user.UserDeleteToken; -import com.nonononoki.alovoa.entity.user.UserPasswordToken; -import com.nonononoki.alovoa.entity.user.UserRegisterToken; +import com.nonononoki.alovoa.entity.user.*; import com.nonononoki.alovoa.model.AlovoaException; -import com.nonononoki.alovoa.model.UserDto; import com.nonononoki.alovoa.repo.ConversationRepository; import com.nonononoki.alovoa.repo.UserRepository; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; @@ -20,10 +18,13 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.transaction.annotation.Transactional; -import java.io.IOException; -import java.security.GeneralSecurityException; +import java.util.Collections; import java.util.List; -import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; @SpringBootTest @ActiveProfiles("test") @@ -81,6 +82,7 @@ void after() throws Exception { RegisterServiceTest.deleteAllUsers(userService, authService, captchaService, conversationRepo, userRepo); } + @Disabled @Test void test() throws Exception { User user1 = testUsers.get(0); @@ -105,28 +107,24 @@ void test() throws Exception { @Test - void testLikeMatchAndChatEmails() throws AlovoaException, GeneralSecurityException, IOException { + void testChatMailSend() throws AlovoaException { User user1 = testUsers.get(0); User user2 = testUsers.get(1); - //user1 likes user2 - Mockito.when(authService.getCurrentUser()).thenReturn(user1); - Mockito.when(authService.getCurrentUser(true)).thenReturn(user1); - - UUID uuid2 = user2.getUuid(); - userService.likeUser(uuid2, null); - - //user2 likes user1 - Mockito.when(authService.getCurrentUser()).thenReturn(user2); - Mockito.when(authService.getCurrentUser(true)).thenReturn(user2); - - UUID uuid1 = user1.getUuid(); - userService.likeUser(uuid1, null); + when(authService.getCurrentUser()).thenReturn(user1); + when(authService.getCurrentUser(true)).thenReturn(user1); - //match must occur since both like each other. + Message message1 = new Message(); + message1.setUserFrom(user1); + Message message2 = new Message(); + message2.setUserFrom(user1); + Conversation convo1 = mock(Conversation.class); + when(convo1.getMessages()).thenReturn(List.of(message1)); + Conversation convo2 = mock(Conversation.class); + when(convo2.getMessages()).thenReturn(List.of(message1, message2)); - //user2 sends a message to user1 - messageService.send(user1.getConversations().get(0).getId(), "Hello this is a test message."); + assertTrue(mailService.sendChatNotificationMail(user2, user1, "message", convo1)); + assertFalse(mailService.sendChatNotificationMail(user2, user1, "message", convo2)); } }