Skip to content
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

Open
wants to merge 3 commits into
base: be/dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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..*(..))")
Expand All @@ -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("게시글을 이용할 수 없습니다.");
}

return ownableOptional;
}

@Pointcut("execution(net.pengcook.user.domain.Ownable+ net.pengcook..repository..*(..))")
public void repositoryMethodsReturningOwnable() {
public void repositoryMethodsReturningAuthorAble() {
}

@Around("repositoryMethodsReturningOwnable()")
@Around("repositoryMethodsReturningAuthorAble()")
public Object filterBlockedAuthor(ProceedingJoinPoint joinPoint) throws Throwable {
Ownable ownable = (Ownable) joinPoint.proceed();

Expand All @@ -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;
Expand All @@ -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
Expand Up @@ -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) {
return users.stream()
.anyMatch(user -> user.isSameUser(userId));
}
Expand Down
15 changes: 15 additions & 0 deletions backend/src/main/java/net/pengcook/user/domain/BlockerGroup.java
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
Expand Up @@ -9,6 +9,8 @@
public interface UserBlockRepository extends JpaRepository<UserBlock, Long> {
List<UserBlock> findAllByBlockerId(long blockerId);

List<UserBlock> findAllByBlockeeId(long blockeeId);

void deleteByBlockeeId(long blockeeId);

void deleteByBlockerId(long blockerId);
Expand Down
15 changes: 12 additions & 3 deletions backend/src/main/java/net/pengcook/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import net.pengcook.like.repository.RecipeLikeRepository;
import net.pengcook.recipe.repository.RecipeRepository;
import net.pengcook.recipe.service.RecipeService;
import net.pengcook.user.domain.BlockedUserGroup;
import net.pengcook.user.domain.BlockeeGroup;
import net.pengcook.user.domain.BlockerGroup;
import net.pengcook.user.domain.User;
import net.pengcook.user.domain.UserBlock;
import net.pengcook.user.domain.UserReport;
Expand Down Expand Up @@ -109,11 +110,19 @@ public ReportResponse report(long reporterId, ReportRequest reportRequest) {
}

@Transactional(readOnly = true)
public BlockedUserGroup getBlockedUserGroup(long blockerId) {
public BlockeeGroup getBlockeeGroup(long blockerId) {

return userBlockRepository.findAllByBlockerId(blockerId).stream()
.map(UserBlock::getBlockee)
.collect(Collectors.collectingAndThen(Collectors.toSet(), BlockedUserGroup::new));
.collect(Collectors.collectingAndThen(Collectors.toSet(), BlockeeGroup::new));
}

@Transactional(readOnly = true)
public BlockerGroup getBlockerGroup(long blockeeId) {

return userBlockRepository.findAllByBlockeeId(blockeeId).stream()
.map(UserBlock::getBlocker)
.collect(Collectors.collectingAndThen(Collectors.toSet(), BlockerGroup::new));
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.pengcook.image.service.ImageClientService;
import net.pengcook.like.repository.RecipeLikeRepository;
import net.pengcook.recipe.repository.RecipeRepository;
import net.pengcook.user.domain.BlockedUserGroup;
import net.pengcook.user.domain.BlockeeGroup;
import net.pengcook.user.domain.Reason;
import net.pengcook.user.domain.Type;
import net.pengcook.user.domain.UserReport;
Expand Down Expand Up @@ -192,15 +192,15 @@ void blockUserWhenNotExistBlockee() {

@Test
@DisplayName("차단한 사용자들의 목록을 불러올 수 있다.")
void getBlockedUserGroup() {
void getBlockeeGroup() {
long blockerId = 1L;

BlockedUserGroup blockedUserGroup = userService.getBlockedUserGroup(blockerId);
BlockeeGroup blockeeGroup = userService.getBlockeeGroup(blockerId);

assertAll(
() -> assertThat(blockedUserGroup.isBlocked(2L)).isTrue(),
() -> assertThat(blockedUserGroup.isBlocked(3L)).isTrue(),
() -> assertThat(blockedUserGroup.isBlocked(4L)).isFalse()
() -> assertThat(blockeeGroup.contains(2L)).isTrue(),
() -> assertThat(blockeeGroup.contains(3L)).isTrue(),
() -> assertThat(blockeeGroup.contains(4L)).isFalse()
);
}

Expand Down
Loading