diff --git a/src/main/java/com/chat/yourway/dto/response/LastMessageResponseDto.java b/src/main/java/com/chat/yourway/dto/response/LastMessageResponseDto.java new file mode 100644 index 00000000..1f8426b9 --- /dev/null +++ b/src/main/java/com/chat/yourway/dto/response/LastMessageResponseDto.java @@ -0,0 +1,36 @@ +package com.chat.yourway.dto.response; + +import java.time.LocalDateTime; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +@NoArgsConstructor +@Getter +@Setter +@ToString +public class LastMessageResponseDto { + + private static final int MAX_LENGTH = 20; + + private LocalDateTime timestamp; + private String sentFrom; + private String lastMessage; + + public LastMessageResponseDto(LocalDateTime timestamp, String sentFrom, String lastMessage) { + this.timestamp = timestamp; + this.sentFrom = sentFrom; + this.lastMessage = lastMessage; + } + + public void setLastMessage(String lastMessage) { + if (lastMessage.length() <= MAX_LENGTH) { + this.lastMessage = lastMessage; + } else { + this.lastMessage = lastMessage.substring(0, MAX_LENGTH) + "..."; + } + + } + +} diff --git a/src/main/java/com/chat/yourway/dto/response/MessageNotificationResponseDto.java b/src/main/java/com/chat/yourway/dto/response/MessageNotificationResponseDto.java index 4d5f2475..baf05554 100644 --- a/src/main/java/com/chat/yourway/dto/response/MessageNotificationResponseDto.java +++ b/src/main/java/com/chat/yourway/dto/response/MessageNotificationResponseDto.java @@ -20,6 +20,6 @@ public class MessageNotificationResponseDto { private EventType status; private Integer unreadMessages; private LocalDateTime lastRead; - private String lastMessage; + private LastMessageResponseDto lastMessage; } diff --git a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java index b30518a9..9d205b91 100644 --- a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java +++ b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java @@ -4,6 +4,7 @@ import static com.chat.yourway.model.event.EventType.UNSUBSCRIBED; import com.chat.yourway.config.websocket.WebsocketProperties; +import com.chat.yourway.dto.response.LastMessageResponseDto; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.service.interfaces.ChatNotificationService; import com.chat.yourway.service.interfaces.ContactEventService; @@ -29,7 +30,7 @@ public class StompSubscriptionListener { private final ContactEventService contactEventService; private final ChatNotificationService chatNotificationService; - private static String lastMessage; + private static LastMessageResponseDto lastMessageDto; private static final String USER_DESTINATION = "/user"; private static final String SLASH = "/"; @@ -40,10 +41,10 @@ public void handleWebSocketSubscribeListener(SessionSubscribeEvent event) { try { if (isTopicDestination(destination)) { - lastMessage = contactEventService.getByTopicIdAndEmail(getTopicId(event), email) + lastMessageDto = contactEventService.getByTopicIdAndEmail(getTopicId(event), email) .getLastMessage(); var contactEvent = new ContactEvent(email, getTopicId(event), SUBSCRIBED, - getTimestamp(event), lastMessage); + getTimestamp(event), lastMessageDto); contactEventService.save(contactEvent); } @@ -64,7 +65,7 @@ public void handleWebSocketUnsubscribeListener(SessionUnsubscribeEvent event) { try { if (isTopicDestination(destination)) { var contactEvent = new ContactEvent(email, getTopicId(event), UNSUBSCRIBED, - getTimestamp(event), lastMessage); + getTimestamp(event), lastMessageDto); contactEventService.save(contactEvent); } diff --git a/src/main/java/com/chat/yourway/model/event/ContactEvent.java b/src/main/java/com/chat/yourway/model/event/ContactEvent.java index c67a8da2..e782d795 100644 --- a/src/main/java/com/chat/yourway/model/event/ContactEvent.java +++ b/src/main/java/com/chat/yourway/model/event/ContactEvent.java @@ -1,5 +1,6 @@ package com.chat.yourway.model.event; +import com.chat.yourway.dto.response.LastMessageResponseDto; import java.time.LocalDateTime; import lombok.Getter; import lombok.NoArgsConstructor; @@ -25,11 +26,11 @@ public class ContactEvent { private Integer topicId; private EventType eventType; private LocalDateTime timestamp; - private String lastMessage; + private LastMessageResponseDto lastMessage; - private static final int MAX_LENGTH = 20; - public ContactEvent(String email, Integer topicId, EventType eventType, LocalDateTime timestamp, String lastMessage) { + public ContactEvent(String email, Integer topicId, EventType eventType, LocalDateTime timestamp, + LastMessageResponseDto lastMessage) { this.id = email + "_" + topicId; this.email = email; this.topicId = topicId; @@ -38,12 +39,4 @@ public ContactEvent(String email, Integer topicId, EventType eventType, LocalDat this.lastMessage = lastMessage; } - public void setLastMessage(String lastMessage) { - if (lastMessage.length() <= MAX_LENGTH) { - this.lastMessage = lastMessage; - } else { - this.lastMessage = lastMessage.substring(0, MAX_LENGTH) + "..."; - } - - } } diff --git a/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java b/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java index 46e43832..26c1b9d9 100644 --- a/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java @@ -4,6 +4,7 @@ import com.chat.yourway.dto.request.MessagePrivateRequestDto; import com.chat.yourway.dto.request.MessagePublicRequestDto; import com.chat.yourway.dto.request.PageRequestDto; +import com.chat.yourway.dto.response.LastMessageResponseDto; import com.chat.yourway.dto.response.MessageResponseDto; import com.chat.yourway.service.interfaces.ChatMessageService; import com.chat.yourway.service.interfaces.ChatNotificationService; @@ -67,7 +68,12 @@ public List sendMessageHistoryByTopicId(Integer topicId, } private void sendToTopic(Integer topicId, MessageResponseDto messageDto) { - contactEventService.setLastMessageToAllTopicSubscribers(topicId, messageDto.getContent()); + var lastMessageDto = new LastMessageResponseDto(); + lastMessageDto.setTimestamp(messageDto.getTimestamp()); + lastMessageDto.setSentFrom(messageDto.getSentFrom()); + lastMessageDto.setLastMessage(messageDto.getContent()); + + contactEventService.setLastMessageToAllTopicSubscribers(topicId, lastMessageDto); simpMessagingTemplate.convertAndSend(toTopicDestination(topicId), messageDto); chatNotificationService.notifyTopicSubscribers(topicId); } diff --git a/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java b/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java index 9003f7ce..b742d8dc 100644 --- a/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java @@ -2,6 +2,7 @@ import static com.chat.yourway.model.event.EventType.ONLINE; +import com.chat.yourway.dto.response.LastMessageResponseDto; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.model.event.EventType; import com.chat.yourway.repository.ContactEventRedisRepository; @@ -30,7 +31,7 @@ public ContactEvent getByTopicIdAndEmail(Integer topicId, String email) { log.trace("Started getByTopicIdAndEmail, topicId [{}], email [{}]", topicId, email); return contactEventRedisRepository.findById(email + "_" + topicId) - .orElse(new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), "")); + .orElse(new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), null)); } @Override @@ -61,7 +62,7 @@ public List getAllByTopicId(Integer topicId) { } @Override - public void setLastMessageToAllTopicSubscribers(Integer topicId, String message) { + public void setLastMessageToAllTopicSubscribers(Integer topicId, LastMessageResponseDto message) { log.trace("Started setLastMessageToAllTopicSubscribers, topic id [{}], last message [{}]", topicId, message); diff --git a/src/main/java/com/chat/yourway/service/interfaces/ContactEventService.java b/src/main/java/com/chat/yourway/service/interfaces/ContactEventService.java index 3fcc52e7..ae199c9f 100644 --- a/src/main/java/com/chat/yourway/service/interfaces/ContactEventService.java +++ b/src/main/java/com/chat/yourway/service/interfaces/ContactEventService.java @@ -1,5 +1,6 @@ package com.chat.yourway.service.interfaces; +import com.chat.yourway.dto.response.LastMessageResponseDto; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.model.event.EventType; import java.util.List; @@ -44,8 +45,8 @@ public interface ContactEventService { * Set last message to all topic subscribers events. * * @param topicId topic id. - * @param message last message. + * @param lastMessageDto last message Dto. */ - void setLastMessageToAllTopicSubscribers(Integer topicId, String message); + void setLastMessageToAllTopicSubscribers(Integer topicId, LastMessageResponseDto lastMessageDto); } diff --git a/src/test/java/com/chat/yourway/integration/controller/ChatControllerTest.java b/src/test/java/com/chat/yourway/integration/controller/ChatControllerTest.java index f53e9ce7..761d16bc 100644 --- a/src/test/java/com/chat/yourway/integration/controller/ChatControllerTest.java +++ b/src/test/java/com/chat/yourway/integration/controller/ChatControllerTest.java @@ -11,6 +11,7 @@ import com.chat.yourway.dto.request.MessagePrivateRequestDto; import com.chat.yourway.dto.request.MessagePublicRequestDto; import com.chat.yourway.dto.request.PageRequestDto; +import com.chat.yourway.dto.response.LastMessageResponseDto; import com.chat.yourway.dto.response.MessageNotificationResponseDto; import com.chat.yourway.dto.response.MessageResponseDto; import com.chat.yourway.integration.controller.websocketclient.TestStompFrameHandler; @@ -196,7 +197,12 @@ void getMessages_shouldReturnReceivedMessagesHistoryFromTopic() { void notifyTopicSubscribers_shouldNotifyTopicSubscribersIfSubscribeEvent() { // Given int topicId = 12; - var event = new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), "Hi"); + var lastMessageDto = new LastMessageResponseDto(); + lastMessageDto.setTimestamp(LocalDateTime.now()); + lastMessageDto.setSentFrom("vasil@gmail.com"); + lastMessageDto.setLastMessage("Hi"); + + var event = new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), lastMessageDto); saveContactEvent(event); //Stored subscription results for testing CompletableFuture resultKeeper = new CompletableFuture<>(); @@ -223,7 +229,12 @@ void notifyTopicSubscribers_shouldNotifyTopicSubscribersIfSubscribeEvent() { void notifyTopicSubscribers_shouldNotifyTopicSubscribersIfAnyContactSubscribedToTopic() { // Given int topicId = 12; - var event = new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), "Hi"); + var lastMessageDto = new LastMessageResponseDto(); + lastMessageDto.setTimestamp(LocalDateTime.now()); + lastMessageDto.setSentFrom("vasil@gmail.com"); + lastMessageDto.setLastMessage("Hi"); + + var event = new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), lastMessageDto); saveContactEvent(event); //Stored subscription results for testing CompletableFuture resultKeeper = new CompletableFuture<>(); diff --git a/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java b/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java index 8a717ba9..481ce221 100644 --- a/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java +++ b/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java @@ -13,6 +13,7 @@ import static org.springframework.messaging.simp.stomp.StompCommand.UNSUBSCRIBE; import com.chat.yourway.config.websocket.WebsocketProperties; +import com.chat.yourway.dto.response.LastMessageResponseDto; import com.chat.yourway.listener.StompSubscriptionListener; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.service.interfaces.ChatNotificationService; @@ -68,9 +69,13 @@ public void handleWebSocketSubscribeListener_shouldSaveEvent() { int topicId = 1; String destination = "/topic/" + topicId; - String lastMessage = "Hello"; + var lastMessageDto = new LastMessageResponseDto(); + lastMessageDto.setTimestamp(LocalDateTime.now()); + lastMessageDto.setSentFrom("vasil@gmail.com"); + lastMessageDto.setLastMessage("Hello"); + ContactEvent contactEvent = new ContactEvent(); - contactEvent.setLastMessage(lastMessage); + contactEvent.setLastMessage(lastMessageDto); var event = createSubscribeEvent(destination, getPrincipal(email, password)); @@ -87,7 +92,7 @@ public void handleWebSocketSubscribeListener_shouldSaveEvent() { assertThat(capturedEvent.getEmail()).isEqualTo(email); assertThat(capturedEvent.getTimestamp()).isInstanceOfAny(LocalDateTime.class); assertThat(capturedEvent.getEventType()).isEqualTo(SUBSCRIBED); - assertThat(capturedEvent.getLastMessage()).isEqualTo(lastMessage); + assertThat(capturedEvent.getLastMessage()).isEqualTo(lastMessageDto); } @Test diff --git a/src/test/java/com/chat/yourway/unit/service/impl/ContactEventServiceImplTest.java b/src/test/java/com/chat/yourway/unit/service/impl/ContactEventServiceImplTest.java index b3d24c1a..da8fb7b5 100644 --- a/src/test/java/com/chat/yourway/unit/service/impl/ContactEventServiceImplTest.java +++ b/src/test/java/com/chat/yourway/unit/service/impl/ContactEventServiceImplTest.java @@ -7,6 +7,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.chat.yourway.dto.response.LastMessageResponseDto; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.model.event.EventType; import com.chat.yourway.repository.ContactEventRedisRepository; @@ -40,7 +41,7 @@ void save_shouldSaveToRepository() { // Given String email = "vasil@gmail.com"; int topicId = 1; - ContactEvent contactEvent = new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), ""); + ContactEvent contactEvent = new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), null); // When contactEventService.save(contactEvent); @@ -55,8 +56,7 @@ void getByTopicIdAndEmail_shouldReturnContactEventFromRepository() { // Given Integer topicId = 1; String email = "vasil@gmail.com"; - ContactEvent expectedContactEvent = new ContactEvent(email, topicId, ONLINE, - LocalDateTime.now(), ""); + ContactEvent expectedContactEvent = new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), null); when(contactEventRedisRepository.findById(email + "_" + topicId)) .thenReturn(Optional.of(expectedContactEvent)); @@ -91,8 +91,8 @@ void getAllByEmail_shouldReturnContactEventsFromRepository() { // Given String email = "vasil@gmail.com"; List expectedContactEvents = Arrays.asList( - new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), ""), - new ContactEvent(email, 2, OFFLINE, LocalDateTime.now(), "") + new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), null), + new ContactEvent(email, 2, OFFLINE, LocalDateTime.now(), null) ); when(contactEventRedisRepository.findAllByEmail(email)).thenReturn(expectedContactEvents); @@ -110,8 +110,8 @@ void updateEventTypeByEmail_shouldUpdateEventTypesInRepository() { String email = "vasil@gmail.com"; EventType newEventType = OFFLINE; List events = Arrays.asList( - new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), ""), - new ContactEvent(email, 2, ONLINE, LocalDateTime.now(), "") + new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), null), + new ContactEvent(email, 2, ONLINE, LocalDateTime.now(), null) ); when(contactEventRedisRepository.findAllByEmail(email)).thenReturn(events); @@ -129,8 +129,8 @@ void getAllByTopicId_shouldReturnContactEventsFromRepository() { // Given Integer topicId = 1; List expectedContactEvents = Arrays.asList( - new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), ""), - new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), "") + new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), null), + new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), null) ); when(contactEventRedisRepository.findAllByTopicId(topicId)).thenReturn(expectedContactEvents); @@ -146,18 +146,22 @@ void getAllByTopicId_shouldReturnContactEventsFromRepository() { void setLastMessageToAllTopicSubscribers_shouldUpdateLastMessagesInRepository() { // Given Integer topicId = 1; - String newMessage = "New message"; + var lastMessageDto = new LastMessageResponseDto(); + lastMessageDto.setTimestamp(LocalDateTime.now()); + lastMessageDto.setSentFrom("vasil@gmail.com"); + lastMessageDto.setLastMessage("New message"); + List events = Arrays.asList( - new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), ""), - new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), "") + new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), null), + new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), null) ); when(contactEventRedisRepository.findAllByTopicId(topicId)).thenReturn(events); // When - contactEventService.setLastMessageToAllTopicSubscribers(topicId, newMessage); + contactEventService.setLastMessageToAllTopicSubscribers(topicId, lastMessageDto); // Then - events.forEach(e -> e.setLastMessage(newMessage)); + events.forEach(e -> e.setLastMessage(lastMessageDto)); verify(contactEventRedisRepository, times(1)).saveAll(events); } diff --git a/src/test/java/com/chat/yourway/unit/service/impl/NotificationServiceImplTest.java b/src/test/java/com/chat/yourway/unit/service/impl/NotificationServiceImplTest.java index 51bd13a5..ae4094f6 100644 --- a/src/test/java/com/chat/yourway/unit/service/impl/NotificationServiceImplTest.java +++ b/src/test/java/com/chat/yourway/unit/service/impl/NotificationServiceImplTest.java @@ -10,6 +10,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.chat.yourway.dto.response.LastMessageResponseDto; import com.chat.yourway.dto.response.MessageNotificationResponseDto; import com.chat.yourway.mapper.MessageNotificationMapper; import com.chat.yourway.model.event.ContactEvent; @@ -46,9 +47,14 @@ class NotificationServiceImplTest { void notifyTopicSubscribers_shouldReturnListOfNotificationsWithUnreadMessageCounts() { // Given Integer topicId = 1; + var lastMessageDto = new LastMessageResponseDto(); + lastMessageDto.setTimestamp(LocalDateTime.now()); + lastMessageDto.setSentFrom("vasil@gmail.com"); + lastMessageDto.setLastMessage("Hello"); + List events = Arrays.asList( - new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), "Hello"), - new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), "Hello") + new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), lastMessageDto), + new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), lastMessageDto) ); var expectedNotifications = events.stream()