diff --git a/src/main/java/org/sopt/app/application/poke/FriendService.java b/src/main/java/org/sopt/app/application/poke/FriendService.java index 9fad1a19..a3c60e6c 100644 --- a/src/main/java/org/sopt/app/application/poke/FriendService.java +++ b/src/main/java/org/sopt/app/application/poke/FriendService.java @@ -23,6 +23,9 @@ public class FriendService { public List findAllFriendIdsByUserIdRandomly(Long userId, List excludeUserIds, int limitNum) { return friendRepository.getFriendRandom(userId, excludeUserIds, limitNum); } + public List findAllFriendIdsByUserIdRandomlyIncludeDuplicatedFriend(Long userId, List excludeUserIds, int limitNum) { + return friendRepository.getFriendRandomIncludeDuplicated(userId, excludeUserIds, limitNum); + } public List findAllFriendsByFriendship(Long userId, Integer lowerLimit, Integer upperLimit) { return friendRepository.findAllByUserIdAndPokeCountBetweenOrderByPokeCountDesc( diff --git a/src/main/java/org/sopt/app/facade/PokeFacade.java b/src/main/java/org/sopt/app/facade/PokeFacade.java index f83a0c7e..afd53115 100644 --- a/src/main/java/org/sopt/app/facade/PokeFacade.java +++ b/src/main/java/org/sopt/app/facade/PokeFacade.java @@ -101,7 +101,11 @@ private List pickRandomUserIds( @Transactional(readOnly = true) public List getRecommendFriendsOfUsersFriend(User user) { val hasPokeMeBeforeUserIds = pokeHistoryService.getPokeFriendIds(user.getId()); - val friendsUserIds = friendService.findAllFriendIdsByUserIdRandomly(user.getId(), hasPokeMeBeforeUserIds, 2); + List friendsUserIds = friendService.findAllFriendIdsByUserIdRandomly(user.getId(), hasPokeMeBeforeUserIds, 2); + if (friendsUserIds.isEmpty()) { + friendsUserIds = friendService.findAllFriendIdsByUserIdRandomlyIncludeDuplicatedFriend( + user.getId(), hasPokeMeBeforeUserIds, 2); + } return friendsUserIds.stream().map( friendsUserId -> { val friendUser = userService.getUserProfile(friendsUserId); diff --git a/src/main/java/org/sopt/app/interfaces/postgres/FriendCustomRepository.java b/src/main/java/org/sopt/app/interfaces/postgres/FriendCustomRepository.java index 14eee861..dc3be0bd 100644 --- a/src/main/java/org/sopt/app/interfaces/postgres/FriendCustomRepository.java +++ b/src/main/java/org/sopt/app/interfaces/postgres/FriendCustomRepository.java @@ -5,4 +5,6 @@ public interface FriendCustomRepository { List getFriendRandom(Long userId, List excludeUserIds, int limitNum); + + List getFriendRandomIncludeDuplicated(Long userId, List excludeUserIds, int limitNum); } diff --git a/src/main/java/org/sopt/app/interfaces/postgres/FriendRepositoryImpl.java b/src/main/java/org/sopt/app/interfaces/postgres/FriendRepositoryImpl.java index 6ec0ee6b..3ac5187f 100644 --- a/src/main/java/org/sopt/app/interfaces/postgres/FriendRepositoryImpl.java +++ b/src/main/java/org/sopt/app/interfaces/postgres/FriendRepositoryImpl.java @@ -15,20 +15,14 @@ public class FriendRepositoryImpl implements FriendCustomRepository { public List getFriendRandom(Long userId, List excludeUserIds, int limitNum) { QFriend main = new QFriend("main"); QFriend sub = new QFriend("sub"); - QFriend f = new QFriend("f"); - - SubQueryExpression excludedFriendIds = - queryFactory - .select(f.friendUserId) - .from(f) - .where(f.userId.eq(userId)); + SubQueryExpression queryOfExcludeIds = getQueryOfExcludeIds(userId); return queryFactory .select(sub.userId) .from(main) .join(sub).on(main.friendUserId.eq(sub.userId)) .where(main.userId.eq(userId) .and(sub.friendUserId.ne(userId)) - .and(sub.friendUserId.notIn(excludedFriendIds)) // Exclude the friend IDs + .and(sub.friendUserId.notIn(queryOfExcludeIds)) // Exclude the friend IDs .and(sub.friendUserId.notIn(excludeUserIds))) // Exclude friend who user have to reply first IDs .groupBy(sub.userId) .having(sub.userId.count().goe(1)) @@ -36,4 +30,29 @@ public List getFriendRandom(Long userId, List excludeUserIds, int li .limit(2) .fetch(); } + + public List getFriendRandomIncludeDuplicated(Long userId, List excludeUserIds, int limitNum) { + QFriend main = new QFriend("main"); + QFriend sub = new QFriend("sub"); + return queryFactory + .select(sub.userId) + .from(main) + .join(sub).on(main.friendUserId.eq(sub.userId)) + .where(main.userId.eq(userId) + .and(sub.friendUserId.ne(userId)) + .and(sub.friendUserId.notIn(excludeUserIds))) // Exclude friend who user have to reply first IDs + .groupBy(sub.userId) + .having(sub.userId.count().goe(1)) + .orderBy(Expressions.numberTemplate(Long.class, "RANDOM()").asc()) + .limit(2) + .fetch(); + } + + private SubQueryExpression getQueryOfExcludeIds(Long userId) { + QFriend f = new QFriend("f"); + return queryFactory + .select(f.friendUserId) + .from(f) + .where(f.userId.eq(userId)); + } }