Skip to content

Commit

Permalink
Merge pull request #50 from YAPP-Github/feature/PC-416-settings-api
Browse files Browse the repository at this point in the history
[PC-416] 유저 설정 기능 구현
  • Loading branch information
devchlee12 authored Feb 12, 2025
2 parents 29f234b + aa3f35a commit 45501be
Show file tree
Hide file tree
Showing 10 changed files with 319 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yapp.core.auth.AuthToken;
import org.yapp.core.auth.AuthTokenGenerator;
import org.yapp.core.auth.jwt.JwtUtil;
import org.yapp.core.auth.token.RefreshTokenService;
import org.yapp.core.domain.user.RoleStatus;
import org.yapp.core.domain.user.User;
import org.yapp.domain.auth.application.oauth.OauthProvider;
import org.yapp.domain.auth.application.oauth.OauthProviderResolver;
import org.yapp.domain.auth.presentation.dto.request.OauthLoginRequest;
import org.yapp.domain.auth.presentation.dto.response.OauthLoginResponse;
import org.yapp.domain.setting.application.SettingService;
import org.yapp.domain.user.dao.UserRepository;

@Service
@RequiredArgsConstructor
public class OauthService {

private final OauthProviderResolver oauthProviderResolver;
private final JwtUtil jwtUtil;
private final UserRepository userRepository;
private final RefreshTokenService refreshTokenService;
private final AuthTokenGenerator authTokenGenerator;
private final SettingService settingService;

@Transactional
public OauthLoginResponse login(OauthLoginRequest request) {
OauthProvider oauthProvider = oauthProviderResolver.find(request.getProviderName());
String oauthId =
Expand All @@ -36,6 +38,7 @@ public OauthLoginResponse login(OauthLoginRequest request) {
User newUser = User.builder().oauthId(oauthId).role(RoleStatus.NONE.getStatus()).build();
User savedUser = userRepository.save(newUser);
Long userId = savedUser.getId();
settingService.createSetting(userId);
AuthToken token = authTokenGenerator.generate(userId, savedUser.getOauthId(), "NONE");
String accessToken = token.accessToken();
String refreshToken = token.refreshToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.yapp.core.domain.profile.Profile;
import org.yapp.core.domain.profile.ProfileValuePick;
import org.yapp.domain.match.application.MatchService;
Expand All @@ -20,97 +21,98 @@
@Component
public class GreedyMatchingAlgorithm implements MatchingAlgorithm {

private final ProfileValuePickService profileValuePickService;
private final MatchService matchService;
private final List<Blocker> blockers;
private final ProfileValuePickService profileValuePickService;
private final MatchService matchService;
private final List<Blocker> blockers;

@Override
public List<Profile> doMatch(List<Profile> profiles) {
PriorityQueue<Edge> priorityEdges = getPriorityEdges(profiles);
Set<Long> matchedSet = greedyMatch(priorityEdges);
List<Profile> unmatchedProfiles = new ArrayList<>();
for (Profile profile : profiles) {
if (!matchedSet.contains(profile.getUser().getId())) {
unmatchedProfiles.add(profile);
}
}
return unmatchedProfiles;
@Override
@Transactional
public List<Profile> doMatch(List<Profile> profiles) {
PriorityQueue<Edge> priorityEdges = getPriorityEdges(profiles);
Set<Long> matchedSet = greedyMatch(priorityEdges);
List<Profile> unmatchedProfiles = new ArrayList<>();
for (Profile profile : profiles) {
if (!matchedSet.contains(profile.getUser().getId())) {
unmatchedProfiles.add(profile);
}
}
return unmatchedProfiles;
}

private Set<Long> greedyMatch(PriorityQueue<Edge> priorityEdges) {
Set<Long> matchedSet = new HashSet<>();
while (!priorityEdges.isEmpty()) {
Edge edge = priorityEdges.poll();
if (matchedSet.contains(edge.user1Id) || matchedSet.contains(edge.user2Id)) {
continue;
}
matchedSet.add(edge.user1Id);
matchedSet.add(edge.user2Id);
private Set<Long> greedyMatch(PriorityQueue<Edge> priorityEdges) {
Set<Long> matchedSet = new HashSet<>();
while (!priorityEdges.isEmpty()) {
Edge edge = priorityEdges.poll();
if (matchedSet.contains(edge.user1Id) || matchedSet.contains(edge.user2Id)) {
continue;
}
matchedSet.add(edge.user1Id);
matchedSet.add(edge.user2Id);

matchService.createMatchInfo(edge.user1Id, edge.user2Id);
}
return matchedSet;
matchService.createMatchInfo(edge.user1Id, edge.user2Id);
}
return matchedSet;
}

private PriorityQueue<Edge> getPriorityEdges(List<Profile> profiles) {
PriorityQueue<Edge> priorityEdgeQueue = new PriorityQueue<Edge>();
for (Profile profile1 : profiles) {
for (Profile profile2 : profiles) {
if (profile1.getId().equals(profile2.getId())) {
continue;
}
if (checkBlock(profile1.getUser().getId(), profile2.getUser().getId())) {
continue;
}
int weight = calculateWeight(profile1.getId(), profile2.getId());
priorityEdgeQueue.add(
new Edge(weight, profile1.getUser().getId(), profile2.getUser().getId()));
}
private PriorityQueue<Edge> getPriorityEdges(List<Profile> profiles) {
PriorityQueue<Edge> priorityEdgeQueue = new PriorityQueue<Edge>();
for (Profile profile1 : profiles) {
for (Profile profile2 : profiles) {
if (profile1.getId().equals(profile2.getId())) {
continue;
}
if (checkBlock(profile1.getUser().getId(), profile2.getUser().getId())) {
continue;
}
return priorityEdgeQueue;
int weight = calculateWeight(profile1.getId(), profile2.getId());
priorityEdgeQueue.add(
new Edge(weight, profile1.getUser().getId(), profile2.getUser().getId()));
}
}
return priorityEdgeQueue;
}

private boolean checkBlock(Long blockingUserId, Long blockedUserId) {
for (Blocker blocker : blockers) {
boolean blocked =
blocker.blocked(blockingUserId, blockedUserId) || blocker.blocked(blockedUserId,
blockingUserId);
if (blocked) {
return true;
}
}
return false;
private boolean checkBlock(Long blockingUserId, Long blockedUserId) {
for (Blocker blocker : blockers) {
boolean blocked =
blocker.blocked(blockingUserId, blockedUserId) || blocker.blocked(blockedUserId,
blockingUserId);
if (blocked) {
return true;
}
}
return false;
}

private int calculateWeight(Long fromProfileId, Long toProfileId) {
List<ProfileValuePick> profileValuePicksOfFrom =
profileValuePickService.getAllProfileValuePicksByProfileId(fromProfileId);
List<ProfileValuePick> profileValuePicksOfTo = profileValuePickService.getAllProfileValuePicksByProfileId(
toProfileId);
private int calculateWeight(Long fromProfileId, Long toProfileId) {
List<ProfileValuePick> profileValuePicksOfFrom =
profileValuePickService.getAllProfileValuePicksByProfileId(fromProfileId);
List<ProfileValuePick> profileValuePicksOfTo = profileValuePickService.getAllProfileValuePicksByProfileId(
toProfileId);

int valueListSize = profileValuePicksOfFrom.size();
int sumOfWeight = 0;
for (int i = 0; i < valueListSize; i++) {
ProfileValuePick profileValuePickOfFrom = profileValuePicksOfFrom.get(i);
ProfileValuePick profileValuePickOfTo = profileValuePicksOfTo.get(i);
if (profileValuePickOfFrom.getSelectedAnswer()
.equals(profileValuePickOfTo.getSelectedAnswer())) {
sumOfWeight++;
}
}
return sumOfWeight;
int valueListSize = profileValuePicksOfFrom.size();
int sumOfWeight = 0;
for (int i = 0; i < valueListSize; i++) {
ProfileValuePick profileValuePickOfFrom = profileValuePicksOfFrom.get(i);
ProfileValuePick profileValuePickOfTo = profileValuePicksOfTo.get(i);
if (profileValuePickOfFrom.getSelectedAnswer()
.equals(profileValuePickOfTo.getSelectedAnswer())) {
sumOfWeight++;
}
}
return sumOfWeight;
}

@AllArgsConstructor
private static class Edge implements Comparable<Edge> {
@AllArgsConstructor
private static class Edge implements Comparable<Edge> {

private int weight;
private Long user1Id;
private Long user2Id;
private int weight;
private Long user1Id;
private Long user2Id;

@Override
public int compareTo(Edge o) {
return o.weight - this.weight;
}
@Override
public int compareTo(Edge o) {
return o.weight - this.weight;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,25 @@
import org.springframework.stereotype.Component;
import org.yapp.core.domain.user.User;
import org.yapp.domain.block.application.BlockContactService;
import org.yapp.domain.setting.application.SettingService;
import org.yapp.domain.user.application.UserService;

@Component
@RequiredArgsConstructor
public class AcquaintanceBasedBlocker implements Blocker {

private final BlockContactService blockContactService;
private final UserService userService;
private final BlockContactService blockContactService;
private final UserService userService;
private final SettingService settingService;

@Override
public boolean blocked(Long blockingUserId, Long blockedUserId) {
User user = userService.getUserById(blockingUserId);
return blockContactService.checkIfUserBlockedPhoneNumber(blockedUserId,
user.getPhoneNumber());
@Override
public boolean blocked(Long blockingUserId, Long blockedUserId) {
boolean acquaintanceBlockEnabled = settingService.isAcquaintanceBlockEnabled(blockingUserId);
if (!acquaintanceBlockEnabled) {
return false;
}
User user = userService.getUserById(blockingUserId);
return blockContactService.checkIfUserBlockedPhoneNumber(blockedUserId,
user.getPhoneNumber());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.yapp.domain.setting.application;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yapp.core.domain.setting.Setting;
import org.yapp.core.exception.ApplicationException;
import org.yapp.core.exception.error.code.SettingErrorCode;
import org.yapp.domain.setting.dao.SettingRepository;
import org.yapp.domain.setting.dto.response.SettingInfoResponse;

@Service
@RequiredArgsConstructor
public class SettingService {

private final SettingRepository settingRepository;

@Transactional
public void setNotificationStatus(Long userId, boolean status) {
Setting userSetting = getUserSetting(userId);
userSetting.updateNotification(status);
}

@Transactional
public void setMatchNotificationStatus(Long userId, boolean status) {
Setting userSetting = getUserSetting(userId);
userSetting.updateMatchNotification(status);
}

@Transactional
public void setAcquaintanceBlockStatus(Long userId, boolean status) {
Setting userSetting = getUserSetting(userId);
userSetting.updateAcquaintanceBlock(status);
}

@Transactional
public void createSetting(Long userId) {
Setting setting = new Setting(userId, true, true, true);
settingRepository.save(setting);
}

public Setting getUserSetting(Long userId) {
return settingRepository.findByUserId(userId).orElseThrow(() -> new ApplicationException(
SettingErrorCode.NOT_FOUND_SETTING));
}

public boolean isUserNotificationEnabled(Long userId) {
Setting userSetting = getUserSetting(userId);
return userSetting.isNotification();
}

public boolean isMatchNotificationEnabled(Long userId) {
Setting userSetting = getUserSetting(userId);
return userSetting.isNotification() && userSetting.isMatchNotification();
}

public boolean isAcquaintanceBlockEnabled(Long userId) {
Setting userSetting = getUserSetting(userId);
return userSetting.isAcquaintanceBlock();
}

public SettingInfoResponse getUserSettingInfo(Long userId) {
Setting userSetting = getUserSetting(userId);
return new SettingInfoResponse(userSetting.isNotification(), userSetting.isMatchNotification(),
userSetting.isAcquaintanceBlock());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.yapp.domain.setting.dao;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.yapp.core.domain.setting.Setting;

public interface SettingRepository extends JpaRepository<Setting, Long> {

Optional<Setting> findByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.yapp.domain.setting.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class SettingToggleRequest {

private Boolean toggle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.yapp.domain.setting.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class SettingInfoResponse {

private Boolean isNotificationEnabled;
private Boolean isMatchNotificationEnabled;
private Boolean isAcquaintanceBlockEnabled;
}
Loading

0 comments on commit 45501be

Please sign in to comment.