Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge develop in to master and delete develop branch #83

Merged
merged 7 commits into from
May 25, 2024
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 @@ -9,6 +9,7 @@
import com.chat.yourway.service.interfaces.ChatMessageService;
import com.chat.yourway.service.interfaces.ChatNotificationService;
import com.chat.yourway.service.interfaces.ContactEventService;
import com.chat.yourway.service.interfaces.ContactService;
import com.chat.yourway.service.interfaces.MessageService;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,13 +28,15 @@ public class ChatMessageServiceImpl implements ChatMessageService {
private final MessageService messageService;
private final ContactEventService contactEventService;
private final ChatNotificationService chatNotificationService;
private final ContactService contactService;

@Transactional
@Override
public MessageResponseDto sendToPublicTopic(Integer topicId, MessagePublicRequestDto message,
String email) {
log.trace("Started contact email: [{}] sendToTopic id: [{}]", email, topicId);
MessageResponseDto messageResponseDto = messageService.createPublic(topicId, message, email);
messageResponseDto.setSentFrom(contactService.findByEmail(email).getNickname());

sendToTopic(topicId, messageResponseDto);

Expand All @@ -49,6 +52,10 @@ public MessageResponseDto sendToPrivateTopic(Integer topicId, MessagePrivateRequ
log.trace("Started contact email: [{}] sendToPrivateTopic email: [{}]", email, sendTo);
MessageResponseDto messageResponseDto = messageService.createPrivate(message, email);

messageResponseDto.setSentFrom(contactService.findByEmail(email).getNickname());
messageResponseDto.setSendTo(
contactService.findByEmail(messageResponseDto.getSendTo()).getNickname());

sendToTopic(topicId, messageResponseDto);

log.trace("Contact [{}] sent message to [{}]", email, sendTo);
Expand All @@ -60,7 +67,15 @@ public List<MessageResponseDto> sendMessageHistoryByTopicId(Integer topicId,
PageRequestDto pageRequestDto, String email) {
log.trace("Started sendMessageHistoryByTopicId = [{}]", topicId);

List<MessageResponseDto> messages = messageService.findAllByTopicId(topicId, pageRequestDto);
List<MessageResponseDto> messages = messageService.findAllByTopicId(topicId, pageRequestDto)
.stream()
.peek(m -> {
m.setSentFrom(contactService.findByEmail(m.getSentFrom()).getNickname());
if (isPrivate(m)) {
m.setSendTo(contactService.findByEmail(m.getSendTo()).getNickname());
}
})
.toList();
simpMessagingTemplate.convertAndSendToUser(email, toTopicDestination(topicId), messages);

log.trace("Message history was sent to topicId = [{}]", topicId);
Expand All @@ -85,4 +100,8 @@ private String toTopicDestination(Integer topicId) {
return properties.getTopicPrefix() + "/" + topicId;
}

private boolean isPrivate(MessageResponseDto message) {
return message.getSendTo().contains("@");
}

}
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 @@ -125,7 +125,7 @@ void sendToTopic_shouldReturnCorrectSendToPublicTopicAndReceivedMessageFromTopic
MessageResponseDto messageResponseDto = resultKeeper.get(3, SECONDS);
assertThat(messageResponseDto).isNotNull();
assertThat(messageResponseDto.getId()).isNotNull();
assertThat(messageResponseDto.getSentFrom()).isEqualTo("[email protected]");
assertThat(messageResponseDto.getSentFrom()).isEqualTo("Vasil");
assertThat(messageResponseDto.getSendTo()).isEqualTo("Topic id=" + topicId);
assertThat(messageResponseDto.getTimestamp()).isNotNull();
assertThat(messageResponseDto.getContent()).isEqualTo(messageRequestDto.getContent());
Expand Down Expand Up @@ -155,8 +155,8 @@ void sendToContact_shouldReturnCorrectReceivedPrivateMessageFromSelfToSelf() {
MessageResponseDto messageResponseDto = resultKeeper.get(3, SECONDS);
assertThat(messageResponseDto).isNotNull();
assertThat(messageResponseDto.getId()).isNotNull();
assertThat(messageResponseDto.getSentFrom()).isEqualTo("[email protected]");
assertThat(messageResponseDto.getSendTo()).isEqualTo("[email protected]");
assertThat(messageResponseDto.getSentFrom()).isEqualTo("Vasil");
assertThat(messageResponseDto.getSendTo()).isEqualTo("Vasil");
assertThat(messageResponseDto.getTimestamp()).isNotNull();
assertThat(messageResponseDto.getContent()).isEqualTo(messageRequestDto.getContent());
}
Expand Down Expand Up @@ -185,9 +185,9 @@ void getMessages_shouldReturnReceivedMessagesHistoryFromTopic() {
assertThat(messageResponseDtos).extracting("id").isNotNull();
assertThat(messageResponseDtos).extracting("timestamp").isNotNull();
assertThat(messageResponseDtos).extracting("sentFrom")
.contains("[email protected]", "[email protected]");
.contains("Anton", "Vasil");
assertThat(messageResponseDtos).extracting("sendTo")
.contains("[email protected]", "[email protected]");
.contains("Vasil", "Anton");
assertThat(messageResponseDtos).extracting("content")
.contains("hello Vasil!", "hello Anton!");
}
Expand Down
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
Loading