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

Chat 229 typing event #75

Merged
merged 5 commits into from
May 6, 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 @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/chat/yourway/controller/TypingController.java
Original file line number Diff line number Diff line change
@@ -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 updateTypingEvent(@DestinationVariable boolean isTyping, Principal principal) {
String email = principal.getName();
chatTypingEventService.updateTypingEvent(isTyping, email);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.chat.yourway.dto.response;
package com.chat.yourway.dto.response.error;

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.chat.yourway.dto.response;
package com.chat.yourway.dto.response.notification;

import java.time.LocalDateTime;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.chat.yourway.dto.response;
package com.chat.yourway.dto.response.notification;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -19,5 +19,6 @@ public class TopicNotificationResponseDto {

private LastMessageResponseDto lastMessage;

private TypingEventResponseDto typingEvent;

}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
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.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;
Expand All @@ -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 = "/";
Expand All @@ -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);
}
Expand All @@ -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);
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/chat/yourway/mapper/NotificationMapper.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);

}
7 changes: 5 additions & 2 deletions src/main/java/com/chat/yourway/model/event/ContactEvent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chat.yourway.model.event;

import com.chat.yourway.dto.response.LastMessageResponseDto;
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;
Expand Down Expand Up @@ -28,17 +29,19 @@ 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;
this.eventType = eventType;
this.timestamp = timestamp;
this.unreadMessages = unreadMessages;
this.lastMessage = lastMessage;
this.typingEvent = typingEvent;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 updateTypingEvent(Boolean isTyping, String email) {
log.info("Start updateTypingEvent 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
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.dto.response.notification.TypingEventResponseDto;
import com.chat.yourway.model.event.ContactEvent;
import com.chat.yourway.model.event.EventType;
import com.chat.yourway.repository.ContactEventRedisRepository;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
});


}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.chat.yourway.service.interfaces;

public interface ChatTypingEventService {

/**
* Update typing event by user email.
*
* @param isTyping typing status.
* @param email user email.
*/
void updateTypingEvent(Boolean isTyping, String email);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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);

}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Loading
Loading