From d94036c2a16606789cfb24467e8f2a206c02c822 Mon Sep 17 00:00:00 2001 From: Dmytro Trotsenko Date: Wed, 13 Mar 2024 22:35:46 +0200 Subject: [PATCH 1/9] fix: fixed notification and event type --- .../chat/yourway/listener/StompSubscriptionListener.java | 7 ++++--- src/main/java/com/chat/yourway/model/event/EventType.java | 3 +-- .../unit/listener/StompSubscriptionListenerTest.java | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java index 2fae796c..bf0766cc 100644 --- a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java +++ b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java @@ -1,7 +1,7 @@ package com.chat.yourway.listener; +import static com.chat.yourway.model.event.EventType.ONLINE; import static com.chat.yourway.model.event.EventType.SUBSCRIBED; -import static com.chat.yourway.model.event.EventType.UNSUBSCRIBED; import com.chat.yourway.config.websocket.WebsocketProperties; import com.chat.yourway.dto.response.LastMessageResponseDto; @@ -46,10 +46,11 @@ public void handleWebSocketSubscribeListener(SessionSubscribeEvent event) { .getLastMessage(); var contactEvent = new ContactEvent(email, getTopicId(event), SUBSCRIBED, getTimestamp(event), lastMessageDto); + contactEventService.updateEventTypeByEmail(ONLINE, email); contactEventService.save(contactEvent); } - chatNotificationService.notifyTopicSubscribers(getTopicId(event)); + chatNotificationService.notifyAllWhoSubscribedToSameUserTopic(email); chatNotificationService.notifyAllWhoSubscribedToTopic(getTopicId(event)); } catch (NumberFormatException e) { @@ -70,7 +71,7 @@ public void handleWebSocketUnsubscribeListener(SessionUnsubscribeEvent event) { try { if (isTopicDestination(destination)) { - var contactEvent = new ContactEvent(email, getTopicId(event), UNSUBSCRIBED, + var contactEvent = new ContactEvent(email, getTopicId(event), ONLINE, getTimestamp(event), lastMessageDto); contactEventService.save(contactEvent); } diff --git a/src/main/java/com/chat/yourway/model/event/EventType.java b/src/main/java/com/chat/yourway/model/event/EventType.java index 03916f44..f72005fe 100644 --- a/src/main/java/com/chat/yourway/model/event/EventType.java +++ b/src/main/java/com/chat/yourway/model/event/EventType.java @@ -3,6 +3,5 @@ public enum EventType { ONLINE, OFFLINE, - SUBSCRIBED, - UNSUBSCRIBED + SUBSCRIBED } 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 481ce221..38685b4a 100644 --- a/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java +++ b/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java @@ -1,7 +1,7 @@ package com.chat.yourway.unit.listener; +import static com.chat.yourway.model.event.EventType.ONLINE; import static com.chat.yourway.model.event.EventType.SUBSCRIBED; -import static com.chat.yourway.model.event.EventType.UNSUBSCRIBED; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.mockito.ArgumentMatchers.anyInt; @@ -108,7 +108,7 @@ public void handleWebSocketSubscribeListener_shouldNotifyTopicSubscribers() { stompSubscriptionListener.handleWebSocketSubscribeListener(event); // Then - verify(chatNotificationService, times(1)).notifyTopicSubscribers(topicId); + verify(chatNotificationService, times(1)).notifyAllWhoSubscribedToSameUserTopic(email); } @Test @@ -132,7 +132,7 @@ public void handleWebSocketUnsubscribeListener_shouldSaveEvent() { assertThat(capturedEvent.getTopicId()).isEqualTo(topicId); assertThat(capturedEvent.getEmail()).isEqualTo(email); assertThat(capturedEvent.getTimestamp()).isInstanceOfAny(LocalDateTime.class); - assertThat(capturedEvent.getEventType()).isEqualTo(UNSUBSCRIBED); + assertThat(capturedEvent.getEventType()).isEqualTo(ONLINE); } @Test From 2e4b462b94ac6179345aa63110a49ebad2c2affd Mon Sep 17 00:00:00 2001 From: Dmytro Trotsenko Date: Sun, 17 Mar 2024 11:52:30 +0200 Subject: [PATCH 2/9] bugfix: fixed disconnect from websocket. Set time to first message. --- .../chat/yourway/config/websocket/WebSocketConfig.java | 8 ++++++++ .../yourway/config/websocket/WebsocketProperties.java | 1 + src/main/resources/application.properties | 5 +++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/chat/yourway/config/websocket/WebSocketConfig.java b/src/main/java/com/chat/yourway/config/websocket/WebSocketConfig.java index fb74aeee..c14d7a3e 100644 --- a/src/main/java/com/chat/yourway/config/websocket/WebSocketConfig.java +++ b/src/main/java/com/chat/yourway/config/websocket/WebSocketConfig.java @@ -6,6 +6,7 @@ import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; +import org.springframework.web.socket.config.annotation.WebSocketTransportRegistration; @Configuration @EnableWebSocketMessageBroker @@ -25,4 +26,11 @@ public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint(properties.getEndpoint()); registry.addEndpoint(properties.getEndpoint()).setAllowedOriginPatterns("*").withSockJS(); } + + + @Override + public void configureWebSocketTransport(WebSocketTransportRegistration registry) { + registry.setTimeToFirstMessage(properties.getTimeToFirstMessage()); + } + } diff --git a/src/main/java/com/chat/yourway/config/websocket/WebsocketProperties.java b/src/main/java/com/chat/yourway/config/websocket/WebsocketProperties.java index 83e8eca4..8c000619 100644 --- a/src/main/java/com/chat/yourway/config/websocket/WebsocketProperties.java +++ b/src/main/java/com/chat/yourway/config/websocket/WebsocketProperties.java @@ -17,5 +17,6 @@ public class WebsocketProperties { private String topicPrefix; private String notifyPrefix; private String errorPrefix; + private int TimeToFirstMessage; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 023afd21..ecf8cbc5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -16,7 +16,7 @@ spring.data.redis.password=${REDIS_PASSWORD:admin} #Hibernate spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect -spring.jpa.show-sql=true +#spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true spring.jpa.hibernate.ddl-auto=validate @@ -30,6 +30,7 @@ socket.endpoint=/chat socket.topic-prefix=/topic socket.notify-prefix=/specific/notify socket.error-prefix=/specific/error +socket.time-to-first-message=999999 #Security: security.jwt.token-type=Bearer @@ -58,5 +59,5 @@ spring.mail.properties.mail.smtp.starttls.required=true message.max.amount.reports=2 #Logging: -logging.level.com.chat.yourway=${LOGGING_LEVEL:info} +logging.level.com.chat.yourway=${LOGGING_LEVEL:trace} From 7f88ea827b6dd5a9519af28a512e57ac7899050b Mon Sep 17 00:00:00 2001 From: Dmytro Trotsenko Date: Sun, 24 Mar 2024 13:30:40 +0200 Subject: [PATCH 3/9] fix: refactored and optimized notifications --- .../TopicNotificationResponseDto.java | 2 +- .../listener/StompSubscriptionListener.java | 9 +++-- .../mapper/MessageNotificationMapper.java | 16 -------- .../yourway/mapper/NotificationMapper.java | 22 ++++++++++ .../yourway/model/event/ContactEvent.java | 4 +- .../service/ChatMessageServiceImpl.java | 10 ++--- .../service/ChatNotificationServiceImpl.java | 40 +++++++++++++++++-- .../service/ContactEventServiceImpl.java | 17 +++++--- .../service/NotificationServiceImpl.java | 38 +++++++++--------- .../interfaces/ChatNotificationService.java | 16 +++++++- .../interfaces/ContactEventService.java | 2 +- .../interfaces/NotificationService.java | 14 +++++-- src/main/resources/application.properties | 4 +- .../controller/ChatControllerTest.java | 4 +- .../impl/ContactEventServiceImplTest.java | 24 ++++++----- .../impl/NotificationServiceImplTest.java | 12 +++--- 16 files changed, 154 insertions(+), 80 deletions(-) delete mode 100644 src/main/java/com/chat/yourway/mapper/MessageNotificationMapper.java create mode 100644 src/main/java/com/chat/yourway/mapper/NotificationMapper.java diff --git a/src/main/java/com/chat/yourway/dto/response/TopicNotificationResponseDto.java b/src/main/java/com/chat/yourway/dto/response/TopicNotificationResponseDto.java index 5de5165d..744604f4 100644 --- a/src/main/java/com/chat/yourway/dto/response/TopicNotificationResponseDto.java +++ b/src/main/java/com/chat/yourway/dto/response/TopicNotificationResponseDto.java @@ -13,7 +13,7 @@ @ToString public class TopicNotificationResponseDto { - private TopicResponseDto topic; + private Integer topicId; private Integer unreadMessages; diff --git a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java index bf0766cc..2b38f4e7 100644 --- a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java +++ b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java @@ -44,8 +44,11 @@ public void handleWebSocketSubscribeListener(SessionSubscribeEvent event) { if (isTopicDestination(destination)) { lastMessageDto = contactEventService.getByTopicIdAndEmail(getTopicId(event), email) .getLastMessage(); + int unreadMessages = contactEventService.getByTopicIdAndEmail(getTopicId(event), email) + .getUnreadMessages(); + var contactEvent = new ContactEvent(email, getTopicId(event), SUBSCRIBED, - getTimestamp(event), lastMessageDto); + getTimestamp(event), unreadMessages, lastMessageDto); contactEventService.updateEventTypeByEmail(ONLINE, email); contactEventService.save(contactEvent); } @@ -58,7 +61,7 @@ public void handleWebSocketSubscribeListener(SessionSubscribeEvent event) { } if (destination.equals(USER_DESTINATION + properties.getNotifyPrefix() + TOPICS_DESTINATION)) { - chatNotificationService.notifyAllPublicTopics(getEmail(event)); + chatNotificationService.notifyAllTopics(getEmail(event)); } log.info("Contact [{}] subscribe to [{}]", email, destination); @@ -72,7 +75,7 @@ public void handleWebSocketUnsubscribeListener(SessionUnsubscribeEvent event) { try { if (isTopicDestination(destination)) { var contactEvent = new ContactEvent(email, getTopicId(event), ONLINE, - getTimestamp(event), lastMessageDto); + getTimestamp(event), 0, lastMessageDto); contactEventService.save(contactEvent); } diff --git a/src/main/java/com/chat/yourway/mapper/MessageNotificationMapper.java b/src/main/java/com/chat/yourway/mapper/MessageNotificationMapper.java deleted file mode 100644 index 7a80c96e..00000000 --- a/src/main/java/com/chat/yourway/mapper/MessageNotificationMapper.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.chat.yourway.mapper; - -import com.chat.yourway.dto.response.MessageNotificationResponseDto; -import com.chat.yourway.model.event.ContactEvent; -import org.mapstruct.Mapper; -import org.mapstruct.Mapping; - -@Mapper(componentModel = "spring") -public interface MessageNotificationMapper { - - @Mapping(target = "status", source = "eventType") - @Mapping(target = "lastRead", source = "timestamp") - @Mapping(target = "email", source = "email") - MessageNotificationResponseDto toNotificationResponseDto(ContactEvent event); - -} diff --git a/src/main/java/com/chat/yourway/mapper/NotificationMapper.java b/src/main/java/com/chat/yourway/mapper/NotificationMapper.java new file mode 100644 index 00000000..591a5454 --- /dev/null +++ b/src/main/java/com/chat/yourway/mapper/NotificationMapper.java @@ -0,0 +1,22 @@ +package com.chat.yourway.mapper; + +import com.chat.yourway.dto.response.MessageNotificationResponseDto; +import com.chat.yourway.dto.response.TopicNotificationResponseDto; +import com.chat.yourway.model.event.ContactEvent; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +@Mapper(componentModel = "spring") +public interface NotificationMapper { + + @Mapping(target = "status", source = "eventType") + @Mapping(target = "lastRead", source = "timestamp") + @Mapping(target = "email", source = "email") + MessageNotificationResponseDto toMessageNotificationResponseDto(ContactEvent event); + + @Mapping(target = "topicId", source = "topicId") + @Mapping(target = "unreadMessages", source = "unreadMessages") + @Mapping(target = "lastMessage", source = "lastMessage") + TopicNotificationResponseDto toTopicNotificationResponseDto(ContactEvent event); + +} 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 e782d795..8f7e9f15 100644 --- a/src/main/java/com/chat/yourway/model/event/ContactEvent.java +++ b/src/main/java/com/chat/yourway/model/event/ContactEvent.java @@ -26,16 +26,18 @@ public class ContactEvent { private Integer topicId; private EventType eventType; private LocalDateTime timestamp; + private int unreadMessages; private LastMessageResponseDto lastMessage; public ContactEvent(String email, Integer topicId, EventType eventType, LocalDateTime timestamp, - LastMessageResponseDto lastMessage) { + int unreadMessages, LastMessageResponseDto lastMessage) { this.id = email + "_" + topicId; this.email = email; this.topicId = topicId; this.eventType = eventType; this.timestamp = timestamp; + this.unreadMessages = unreadMessages; this.lastMessage = lastMessage; } diff --git a/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java b/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java index 8191aee0..5fac88f1 100644 --- a/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java @@ -69,16 +69,16 @@ public List sendMessageHistoryByTopicId(Integer topicId, private void sendToTopic(Integer topicId, MessageResponseDto messageDto) { var lastMessageDto = new LastMessageResponseDto(); - lastMessageDto.setTimestamp(messageDto.getTimestamp()); - lastMessageDto.setSentFrom(messageDto.getSentFrom()); - lastMessageDto.setLastMessage(messageDto.getContent()); + lastMessageDto.setTimestamp(messageDto.getTimestamp()); + lastMessageDto.setSentFrom(messageDto.getSentFrom()); + lastMessageDto.setLastMessage(messageDto.getContent()); - contactEventService.setLastMessageToAllTopicSubscribers(topicId, lastMessageDto); + contactEventService.updateMessageInfoForAllTopicSubscribers(topicId, lastMessageDto); simpMessagingTemplate.convertAndSend(toTopicDestination(topicId), messageDto); chatNotificationService.notifyTopicSubscribers(topicId); - chatNotificationService.notifyAllWhoSubscribedToTopic(topicId); + chatNotificationService.updateNotificationForAllWhoSubscribedToTopic(topicId); } private String toTopicDestination(Integer topicId) { diff --git a/src/main/java/com/chat/yourway/service/ChatNotificationServiceImpl.java b/src/main/java/com/chat/yourway/service/ChatNotificationServiceImpl.java index a3b5772b..c833f187 100644 --- a/src/main/java/com/chat/yourway/service/ChatNotificationServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/ChatNotificationServiceImpl.java @@ -27,25 +27,57 @@ public void notifyTopicSubscribers(Integer topicId) { .forEach(n -> simpMessagingTemplate.convertAndSendToUser( n.getEmail(), toNotifyMessageDest(topicId), notifications)); - log.trace("All subscribers was notified by topic id = [{}]", topicId); + log.info("All subscribers was notified by topic id = [{}]", topicId); } @Override public void notifyAllWhoSubscribedToSameUserTopic(String userEmail) { + log.trace("Started notifyAllWhoSubscribedToSameUserTopic, user email = [{}]", userEmail); + contactEventService.getAllByEmail(userEmail) .forEach(e -> notifyTopicSubscribers(e.getTopicId())); + + log.info("All subscribers who subscribed to same topic was notified, email = [{}]", userEmail); } @Override - public void notifyAllPublicTopics(String email){ - var notifiedTopics = notificationService.notifyAllPublicTopicsByEmail(email); + public void notifyAllTopics(String email) { + log.trace("Started notifyAllTopics, email = [{}]", email); + + var notifiedTopics = notificationService.notifyAllTopicsByEmail(email); simpMessagingTemplate.convertAndSendToUser(email, toNotifyTopicsDest(), notifiedTopics); + + log.info("All topics was notified for user email = [{}]", email); } @Override public void notifyAllWhoSubscribedToTopic(Integer topicId) { + log.trace("Started notifyAllWhoSubscribedToTopic, topicId = [{}]", topicId); + + contactEventService.getAllByTopicId(topicId) + .forEach(e -> notifyAllTopics(e.getEmail())); + + log.info("All subscribed users was notified, topicId = [{}]", topicId); + } + + @Override + public void updateNotificationForAllTopics(String email) { + log.trace("Started updateNotificationForAllTopics, email = [{}]", email); + + var notifiedTopics = notificationService.updateTopicNotification(email); + simpMessagingTemplate.convertAndSendToUser(email, toNotifyTopicsDest(), notifiedTopics); + + log.info("All topics for user was notified, email = [{}]", email); + } + + @Override + public void updateNotificationForAllWhoSubscribedToTopic(Integer topicId) { + log.trace("Started updateNotificationForAllWhoSubscribedToTopic, topicId = [{}]", topicId); + contactEventService.getAllByTopicId(topicId) - .forEach(e -> notifyAllPublicTopics(e.getEmail())); + .forEach(e -> updateNotificationForAllTopics(e.getEmail())); + + log.info("Topic notifications was updated for all subscribed users, topicId = [{}]", topicId); } private String toNotifyMessageDest(Integer topicId) { diff --git a/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java b/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java index b742d8dc..aace67d0 100644 --- a/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java @@ -1,6 +1,7 @@ package com.chat.yourway.service; import static com.chat.yourway.model.event.EventType.ONLINE; +import static com.chat.yourway.model.event.EventType.SUBSCRIBED; import com.chat.yourway.dto.response.LastMessageResponseDto; import com.chat.yourway.model.event.ContactEvent; @@ -31,7 +32,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(), null)); + .orElse(new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), 0, null)); } @Override @@ -62,15 +63,21 @@ public List getAllByTopicId(Integer topicId) { } @Override - public void setLastMessageToAllTopicSubscribers(Integer topicId, LastMessageResponseDto message) { - log.trace("Started setLastMessageToAllTopicSubscribers, topic id [{}], last message [{}]", + public void updateMessageInfoForAllTopicSubscribers(Integer topicId, + LastMessageResponseDto message) { + log.trace("Started updateMessageInfoForAllTopicSubscribers, topic id [{}], last message [{}]", topicId, message); List events = getAllByTopicId(topicId).stream() - .peek(e -> e.setLastMessage(message)) + .peek(e -> { + if (!e.getEventType().equals(SUBSCRIBED)) { + e.setUnreadMessages(e.getUnreadMessages() + 1); + } + e.setLastMessage(message); + }) .toList(); - log.trace("Last message [{}] was set to all topic id [{}] subscribers", message, topicId); + log.trace("Message info [{}] was updated for all topic id [{}] subscribers", message, topicId); contactEventRedisRepository.saveAll(events); } diff --git a/src/main/java/com/chat/yourway/service/NotificationServiceImpl.java b/src/main/java/com/chat/yourway/service/NotificationServiceImpl.java index f23060ad..6639f943 100644 --- a/src/main/java/com/chat/yourway/service/NotificationServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/NotificationServiceImpl.java @@ -2,12 +2,11 @@ import com.chat.yourway.dto.response.MessageNotificationResponseDto; import com.chat.yourway.dto.response.TopicNotificationResponseDto; -import com.chat.yourway.mapper.MessageNotificationMapper; +import com.chat.yourway.mapper.NotificationMapper; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.service.interfaces.ContactEventService; import com.chat.yourway.service.interfaces.MessageService; import com.chat.yourway.service.interfaces.NotificationService; -import com.chat.yourway.service.interfaces.TopicService; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -19,39 +18,40 @@ public class NotificationServiceImpl implements NotificationService { private final ContactEventService contactEventService; - private final TopicService topicService; private final MessageService messageService; - private final MessageNotificationMapper notificationMapper; + private final NotificationMapper notificationMapper; @Override public List notifyTopicSubscribers(Integer topicId) { log.trace("Started notifyTopicSubscribers by topic id [{}]", topicId); return contactEventService.getAllByTopicId(topicId).stream() - .map(notificationMapper::toNotificationResponseDto) + .map(notificationMapper::toMessageNotificationResponseDto) .toList(); } @Override - public List notifyAllPublicTopicsByEmail(String email) { - - return topicService.findAllPublic().stream() - .map(topic -> { - var event = contactEventService.getByTopicIdAndEmail(topic.getId(), email); - var topicNotificationDto = new TopicNotificationResponseDto(); - topicNotificationDto.setTopic(topic); - topicNotificationDto.setUnreadMessages(countUnreadMessages(event)); - topicNotificationDto.setLastMessage(event.getLastMessage()); - return topicNotificationDto; - }) + public List notifyAllTopicsByEmail(String email) { + + return contactEventService.getAllByEmail(email).stream() + .peek(this::countUnreadMessages) + .map(notificationMapper::toTopicNotificationResponseDto) + .toList(); + } + + @Override + public List updateTopicNotification(String email) { + return contactEventService.getAllByEmail(email).stream() + .map(notificationMapper::toTopicNotificationResponseDto) .toList(); } - private int countUnreadMessages(ContactEvent event) { - return messageService.countMessagesBetweenTimestampByTopicId( + private void countUnreadMessages(ContactEvent event) { + event.setUnreadMessages(messageService.countMessagesBetweenTimestampByTopicId( event.getTopicId(), event.getEmail(), - event.getTimestamp()); + event.getTimestamp())); + contactEventService.save(event); } } diff --git a/src/main/java/com/chat/yourway/service/interfaces/ChatNotificationService.java b/src/main/java/com/chat/yourway/service/interfaces/ChatNotificationService.java index aef3056a..d05ff51f 100644 --- a/src/main/java/com/chat/yourway/service/interfaces/ChatNotificationService.java +++ b/src/main/java/com/chat/yourway/service/interfaces/ChatNotificationService.java @@ -21,7 +21,7 @@ public interface ChatNotificationService { * * @param userEmail user email. */ - void notifyAllPublicTopics(String userEmail); + void notifyAllTopics(String userEmail); /** * Notify everyone who is subscribed to the same topic. @@ -30,4 +30,18 @@ public interface ChatNotificationService { */ void notifyAllWhoSubscribedToTopic(Integer topicId); + /** + * Update topic notification for user who subscribed to the same topic. + * + * @param topicId The id of the topic. + */ + void updateNotificationForAllWhoSubscribedToTopic(Integer topicId); + + /** + * Update notification for all topics. + * + * @param email user email. + */ + void updateNotificationForAllTopics(String email); + } 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 ae199c9f..69f225ff 100644 --- a/src/main/java/com/chat/yourway/service/interfaces/ContactEventService.java +++ b/src/main/java/com/chat/yourway/service/interfaces/ContactEventService.java @@ -47,6 +47,6 @@ public interface ContactEventService { * @param topicId topic id. * @param lastMessageDto last message Dto. */ - void setLastMessageToAllTopicSubscribers(Integer topicId, LastMessageResponseDto lastMessageDto); + void updateMessageInfoForAllTopicSubscribers(Integer topicId, LastMessageResponseDto lastMessageDto); } diff --git a/src/main/java/com/chat/yourway/service/interfaces/NotificationService.java b/src/main/java/com/chat/yourway/service/interfaces/NotificationService.java index a14ab271..b549bc63 100644 --- a/src/main/java/com/chat/yourway/service/interfaces/NotificationService.java +++ b/src/main/java/com/chat/yourway/service/interfaces/NotificationService.java @@ -15,11 +15,19 @@ public interface NotificationService { List notifyTopicSubscribers(Integer topicId); /** - * Retrieves a list of notifying all public topics. + * Retrieves a list of notifying all topics. * * @param email user email. - * @return A list of public topic's information. + * @return A list of topic's information. */ - List notifyAllPublicTopicsByEmail(String email); + List notifyAllTopicsByEmail(String email); + + /** + * Update info and return a list of notifying all topics. + * + * @param email user email. + * @return A list of topic's information. + */ + List updateTopicNotification(String email); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ecf8cbc5..7255fc40 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -16,7 +16,7 @@ spring.data.redis.password=${REDIS_PASSWORD:admin} #Hibernate spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect -#spring.jpa.show-sql=true +spring.jpa.show-sql=false spring.jpa.properties.hibernate.format_sql=true spring.jpa.hibernate.ddl-auto=validate @@ -59,5 +59,5 @@ spring.mail.properties.mail.smtp.starttls.required=true message.max.amount.reports=2 #Logging: -logging.level.com.chat.yourway=${LOGGING_LEVEL:trace} +logging.level.com.chat.yourway=${LOGGING_LEVEL:info} 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 86063ceb..6d2ab90f 100644 --- a/src/test/java/com/chat/yourway/integration/controller/ChatControllerTest.java +++ b/src/test/java/com/chat/yourway/integration/controller/ChatControllerTest.java @@ -202,7 +202,7 @@ void notifyTopicSubscribers_shouldNotifyTopicSubscribersIfSubscribeEvent() { lastMessageDto.setSentFrom("vasil@gmail.com"); lastMessageDto.setLastMessage("Hi"); - var event = new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), + var event = new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, lastMessageDto); saveContactEvent(event); //Stored subscription results for testing @@ -233,7 +233,7 @@ void notifyTopicSubscribers_shouldNotifyTopicSubscribersIfAnyContactSubscribedTo lastMessageDto.setSentFrom("vasil@gmail.com"); lastMessageDto.setLastMessage("Hi"); - var event = new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), + var event = new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, lastMessageDto); saveContactEvent(event); //Stored subscription results for testing 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 da8fb7b5..7c263f01 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 @@ -41,7 +41,8 @@ void save_shouldSaveToRepository() { // Given String email = "vasil@gmail.com"; int topicId = 1; - ContactEvent contactEvent = new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), null); + ContactEvent contactEvent = new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), 0, + null); // When contactEventService.save(contactEvent); @@ -56,7 +57,8 @@ void getByTopicIdAndEmail_shouldReturnContactEventFromRepository() { // Given Integer topicId = 1; String email = "vasil@gmail.com"; - ContactEvent expectedContactEvent = new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), null); + ContactEvent expectedContactEvent = new ContactEvent(email, topicId, ONLINE, + LocalDateTime.now(), 0, null); when(contactEventRedisRepository.findById(email + "_" + topicId)) .thenReturn(Optional.of(expectedContactEvent)); @@ -91,8 +93,8 @@ void getAllByEmail_shouldReturnContactEventsFromRepository() { // Given String email = "vasil@gmail.com"; List expectedContactEvents = Arrays.asList( - new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), null), - new ContactEvent(email, 2, OFFLINE, LocalDateTime.now(), null) + new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), 0, null), + new ContactEvent(email, 2, OFFLINE, LocalDateTime.now(), 0, null) ); when(contactEventRedisRepository.findAllByEmail(email)).thenReturn(expectedContactEvents); @@ -110,8 +112,8 @@ void updateEventTypeByEmail_shouldUpdateEventTypesInRepository() { String email = "vasil@gmail.com"; EventType newEventType = OFFLINE; List events = Arrays.asList( - new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), null), - new ContactEvent(email, 2, ONLINE, LocalDateTime.now(), null) + new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), 0, null), + new ContactEvent(email, 2, ONLINE, LocalDateTime.now(), 0, null) ); when(contactEventRedisRepository.findAllByEmail(email)).thenReturn(events); @@ -129,8 +131,8 @@ void getAllByTopicId_shouldReturnContactEventsFromRepository() { // Given Integer topicId = 1; List expectedContactEvents = Arrays.asList( - new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), null), - new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), null) + new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, null), + new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), 0, null) ); when(contactEventRedisRepository.findAllByTopicId(topicId)).thenReturn(expectedContactEvents); @@ -152,13 +154,13 @@ void setLastMessageToAllTopicSubscribers_shouldUpdateLastMessagesInRepository() lastMessageDto.setLastMessage("New message"); List events = Arrays.asList( - new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), null), - new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), null) + new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, null), + new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), 0, null) ); when(contactEventRedisRepository.findAllByTopicId(topicId)).thenReturn(events); // When - contactEventService.setLastMessageToAllTopicSubscribers(topicId, lastMessageDto); + contactEventService.updateMessageInfoForAllTopicSubscribers(topicId, lastMessageDto); // Then events.forEach(e -> e.setLastMessage(lastMessageDto)); 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 dde09bcd..ab82a331 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,7 +10,7 @@ import com.chat.yourway.dto.response.LastMessageResponseDto; import com.chat.yourway.dto.response.MessageNotificationResponseDto; -import com.chat.yourway.mapper.MessageNotificationMapper; +import com.chat.yourway.mapper.NotificationMapper; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.service.NotificationServiceImpl; import com.chat.yourway.service.interfaces.ContactEventService; @@ -31,7 +31,7 @@ class NotificationServiceImplTest { private ContactEventService contactEventService; @Mock - private MessageNotificationMapper notificationMapper; + private NotificationMapper notificationMapper; @InjectMocks private NotificationServiceImpl notificationService; @@ -47,8 +47,8 @@ void notifyTopicSubscribers_shouldReturnListOfNotifications() { lastMessageDto.setLastMessage("Hello"); List events = Arrays.asList( - new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), lastMessageDto), - new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), lastMessageDto) + new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, lastMessageDto), + new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), 0, lastMessageDto) ); var expectedNotifications = events.stream() @@ -63,7 +63,7 @@ void notifyTopicSubscribers_shouldReturnListOfNotifications() { .toList(); when(contactEventService.getAllByTopicId(topicId)).thenReturn(events); - when(notificationMapper.toNotificationResponseDto(any())) + when(notificationMapper.toMessageNotificationResponseDto(any())) .thenReturn(expectedNotifications.get(0), expectedNotifications.get(1)); // When @@ -72,7 +72,7 @@ void notifyTopicSubscribers_shouldReturnListOfNotifications() { // Then assertEquals(expectedNotifications, result, "Should return the expected list of notifications"); - verify(notificationMapper, times(2)).toNotificationResponseDto(any()); + verify(notificationMapper, times(2)).toMessageNotificationResponseDto(any()); } } From 1abe15dacea65d767e0b604acabf57232a7db0b7 Mon Sep 17 00:00:00 2001 From: Dmytro Trotsenko Date: Thu, 28 Mar 2024 22:30:59 +0200 Subject: [PATCH 4/9] refactor: refactored and optimized request to get all topics --- .../yourway/controller/TopicController.java | 7 ++-- .../dto/response/TopicInfoResponseDto.java | 26 ++++++++++++++ .../com/chat/yourway/mapper/TopicMapper.java | 3 ++ .../java/com/chat/yourway/model/Topic.java | 4 +-- .../yourway/repository/TopicRepository.java | 26 +++++++------- .../yourway/service/TopicServiceImpl.java | 13 +++---- .../service/interfaces/TopicService.java | 35 +++++++++---------- .../controller/TopicControllerTest.java | 6 +--- .../service/impl/TopicServiceImplTest.java | 11 ++++-- 9 files changed, 81 insertions(+), 50 deletions(-) create mode 100644 src/main/java/com/chat/yourway/dto/response/TopicInfoResponseDto.java diff --git a/src/main/java/com/chat/yourway/controller/TopicController.java b/src/main/java/com/chat/yourway/controller/TopicController.java index 5bc7f297..ad7f6117 100644 --- a/src/main/java/com/chat/yourway/controller/TopicController.java +++ b/src/main/java/com/chat/yourway/controller/TopicController.java @@ -8,6 +8,7 @@ import com.chat.yourway.dto.request.TopicRequestDto; import com.chat.yourway.dto.response.ApiErrorResponseDto; import com.chat.yourway.dto.response.ContactResponseDto; +import com.chat.yourway.dto.response.TopicInfoResponseDto; import com.chat.yourway.dto.response.TopicResponseDto; import com.chat.yourway.service.interfaces.TopicService; import com.chat.yourway.service.interfaces.TopicSubscriberService; @@ -149,7 +150,7 @@ public TopicResponseDto findById(@PathVariable Integer id) { content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))) }) @GetMapping(path = "/all", produces = APPLICATION_JSON_VALUE) - public List findAllPublic() { + public List findAllPublic() { return topicService.findAllPublic(); } @@ -322,7 +323,7 @@ public void removeToFavouriteTopic( content = @Content(schema = @Schema(implementation = ApiErrorResponseDto.class))) }) @GetMapping(path = "/favourite", produces = APPLICATION_JSON_VALUE) - public List findAllFavouriteTopics( + public List findAllFavouriteTopics( @AuthenticationPrincipal UserDetails userDetails) { return topicService.findAllFavouriteTopics(userDetails); } @@ -333,7 +334,7 @@ public List findAllFavouriteTopics( @ApiResponse(responseCode = "200", description = SUCCESSFULLY_FOUND_TOPIC) }) @GetMapping(path = "/popular/public", produces = APPLICATION_JSON_VALUE) - public List findAllPopularPublicTopics() { + public List findAllPopularPublicTopics() { return topicService.findPopularPublicTopics(); } diff --git a/src/main/java/com/chat/yourway/dto/response/TopicInfoResponseDto.java b/src/main/java/com/chat/yourway/dto/response/TopicInfoResponseDto.java new file mode 100644 index 00000000..d1dec5db --- /dev/null +++ b/src/main/java/com/chat/yourway/dto/response/TopicInfoResponseDto.java @@ -0,0 +1,26 @@ +package com.chat.yourway.dto.response; + +import io.swagger.v3.oas.annotations.media.Schema; +import java.time.LocalDateTime; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +@AllArgsConstructor +@NoArgsConstructor +@Setter +@Getter +@ToString +public class TopicInfoResponseDto { + @Schema(description = "ID", example = "1") + private Integer id; + @Schema(description = "New Topic name", example = "My programming topic") + private String topicName; + @Schema(description = "Email of Topic creator", example = "example@gmail.com") + private String createdBy; + @Schema(description = "Created time") + private LocalDateTime createdAt; + +} diff --git a/src/main/java/com/chat/yourway/mapper/TopicMapper.java b/src/main/java/com/chat/yourway/mapper/TopicMapper.java index 0d71fb5e..2dff6d6e 100644 --- a/src/main/java/com/chat/yourway/mapper/TopicMapper.java +++ b/src/main/java/com/chat/yourway/mapper/TopicMapper.java @@ -1,5 +1,6 @@ package com.chat.yourway.mapper; +import com.chat.yourway.dto.response.TopicInfoResponseDto; import com.chat.yourway.dto.response.TopicResponseDto; import com.chat.yourway.model.Topic; import java.util.List; @@ -12,6 +13,8 @@ public interface TopicMapper { TopicResponseDto toResponseDto(Topic topic); List toListResponseDto(List topics); + List toListInfoResponseDto(List topics); + @Mapping(target = "messages", ignore = true) Topic toEntity(TopicResponseDto topicResponseDto); diff --git a/src/main/java/com/chat/yourway/model/Topic.java b/src/main/java/com/chat/yourway/model/Topic.java index 0eca84d3..1e05c306 100644 --- a/src/main/java/com/chat/yourway/model/Topic.java +++ b/src/main/java/com/chat/yourway/model/Topic.java @@ -48,7 +48,7 @@ public class Topic { @Column(name = "created_at", nullable = false) private LocalDateTime createdAt; - @ManyToMany(fetch = FetchType.EAGER) + @ManyToMany(fetch = FetchType.LAZY) @JoinTable( schema = "chat", name = "topic_tag", @@ -56,7 +56,7 @@ public class Topic { inverseJoinColumns = @JoinColumn(name = "tag_id")) private Set tags; - @OneToMany(mappedBy = "topic", fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @OneToMany(mappedBy = "topic", fetch = FetchType.LAZY, cascade = CascadeType.ALL) private Set topicSubscribers; @OneToMany(mappedBy = "topic", fetch = FetchType.LAZY, cascade = CascadeType.ALL) diff --git a/src/main/java/com/chat/yourway/repository/TopicRepository.java b/src/main/java/com/chat/yourway/repository/TopicRepository.java index 5c0a0b53..de34ae69 100644 --- a/src/main/java/com/chat/yourway/repository/TopicRepository.java +++ b/src/main/java/com/chat/yourway/repository/TopicRepository.java @@ -18,10 +18,10 @@ public interface TopicRepository extends JpaRepository { @Query( value = """ - SELECT * - FROM chat.topic t - WHERE to_tsvector('english', t.topic_name) @@ to_tsquery('english', :query) - """, + SELECT * + FROM chat.topic t + WHERE to_tsvector('english', t.topic_name) @@ to_tsquery('english', :query) + """, nativeQuery = true) List findAllByTopicName(String query); @@ -31,18 +31,16 @@ WHERE to_tsvector('english', t.topic_name) @@ to_tsquery('english', :query) @Query( "select t from Topic t join fetch t.topicSubscribers ts " - + "where ts.contact.email = :contactEmail and ts.isFavouriteTopic = true") + + "where ts.contact.email = :contactEmail and ts.isFavouriteTopic = true") List findAllFavouriteTopicsByContactEmail(String contactEmail); - boolean existsByIdAndIsPublic(int topicId, boolean isPublic); - @Query(nativeQuery = true, value = - "SELECT t.*, COUNT(ts.id) AS ts_count, COUNT(m.id) AS m_count " + - "FROM chat.topic t " + - "JOIN chat.topic_subscriber ts ON t.id = ts.topic_id " + - "JOIN chat.message m ON t.id = m.topic_id " + - "WHERE t.is_public = true " + - "GROUP BY t.id " + - "ORDER BY ts_count DESC, m_count DESC") + "SELECT t.*, COUNT(ts.id) AS ts_count, COUNT(m.id) AS m_count " + + "FROM chat.topic t " + + "JOIN chat.topic_subscriber ts ON t.id = ts.topic_id " + + "JOIN chat.message m ON t.id = m.topic_id " + + "WHERE t.is_public = true " + + "GROUP BY t.id " + + "ORDER BY ts_count DESC, m_count DESC") List findPopularPublicTopics(); } diff --git a/src/main/java/com/chat/yourway/service/TopicServiceImpl.java b/src/main/java/com/chat/yourway/service/TopicServiceImpl.java index 60ad4913..2470e70f 100644 --- a/src/main/java/com/chat/yourway/service/TopicServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/TopicServiceImpl.java @@ -5,6 +5,7 @@ import com.chat.yourway.dto.request.TagRequestDto; import com.chat.yourway.dto.request.TopicPrivateRequestDto; import com.chat.yourway.dto.request.TopicRequestDto; +import com.chat.yourway.dto.response.TopicInfoResponseDto; import com.chat.yourway.dto.response.TopicResponseDto; import com.chat.yourway.exception.ContactEmailNotExist; import com.chat.yourway.exception.TopicAccessException; @@ -100,13 +101,13 @@ public TopicResponseDto findByName(String name) { } @Override - public List findAllPublic() { + public List findAllPublic() { log.trace("Started findAllPublic"); List topics = topicRepository.findAllByIsPublicIsTrue(); log.trace("All public topics was found"); - return topicMapper.toListResponseDto(topics); + return topicMapper.toListInfoResponseDto(topics); } @Override @@ -173,15 +174,15 @@ public String generatePrivateName(String sendTo, String email) { } @Override - public List findAllFavouriteTopics(UserDetails userDetails) { + public List findAllFavouriteTopics(UserDetails userDetails) { String contactEmail = userDetails.getUsername(); - return topicMapper.toListResponseDto( + return topicMapper.toListInfoResponseDto( topicRepository.findAllFavouriteTopicsByContactEmail(contactEmail)); } @Override - public List findPopularPublicTopics() { - return topicMapper.toListResponseDto(topicRepository.findPopularPublicTopics()); + public List findPopularPublicTopics() { + return topicMapper.toListInfoResponseDto(topicRepository.findPopularPublicTopics()); } private Topic createOrUpdateTopic(Topic topic, TopicRequestDto topicRequestDto, String email) { diff --git a/src/main/java/com/chat/yourway/service/interfaces/TopicService.java b/src/main/java/com/chat/yourway/service/interfaces/TopicService.java index 41e1c974..3e305d8a 100644 --- a/src/main/java/com/chat/yourway/service/interfaces/TopicService.java +++ b/src/main/java/com/chat/yourway/service/interfaces/TopicService.java @@ -3,16 +3,15 @@ import com.chat.yourway.dto.request.TagRequestDto; import com.chat.yourway.dto.request.TopicPrivateRequestDto; import com.chat.yourway.dto.request.TopicRequestDto; +import com.chat.yourway.dto.response.TopicInfoResponseDto; import com.chat.yourway.dto.response.TopicResponseDto; import com.chat.yourway.exception.TopicAccessException; import com.chat.yourway.exception.TopicNotFoundException; import com.chat.yourway.exception.ValueNotUniqException; import com.chat.yourway.model.Tag; -import com.chat.yourway.model.Topic; -import org.springframework.security.core.userdetails.UserDetails; - import java.util.List; import java.util.Set; +import org.springframework.security.core.userdetails.UserDetails; public interface TopicService { @@ -20,7 +19,7 @@ public interface TopicService { * Creates a new topic with the specified email of the creator. * * @param topicRequestDto Request object for creating topic. - * @param email The email of the creator. + * @param email The email of the creator. * @return Created topic. * @throws ValueNotUniqException If the topic name already in use. */ @@ -31,7 +30,7 @@ public interface TopicService { * contacts. * * @param topicPrivateDto Request object for creating topic. - * @param email The email of the creator. + * @param email The email of the creator. * @return Created private topic. * @throws ValueNotUniqException If the topic name already in use. */ @@ -40,12 +39,12 @@ public interface TopicService { /** * Update an existing topic with the specified email of the creator. * - * @param topicId The ID of the topic to find. + * @param topicId The ID of the topic to find. * @param topicRequestDto Request object for creating topic. - * @param email The email of the creator. + * @param email The email of the creator. * @return Updated topic. * @throws ValueNotUniqException If the topic name already in use. - * @throws TopicAccessException if the email is not the creator of the topic. + * @throws TopicAccessException if the email is not the creator of the topic. */ TopicResponseDto update(Integer topicId, TopicRequestDto topicRequestDto, String email); @@ -72,12 +71,12 @@ public interface TopicService { * * @return A list of public topics. */ - List findAllPublic(); + List findAllPublic(); /** * Deletes a topic by ID if the specified email is the creator of the topic. * - * @param id The ID of the topic to delete. + * @param id The ID of the topic to delete. * @param email The email of the user. * @throws TopicAccessException if the email is not the creator of the topic. */ @@ -88,7 +87,7 @@ public interface TopicService { * * @param tagName The unique name of the tag for which topics are to be retrieved. * @return A list of {@link TopicResponseDto} objects associated with the given tag. An empty list - * is returned if no topics are found for the specified tag. + * is returned if no topics are found for the specified tag. */ List findTopicsByTagName(String tagName); @@ -116,7 +115,7 @@ public interface TopicService { * separated by "<->" symbol. * * @param sendTo Email address of the receiver. - * @param email Email address of the sender. + * @param email Email address of the sender. * @return Unique private topic name. */ String generatePrivateName(String sendTo, String email); @@ -125,20 +124,20 @@ public interface TopicService { * Retrieves a list of favorite topics for the specified user. * * @param userDetails The details of the user for whom favorite topics are to be retrieved. - * @return A list of {@code TopicResponseDto} objects representing the user's favorite topics. + * @return A list of {@code TopicInfoResponseDto} objects representing the user's favorite topics. */ - List findAllFavouriteTopics(UserDetails userDetails); + List findAllFavouriteTopics(UserDetails userDetails); /** * Retrieves a list of popular public topics. *

- * This method returns a list of {@code TopicResponseDto} objects representing popular topics - * that are marked as public. The popularity is determined by the number of subscribers and messages + * This method returns a list of {@code TopicResponseDto} objects representing popular topics that + * are marked as public. The popularity is determined by the number of subscribers and messages * associated with each topic. * - * @return A list of {@code TopicResponseDto} objects representing popular public topics. + * @return A list of {@code TopicInfoResponseDto} objects representing popular public topics. * @see TopicResponseDto */ - List findPopularPublicTopics(); + List findPopularPublicTopics(); } diff --git a/src/test/java/com/chat/yourway/integration/controller/TopicControllerTest.java b/src/test/java/com/chat/yourway/integration/controller/TopicControllerTest.java index 299d1673..95b3a676 100644 --- a/src/test/java/com/chat/yourway/integration/controller/TopicControllerTest.java +++ b/src/test/java/com/chat/yourway/integration/controller/TopicControllerTest.java @@ -515,11 +515,7 @@ public void findAllPublic_shouldReturnListOfAllPublicTopics() throws Exception { .andExpect(jsonPath("$[0].createdBy").value(savedTopics.get(0).getCreatedBy())) .andExpect(jsonPath("$[1].topicName").value(savedTopics.get(1).getTopicName())) .andExpect(jsonPath("$[1].createdBy").value(savedTopics.get(1).getCreatedBy())) - .andExpect(jsonPath("$[*].createdAt").isNotEmpty()) - .andExpect(jsonPath("$[0].isPublic").value(true)) - .andExpect(jsonPath("$[1].isPublic").value(true)) - .andExpect(jsonPath("$[*].tags").isArray()) - .andExpect(jsonPath("$[*].topicSubscribers").isArray()); + .andExpect(jsonPath("$[*].createdAt").isNotEmpty()); } @Test diff --git a/src/test/java/com/chat/yourway/unit/service/impl/TopicServiceImplTest.java b/src/test/java/com/chat/yourway/unit/service/impl/TopicServiceImplTest.java index 5d113a5d..3fdbcf92 100644 --- a/src/test/java/com/chat/yourway/unit/service/impl/TopicServiceImplTest.java +++ b/src/test/java/com/chat/yourway/unit/service/impl/TopicServiceImplTest.java @@ -17,6 +17,7 @@ import com.chat.yourway.dto.request.TopicPrivateRequestDto; import com.chat.yourway.dto.request.TopicRequestDto; import com.chat.yourway.dto.response.TagResponseDto; +import com.chat.yourway.dto.response.TopicInfoResponseDto; import com.chat.yourway.dto.response.TopicResponseDto; import com.chat.yourway.exception.TopicAccessException; import com.chat.yourway.exception.TopicNotFoundException; @@ -351,7 +352,7 @@ public void findAllPublic_shouldReturnListOfTopicResponseDto() { when(topicRepository.findAllByIsPublicIsTrue()).thenReturn(topics); // When - List topicResponseDtos = topicService.findAllPublic(); + List topicResponseDtos = topicService.findAllPublic(); // Then assertThat(topicResponseDtos).isNotNull(); @@ -369,7 +370,7 @@ public void findAllPublic_shouldReturnEmptyListOfTopicResponseDto() { when(topicRepository.findAllByIsPublicIsTrue()).thenReturn(emptyList); // When - List topicResponseDtos = topicService.findAllPublic(); + List topicResponseDtos = topicService.findAllPublic(); // Then assertThat(topicResponseDtos).isNotNull(); @@ -486,4 +487,10 @@ private void assertTopicEquals(Topic topic, TopicResponseDto topicResponseDto) { assertThat(topicResponseDto.getTopicSubscribers()).isNull(); } + private void assertTopicEquals(Topic topic, TopicInfoResponseDto topicResponseDto) { + assertThat(topicResponseDto.getTopicName()).isEqualTo(topic.getTopicName()); + assertThat(topicResponseDto.getCreatedBy()).isEqualTo(topic.getCreatedBy()); + assertThat(topicResponseDto.getCreatedAt()).isEqualTo(topic.getCreatedAt()); + } + } From 235c85a364b1261d45ed305b5ea9fe041fda2675 Mon Sep 17 00:00:00 2001 From: Dmytro Trotsenko Date: Sun, 31 Mar 2024 12:38:31 +0300 Subject: [PATCH 5/9] refactor: refactored dtos package --- .../com/chat/yourway/controller/AuthenticationController.java | 2 +- .../com/chat/yourway/controller/ChangePasswordController.java | 2 +- .../java/com/chat/yourway/controller/ContactController.java | 2 +- .../java/com/chat/yourway/controller/MessageController.java | 2 +- .../java/com/chat/yourway/controller/TopicController.java | 2 +- .../yourway/dto/response/{ => error}/ApiErrorResponseDto.java | 2 +- .../dto/response/{ => error}/MessageErrorResponseDto.java | 2 +- .../response/{ => notification}/LastMessageResponseDto.java | 2 +- .../{ => notification}/MessageNotificationResponseDto.java | 2 +- .../chat/yourway/exception/handler/ApiExceptionHandler.java | 2 +- .../yourway/exception/handler/WebsocketExceptionHandler.java | 2 +- .../com/chat/yourway/listener/StompSubscriptionListener.java | 2 +- src/main/java/com/chat/yourway/mapper/NotificationMapper.java | 4 ++-- src/main/java/com/chat/yourway/model/event/ContactEvent.java | 2 +- .../java/com/chat/yourway/service/ChatMessageServiceImpl.java | 2 +- .../com/chat/yourway/service/ContactEventServiceImpl.java | 2 +- .../com/chat/yourway/service/NotificationServiceImpl.java | 4 ++-- .../chat/yourway/service/interfaces/ContactEventService.java | 2 +- .../chat/yourway/service/interfaces/NotificationService.java | 4 ++-- .../yourway/integration/controller/ChatControllerTest.java | 4 ++-- .../yourway/unit/listener/StompSubscriptionListenerTest.java | 2 +- .../unit/service/impl/ContactEventServiceImplTest.java | 2 +- .../unit/service/impl/NotificationServiceImplTest.java | 4 ++-- 23 files changed, 28 insertions(+), 28 deletions(-) rename src/main/java/com/chat/yourway/dto/response/{ => error}/ApiErrorResponseDto.java (90%) rename src/main/java/com/chat/yourway/dto/response/{ => error}/MessageErrorResponseDto.java (87%) rename src/main/java/com/chat/yourway/dto/response/{ => notification}/LastMessageResponseDto.java (93%) rename src/main/java/com/chat/yourway/dto/response/{ => notification}/MessageNotificationResponseDto.java (89%) diff --git a/src/main/java/com/chat/yourway/controller/AuthenticationController.java b/src/main/java/com/chat/yourway/controller/AuthenticationController.java index 1319b1e6..0e562186 100644 --- a/src/main/java/com/chat/yourway/controller/AuthenticationController.java +++ b/src/main/java/com/chat/yourway/controller/AuthenticationController.java @@ -15,7 +15,7 @@ import com.chat.yourway.config.openapi.OpenApiExamples; import com.chat.yourway.dto.request.AuthRequestDto; import com.chat.yourway.dto.request.ContactRequestDto; -import com.chat.yourway.dto.response.ApiErrorResponseDto; +import com.chat.yourway.dto.response.error.ApiErrorResponseDto; import com.chat.yourway.dto.response.AuthResponseDto; import com.chat.yourway.security.LogoutService; import com.chat.yourway.service.interfaces.ActivateAccountService; diff --git a/src/main/java/com/chat/yourway/controller/ChangePasswordController.java b/src/main/java/com/chat/yourway/controller/ChangePasswordController.java index f86c642e..c51b9a1b 100644 --- a/src/main/java/com/chat/yourway/controller/ChangePasswordController.java +++ b/src/main/java/com/chat/yourway/controller/ChangePasswordController.java @@ -3,7 +3,7 @@ import com.chat.yourway.config.openapi.OpenApiExamples; import com.chat.yourway.dto.request.ChangePasswordDto; import com.chat.yourway.dto.request.RestorePasswordDto; -import com.chat.yourway.dto.response.ApiErrorResponseDto; +import com.chat.yourway.dto.response.error.ApiErrorResponseDto; import com.chat.yourway.service.interfaces.ChangePasswordService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; diff --git a/src/main/java/com/chat/yourway/controller/ContactController.java b/src/main/java/com/chat/yourway/controller/ContactController.java index 414059ba..45722cfc 100644 --- a/src/main/java/com/chat/yourway/controller/ContactController.java +++ b/src/main/java/com/chat/yourway/controller/ContactController.java @@ -5,7 +5,7 @@ import com.chat.yourway.config.openapi.OpenApiExamples; import com.chat.yourway.dto.request.EditContactProfileRequestDto; -import com.chat.yourway.dto.response.ApiErrorResponseDto; +import com.chat.yourway.dto.response.error.ApiErrorResponseDto; import com.chat.yourway.dto.response.ContactProfileResponseDto; import com.chat.yourway.service.interfaces.ContactService; import io.swagger.v3.oas.annotations.Operation; diff --git a/src/main/java/com/chat/yourway/controller/MessageController.java b/src/main/java/com/chat/yourway/controller/MessageController.java index 07a98d4c..43e9dc09 100644 --- a/src/main/java/com/chat/yourway/controller/MessageController.java +++ b/src/main/java/com/chat/yourway/controller/MessageController.java @@ -4,7 +4,7 @@ import static com.chat.yourway.config.openapi.OpenApiMessages.MESSAGE_NOT_FOUND; import static com.chat.yourway.config.openapi.OpenApiMessages.SUCCESSFULLY_REPORTED_MESSAGE; -import com.chat.yourway.dto.response.ApiErrorResponseDto; +import com.chat.yourway.dto.response.error.ApiErrorResponseDto; import com.chat.yourway.service.interfaces.MessageService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; diff --git a/src/main/java/com/chat/yourway/controller/TopicController.java b/src/main/java/com/chat/yourway/controller/TopicController.java index ad7f6117..ab0dcbcd 100644 --- a/src/main/java/com/chat/yourway/controller/TopicController.java +++ b/src/main/java/com/chat/yourway/controller/TopicController.java @@ -6,7 +6,7 @@ import com.chat.yourway.dto.request.TopicPrivateRequestDto; import com.chat.yourway.dto.request.TopicRequestDto; -import com.chat.yourway.dto.response.ApiErrorResponseDto; +import com.chat.yourway.dto.response.error.ApiErrorResponseDto; import com.chat.yourway.dto.response.ContactResponseDto; import com.chat.yourway.dto.response.TopicInfoResponseDto; import com.chat.yourway.dto.response.TopicResponseDto; diff --git a/src/main/java/com/chat/yourway/dto/response/ApiErrorResponseDto.java b/src/main/java/com/chat/yourway/dto/response/error/ApiErrorResponseDto.java similarity index 90% rename from src/main/java/com/chat/yourway/dto/response/ApiErrorResponseDto.java rename to src/main/java/com/chat/yourway/dto/response/error/ApiErrorResponseDto.java index ede0bbaa..6e19da40 100644 --- a/src/main/java/com/chat/yourway/dto/response/ApiErrorResponseDto.java +++ b/src/main/java/com/chat/yourway/dto/response/error/ApiErrorResponseDto.java @@ -1,4 +1,4 @@ -package com.chat.yourway.dto.response; +package com.chat.yourway.dto.response.error; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Getter; diff --git a/src/main/java/com/chat/yourway/dto/response/MessageErrorResponseDto.java b/src/main/java/com/chat/yourway/dto/response/error/MessageErrorResponseDto.java similarity index 87% rename from src/main/java/com/chat/yourway/dto/response/MessageErrorResponseDto.java rename to src/main/java/com/chat/yourway/dto/response/error/MessageErrorResponseDto.java index 13cd4ac3..0ad9a9a0 100644 --- a/src/main/java/com/chat/yourway/dto/response/MessageErrorResponseDto.java +++ b/src/main/java/com/chat/yourway/dto/response/error/MessageErrorResponseDto.java @@ -1,4 +1,4 @@ -package com.chat.yourway.dto.response; +package com.chat.yourway.dto.response.error; import java.time.LocalDateTime; import lombok.AllArgsConstructor; diff --git a/src/main/java/com/chat/yourway/dto/response/LastMessageResponseDto.java b/src/main/java/com/chat/yourway/dto/response/notification/LastMessageResponseDto.java similarity index 93% rename from src/main/java/com/chat/yourway/dto/response/LastMessageResponseDto.java rename to src/main/java/com/chat/yourway/dto/response/notification/LastMessageResponseDto.java index 1f8426b9..9469a522 100644 --- a/src/main/java/com/chat/yourway/dto/response/LastMessageResponseDto.java +++ b/src/main/java/com/chat/yourway/dto/response/notification/LastMessageResponseDto.java @@ -1,4 +1,4 @@ -package com.chat.yourway.dto.response; +package com.chat.yourway.dto.response.notification; import java.time.LocalDateTime; import lombok.Getter; diff --git a/src/main/java/com/chat/yourway/dto/response/MessageNotificationResponseDto.java b/src/main/java/com/chat/yourway/dto/response/notification/MessageNotificationResponseDto.java similarity index 89% rename from src/main/java/com/chat/yourway/dto/response/MessageNotificationResponseDto.java rename to src/main/java/com/chat/yourway/dto/response/notification/MessageNotificationResponseDto.java index 3fd21933..7c73eebc 100644 --- a/src/main/java/com/chat/yourway/dto/response/MessageNotificationResponseDto.java +++ b/src/main/java/com/chat/yourway/dto/response/notification/MessageNotificationResponseDto.java @@ -1,4 +1,4 @@ -package com.chat.yourway.dto.response; +package com.chat.yourway.dto.response.notification; import com.chat.yourway.model.event.EventType; import java.time.LocalDateTime; diff --git a/src/main/java/com/chat/yourway/exception/handler/ApiExceptionHandler.java b/src/main/java/com/chat/yourway/exception/handler/ApiExceptionHandler.java index a965f60a..2d820887 100644 --- a/src/main/java/com/chat/yourway/exception/handler/ApiExceptionHandler.java +++ b/src/main/java/com/chat/yourway/exception/handler/ApiExceptionHandler.java @@ -6,7 +6,7 @@ import static org.springframework.http.HttpStatus.NOT_FOUND; import static org.springframework.http.HttpStatus.UNAUTHORIZED; -import com.chat.yourway.dto.response.ApiErrorResponseDto; +import com.chat.yourway.dto.response.error.ApiErrorResponseDto; import com.chat.yourway.exception.ContactAlreadySubscribedToTopicException; import com.chat.yourway.exception.ContactEmailNotExist; import com.chat.yourway.exception.ContactNotFoundException; diff --git a/src/main/java/com/chat/yourway/exception/handler/WebsocketExceptionHandler.java b/src/main/java/com/chat/yourway/exception/handler/WebsocketExceptionHandler.java index 9a81ea07..ef911e46 100644 --- a/src/main/java/com/chat/yourway/exception/handler/WebsocketExceptionHandler.java +++ b/src/main/java/com/chat/yourway/exception/handler/WebsocketExceptionHandler.java @@ -1,6 +1,6 @@ package com.chat.yourway.exception.handler; -import com.chat.yourway.dto.response.MessageErrorResponseDto; +import com.chat.yourway.dto.response.error.MessageErrorResponseDto; import com.chat.yourway.exception.MessagePermissionDeniedException; import com.chat.yourway.exception.TopicNotFoundException; import com.chat.yourway.exception.TopicSubscriberNotFoundException; diff --git a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java index 2b38f4e7..2e9e9edf 100644 --- a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java +++ b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java @@ -4,7 +4,7 @@ import static com.chat.yourway.model.event.EventType.SUBSCRIBED; import com.chat.yourway.config.websocket.WebsocketProperties; -import com.chat.yourway.dto.response.LastMessageResponseDto; +import com.chat.yourway.dto.response.notification.LastMessageResponseDto; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.service.interfaces.ChatNotificationService; import com.chat.yourway.service.interfaces.ContactEventService; diff --git a/src/main/java/com/chat/yourway/mapper/NotificationMapper.java b/src/main/java/com/chat/yourway/mapper/NotificationMapper.java index 591a5454..81bf5a04 100644 --- a/src/main/java/com/chat/yourway/mapper/NotificationMapper.java +++ b/src/main/java/com/chat/yourway/mapper/NotificationMapper.java @@ -1,7 +1,7 @@ package com.chat.yourway.mapper; -import com.chat.yourway.dto.response.MessageNotificationResponseDto; -import com.chat.yourway.dto.response.TopicNotificationResponseDto; +import com.chat.yourway.dto.response.notification.MessageNotificationResponseDto; +import com.chat.yourway.dto.response.notification.TopicNotificationResponseDto; import com.chat.yourway.model.event.ContactEvent; import org.mapstruct.Mapper; import org.mapstruct.Mapping; 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 8f7e9f15..ffaa7b00 100644 --- a/src/main/java/com/chat/yourway/model/event/ContactEvent.java +++ b/src/main/java/com/chat/yourway/model/event/ContactEvent.java @@ -1,6 +1,6 @@ package com.chat.yourway.model.event; -import com.chat.yourway.dto.response.LastMessageResponseDto; +import com.chat.yourway.dto.response.notification.LastMessageResponseDto; import java.time.LocalDateTime; import lombok.Getter; import lombok.NoArgsConstructor; diff --git a/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java b/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java index 5fac88f1..7b40e510 100644 --- a/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/ChatMessageServiceImpl.java @@ -4,7 +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.notification.LastMessageResponseDto; import com.chat.yourway.dto.response.MessageResponseDto; import com.chat.yourway.service.interfaces.ChatMessageService; import com.chat.yourway.service.interfaces.ChatNotificationService; diff --git a/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java b/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java index aace67d0..4e36de92 100644 --- a/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java @@ -3,7 +3,7 @@ import static com.chat.yourway.model.event.EventType.ONLINE; import static com.chat.yourway.model.event.EventType.SUBSCRIBED; -import com.chat.yourway.dto.response.LastMessageResponseDto; +import com.chat.yourway.dto.response.notification.LastMessageResponseDto; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.model.event.EventType; import com.chat.yourway.repository.ContactEventRedisRepository; diff --git a/src/main/java/com/chat/yourway/service/NotificationServiceImpl.java b/src/main/java/com/chat/yourway/service/NotificationServiceImpl.java index 6639f943..944718c8 100644 --- a/src/main/java/com/chat/yourway/service/NotificationServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/NotificationServiceImpl.java @@ -1,7 +1,7 @@ package com.chat.yourway.service; -import com.chat.yourway.dto.response.MessageNotificationResponseDto; -import com.chat.yourway.dto.response.TopicNotificationResponseDto; +import com.chat.yourway.dto.response.notification.MessageNotificationResponseDto; +import com.chat.yourway.dto.response.notification.TopicNotificationResponseDto; import com.chat.yourway.mapper.NotificationMapper; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.service.interfaces.ContactEventService; 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 69f225ff..f9c3b5a0 100644 --- a/src/main/java/com/chat/yourway/service/interfaces/ContactEventService.java +++ b/src/main/java/com/chat/yourway/service/interfaces/ContactEventService.java @@ -1,6 +1,6 @@ package com.chat.yourway.service.interfaces; -import com.chat.yourway.dto.response.LastMessageResponseDto; +import com.chat.yourway.dto.response.notification.LastMessageResponseDto; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.model.event.EventType; import java.util.List; diff --git a/src/main/java/com/chat/yourway/service/interfaces/NotificationService.java b/src/main/java/com/chat/yourway/service/interfaces/NotificationService.java index b549bc63..35a09a31 100644 --- a/src/main/java/com/chat/yourway/service/interfaces/NotificationService.java +++ b/src/main/java/com/chat/yourway/service/interfaces/NotificationService.java @@ -1,7 +1,7 @@ package com.chat.yourway.service.interfaces; -import com.chat.yourway.dto.response.MessageNotificationResponseDto; -import com.chat.yourway.dto.response.TopicNotificationResponseDto; +import com.chat.yourway.dto.response.notification.MessageNotificationResponseDto; +import com.chat.yourway.dto.response.notification.TopicNotificationResponseDto; import java.util.List; public interface NotificationService { 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 6d2ab90f..64548ad3 100644 --- a/src/test/java/com/chat/yourway/integration/controller/ChatControllerTest.java +++ b/src/test/java/com/chat/yourway/integration/controller/ChatControllerTest.java @@ -11,8 +11,8 @@ 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.notification.LastMessageResponseDto; +import com.chat.yourway.dto.response.notification.MessageNotificationResponseDto; import com.chat.yourway.dto.response.MessageResponseDto; import com.chat.yourway.integration.controller.websocketclient.TestStompFrameHandler; import com.chat.yourway.integration.extension.PostgresExtension; 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 38685b4a..d30119fd 100644 --- a/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java +++ b/src/test/java/com/chat/yourway/unit/listener/StompSubscriptionListenerTest.java @@ -13,7 +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.dto.response.notification.LastMessageResponseDto; import com.chat.yourway.listener.StompSubscriptionListener; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.service.interfaces.ChatNotificationService; 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 7c263f01..9ef0b2ff 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,7 +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.dto.response.notification.LastMessageResponseDto; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.model.event.EventType; import com.chat.yourway.repository.ContactEventRedisRepository; 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 ab82a331..4522a7e0 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 @@ -8,8 +8,8 @@ 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.dto.response.notification.LastMessageResponseDto; +import com.chat.yourway.dto.response.notification.MessageNotificationResponseDto; import com.chat.yourway.mapper.NotificationMapper; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.service.NotificationServiceImpl; From 397e68f6ae3544b026023ff993578217ee8ac5cf Mon Sep 17 00:00:00 2001 From: Dmytro Trotsenko Date: Sun, 5 May 2024 15:09:50 +0300 Subject: [PATCH 6/9] feat: create TypingController and added getTypingEvent --- .../yourway/controller/TypingController.java | 21 ++++++++++++ .../TopicNotificationResponseDto.java | 3 +- .../notification/TypingEventResponseDto.java | 20 ++++++++++++ .../listener/StompSubscriptionListener.java | 8 +++-- .../yourway/mapper/NotificationMapper.java | 1 + .../yourway/model/event/ContactEvent.java | 5 ++- .../service/ChatTypingEventServiceImpl.java | 32 +++++++++++++++++++ .../service/ContactEventServiceImpl.java | 21 +++++++++++- .../interfaces/ChatTypingEventService.java | 13 ++++++++ .../interfaces/ContactEventService.java | 15 +++++++-- 10 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/chat/yourway/controller/TypingController.java rename src/main/java/com/chat/yourway/dto/response/{ => notification}/TopicNotificationResponseDto.java (78%) create mode 100644 src/main/java/com/chat/yourway/dto/response/notification/TypingEventResponseDto.java create mode 100644 src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java create mode 100644 src/main/java/com/chat/yourway/service/interfaces/ChatTypingEventService.java diff --git a/src/main/java/com/chat/yourway/controller/TypingController.java b/src/main/java/com/chat/yourway/controller/TypingController.java new file mode 100644 index 00000000..fbb846f7 --- /dev/null +++ b/src/main/java/com/chat/yourway/controller/TypingController.java @@ -0,0 +1,21 @@ +package com.chat.yourway.controller; + +import com.chat.yourway.service.interfaces.ChatTypingEventService; +import java.security.Principal; +import lombok.RequiredArgsConstructor; +import org.springframework.messaging.handler.annotation.DestinationVariable; +import org.springframework.messaging.handler.annotation.MessageMapping; +import org.springframework.stereotype.Controller; + +@Controller +@RequiredArgsConstructor +public class TypingController { + + private final ChatTypingEventService chatTypingEventService; + + @MessageMapping("/typing/{isTyping}") + public void getTypingEvent(@DestinationVariable boolean isTyping, Principal principal) { + String email = principal.getName(); + chatTypingEventService.getTypingEvent(isTyping, email); + } +} diff --git a/src/main/java/com/chat/yourway/dto/response/TopicNotificationResponseDto.java b/src/main/java/com/chat/yourway/dto/response/notification/TopicNotificationResponseDto.java similarity index 78% rename from src/main/java/com/chat/yourway/dto/response/TopicNotificationResponseDto.java rename to src/main/java/com/chat/yourway/dto/response/notification/TopicNotificationResponseDto.java index 744604f4..a9d79e97 100644 --- a/src/main/java/com/chat/yourway/dto/response/TopicNotificationResponseDto.java +++ b/src/main/java/com/chat/yourway/dto/response/notification/TopicNotificationResponseDto.java @@ -1,4 +1,4 @@ -package com.chat.yourway.dto.response; +package com.chat.yourway.dto.response.notification; import lombok.AllArgsConstructor; import lombok.Getter; @@ -19,5 +19,6 @@ public class TopicNotificationResponseDto { private LastMessageResponseDto lastMessage; + private TypingEventResponseDto typingEvent; } diff --git a/src/main/java/com/chat/yourway/dto/response/notification/TypingEventResponseDto.java b/src/main/java/com/chat/yourway/dto/response/notification/TypingEventResponseDto.java new file mode 100644 index 00000000..d08919c4 --- /dev/null +++ b/src/main/java/com/chat/yourway/dto/response/notification/TypingEventResponseDto.java @@ -0,0 +1,20 @@ +package com.chat.yourway.dto.response.notification; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; + +@AllArgsConstructor +@NoArgsConstructor +@Setter +@Getter +@ToString +public class TypingEventResponseDto { + + private String email; + + private boolean isTyping; + +} diff --git a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java index 2e9e9edf..00c7c9fa 100644 --- a/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java +++ b/src/main/java/com/chat/yourway/listener/StompSubscriptionListener.java @@ -5,6 +5,7 @@ import com.chat.yourway.config.websocket.WebsocketProperties; import com.chat.yourway.dto.response.notification.LastMessageResponseDto; +import com.chat.yourway.dto.response.notification.TypingEventResponseDto; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.service.interfaces.ChatNotificationService; import com.chat.yourway.service.interfaces.ContactEventService; @@ -31,6 +32,7 @@ public class StompSubscriptionListener { private final ChatNotificationService chatNotificationService; private static LastMessageResponseDto lastMessageDto; + private static TypingEventResponseDto typingEvent; private static final String USER_DESTINATION = "/user"; private static final String TOPICS_DESTINATION = "/topics"; private static final String SLASH = "/"; @@ -46,9 +48,11 @@ public void handleWebSocketSubscribeListener(SessionSubscribeEvent event) { .getLastMessage(); int unreadMessages = contactEventService.getByTopicIdAndEmail(getTopicId(event), email) .getUnreadMessages(); + typingEvent = contactEventService.getByTopicIdAndEmail( + getTopicId(event), email).getTypingEvent(); var contactEvent = new ContactEvent(email, getTopicId(event), SUBSCRIBED, - getTimestamp(event), unreadMessages, lastMessageDto); + getTimestamp(event), unreadMessages, lastMessageDto, typingEvent); contactEventService.updateEventTypeByEmail(ONLINE, email); contactEventService.save(contactEvent); } @@ -75,7 +79,7 @@ public void handleWebSocketUnsubscribeListener(SessionUnsubscribeEvent event) { try { if (isTopicDestination(destination)) { var contactEvent = new ContactEvent(email, getTopicId(event), ONLINE, - getTimestamp(event), 0, lastMessageDto); + getTimestamp(event), 0, lastMessageDto, typingEvent); contactEventService.save(contactEvent); } diff --git a/src/main/java/com/chat/yourway/mapper/NotificationMapper.java b/src/main/java/com/chat/yourway/mapper/NotificationMapper.java index 81bf5a04..cb3738b6 100644 --- a/src/main/java/com/chat/yourway/mapper/NotificationMapper.java +++ b/src/main/java/com/chat/yourway/mapper/NotificationMapper.java @@ -17,6 +17,7 @@ public interface NotificationMapper { @Mapping(target = "topicId", source = "topicId") @Mapping(target = "unreadMessages", source = "unreadMessages") @Mapping(target = "lastMessage", source = "lastMessage") + @Mapping(target = "typingEvent", source = "typingEvent") TopicNotificationResponseDto toTopicNotificationResponseDto(ContactEvent event); } 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 ffaa7b00..f9e301ab 100644 --- a/src/main/java/com/chat/yourway/model/event/ContactEvent.java +++ b/src/main/java/com/chat/yourway/model/event/ContactEvent.java @@ -1,6 +1,7 @@ package com.chat.yourway.model.event; import com.chat.yourway.dto.response.notification.LastMessageResponseDto; +import com.chat.yourway.dto.response.notification.TypingEventResponseDto; import java.time.LocalDateTime; import lombok.Getter; import lombok.NoArgsConstructor; @@ -28,10 +29,11 @@ public class ContactEvent { private LocalDateTime timestamp; private int unreadMessages; private LastMessageResponseDto lastMessage; + private TypingEventResponseDto typingEvent; public ContactEvent(String email, Integer topicId, EventType eventType, LocalDateTime timestamp, - int unreadMessages, LastMessageResponseDto lastMessage) { + int unreadMessages, LastMessageResponseDto lastMessage, TypingEventResponseDto typingEvent) { this.id = email + "_" + topicId; this.email = email; this.topicId = topicId; @@ -39,6 +41,7 @@ public ContactEvent(String email, Integer topicId, EventType eventType, LocalDat this.timestamp = timestamp; this.unreadMessages = unreadMessages; this.lastMessage = lastMessage; + this.typingEvent = typingEvent; } } diff --git a/src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java b/src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java new file mode 100644 index 00000000..20eed85c --- /dev/null +++ b/src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java @@ -0,0 +1,32 @@ +package com.chat.yourway.service; + +import com.chat.yourway.model.event.EventType; +import com.chat.yourway.service.interfaces.ChatNotificationService; +import com.chat.yourway.service.interfaces.ChatTypingEventService; +import com.chat.yourway.service.interfaces.ContactEventService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Slf4j +public class ChatTypingEventServiceImpl implements ChatTypingEventService { + + private final ContactEventService contactEventService; + private final ChatNotificationService chatNotificationService; + + @Override + public void getTypingEvent(Boolean isTyping, String email) { + log.info("Start getTypingEvent isTyping={}, email={}", isTyping, email); + contactEventService.updateTypingEvent(email, isTyping); + + Integer topicId = contactEventService.getAllByEmail(email).stream() + .filter(e -> e.getEventType().equals(EventType.SUBSCRIBED)) + .findFirst() + .orElseThrow() + .getTopicId(); + + chatNotificationService.updateNotificationForAllWhoSubscribedToTopic(topicId); + } +} diff --git a/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java b/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java index 4e36de92..bd06a6fa 100644 --- a/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java @@ -4,6 +4,7 @@ import static com.chat.yourway.model.event.EventType.SUBSCRIBED; import com.chat.yourway.dto.response.notification.LastMessageResponseDto; +import com.chat.yourway.dto.response.notification.TypingEventResponseDto; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.model.event.EventType; import com.chat.yourway.repository.ContactEventRedisRepository; @@ -32,7 +33,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(), 0, null)); + .orElse(new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), 0, null, null)); } @Override @@ -81,4 +82,22 @@ public void updateMessageInfoForAllTopicSubscribers(Integer topicId, contactEventRedisRepository.saveAll(events); } + @Override + public void updateTypingEvent(String email, boolean isTyping) { + + Integer topicId = getAllByEmail(email).stream() + .filter(e -> e.getEventType().equals(EventType.SUBSCRIBED)) + .findFirst() + .orElseThrow() + .getTopicId(); + + getAllByTopicId(topicId) + .forEach(event -> { + event.setTypingEvent(new TypingEventResponseDto(email, isTyping)); + contactEventRedisRepository.save(event); + }); + + + } + } diff --git a/src/main/java/com/chat/yourway/service/interfaces/ChatTypingEventService.java b/src/main/java/com/chat/yourway/service/interfaces/ChatTypingEventService.java new file mode 100644 index 00000000..dd3fbff3 --- /dev/null +++ b/src/main/java/com/chat/yourway/service/interfaces/ChatTypingEventService.java @@ -0,0 +1,13 @@ +package com.chat.yourway.service.interfaces; + +public interface ChatTypingEventService { + + /** + * Get typing event by user email. + * + * @param isTyping typing status. + * @param email user email. + */ + void getTypingEvent(Boolean isTyping, String email); + +} 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 f9c3b5a0..63442e0e 100644 --- a/src/main/java/com/chat/yourway/service/interfaces/ContactEventService.java +++ b/src/main/java/com/chat/yourway/service/interfaces/ContactEventService.java @@ -18,7 +18,7 @@ public interface ContactEventService { * Get contact event by topic id and contact email. * * @param topicId topic id. - * @param email contact email. + * @param email contact email. * @return contact event. */ ContactEvent getByTopicIdAndEmail(Integer topicId, String email); @@ -44,9 +44,18 @@ public interface ContactEventService { /** * Set last message to all topic subscribers events. * - * @param topicId topic id. + * @param topicId topic id. * @param lastMessageDto last message Dto. */ - void updateMessageInfoForAllTopicSubscribers(Integer topicId, LastMessageResponseDto lastMessageDto); + void updateMessageInfoForAllTopicSubscribers(Integer topicId, + LastMessageResponseDto lastMessageDto); + + /** + * Update typing status. + * + * @param email user email. + * @param isTyping typing status. + */ + void updateTypingEvent(String email, boolean isTyping); } From e6c11397d23cda55c68af2288c46c38c13f88605 Mon Sep 17 00:00:00 2001 From: Dmytro Trotsenko Date: Sun, 5 May 2024 15:12:04 +0300 Subject: [PATCH 7/9] feat: added sendTyping function --- src/main/resources/static/index.html | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html index 3548cb92..74316891 100644 --- a/src/main/resources/static/index.html +++ b/src/main/resources/static/index.html @@ -114,7 +114,12 @@

WebSocket Chat Test Client

const sendToPublicTopicDest = "/app/topic/public/"; const sendToPrivateTopicDest = "/app/topic/private/"; const getHistoryTopicDest = "/app/history/topic/"; + const sendIsTypingDest = "/app/typing" + const tokenType = 'Bearer'; + let typingTimer; + let isTyping = false; + let socket = null; let stompClient = null; @@ -225,6 +230,24 @@

WebSocket Chat Test Client

stompClient.send(getHistoryTopicDest + topicId, {}, request); response.innerText += ('<<< ' + request + '\n'); } + + function sendTyping(isTyping) { + stompClient.send(sendIsTypingDest + '/' + isTyping, {}); + } + + document.addEventListener('keydown', function() { + if (!isTyping) { + isTyping = true; + sendTyping(isTyping); + } + + clearTimeout(typingTimer); + typingTimer = setTimeout(function() { + isTyping = false; + sendTyping(isTyping); + }, 2000); + }); + \ No newline at end of file From 1d392987830ddf1e6b91948e17b36b22f5e371e4 Mon Sep 17 00:00:00 2001 From: Dmytro Trotsenko Date: Sun, 5 May 2024 15:14:05 +0300 Subject: [PATCH 8/9] fix: added typingEventDto and fixed tests --- .../controller/ChatControllerTest.java | 7 +++++-- .../impl/ContactEventServiceImplTest.java | 20 +++++++++---------- .../impl/NotificationServiceImplTest.java | 8 ++++++-- 3 files changed, 21 insertions(+), 14 deletions(-) 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 64548ad3..1131c438 100644 --- a/src/test/java/com/chat/yourway/integration/controller/ChatControllerTest.java +++ b/src/test/java/com/chat/yourway/integration/controller/ChatControllerTest.java @@ -14,6 +14,7 @@ import com.chat.yourway.dto.response.notification.LastMessageResponseDto; import com.chat.yourway.dto.response.notification.MessageNotificationResponseDto; import com.chat.yourway.dto.response.MessageResponseDto; +import com.chat.yourway.dto.response.notification.TypingEventResponseDto; import com.chat.yourway.integration.controller.websocketclient.TestStompFrameHandler; import com.chat.yourway.integration.extension.PostgresExtension; import com.chat.yourway.integration.extension.RedisExtension; @@ -201,9 +202,10 @@ void notifyTopicSubscribers_shouldNotifyTopicSubscribersIfSubscribeEvent() { lastMessageDto.setTimestamp(LocalDateTime.now()); lastMessageDto.setSentFrom("vasil@gmail.com"); lastMessageDto.setLastMessage("Hi"); + var typingEventDto = new TypingEventResponseDto("vasil@gmail.com", true); var event = new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, - lastMessageDto); + lastMessageDto, typingEventDto); saveContactEvent(event); //Stored subscription results for testing CompletableFuture resultKeeper = new CompletableFuture<>(); @@ -232,9 +234,10 @@ void notifyTopicSubscribers_shouldNotifyTopicSubscribersIfAnyContactSubscribedTo lastMessageDto.setTimestamp(LocalDateTime.now()); lastMessageDto.setSentFrom("vasil@gmail.com"); lastMessageDto.setLastMessage("Hi"); + var typingEventDto = new TypingEventResponseDto("vasil@gmail.com", true); var event = new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, - lastMessageDto); + lastMessageDto, typingEventDto); saveContactEvent(event); //Stored subscription results for testing CompletableFuture resultKeeper = new CompletableFuture<>(); 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 9ef0b2ff..0fcdb8d8 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 @@ -42,7 +42,7 @@ void save_shouldSaveToRepository() { String email = "vasil@gmail.com"; int topicId = 1; ContactEvent contactEvent = new ContactEvent(email, topicId, ONLINE, LocalDateTime.now(), 0, - null); + null, null); // When contactEventService.save(contactEvent); @@ -58,7 +58,7 @@ void getByTopicIdAndEmail_shouldReturnContactEventFromRepository() { Integer topicId = 1; String email = "vasil@gmail.com"; ContactEvent expectedContactEvent = new ContactEvent(email, topicId, ONLINE, - LocalDateTime.now(), 0, null); + LocalDateTime.now(), 0, null, null); when(contactEventRedisRepository.findById(email + "_" + topicId)) .thenReturn(Optional.of(expectedContactEvent)); @@ -93,8 +93,8 @@ void getAllByEmail_shouldReturnContactEventsFromRepository() { // Given String email = "vasil@gmail.com"; List expectedContactEvents = Arrays.asList( - new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), 0, null), - new ContactEvent(email, 2, OFFLINE, LocalDateTime.now(), 0, null) + new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), 0, null, null), + new ContactEvent(email, 2, OFFLINE, LocalDateTime.now(), 0, null, null) ); when(contactEventRedisRepository.findAllByEmail(email)).thenReturn(expectedContactEvents); @@ -112,8 +112,8 @@ void updateEventTypeByEmail_shouldUpdateEventTypesInRepository() { String email = "vasil@gmail.com"; EventType newEventType = OFFLINE; List events = Arrays.asList( - new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), 0, null), - new ContactEvent(email, 2, ONLINE, LocalDateTime.now(), 0, null) + new ContactEvent(email, 1, ONLINE, LocalDateTime.now(), 0, null, null), + new ContactEvent(email, 2, ONLINE, LocalDateTime.now(), 0, null, null) ); when(contactEventRedisRepository.findAllByEmail(email)).thenReturn(events); @@ -131,8 +131,8 @@ void getAllByTopicId_shouldReturnContactEventsFromRepository() { // Given Integer topicId = 1; List expectedContactEvents = Arrays.asList( - new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, null), - new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), 0, null) + new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, null, null), + new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), 0, null, null) ); when(contactEventRedisRepository.findAllByTopicId(topicId)).thenReturn(expectedContactEvents); @@ -154,8 +154,8 @@ void setLastMessageToAllTopicSubscribers_shouldUpdateLastMessagesInRepository() lastMessageDto.setLastMessage("New message"); List events = Arrays.asList( - new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, null), - new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), 0, null) + new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, null, null), + new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), 0, null, null) ); when(contactEventRedisRepository.findAllByTopicId(topicId)).thenReturn(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 4522a7e0..3f10141f 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 com.chat.yourway.dto.response.notification.LastMessageResponseDto; import com.chat.yourway.dto.response.notification.MessageNotificationResponseDto; +import com.chat.yourway.dto.response.notification.TypingEventResponseDto; import com.chat.yourway.mapper.NotificationMapper; import com.chat.yourway.model.event.ContactEvent; import com.chat.yourway.service.NotificationServiceImpl; @@ -45,10 +46,13 @@ void notifyTopicSubscribers_shouldReturnListOfNotifications() { lastMessageDto.setTimestamp(LocalDateTime.now()); lastMessageDto.setSentFrom("vasil@gmail.com"); lastMessageDto.setLastMessage("Hello"); + var typingEventDto = new TypingEventResponseDto("vasil@gmail.com", true); List events = Arrays.asList( - new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, lastMessageDto), - new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), 0, lastMessageDto) + new ContactEvent("vasil@gmail.com", topicId, ONLINE, LocalDateTime.now(), 0, lastMessageDto, + typingEventDto), + new ContactEvent("anton@gmail.com", topicId, OFFLINE, LocalDateTime.now(), 0, + lastMessageDto, typingEventDto) ); var expectedNotifications = events.stream() From 919edd88d10d1ae2b75cda9bedd17b852f0fe7a8 Mon Sep 17 00:00:00 2001 From: Dmytro Trotsenko Date: Mon, 6 May 2024 20:22:50 +0300 Subject: [PATCH 9/9] fix: renamed getTypingEvent -> updateTypingEvent --- .../java/com/chat/yourway/controller/TypingController.java | 4 ++-- .../com/chat/yourway/service/ChatTypingEventServiceImpl.java | 4 ++-- .../yourway/service/interfaces/ChatTypingEventService.java | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/chat/yourway/controller/TypingController.java b/src/main/java/com/chat/yourway/controller/TypingController.java index fbb846f7..abcfd8b7 100644 --- a/src/main/java/com/chat/yourway/controller/TypingController.java +++ b/src/main/java/com/chat/yourway/controller/TypingController.java @@ -14,8 +14,8 @@ public class TypingController { private final ChatTypingEventService chatTypingEventService; @MessageMapping("/typing/{isTyping}") - public void getTypingEvent(@DestinationVariable boolean isTyping, Principal principal) { + public void updateTypingEvent(@DestinationVariable boolean isTyping, Principal principal) { String email = principal.getName(); - chatTypingEventService.getTypingEvent(isTyping, email); + chatTypingEventService.updateTypingEvent(isTyping, email); } } diff --git a/src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java b/src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java index 20eed85c..0270aa8c 100644 --- a/src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java @@ -17,8 +17,8 @@ public class ChatTypingEventServiceImpl implements ChatTypingEventService { private final ChatNotificationService chatNotificationService; @Override - public void getTypingEvent(Boolean isTyping, String email) { - log.info("Start getTypingEvent isTyping={}, email={}", isTyping, email); + public void updateTypingEvent(Boolean isTyping, String email) { + log.info("Start updateTypingEvent isTyping={}, email={}", isTyping, email); contactEventService.updateTypingEvent(email, isTyping); Integer topicId = contactEventService.getAllByEmail(email).stream() diff --git a/src/main/java/com/chat/yourway/service/interfaces/ChatTypingEventService.java b/src/main/java/com/chat/yourway/service/interfaces/ChatTypingEventService.java index dd3fbff3..66e84118 100644 --- a/src/main/java/com/chat/yourway/service/interfaces/ChatTypingEventService.java +++ b/src/main/java/com/chat/yourway/service/interfaces/ChatTypingEventService.java @@ -3,11 +3,11 @@ public interface ChatTypingEventService { /** - * Get typing event by user email. + * Update typing event by user email. * * @param isTyping typing status. * @param email user email. */ - void getTypingEvent(Boolean isTyping, String email); + void updateTypingEvent(Boolean isTyping, String email); }