-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: direct room 접근 복원 책임 분리 #595
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
57 changes: 57 additions & 0 deletions
57
src/main/java/gg/agit/konect/domain/chat/service/ChatDirectRoomAccessService.java
This file contains hidden or 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,57 @@ | ||
| package gg.agit.konect.domain.chat.service; | ||
|
|
||
| import static gg.agit.konect.global.code.ApiResponseCode.FORBIDDEN_CHAT_ROOM_ACCESS; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import gg.agit.konect.domain.chat.model.ChatRoom; | ||
| import gg.agit.konect.domain.chat.model.ChatRoomMember; | ||
| import gg.agit.konect.domain.chat.repository.ChatRoomMemberRepository; | ||
| import gg.agit.konect.domain.user.model.User; | ||
| import gg.agit.konect.global.exception.CustomException; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional | ||
| public class ChatDirectRoomAccessService { | ||
|
|
||
| private final ChatRoomMemberRepository chatRoomMemberRepository; | ||
|
|
||
| public ChatRoomMember getAccessibleMember(ChatRoom chatRoom, User user) { | ||
| ChatRoomMember member = getMember(chatRoom, user); | ||
| restoreIfVisible(member, chatRoom); | ||
| return member; | ||
| } | ||
|
|
||
| public LocalDateTime prepareAccessAndGetVisibleMessageFrom(ChatRoom chatRoom, User user) { | ||
| ChatRoomMember member = getMember(chatRoom, user); | ||
| LocalDateTime visibleMessageFrom = member.getVisibleMessageFrom(); | ||
| restoreIfVisible(member, chatRoom); | ||
| return visibleMessageFrom; | ||
| } | ||
|
|
||
| private ChatRoomMember getMember(ChatRoom chatRoom, User user) { | ||
| return chatRoomMemberRepository.findByChatRoomIdAndUserId(chatRoom.getId(), user.getId()) | ||
| .orElseThrow(() -> CustomException.of(FORBIDDEN_CHAT_ROOM_ACCESS)); | ||
| } | ||
|
|
||
| /** | ||
| * direct 채팅방에서 나간 사용자가 다시 볼 수 있는 상태인지 확인하고, | ||
| * 새 메시지가 이미 존재하면 나간 상태를 해제한다. | ||
| */ | ||
| private void restoreIfVisible(ChatRoomMember member, ChatRoom chatRoom) { | ||
| if (!member.hasLeft()) { | ||
| return; | ||
| } | ||
|
|
||
| if (!member.hasVisibleMessages(chatRoom)) { | ||
| throw CustomException.of(FORBIDDEN_CHAT_ROOM_ACCESS); | ||
| } | ||
|
|
||
| member.restoreDirectRoom(); | ||
| } | ||
| } | ||
This file contains hidden or 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
125 changes: 125 additions & 0 deletions
125
src/test/java/gg/agit/konect/unit/domain/chat/service/ChatDirectRoomAccessServiceTest.java
This file contains hidden or 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,125 @@ | ||
| package gg.agit.konect.unit.domain.chat.service; | ||
|
|
||
| import static gg.agit.konect.global.code.ApiResponseCode.FORBIDDEN_CHAT_ROOM_ACCESS; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.BDDMockito.given; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.Optional; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.InjectMocks; | ||
| import org.mockito.Mock; | ||
| import org.springframework.test.util.ReflectionTestUtils; | ||
|
|
||
| import gg.agit.konect.domain.chat.model.ChatRoom; | ||
| import gg.agit.konect.domain.chat.model.ChatRoomMember; | ||
| import gg.agit.konect.domain.chat.repository.ChatRoomMemberRepository; | ||
| import gg.agit.konect.domain.chat.service.ChatDirectRoomAccessService; | ||
| import gg.agit.konect.domain.user.enums.UserRole; | ||
| import gg.agit.konect.domain.user.model.User; | ||
| import gg.agit.konect.global.exception.CustomException; | ||
| import gg.agit.konect.support.ServiceTestSupport; | ||
| import gg.agit.konect.support.fixture.UniversityFixture; | ||
| import gg.agit.konect.support.fixture.UserFixture; | ||
|
|
||
| class ChatDirectRoomAccessServiceTest extends ServiceTestSupport { | ||
|
|
||
| private static final LocalDateTime BASE_TIME = LocalDateTime.of(2026, 4, 27, 10, 0); | ||
|
|
||
| @Mock | ||
| private ChatRoomMemberRepository chatRoomMemberRepository; | ||
|
|
||
| @InjectMocks | ||
| private ChatDirectRoomAccessService chatDirectRoomAccessService; | ||
|
|
||
| @Test | ||
| @DisplayName("접근 가능한 direct room 멤버를 반환한다") | ||
| void getAccessibleMemberReturnsMember() { | ||
| User user = user(10); | ||
| ChatRoom room = room(1); | ||
| ChatRoomMember member = member(room, user); | ||
| given(chatRoomMemberRepository.findByChatRoomIdAndUserId(room.getId(), user.getId())) | ||
| .willReturn(Optional.of(member)); | ||
|
|
||
| ChatRoomMember result = chatDirectRoomAccessService.getAccessibleMember(room, user); | ||
|
|
||
| assertThat(result).isSameAs(member); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("나간 direct room에 새 메시지가 있으면 접근 시 나간 상태를 해제한다") | ||
| void getAccessibleMemberRestoresLeftMemberWhenVisibleMessageExists() { | ||
| User user = user(10); | ||
| ChatRoom room = room(1); | ||
| room.updateLastMessage("새 메시지", BASE_TIME.plusHours(2)); | ||
| ChatRoomMember member = member(room, user); | ||
| member.leaveDirectRoom(BASE_TIME.plusHours(1)); | ||
| given(chatRoomMemberRepository.findByChatRoomIdAndUserId(room.getId(), user.getId())) | ||
| .willReturn(Optional.of(member)); | ||
|
|
||
| chatDirectRoomAccessService.getAccessibleMember(room, user); | ||
|
|
||
| assertThat(member.hasLeft()).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("나간 direct room에 새 메시지가 없으면 접근을 거부한다") | ||
| void getAccessibleMemberRejectsLeftMemberWithoutVisibleMessage() { | ||
| User user = user(10); | ||
| ChatRoom room = room(1); | ||
| ChatRoomMember member = member(room, user); | ||
| member.leaveDirectRoom(BASE_TIME.plusHours(1)); | ||
| given(chatRoomMemberRepository.findByChatRoomIdAndUserId(room.getId(), user.getId())) | ||
| .willReturn(Optional.of(member)); | ||
|
|
||
| assertThatThrownBy(() -> chatDirectRoomAccessService.getAccessibleMember(room, user)) | ||
| .isInstanceOf(CustomException.class) | ||
| .satisfies(exception -> | ||
| assertThat(((CustomException)exception).getErrorCode()).isEqualTo(FORBIDDEN_CHAT_ROOM_ACCESS)); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("접근 준비는 복원 전 visibleMessageFrom을 반환한다") | ||
| void prepareAccessAndGetVisibleMessageFromReturnsPreviousVisibilityBoundary() { | ||
| User user = user(10); | ||
| ChatRoom room = room(1); | ||
| room.updateLastMessage("새 메시지", BASE_TIME.plusHours(2)); | ||
| ChatRoomMember member = member(room, user); | ||
| LocalDateTime visibleMessageFrom = BASE_TIME.plusHours(1); | ||
| ReflectionTestUtils.setField(member, "leftAt", visibleMessageFrom); | ||
| ReflectionTestUtils.setField(member, "visibleMessageFrom", visibleMessageFrom); | ||
| given(chatRoomMemberRepository.findByChatRoomIdAndUserId(room.getId(), user.getId())) | ||
| .willReturn(Optional.of(member)); | ||
|
|
||
| LocalDateTime result = chatDirectRoomAccessService.prepareAccessAndGetVisibleMessageFrom(room, user); | ||
|
|
||
| assertThat(result).isEqualTo(visibleMessageFrom); | ||
| assertThat(member.hasLeft()).isFalse(); | ||
| } | ||
|
|
||
| private User user(Integer id) { | ||
| return UserFixture.createUserWithId( | ||
| UniversityFixture.createWithId(1), | ||
| id, | ||
| "사용자" + id, | ||
| "2024" + String.format("%04d", id), | ||
| UserRole.USER | ||
| ); | ||
| } | ||
|
|
||
| private ChatRoom room(Integer id) { | ||
| ChatRoom room = ChatRoom.directOf(); | ||
| ReflectionTestUtils.setField(room, "id", id); | ||
| ReflectionTestUtils.setField(room, "createdAt", BASE_TIME); | ||
| return room; | ||
| } | ||
|
|
||
| private ChatRoomMember member(ChatRoom room, User user) { | ||
| ChatRoomMember member = ChatRoomMember.of(room, user, BASE_TIME); | ||
| ReflectionTestUtils.setField(member, "createdAt", BASE_TIME); | ||
| return member; | ||
| } | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.