-
Notifications
You must be signed in to change notification settings - Fork 4
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
Ping 통신을 위한 데이터 타입 변경 #774
Open
swonny
wants to merge
24
commits into
develop-be
Choose a base branch
from
refactor/772
base: develop-be
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
58bd98b
refactor: 메시지 전송 로직 분리
swonny 1d2ba16
refactor: PING response 추가
swonny c03ddef
refactor: 메서드 분리
swonny 81f6c2d
test: 실패하는 테스트 수정
swonny 76b9b21
refactor: 채팅 타입 enum 설정
swonny 0d108cb
refactor: 채팅 타입 상수로 분리
swonny 7c2aa4f
test: 채팅 데이터타입 테스트 추가
swonny fc0259e
test: warning 제거
swonny 9aa8fab
style: 개행 추가 및 메서드 순서 정렬
swonny 90fea14
refactor: Ping과 Chat 타입 별 provider 생성
swonny 909d028
refactor: provider명 변경
swonny 2c5a43e
fix: 요구사항에 맞춰 반환하는 변수명 변경
swonny 193c332
refactor: ping handler 메서드 분리
swonny 514e0ac
refactor: ping과 message 전송시 사용되는 data를 Dto로 변환하여 사용하도록 수정
swonny 0e56a7d
test: 사용자 아이디에 해당하는 웹소켓세션 반환 메서드 테스트 추가
swonny b8f6d51
fix: MockitoJunitRunner 활성화
swonny 4247351
refactor: 메시지와 채팅 타입이 대소문자 구분없이 매핑되도록 수정
JJ503 c4bc7ef
test: 웹소켓 메시지 전송시 알림 전송, 메시지 로그 업데이트 이벤트 호출 테스트 추가
swonny 6a0a63f
test: 실패하는 테스트 수정
swonny f20d6b8
test: 불필요한 필드 제거
swonny 84a6a7f
chore: 로그 확인을 위한 알림 전송 임시 로그 추가
swonny c6e8e91
chore: 로그 확인을 위한 메시지 로그 업데이트 임시 로그 추가
swonny ceef7d0
chore: 로그 확인을 위한 알림 전송 임시 로그 수정
swonny 5400886
chore: 로그 확인을 위한 임시 로그 수정
swonny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
backend/ddang/src/main/java/com/ddang/ddang/chat/handler/ChatHandleProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.ddang.ddang.chat.handler; | ||
|
||
import com.ddang.ddang.websocket.handler.dto.ChatMessageType; | ||
import com.ddang.ddang.websocket.handler.dto.SendMessageDto; | ||
import com.ddang.ddang.websocket.handler.dto.SessionAttributeDto; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface ChatHandleProvider { | ||
|
||
List<SendMessageDto> createResponse( | ||
final SessionAttributeDto sessionAttributeDto, | ||
final Map<String, String> data | ||
) throws JsonProcessingException; | ||
|
||
ChatMessageType supportsChatType(); | ||
} |
23 changes: 23 additions & 0 deletions
23
...end/ddang/src/main/java/com/ddang/ddang/chat/handler/ChatHandleTypeProviderComposite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.ddang.ddang.chat.handler; | ||
|
||
import com.ddang.ddang.websocket.handler.dto.ChatMessageType; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
public class ChatHandleTypeProviderComposite { | ||
|
||
private final Map<ChatMessageType, ChatHandleProvider> mappings; | ||
|
||
public ChatHandleTypeProviderComposite(final Set<ChatHandleProvider> providers) { | ||
this.mappings = providers.stream() | ||
.collect(Collectors.toMap(ChatHandleProvider::supportsChatType, provider -> provider)); | ||
} | ||
|
||
public ChatHandleProvider findProvider(final ChatMessageType chatMessageType) { | ||
return mappings.get(chatMessageType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
backend/ddang/src/main/java/com/ddang/ddang/chat/handler/MessageTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package com.ddang.ddang.chat.handler; | ||
|
||
import com.ddang.ddang.chat.application.MessageService; | ||
import com.ddang.ddang.chat.application.dto.CreateMessageDto; | ||
import com.ddang.ddang.chat.application.event.MessageNotificationEvent; | ||
import com.ddang.ddang.chat.application.event.UpdateReadMessageLogEvent; | ||
import com.ddang.ddang.chat.domain.Message; | ||
import com.ddang.ddang.chat.domain.WebSocketChatSessions; | ||
import com.ddang.ddang.chat.domain.WebSocketSessions; | ||
import com.ddang.ddang.chat.handler.dto.ChatMessageDataDto; | ||
import com.ddang.ddang.chat.handler.dto.MessageDataDto; | ||
import com.ddang.ddang.chat.handler.dto.MessageDto; | ||
import com.ddang.ddang.chat.handler.dto.SendChatResponse; | ||
import com.ddang.ddang.chat.handler.dto.SendMessageStatus; | ||
import com.ddang.ddang.chat.presentation.dto.request.CreateMessageRequest; | ||
import com.ddang.ddang.websocket.handler.dto.ChatMessageType; | ||
import com.ddang.ddang.websocket.handler.dto.SendMessageDto; | ||
import com.ddang.ddang.websocket.handler.dto.SessionAttributeDto; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.socket.TextMessage; | ||
import org.springframework.web.socket.WebSocketSession; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MessageTypeHandler implements ChatHandleProvider { | ||
|
||
private final WebSocketChatSessions sessions; | ||
private final ObjectMapper objectMapper; | ||
private final MessageService messageService; | ||
private final ApplicationEventPublisher messageLogEventPublisher; | ||
private final ApplicationEventPublisher messageNotificationEventPublisher; | ||
|
||
@Override | ||
public ChatMessageType supportsChatType() { | ||
return ChatMessageType.MESSAGE; | ||
} | ||
|
||
@Override | ||
public List<SendMessageDto> createResponse( | ||
final SessionAttributeDto sessionAttributeDto, | ||
final Map<String, String> data | ||
) throws JsonProcessingException { | ||
final Long writerId = sessionAttributeDto.userId(); | ||
final MessageDataDto messageDataDto = MessageDataDto.from(data); | ||
|
||
final Message message = createMessage(data, writerId); | ||
|
||
sendNotificationIfReceiverNotInSession(message, sessionAttributeDto); | ||
|
||
return createSendMessages(message, writerId, messageDataDto.chatRoomId()); | ||
} | ||
|
||
private Message createMessage(final Map<String, String> data, final long writerId) { | ||
final ChatMessageDataDto messageData = objectMapper.convertValue(data, ChatMessageDataDto.class); | ||
final CreateMessageDto createMessageDto = createMessageDto(messageData, writerId); | ||
|
||
return messageService.create(createMessageDto); | ||
} | ||
|
||
private void sendNotificationIfReceiverNotInSession( | ||
final Message message, | ||
final SessionAttributeDto sessionAttribute | ||
) { | ||
if (!sessions.containsByUserId(message.getChatRoom().getId(), message.getReceiver().getId())) { | ||
final String profileImageAbsoluteUrl = String.valueOf(sessionAttribute.baseUrl()); | ||
messageNotificationEventPublisher.publishEvent(new MessageNotificationEvent( | ||
message, | ||
profileImageAbsoluteUrl | ||
)); | ||
} | ||
} | ||
|
||
private List<SendMessageDto> createSendMessages( | ||
final Message message, | ||
final Long writerId, | ||
final Long chatRoomId | ||
) throws JsonProcessingException { | ||
final WebSocketSessions groupSessions = sessions.findSessionsByChatRoomId(message.getChatRoom().getId()); | ||
|
||
final List<SendMessageDto> sendMessageDtos = new ArrayList<>(); | ||
for (final WebSocketSession currentSession : groupSessions.getSessions()) { | ||
final MessageDto messageDto = MessageDto.of(message, isMyMessage(currentSession, writerId)); | ||
final TextMessage textMessage = createTextMessage(messageDto); | ||
sendMessageDtos.add(new SendMessageDto(currentSession, textMessage)); | ||
updateReadMessageLog(currentSession, chatRoomId, message); | ||
} | ||
|
||
return sendMessageDtos; | ||
} | ||
|
||
private void updateReadMessageLog( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 필수해당 메서드가 |
||
final WebSocketSession currentSession, | ||
final Long chatRoomId, | ||
final Message message | ||
) { | ||
final SessionAttributeDto sessionAttributes = convertToSessionAttributeDto(currentSession); | ||
final UpdateReadMessageLogEvent updateReadMessageLogEvent = new UpdateReadMessageLogEvent( | ||
sessionAttributes.userId(), | ||
chatRoomId, | ||
message.getId() | ||
); | ||
messageLogEventPublisher.publishEvent(updateReadMessageLogEvent); | ||
} | ||
|
||
private SessionAttributeDto convertToSessionAttributeDto(final WebSocketSession session) { | ||
final Map<String, Object> attributes = session.getAttributes(); | ||
|
||
return objectMapper.convertValue(attributes, SessionAttributeDto.class); | ||
} | ||
|
||
private CreateMessageDto createMessageDto(final ChatMessageDataDto messageData, final Long userId) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 필수
|
||
final CreateMessageRequest request = new CreateMessageRequest(messageData.receiverId(), messageData.contents()); | ||
|
||
return CreateMessageDto.of(userId, messageData.chatRoomId(), request); | ||
} | ||
|
||
private boolean isMyMessage( | ||
final WebSocketSession session, | ||
final Long writerId | ||
) { | ||
final long userId = Long.parseLong(String.valueOf(session.getAttributes().get("userId"))); | ||
|
||
return writerId.equals(userId); | ||
} | ||
|
||
private TextMessage createTextMessage(final MessageDto messageDto) throws JsonProcessingException { | ||
final SendChatResponse sendChatResponse = new SendChatResponse(SendMessageStatus.SUCCESS, List.of(messageDto)); | ||
|
||
return new TextMessage(objectMapper.writeValueAsString(sendChatResponse)); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
질문
인메모리기에 db와 동일하게
get
대신find
로 변경해 주신 게 맞을까요??