Skip to content

Commit

Permalink
fix: 🔧 The server sends a success message after a chat message is sav…
Browse files Browse the repository at this point in the history
…ed successfully (#214)

* 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
  • Loading branch information
psychology50 authored Jan 7, 2025
1 parent 7303258 commit 092e7aa
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 클래스
*/
Expand All @@ -12,7 +14,9 @@ public record SendMessageCommand(
String content,
MessageContentType contentType,
MessageCategoryType categoryType,
long senderId
long senderId,
String senderName,
Map<String, Object> messageIdHeader
) {
public SendMessageCommand {
if (chatRoomId <= 0) {
Expand Down Expand Up @@ -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<String, String> : `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<String, Object> messageIdHeader) {
return new SendMessageCommand(
chatRoomId,
content,
contentType,
MessageCategoryType.NORMAL,
senderId
senderId,
senderName,
messageIdHeader
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) }
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,28 +17,42 @@ class ChatMessageSendService(
private val chatMessageService: ChatMessageService,
private val messageBrokerAdapter: MessageBrokerAdapter,
private val idGenerator: IdGenerator<Long>,
private val chatExchangeProperties: ChatExchangeProperties
private val chatExchangeProperties: ChatExchangeProperties,
private val simpMessagingTemplate: SimpMessagingTemplate
) {
/**
* 채팅 메시지를 전송한다.
*
* @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()
}

0 comments on commit 092e7aa

Please sign in to comment.