Skip to content

Commit

Permalink
Improve MailService: Do not send multiple email notifications when so…
Browse files Browse the repository at this point in the history
…meone sends multiple messages consecutively.
  • Loading branch information
Nonononoki committed Oct 6, 2024
1 parent 58e3afe commit 7fc3661
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
36 changes: 21 additions & 15 deletions src/main/java/com/nonononoki/alovoa/service/MailService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
46 changes: 22 additions & 24 deletions src/test/java/com/nonononoki/alovoa/service/MailServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}

}

0 comments on commit 7fc3661

Please sign in to comment.