Skip to content

Commit

Permalink
Merge pull request #82 from Chat-Your-Way/bug-fix-update-typing-statu…
Browse files Browse the repository at this point in the history
…s-when-user-disconected

Bug fix update typing status when user disconnected
  • Loading branch information
LionnoiL authored May 20, 2024
2 parents 3066f90 + d7d7679 commit 915617d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,6 +26,7 @@
public class StompConnectionListenerTest {
@Mock private ContactEventService contactEventService;
@Mock private ChatNotificationService chatNotificationService;
@Mock private ChatTypingEventService chatTypingEventService;
@InjectMocks private StompConnectionListener stompConnectionListener;

@Test
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 915617d

Please sign in to comment.