diff --git a/src/main/java/com/chat/yourway/listener/StompConnectionListener.java b/src/main/java/com/chat/yourway/listener/StompConnectionListener.java index 452d33d6..b8e73fee 100644 --- a/src/main/java/com/chat/yourway/listener/StompConnectionListener.java +++ b/src/main/java/com/chat/yourway/listener/StompConnectionListener.java @@ -4,6 +4,7 @@ import static com.chat.yourway.model.event.EventType.ONLINE; import com.chat.yourway.service.interfaces.ChatNotificationService; +import com.chat.yourway.service.interfaces.ChatTypingEventService; import com.chat.yourway.service.interfaces.ContactEventService; import java.util.Objects; import lombok.RequiredArgsConstructor; @@ -21,6 +22,7 @@ public class StompConnectionListener { private final ContactEventService contactEventService; private final ChatNotificationService chatNotificationService; + private final ChatTypingEventService chatTypingEventService; @EventListener public void handleWebSocketConnectListener(SessionConnectEvent event) { @@ -32,6 +34,7 @@ public void handleWebSocketConnectListener(SessionConnectEvent event) { @EventListener public void handleWebSocketDisconnectListener(SessionDisconnectEvent event) { String email = getEmail(event); + chatTypingEventService.updateTypingEvent(false, email); contactEventService.updateEventTypeByEmail(OFFLINE, email); chatNotificationService.notifyAllWhoSubscribedToSameUserTopic(email); log.info("Contact [{}] is disconnected", email); diff --git a/src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java b/src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java index 0270aa8c..19a34b95 100644 --- a/src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/ChatTypingEventServiceImpl.java @@ -21,12 +21,9 @@ 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() + contactEventService.getAllByEmail(email).stream() .filter(e -> e.getEventType().equals(EventType.SUBSCRIBED)) - .findFirst() - .orElseThrow() - .getTopicId(); - - chatNotificationService.updateNotificationForAllWhoSubscribedToTopic(topicId); + .forEach(e -> chatNotificationService.updateNotificationForAllWhoSubscribedToTopic( + e.getTopicId())); } } diff --git a/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java b/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java index bd06a6fa..d202f63c 100644 --- a/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java +++ b/src/main/java/com/chat/yourway/service/ContactEventServiceImpl.java @@ -84,20 +84,15 @@ public void updateMessageInfoForAllTopicSubscribers(Integer topicId, @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); - }); - - + log.trace("Started updateTypingEvent, email [{}], isTyping [{}]", email, isTyping); + + getAllByEmail(email).stream() + .filter(event -> event.getEventType().equals(SUBSCRIBED)) + .forEach(event -> getAllByTopicId(event.getTopicId()) + .forEach(e -> { + e.setTypingEvent(new TypingEventResponseDto(email, isTyping)); + contactEventRedisRepository.save(e); + })); } } diff --git a/src/test/java/com/chat/yourway/unit/listener/StompConnectionListenerTest.java b/src/test/java/com/chat/yourway/unit/listener/StompConnectionListenerTest.java index 3e020e9c..a8e1042f 100644 --- a/src/test/java/com/chat/yourway/unit/listener/StompConnectionListenerTest.java +++ b/src/test/java/com/chat/yourway/unit/listener/StompConnectionListenerTest.java @@ -6,6 +6,7 @@ import com.chat.yourway.listener.StompConnectionListener; import com.chat.yourway.service.interfaces.ChatNotificationService; +import com.chat.yourway.service.interfaces.ChatTypingEventService; import com.chat.yourway.service.interfaces.ContactEventService; import java.security.Principal; import org.junit.jupiter.api.Test; @@ -25,6 +26,7 @@ public class StompConnectionListenerTest { @Mock private ContactEventService contactEventService; @Mock private ChatNotificationService chatNotificationService; + @Mock private ChatTypingEventService chatTypingEventService; @InjectMocks private StompConnectionListener stompConnectionListener; @Test @@ -54,6 +56,7 @@ public void testHandleWebSocketDisconnectListener() { // Then verify(contactEventService).updateEventTypeByEmail(OFFLINE, email); verify(chatNotificationService).notifyAllWhoSubscribedToSameUserTopic(email); + verify(chatTypingEventService).updateTypingEvent(false, email); } private SessionConnectEvent createConnectEvent(String email, String password) {