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 232 user nickname #78

Closed
wants to merge 15 commits into from
Closed
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 @@ -19,6 +19,9 @@
@Server(
description = "Local ENV",
url = "http://localhost:8080"
),@Server(
description = "Local ENV 2",
url = "http://localhost:5557"
),
@Server(
description = "PROD ENV",
Expand Down
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
Expand Up @@ -19,7 +19,9 @@ public class MessageResponseDto {
private Integer id;
private LocalDateTime timestamp;
private String sentFrom;
private String sentFromNickname;
private String sendTo;
private String sendToNickname;
private String content;

}
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 All @@ -15,6 +15,7 @@ public class LastMessageResponseDto {
private static final int MAX_LENGTH = 20;

private LocalDateTime timestamp;
private String sentFromNickname;
private String sentFrom;
private String lastMessage;

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: 5 additions & 0 deletions src/main/java/com/chat/yourway/mapper/MessageMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import com.chat.yourway.model.Message;
import java.util.List;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "spring")
public interface MessageMapper {

@Mapping(source = "sender.nickname", target = "sentFromNickname")
@Mapping(source = "receiver.nickname", target = "sendToNickname",
defaultExpression = "java(message.getReceiver() != null ? message.getReceiver().getNickname() : null )"
)
MessageResponseDto toResponseDto(Message message);

List<MessageResponseDto> toListResponseDto(List<Message> messages);
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);

}
6 changes: 6 additions & 0 deletions src/main/java/com/chat/yourway/model/Contact.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@ public class Contact implements UserDetails {
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "contact_seq_gen")
@SequenceGenerator(name = "contact_seq_gen", sequenceName = "chat.contact_id_seq", allocationSize = 1)
private Integer id;

@Column(name = "nickname", nullable = false, unique = true)
private String nickname;

@Column(name = "avatar_id", nullable = false)
private Byte avatarId;

@Column(name = "email", nullable = false, unique = true)
private String email;

@Column(name = "password", nullable = false, length = 2048)
private String password;

@Column(name = "is_active", nullable = false)
private Boolean isActive;

@Column(name = "is_private", nullable = false)
private Boolean isPrivate;

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/chat/yourway/model/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,36 @@
@Entity
@Table(schema = "chat", name = "message")
public class Message {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "message_seq_gen")
@SequenceGenerator(name = "message_seq_gen", sequenceName = "chat.message_id_seq", allocationSize = 1)
private Integer id;

@Column(name = "sent_from", nullable = false)
private String sentFrom;

@Column(name = "send_to")
private String sendTo;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "sender_id")
private Contact sender;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "receiver_id")
private Contact receiver;

@Column(name = "content", nullable = false)
private String content;

@Column(name = "timestamp", nullable = false)
private LocalDateTime timestamp;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "topic_id", referencedColumnName = "id", nullable = false)
private Topic topic;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
schema = "chat",
Expand Down
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 Expand Up @@ -72,6 +72,7 @@ private void sendToTopic(Integer topicId, MessageResponseDto messageDto) {
lastMessageDto.setTimestamp(messageDto.getTimestamp());
lastMessageDto.setSentFrom(messageDto.getSentFrom());
lastMessageDto.setLastMessage(messageDto.getContent());
lastMessageDto.setSentFromNickname(messageDto.getSentFromNickname());

contactEventService.updateMessageInfoForAllTopicSubscribers(topicId, lastMessageDto);

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);
}
}
Loading
Loading