-
Notifications
You must be signed in to change notification settings - Fork 0
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
♻️ user block logic #437
base: be/dev
Are you sure you want to change the base?
♻️ user block logic #437
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,10 @@ | |
import java.util.Optional; | ||
import lombok.RequiredArgsConstructor; | ||
import net.pengcook.authentication.domain.UserInfo; | ||
import net.pengcook.user.domain.BlockeeGroup; | ||
import net.pengcook.user.domain.BlockerGroup; | ||
import net.pengcook.user.domain.Ownable; | ||
import net.pengcook.user.domain.BlockedUserGroup; | ||
import net.pengcook.user.exception.ForbiddenException; | ||
import net.pengcook.user.service.UserService; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
|
@@ -35,9 +37,10 @@ public Object filterBlockedAuthorsFromList(ProceedingJoinPoint joinPoint) throws | |
return ownables; | ||
} | ||
|
||
BlockedUserGroup blockedUserGroup = userService.getBlockedUserGroup(userInfo.getId()); | ||
BlockeeGroup blockeeGroup = userService.getBlockeeGroup(userInfo.getId()); | ||
BlockerGroup blockerGroup = userService.getBlockerGroup(userInfo.getId()); | ||
|
||
return filterBlockedUsers(ownables, blockedUserGroup); | ||
return filterBlockedUsers(ownables, blockeeGroup, blockerGroup); | ||
} | ||
|
||
@Pointcut("execution(java.util.Optional<net.pengcook.user.domain.Ownable+> net.pengcook..repository..*(..))") | ||
|
@@ -53,19 +56,24 @@ public Object filterBlockedAuthorFromOptional(ProceedingJoinPoint joinPoint) thr | |
return ownableOptional; | ||
} | ||
|
||
BlockedUserGroup blockedUserGroup = userService.getBlockedUserGroup(userInfo.getId()); | ||
if (blockedUserGroup.isBlocked(ownableOptional.get().getOwnerId())) { | ||
return Optional.empty(); | ||
BlockeeGroup blockeeGroup = userService.getBlockeeGroup(userInfo.getId()); | ||
if (blockeeGroup.contains(ownableOptional.get().getOwnerId())) { | ||
throw new ForbiddenException("차단한 사용자입니다."); | ||
} | ||
|
||
BlockerGroup blockerGroup = userService.getBlockerGroup(userInfo.getId()); | ||
if (blockerGroup.contains(ownableOptional.get().getOwnerId())) { | ||
throw new ForbiddenException("게시글을 이용할 수 없습니다."); | ||
} | ||
Comment on lines
+59
to
67
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.
|
||
|
||
return ownableOptional; | ||
} | ||
|
||
@Pointcut("execution(net.pengcook.user.domain.Ownable+ net.pengcook..repository..*(..))") | ||
public void repositoryMethodsReturningOwnable() { | ||
public void repositoryMethodsReturningAuthorAble() { | ||
} | ||
|
||
@Around("repositoryMethodsReturningOwnable()") | ||
@Around("repositoryMethodsReturningAuthorAble()") | ||
Comment on lines
-65
to
+76
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. 컨플릭트 해결할 때 잘못 고른 거 같아요...!! |
||
public Object filterBlockedAuthor(ProceedingJoinPoint joinPoint) throws Throwable { | ||
Ownable ownable = (Ownable) joinPoint.proceed(); | ||
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. 바로 타입캐스팅을 하지 말고 |
||
|
||
|
@@ -74,9 +82,14 @@ public Object filterBlockedAuthor(ProceedingJoinPoint joinPoint) throws Throwabl | |
return ownable; | ||
} | ||
|
||
BlockedUserGroup blockedUserGroup = userService.getBlockedUserGroup(userInfo.getId()); | ||
if (blockedUserGroup.isBlocked(ownable.getOwnerId())) { | ||
return null; | ||
BlockeeGroup blockeeGroup = userService.getBlockeeGroup(userInfo.getId()); | ||
if (blockeeGroup.contains(ownable.getOwnerId())) { | ||
throw new ForbiddenException("차단한 사용자입니다."); | ||
} | ||
|
||
BlockerGroup blockerGroup = userService.getBlockerGroup(userInfo.getId()); | ||
if (blockerGroup.contains(ownable.getOwnerId())) { | ||
throw new ForbiddenException("게시글을 이용할 수 없습니다."); | ||
} | ||
|
||
return ownable; | ||
|
@@ -91,9 +104,14 @@ private UserInfo getCurrentUserInfo() { | |
} | ||
} | ||
|
||
private List<Ownable> filterBlockedUsers(List<Ownable> ownables, BlockedUserGroup blockedUserGroup) { | ||
return ownables.stream() | ||
.filter(item -> !blockedUserGroup.isBlocked(item.getOwnerId())) | ||
private List<Ownable> filterBlockedUsers( | ||
List<Ownable> authorAbles, | ||
BlockeeGroup blockeeGroup, | ||
BlockerGroup blockerGroup | ||
) { | ||
return authorAbles.stream() | ||
.filter(item -> !blockeeGroup.contains(item.getOwnerId())) | ||
.filter(item -> !blockerGroup.contains(item.getOwnerId())) | ||
.toList(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,11 @@ | |
import lombok.AllArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
public class BlockedUserGroup { | ||
public class BlockeeGroup { | ||
|
||
private final Set<User> users; | ||
|
||
public boolean isBlocked(long userId) { | ||
public boolean contains(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.
|
||
return users.stream() | ||
.anyMatch(user -> user.isSameUser(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. 두 클래스가 하는 일이 완전히 똑같은데, 나눠져 있을 필요가 있을까요?? 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. 저는 사실 이 클래스의 존재 자체에 약간 회의적인데요... 그리고 위에 단 리뷰
의 연장선에서, blocker와 blockee를 따로 볼 필요가 있을까? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package net.pengcook.user.domain; | ||
|
||
import java.util.Set; | ||
import lombok.AllArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
public class BlockerGroup { | ||
|
||
private final Set<User> users; | ||
|
||
public boolean contains(long userId) { | ||
return users.stream() | ||
.anyMatch(user -> user.isSameUser(userId)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package net.pengcook.user.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
public class ForbiddenException extends UserException { | ||
public ForbiddenException(String message) { | ||
super(HttpStatus.FORBIDDEN, message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,18 +6,18 @@ | |
import java.util.Set; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class BlockedUserGroupTest { | ||
class BlockeeGroupTest { | ||
|
||
@Test | ||
void isBlocked() { | ||
User loki = new User(1L, "[email protected]", "loki", "로키", "loki.jpg", "KOREA"); | ||
User pond = new User(2L, "[email protected]", "pond", "폰드", "pond.jpg", "KOREA"); | ||
|
||
BlockedUserGroup blockedUserGroup = new BlockedUserGroup(Set.of(pond)); | ||
BlockeeGroup blockeeGroup = new BlockeeGroup(Set.of(pond)); | ||
|
||
assertAll( | ||
() -> assertThat(blockedUserGroup.isBlocked(loki.getId())).isFalse(), | ||
() -> assertThat(blockedUserGroup.isBlocked(pond.getId())).isTrue() | ||
() -> assertThat(blockeeGroup.contains(loki.getId())).isFalse(), | ||
() -> assertThat(blockeeGroup.contains(pond.getId())).isTrue() | ||
); | ||
} | ||
} |
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.
단건조회 관련 커버리지가 낮은데 단건조회를 했을 때 차단 상태면 보이지 않는 것을 확인하는 테스트를 추가하면 어떨까요???
현재는 목록만 존재하네요~~~