From 092e7aab20a070ebde3f94613e5622170e364904 Mon Sep 17 00:00:00 2001 From: JaeSeo Yang <96044622+psychology50@users.noreply.github.com> Date: Tue, 7 Jan 2025 15:42:49 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=94=A7=20The=20server=20sends=20a?= =?UTF-8?q?=20success=20message=20after=20a=20chat=20message=20is=20saved?= =?UTF-8?q?=20successfully=20(#214)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: add sender-name & message-id-header field in the send-message-command * fix: success message send to client after finished chat send process * fix: extract x-message-id header at the send-message-controller * fix: convert header parameter type to string * style: convert send-message-service like kotlin --- .../socket/command/SendMessageCommand.java | 26 +++++++---- .../controller/ChatMessageController.kt | 8 +++- .../socket/service/ChatMessageSendService.kt | 43 +++++++++++++------ 3 files changed, 53 insertions(+), 24 deletions(-) diff --git a/pennyway-socket/src/main/java/kr/co/pennyway/socket/command/SendMessageCommand.java b/pennyway-socket/src/main/java/kr/co/pennyway/socket/command/SendMessageCommand.java index 27708e871..f27a912e5 100644 --- a/pennyway-socket/src/main/java/kr/co/pennyway/socket/command/SendMessageCommand.java +++ b/pennyway-socket/src/main/java/kr/co/pennyway/socket/command/SendMessageCommand.java @@ -4,6 +4,8 @@ import kr.co.pennyway.domain.domains.message.type.MessageContentType; import kr.co.pennyway.socket.common.constants.SystemMessageConstants; +import java.util.Map; + /** * 채팅 메시지 전송을 위한 Command 클래스 */ @@ -12,7 +14,9 @@ public record SendMessageCommand( String content, MessageContentType contentType, MessageCategoryType categoryType, - long senderId + long senderId, + String senderName, + Map messageIdHeader ) { public SendMessageCommand { if (chatRoomId <= 0) { @@ -45,26 +49,32 @@ public static SendMessageCommand createSystemMessage(long chatRoomId, String con content, MessageContentType.TEXT, MessageCategoryType.SYSTEM, - SystemMessageConstants.SYSTEM_SENDER_ID + SystemMessageConstants.SYSTEM_SENDER_ID, + null, + null ); } /** * 사용자 메시지를 생성합니다. * - * @param chatRoomId long : 채팅방 아이디 - * @param content String : 메시지 내용 - * @param contentType {@link MessageContentType} : 메시지 타입 - * @param senderId long : 발신자 아이디 + * @param chatRoomId long : 채팅방 아이디 + * @param content String : 메시지 내용 + * @param contentType {@link MessageContentType} : 메시지 타입 + * @param senderId long : 발신자 아이디 + * @param senderName String : 발신자 이름 + * @param messageIdHeader Map : `x-message-id` 헤더. null일 경우 성공 메시지를 반환하지 않음. * @return {@link MessageCategoryType#NORMAL}로 생성된 SendMessageCommand */ - public static SendMessageCommand createUserMessage(long chatRoomId, String content, MessageContentType contentType, long senderId) { + public static SendMessageCommand createUserMessage(long chatRoomId, String content, MessageContentType contentType, long senderId, String senderName, Map messageIdHeader) { return new SendMessageCommand( chatRoomId, content, contentType, MessageCategoryType.NORMAL, - senderId + senderId, + senderName, + messageIdHeader ); } } \ No newline at end of file diff --git a/pennyway-socket/src/main/java/kr/co/pennyway/socket/controller/ChatMessageController.kt b/pennyway-socket/src/main/java/kr/co/pennyway/socket/controller/ChatMessageController.kt index d7a233729..bb3919fef 100644 --- a/pennyway-socket/src/main/java/kr/co/pennyway/socket/controller/ChatMessageController.kt +++ b/pennyway-socket/src/main/java/kr/co/pennyway/socket/controller/ChatMessageController.kt @@ -7,6 +7,7 @@ import kr.co.pennyway.socket.common.security.authenticate.UserPrincipal import kr.co.pennyway.socket.service.ChatMessageSendService import kr.co.pennyway.socket.service.LastMessageIdSaveService import org.springframework.messaging.handler.annotation.DestinationVariable +import org.springframework.messaging.handler.annotation.Header import org.springframework.messaging.handler.annotation.MessageMapping import org.springframework.stereotype.Controller import org.springframework.validation.annotation.Validated @@ -26,14 +27,17 @@ class ChatMessageController( fun sendMessage( @DestinationVariable chatRoomId: Long, @Validated payload: ChatMessageDto.Request, - principal: UserPrincipal + principal: UserPrincipal, + @Header("x-message-id") messageId: String? ) { chatMessageSendService.execute( SendMessageCommand.createUserMessage( chatRoomId, payload.content(), payload.contentType(), - principal.userId + principal.userId, + principal.name, + messageId?.let { mapOf("x-message-id" to it) } ) ) } diff --git a/pennyway-socket/src/main/java/kr/co/pennyway/socket/service/ChatMessageSendService.kt b/pennyway-socket/src/main/java/kr/co/pennyway/socket/service/ChatMessageSendService.kt index 8f9337b1f..4f8fb2c61 100644 --- a/pennyway-socket/src/main/java/kr/co/pennyway/socket/service/ChatMessageSendService.kt +++ b/pennyway-socket/src/main/java/kr/co/pennyway/socket/service/ChatMessageSendService.kt @@ -8,6 +8,7 @@ import kr.co.pennyway.infra.common.properties.ChatExchangeProperties import kr.co.pennyway.socket.command.SendMessageCommand import kr.co.pennyway.socket.common.dto.ChatMessageDto import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.messaging.simp.SimpMessagingTemplate import org.springframework.stereotype.Component @Component @@ -16,7 +17,8 @@ class ChatMessageSendService( private val chatMessageService: ChatMessageService, private val messageBrokerAdapter: MessageBrokerAdapter, private val idGenerator: IdGenerator, - private val chatExchangeProperties: ChatExchangeProperties + private val chatExchangeProperties: ChatExchangeProperties, + private val simpMessagingTemplate: SimpMessagingTemplate ) { /** * 채팅 메시지를 전송한다. @@ -24,20 +26,33 @@ class ChatMessageSendService( * @param command SendMessageCommand : 채팅 메시지 전송을 위한 Command */ fun execute(command: SendMessageCommand) { - val message = ChatMessageBuilder.builder() - .chatRoomId(command.chatRoomId()) - .chatId(idGenerator.generate()) - .content(command.content()) - .contentType(command.contentType()) - .categoryType(command.categoryType()) - .sender(command.senderId()) - .build() + val message = command.toChatMessage(command) .let { chatMessageService.create(it) } - messageBrokerAdapter.convertAndSend( - chatExchangeProperties.exchange, - "chat.room.${command.chatRoomId()}", - ChatMessageDto.Response.from(message) - ) + with(chatExchangeProperties) { + messageBrokerAdapter.convertAndSend( + exchange, + "chat.room.${command.chatRoomId}", + ChatMessageDto.Response.from(message) + ) + } + + command.senderName.takeIf { it.isNotBlank() }?.let { senderName -> + simpMessagingTemplate.convertAndSendToUser( + senderName, + "/queue/success", + Unit, + command.messageIdHeader() + ) + } } + + private fun SendMessageCommand.toChatMessage(command: SendMessageCommand) = ChatMessageBuilder.builder() + .chatRoomId(command.chatRoomId()) + .chatId(idGenerator.generate()) + .content(command.content()) + .contentType(command.contentType()) + .categoryType(command.categoryType()) + .sender(command.senderId()) + .build() } \ No newline at end of file