Skip to content

Commit

Permalink
Style: createChatRoom() 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jzakka committed Feb 17, 2024
1 parent dbc5b46 commit 45e60d7
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 40 deletions.
12 changes: 7 additions & 5 deletions src/main/java/com/stoury/config/sse/SseEmitters.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.stoury.config.sse;

import com.stoury.dto.chat.ChatMessageResponse;
import com.stoury.exception.chat.ChatMessageSendException;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
Expand All @@ -15,17 +16,18 @@
@Slf4j
public class SseEmitters {
public static final long TIMEOUT = 60 * 1000L;
public static final String ROOM_ID_NULL_MESSAGE = "Room id cannot be null";
private static final Map<Long, SseEmitter> chatRoomEmitters = new ConcurrentHashMap<>();

public SseEmitter get(Long roomId){
Long roomIdNotNull = Objects.requireNonNull(roomId, "Room id cannot be null");
public SseEmitter get(Long roomId) {
Long roomIdNotNull = Objects.requireNonNull(roomId, ROOM_ID_NULL_MESSAGE);

return chatRoomEmitters.computeIfAbsent(roomIdNotNull, key -> chatRoomEmitter(roomIdNotNull));
}

@NotNull
private SseEmitter chatRoomEmitter(Long roomId) {
Long roomIdNotNull = Objects.requireNonNull(roomId, "Room id cannot be null");
Long roomIdNotNull = Objects.requireNonNull(roomId, ROOM_ID_NULL_MESSAGE);

SseEmitter emitter = new SseEmitter(TIMEOUT);
emitter.onCompletion(() -> chatRoomEmitters.remove(roomIdNotNull, emitter));
Expand All @@ -34,7 +36,7 @@ private SseEmitter chatRoomEmitter(Long roomId) {
}

public void broadCast(Long roomId, ChatMessageResponse chatMessage) {
Long roomIdNotNull = Objects.requireNonNull(roomId, "Room id cannot be null");
Long roomIdNotNull = Objects.requireNonNull(roomId, ROOM_ID_NULL_MESSAGE);

log.info("Trying to connect to {}", roomId);
SseEmitter emitter = get(roomIdNotNull);
Expand All @@ -43,7 +45,7 @@ public void broadCast(Long roomId, ChatMessageResponse chatMessage) {
.name("ChatMessage")
.data(chatMessage));
} catch (IOException e) {
throw new RuntimeException(e);
throw new ChatMessageSendException(e);
}
log.info("Established");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/stoury/event/EventHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.stoury.domain.ChatMessage;
import com.stoury.domain.ChatRoom;
import com.stoury.domain.Member;
import com.stoury.exception.ChatRoomSearchException;
import com.stoury.exception.chat.ChatRoomSearchException;
import com.stoury.exception.member.MemberSearchException;
import com.stoury.repository.ChatMessageRepository;
import com.stoury.repository.ChatRoomRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.stoury.exception.chat;

public class ChatMessageSendException extends RuntimeException {
public ChatMessageSendException(Throwable cause) {
super("Failed to send chatting message.", cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.stoury.exception;
package com.stoury.exception.chat;

public class ChatRoomSearchException extends RuntimeException{
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.stoury.exception;
package com.stoury.exception.comment;

public class CommentCreateException extends RuntimeException {
public CommentCreateException(String message) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.stoury.exception;
package com.stoury.exception.comment;

public class CommentSearchException extends RuntimeException {
public CommentSearchException() {
Expand Down
22 changes: 10 additions & 12 deletions src/main/java/com/stoury/service/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.stoury.dto.chat.ChatMessageResponse;
import com.stoury.dto.chat.ChatRoomResponse;
import com.stoury.event.ChatMessageSaveEvent;
import com.stoury.exception.ChatRoomSearchException;
import com.stoury.exception.chat.ChatRoomSearchException;
import com.stoury.exception.authentication.NotAuthorizedException;
import com.stoury.exception.member.MemberSearchException;
import com.stoury.repository.ChatMessageRepository;
Expand Down Expand Up @@ -46,20 +46,14 @@ public ChatRoomResponse createChatRoom(Long senderId, Long receiverId) {
return ChatRoomResponse.from(savedChatRoom);
}

@Transactional
protected ChatMessageResponse createChatMessage(Long senderId, Long chatRoomId, String textContent) {
Long senderIdNotNull = Objects.requireNonNull(senderId);
Long chatRoomIdNotNull = Objects.requireNonNull(chatRoomId);
if (!StringUtils.hasText(textContent)) {
throw new IllegalArgumentException("Message content cannot be empty.");
}
protected ChatMessageResponse publishChatMessageSaveEvent(Long senderId, Long chatRoomId, String textContent) {
Member sender = memberRepository.findById(senderId).orElseThrow(MemberSearchException::new);

Member sender = memberRepository.findById(senderIdNotNull).orElseThrow(MemberSearchException::new);
LocalDateTime createdAt = LocalDateTime.now();
ChatMessageSaveEvent chatMessageSaveEvent = ChatMessageSaveEvent.builder()
.source(this)
.memberId(senderIdNotNull)
.chatRoomId(chatRoomIdNotNull)
.memberId(senderId)
.chatRoomId(chatRoomId)
.textContent(textContent)
.createdAt(createdAt)
.build();
Expand Down Expand Up @@ -90,7 +84,11 @@ public List<ChatMessageResponse> getPreviousChatMessages(Long memberId, Long cha
@Transactional
public ChatMessageResponse sendChatMessage(Long memberId, Long chatRoomId, String textContent) {
checkIfRoomMember(memberId, chatRoomId);
ChatMessageResponse chatMessage = createChatMessage(memberId, chatRoomId, textContent);
if (!StringUtils.hasText(textContent)) {
throw new IllegalArgumentException("Message content cannot be empty.");
}

ChatMessageResponse chatMessage = publishChatMessageSaveEvent(memberId, chatRoomId, textContent);
sseEmitters.broadCast(chatRoomId, chatMessage);
return chatMessage;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/stoury/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.stoury.domain.Member;
import com.stoury.dto.comment.ChildCommentResponse;
import com.stoury.dto.comment.CommentResponse;
import com.stoury.exception.CommentCreateException;
import com.stoury.exception.CommentSearchException;
import com.stoury.exception.comment.CommentCreateException;
import com.stoury.exception.comment.CommentSearchException;
import com.stoury.exception.authentication.NotAuthorizedException;
import com.stoury.exception.feed.FeedSearchException;
import com.stoury.exception.member.MemberSearchException;
Expand Down
30 changes: 15 additions & 15 deletions src/test/groovy/com/stoury/service/ChatServiceTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,11 @@ class ChatServiceTest extends Specification {
memerRepository.findById(sender.id) >> Optional.of(sender)
chatRoomRepository.findById(chatRoom.id) >> Optional.of(chatRoom)
when:
chatService.createChatMessage(sender.id, chatRoom.id, "Hello, World!")
chatService.publishChatMessageSaveEvent(sender.id, chatRoom.id, "Hello, World!")
then:
1 * eventPublisher.publishEvent(_ as ChatMessageSaveEvent)
}

def "채팅메시지 생성불가, 메시지 없음"() {
given:
def sender = new Member("sender@email.com", "pwdpwd123", "sender", null)
def chatRoom = new ChatRoom(List.of(sender, Mock(Member)))
sender.id = 1
chatRoom.id = 1
memerRepository.findById(sender.id) >> Optional.of(sender)
chatRoomRepository.findById(chatRoom.id) >> Optional.of(chatRoom)
when:
chatService.createChatMessage(sender.id, chatRoom.id, "")
then:
thrown(IllegalArgumentException)
}

def "이전 채팅 불러오기"() {
given:
def sender1 = new Member("sender1@email.com", "pwdpwd123", "sender1", null)
Expand Down Expand Up @@ -156,4 +142,18 @@ class ChatServiceTest extends Specification {
then:
thrown(NotAuthorizedException)
}

def "채팅전송 불가, 메시지 없음"() {
given:
def sender = new Member("sender@email.com", "pwdpwd123", "sender", null)
def chatRoom = new ChatRoom(List.of(sender, Mock(Member)))
sender.id = 1
chatRoom.id = 1
memerRepository.findById(sender.id) >> Optional.of(sender)
chatRoomRepository.findById(chatRoom.id) >> Optional.of(chatRoom)
when:
chatService.sendChatMessage(sender.id, chatRoom.id, "")
then:
thrown(IllegalArgumentException)
}
}
4 changes: 2 additions & 2 deletions src/test/groovy/com/stoury/service/CommentServiceTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import com.stoury.domain.Comment
import com.stoury.domain.Feed
import com.stoury.domain.Member
import com.stoury.dto.comment.CommentResponse
import com.stoury.exception.CommentCreateException
import com.stoury.exception.CommentSearchException
import com.stoury.exception.comment.CommentCreateException
import com.stoury.exception.comment.CommentSearchException
import com.stoury.repository.CommentRepository
import com.stoury.repository.FeedRepository
import com.stoury.repository.MemberRepository
Expand Down

0 comments on commit 45e60d7

Please sign in to comment.