Skip to content

Commit

Permalink
Fix/simple poke profile resopnse (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
yummygyudon authored Dec 27, 2023
2 parents cd9207b + cf64910 commit f7f7c50
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ public class FriendService {

private final FriendRepository friendRepository;

public List<Long> findAllFriendIdsByUserIdRandomly(Long userId, int limitNum) {
return friendRepository.getFriendRandom(userId, limitNum);
public List<Long> findAllFriendIdsByUserIdRandomly(Long userId, List<Long> excludeUserIds, int limitNum) {
return friendRepository.getFriendRandom(userId, excludeUserIds, limitNum);
}
public List<Long> findAllFriendIdsByUserIdRandomlyIncludeDuplicatedFriend(Long userId, List<Long> excludeUserIds, int limitNum) {
return friendRepository.getFriendRandomIncludeDuplicated(userId, excludeUserIds, limitNum);
}

public List<Friend> findAllFriendsByFriendship(Long userId, Integer lowerLimit, Integer upperLimit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@
public class PokeMessageService {

private static final int MESSAGES_QUANTITY_AT_ONCE = 5;
private static final String MESSAGES_HEADER_FOR_POKE = "ํ•จ๊ป˜ ๋ณด๋‚ผ ๋ฉ”์‹œ์ง€๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”";
private static final String MESSAGES_HEADER_FOR_REPLY = "๋‹ต์žฅํ•˜๊ณ  ์‹ถ์€ ๋ฉ”์‹œ์ง€๋ฅผ ์„ ํƒํ•ด์ฃผ์„ธ์š”";
private final PokeMessageRepository messageRepository;

public String getMessagesHeaderComment(String type) {
PokeMessageType pokeMessageType = PokeMessageType.ofParam(type);
if (pokeMessageType.equals(PokeMessageType.REPLY_NEW)) {
return MESSAGES_HEADER_FOR_REPLY;
}
return MESSAGES_HEADER_FOR_POKE;
}
public List<PokeMessage> pickRandomMessageByTypeOf(String type) {
PokeMessageType messageType = PokeMessageType.ofParam(type);
val messages = messageRepository.findAllByType(messageType);
Expand Down
44 changes: 37 additions & 7 deletions src/main/java/org/sopt/app/facade/PokeFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
@Service
@RequiredArgsConstructor
public class PokeFacade {

private static final String NEW_FRIEND_NO_MUTUAL = "์ƒˆ๋กœ์šด ์นœ๊ตฌ";
private static final String NEW_FRIEND_ONE_MUTUAL = "%s์˜ ์นœ๊ตฌ";
private static final String NEW_FRIEND_MANY_MUTUAL = "%s ์™ธ %d๋ช…๊ณผ ์นœ๊ตฌ";
private final PlaygroundAuthService playgroundAuthService;
private final UserService userService;
private final FriendService friendService;
Expand All @@ -43,6 +45,9 @@ public List<PokeResponse.PokeMessage> getPokingMessages(String type) {
)
.toList();
}
public String getPokingMessageHeader(String type) {
return pokeMessageService.getMessagesHeaderComment(type);
}

@Transactional(readOnly = true)
public List<SimplePokeProfile> getRecommendUserForNew(String playgroundToken, Long userPlaygroundId) {
Expand Down Expand Up @@ -76,7 +81,7 @@ private List<SimplePokeProfile> makeDummySimplePokeProfile(List<UserProfile> use
part,
0,
Friendship.NON_FRIEND.getFriendshipName(),
List.of(),
NEW_FRIEND_NO_MUTUAL,
true,
false
);
Expand All @@ -95,7 +100,12 @@ private List<Long> pickRandomUserIds(

@Transactional(readOnly = true)
public List<PokeResponse.Friend> getRecommendFriendsOfUsersFriend(User user) {
val friendsUserIds = friendService.findAllFriendIdsByUserIdRandomly(user.getId(), 2);
val hasPokeMeBeforeUserIds = pokeHistoryService.getPokeFriendIds(user.getId());
List<Long> 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);
Expand Down Expand Up @@ -188,7 +198,6 @@ public List<SimplePokeProfile> getFriend(User user) {

val pokeHistory = pokeHistoryService.getAllOfPokeBetween(user.getId(), friendId.get(0)).get(0);
val isAlreadyPoke = pokeHistory.getPokerId().equals(user.getId());

return List.of(
SimplePokeProfile.of(
friendUserProfile.get(0).getUserId(),
Expand All @@ -200,7 +209,9 @@ public List<SimplePokeProfile> getFriend(User user) {
friendProfile.get(0).getActivities().get(0).getPart(),
friendRelationInfo.getPokeCount(),
friendRelationInfo.getRelationName(),
mutualFriendNames,
mutualFriendNames.size() == 0 ? NEW_FRIEND_NO_MUTUAL :
mutualFriendNames.size() == 1 ? String.format(NEW_FRIEND_ONE_MUTUAL, mutualFriendNames.get(0))
: String.format(NEW_FRIEND_MANY_MUTUAL, mutualFriendNames.get(0), mutualFriendNames.size()-1),
false,
isAlreadyPoke
)
Expand Down Expand Up @@ -261,6 +272,25 @@ public SimplePokeProfile getPokeHistoryProfile(User user, Long friendId, Long po
PokeInfo.PokedUserInfo friendUserInfo = getFriendUserInfo(
user, friendId);

List<String> mutualFriendNames = friendUserInfo.getMutualFriendNames();
if (friendUserInfo.getRelation().getPokeCount() == 0) {
return SimplePokeProfile.of(
friendUserInfo.getUserId(),
friendUserInfo.getPlaygroundId(),
friendUserInfo.getProfileImage() == null ? "" : friendUserInfo.getProfileImage(),
friendUserInfo.getName(),
pokeDetail.getMessage(),
friendUserInfo.getGeneration(),
friendUserInfo.getPart(),
0,
friendUserInfo.getRelation().getRelationName(),
mutualFriendNames.size() == 0 ? NEW_FRIEND_NO_MUTUAL :
mutualFriendNames.size() == 1 ? String.format(NEW_FRIEND_ONE_MUTUAL, mutualFriendNames.get(0))
: String.format(NEW_FRIEND_MANY_MUTUAL, mutualFriendNames.get(0), mutualFriendNames.size()-1),
true,
isAlreadyReply
);
}
return SimplePokeProfile.of(
friendUserInfo.getUserId(),
friendUserInfo.getPlaygroundId(),
Expand All @@ -271,8 +301,8 @@ public SimplePokeProfile getPokeHistoryProfile(User user, Long friendId, Long po
friendUserInfo.getPart(),
friendUserInfo.getRelation().getPokeCount(),
friendUserInfo.getRelation().getRelationName(),
friendUserInfo.getMutualFriendNames(),
friendUserInfo.getRelation().getPokeCount() == 0,
friendUserInfo.getRelation().getRelationName(),
false,
isAlreadyReply
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package org.sopt.app.interfaces.postgres;

import java.util.List;
import org.sopt.app.domain.entity.Friend;

public interface FriendCustomRepository {

List<Long> getFriendRandom(Long userId, int limitNum);
List<Long> getFriendRandom(Long userId, List<Long> excludeUserIds, int limitNum);

List<Long> getFriendRandomIncludeDuplicated(Long userId, List<Long> excludeUserIds, int limitNum);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,54 @@
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.sopt.app.domain.entity.Friend;
import org.sopt.app.domain.entity.QFriend;

@RequiredArgsConstructor
public class FriendRepositoryImpl implements FriendCustomRepository {

private final JPAQueryFactory queryFactory;

public List<Long> getFriendRandom(Long userId, int limitNum) {
public List<Long> getFriendRandom(Long userId, List<Long> excludeUserIds, int limitNum) {
QFriend main = new QFriend("main");
QFriend sub = new QFriend("sub");
QFriend f = new QFriend("f");

SubQueryExpression<Long> excludedFriendIds =
queryFactory
.select(f.friendUserId)
.from(f)
.where(f.userId.eq(userId));

SubQueryExpression<Long> 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))
.orderBy(Expressions.numberTemplate(Long.class, "RANDOM()").asc())
.limit(2)
.fetch();
}

public List<Long> getFriendRandomIncludeDuplicated(Long userId, List<Long> 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<Long> getQueryOfExcludeIds(Long userId) {
QFriend f = new QFriend("f");
return queryFactory
.select(f.friendUserId)
.from(f)
.where(f.userId.eq(userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public interface PokeHistoryRepository extends JpaRepository<PokeHistory, Long>
List<PokeHistory> findAllByPokedIdAndIsReplyIsFalseOrderByCreatedAtDesc(Long pokedId);
Page<PokeHistory> findAllByPokedIdAndIsReplyIsFalseOrderByCreatedAtDesc(Long pokedId, Pageable pageable);

List<PokeHistory> findAllByPokerIdAndPokedIdOrderByCreatedAtDesc(Long pokerId, Long pokedId);

@Query("SELECT ph FROM PokeHistory ph WHERE ((ph.pokerId = :userId AND ph.pokedId = :friendId) OR (ph.pokerId = :friendId AND ph.pokedId = :userId)) AND ph.isReply = false ORDER BY ph.createdAt DESC ")
List<PokeHistory> findAllWithFriendOrderByCreatedAtDesc(@Param("userId") Long userId, @Param("friendId") Long friendId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public ResponseEntity<PokeMessageList> getPokeMessages(
@RequestParam("messageType") String messageType
) {
val messages = pokeFacade.getPokingMessages(messageType);
val response = PokeMessageList.of(messages);
val pokingMessageHeader = pokeFacade.getPokingMessageHeader(messageType);
val response = PokeMessageList.of(pokingMessageHeader, messages);
return ResponseEntity.ok(response);
}

Expand Down
22 changes: 17 additions & 5 deletions src/main/java/org/sopt/app/presentation/poke/PokeResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ public static PokeToMeHistoryList of(
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@ToString
public static class PokeMessageList implements MessageList {
private String header;
private List<PokeMessage> messages;

public static PokeMessageList of(
String header,
List<PokeMessage> messages
) {
return new PokeMessageList(messages);
return new PokeMessageList(header, messages);
}
}

Expand Down Expand Up @@ -162,20 +164,30 @@ public static class SimplePokeProfile {
private Integer pokeNum;
@Schema(description = "๊ด€๊ณ„ ์ด๋ฆ„", example = "์ฒœ์ƒ์—ฐ๋ถ„")
private String relationName;
@Schema(description = "ํ•จ๊ป˜ ์นœ๊ตฌ ๊ด€๊ณ„์ธ ์นœ๊ตฌ๋“ค์˜ ์ด๋ฆ„", example = "['์ œ๊ฐˆ์†กํ˜„', '์™•๊ฑด๋ชจ', '์ง„๋™๊ทœ', '์ฐจ์Šนํ˜ธ']")
private List<String> mutual;

//TODO: ๊ฑด๋ชจ Align ์ดํ›„ ์‚ญ์ œ
// @Schema(description = "ํ•จ๊ป˜ ์นœ๊ตฌ ๊ด€๊ณ„์ธ ์นœ๊ตฌ๋“ค์˜ ์ด๋ฆ„", example = "['์ œ๊ฐˆ์†กํ˜„', '์™•๊ฑด๋ชจ', '์ง„๋™๊ทœ', '์ฐจ์Šนํ˜ธ']")
// private List<String> mutual;
@Schema(description = "ํ•จ๊ป˜ ์•„๋Š” ์นœ๊ตฌ๊ด€๊ณ„ ๋ฌธ๊ตฌ", example = "์ œ๊ฐˆ์†กํ˜„ ์™ธ 1๋ช…๊ณผ ์นœ๊ตฌ")
private String mutualRelationMessage;

@Schema(description = "์ด์ „์— ์ฐŒ๋ฅธ ์ด๋ ฅ์ด ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€", example = "false")
private Boolean isFirstMeet;
@Schema(description = "์ด๋ฏธ ์˜ค๋Š˜ ์ฐ”๋ €๋Š”์ง€์— ๋Œ€ํ•œ ์—ฌ๋ถ€", example = "true")
private Boolean isAlreadyPoke;

public static SimplePokeProfile of(
Long userId, Long playgroundId, String profileImage, String name, String message,
Integer generation, String part, Integer pickNum, String relationName, List<String> mutual, Boolean isFirstMeet,
Integer generation, String part, Integer pickNum, String relationName,
// , List<String> mutual
String mutualRelationMessage, Boolean isFirstMeet,
Boolean isAlreadyPoke
) {
return new SimplePokeProfile(
userId, playgroundId, profileImage, name, message, generation, part, pickNum, relationName, mutual, isFirstMeet,
userId, playgroundId, profileImage, name, message, generation, part, pickNum, relationName
// , mutual
, mutualRelationMessage
, isFirstMeet,
isAlreadyPoke
);
}
Expand Down

0 comments on commit f7f7c50

Please sign in to comment.