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

Develop #70

Merged
merged 2 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ public class WebsocketProperties {
private String topicPrefix;
private String notifyPrefix;
private String errorPrefix;
private int TimeToFirstMessage;

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/chat/yourway/model/event/EventType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
public enum EventType {
ONLINE,
OFFLINE,
SUBSCRIBED,
UNSUBSCRIBED
SUBSCRIBED
}
5 changes: 3 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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}

Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading