Skip to content

Commit

Permalink
[RELEASE] 1.0.0 1차 스프린트 개발사항 1차 배포
Browse files Browse the repository at this point in the history
[RELEASE] 1.0.0 1차 스프린트 개발사항 1차 배포
  • Loading branch information
ChaeAg authored Jan 12, 2025
2 parents 59476f3 + 90974d2 commit 06abb33
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/websoso/WSSServer/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
import org.websoso.WSSServer.domain.common.BaseEntity;
import org.websoso.WSSServer.domain.common.Gender;
import org.websoso.WSSServer.domain.common.Role;
import org.websoso.WSSServer.dto.user.EditMyInfoRequest;
Expand All @@ -39,7 +40,7 @@
name = "UNIQUE_SOCIAL_ID_CONSTRAINT",
columnNames = "social_id")
})
public class User {
public class User extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.websoso.WSSServer.domain.common;

public enum DiscordWebhookMessageType {
WITHDRAW, REPORT
WITHDRAW, REPORT, JOIN
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.websoso.WSSServer.domain.common;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum SocialLoginType {
KAKAO("카카오"), APPLE("애플");

private final String label;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.websoso.WSSServer.oauth2.service;

import static org.websoso.WSSServer.domain.common.DiscordWebhookMessageType.JOIN;
import static org.websoso.WSSServer.domain.common.SocialLoginType.APPLE;
import static org.websoso.WSSServer.exception.error.CustomAppleLoginError.CLIENT_SECRET_CREATION_FAILED;
import static org.websoso.WSSServer.exception.error.CustomAppleLoginError.EMPTY_JWT;
import static org.websoso.WSSServer.exception.error.CustomAppleLoginError.HEADER_PARSING_FAILED;
Expand Down Expand Up @@ -53,6 +55,7 @@
import org.websoso.WSSServer.domain.RefreshToken;
import org.websoso.WSSServer.domain.User;
import org.websoso.WSSServer.domain.UserAppleToken;
import org.websoso.WSSServer.domain.common.DiscordWebhookMessage;
import org.websoso.WSSServer.dto.auth.AppleLoginRequest;
import org.websoso.WSSServer.dto.auth.ApplePublicKey;
import org.websoso.WSSServer.dto.auth.ApplePublicKeys;
Expand All @@ -62,6 +65,8 @@
import org.websoso.WSSServer.repository.RefreshTokenRepository;
import org.websoso.WSSServer.repository.UserAppleTokenRepository;
import org.websoso.WSSServer.repository.UserRepository;
import org.websoso.WSSServer.service.MessageFormatter;
import org.websoso.WSSServer.service.MessageService;

@Transactional
@Service
Expand All @@ -81,6 +86,7 @@ public class AppleService {
private final UserRepository userRepository;
private final UserAppleTokenRepository userAppleTokenRepository;
private final JwtProvider jwtProvider;
private final MessageService messageService;

@Value("${apple.public-keys-url}")
private String applePublicKeysUrl;
Expand Down Expand Up @@ -278,9 +284,12 @@ private MultiValueMap<String, String> createTokenRequestParams(String authorizat
private AuthResponse authenticate(String socialId, String email, String nickname, String appleRefreshToken) {
User user = userRepository.findBySocialId(socialId);

boolean isNewUser = false;

if (user == null) {
user = userRepository.save(User.createBySocial(socialId, nickname, email));
userAppleTokenRepository.save(UserAppleToken.create(user, appleRefreshToken));
isNewUser = true;
}

UserAuthentication userAuthentication = new UserAuthentication(user.getUserId(), null, null);
Expand All @@ -291,6 +300,11 @@ private AuthResponse authenticate(String socialId, String email, String nickname

boolean isRegister = !user.getNickname().contains("*");

if (isNewUser) {
messageService.sendDiscordWebhookMessage(
DiscordWebhookMessage.of(MessageFormatter.formatUserJoinMessage(user, APPLE), JOIN));
}

return AuthResponse.of(accessToken, refreshToken, isRegister);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.websoso.WSSServer.oauth2.service;

import static org.websoso.WSSServer.domain.common.DiscordWebhookMessageType.JOIN;
import static org.websoso.WSSServer.domain.common.SocialLoginType.KAKAO;
import static org.websoso.WSSServer.exception.error.CustomKakaoError.INVALID_KAKAO_ACCESS_TOKEN;
import static org.websoso.WSSServer.exception.error.CustomKakaoError.KAKAO_SERVER_ERROR;

Expand All @@ -16,11 +18,14 @@
import org.websoso.WSSServer.config.jwt.UserAuthentication;
import org.websoso.WSSServer.domain.RefreshToken;
import org.websoso.WSSServer.domain.User;
import org.websoso.WSSServer.domain.common.DiscordWebhookMessage;
import org.websoso.WSSServer.dto.auth.AuthResponse;
import org.websoso.WSSServer.exception.exception.CustomKakaoException;
import org.websoso.WSSServer.oauth2.dto.KakaoUserInfo;
import org.websoso.WSSServer.repository.RefreshTokenRepository;
import org.websoso.WSSServer.repository.UserRepository;
import org.websoso.WSSServer.service.MessageFormatter;
import org.websoso.WSSServer.service.MessageService;

@Service
@Transactional
Expand All @@ -30,6 +35,7 @@ public class KakaoService {
private final UserRepository userRepository;
private final RefreshTokenRepository refreshTokenRepository;
private final JwtProvider jwtProvider;
private final MessageService messageService;

@Value("${kakao.user-info-url}")
private String kakaoUserInfoUrl;
Expand Down Expand Up @@ -63,9 +69,11 @@ public AuthResponse getUserInfoFromKakao(String kakaoAccessToken) {
String socialId = "kakao_" + kakaoUserInfo.id();
String defaultNickname = "k*" + kakaoUserInfo.id().toString().substring(2, 10);

boolean isNewUser = false;
User user = userRepository.findBySocialId(socialId);
if (user == null) {
user = userRepository.save(User.createBySocial(socialId, defaultNickname, kakaoUserInfo.email()));
isNewUser = true;
}

UserAuthentication userAuthentication = new UserAuthentication(user.getUserId(), null, null);
Expand All @@ -77,6 +85,10 @@ public AuthResponse getUserInfoFromKakao(String kakaoAccessToken) {

boolean isRegister = !user.getNickname().contains("*");

if (isNewUser) {
messageService.sendDiscordWebhookMessage(
DiscordWebhookMessage.of(MessageFormatter.formatUserJoinMessage(user, KAKAO), JOIN));
}
return AuthResponse.of(accessToken, refreshToken, isRegister);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface FeedCategoryRepository extends JpaRepository<FeedCategory, Long

List<FeedCategory> findByFeed(Feed feed);

void deleteByCategory(Category category);
void deleteByCategoryAndFeed(Category category, Feed feed);

@Query(value = "SELECT fc.feed FROM FeedCategory fc "
+ "WHERE fc.category = :category "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void updateFeedCategory(Feed feed, List<String> relevantCategories) {
}

for (Category category : categories) {
feedcategoryRepository.deleteByCategory(category);
feedcategoryRepository.deleteByCategoryAndFeed(category, feed);
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/websoso/WSSServer/service/MessageFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.websoso.WSSServer.domain.Feed;
import org.websoso.WSSServer.domain.User;
import org.websoso.WSSServer.domain.common.ReportedType;
import org.websoso.WSSServer.domain.common.SocialLoginType;

public class MessageFormatter {

Expand Down Expand Up @@ -39,6 +40,13 @@ public class MessageFormatter {
"유저 닉네임 : %s\n\n" +
"[탈퇴 사유]\n%s\n\n```";

private static final String USER_JOIN_MESSAGE =
"```[%s] 새로운 사용자가 가입하였습니다.\n\n" +
"[가입한 사용자]\n" +
"로그인 방식 : %s\n" +
"유저 아이디 : %d\n" +
"가입을 환영합니다!\n\n```";

public static String formatFeedReportMessage(Feed feed, ReportedType reportedType, int reportedCount,
boolean isHidden) {
String hiddenMessage = isHidden ? "해당 피드는 숨김 처리되었습니다." : "해당 피드는 숨김 처리되지 않았습니다.";
Expand Down Expand Up @@ -87,4 +95,13 @@ public static String formatUserWithdrawMessage(Long userId, String userNickname,
reason
);
}

public static String formatUserJoinMessage(User user, SocialLoginType socialLoginType) {
return String.format(
USER_JOIN_MESSAGE,
user.getCreatedDate(),
socialLoginType.getLabel(),
user.getUserId()
);
}
}
18 changes: 14 additions & 4 deletions src/main/java/org/websoso/WSSServer/service/MessageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpStatus.NO_CONTENT;
import static org.websoso.WSSServer.domain.common.DiscordWebhookMessageType.JOIN;
import static org.websoso.WSSServer.domain.common.DiscordWebhookMessageType.REPORT;
import static org.websoso.WSSServer.domain.common.DiscordWebhookMessageType.WITHDRAW;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -19,21 +21,29 @@ public class MessageService {

@Value("${logging.discord.report-webhook-url}")
private String discordReportWebhookUrl;

@Value("${logging.discord.withdraw-webhook-url}")
private String discordWithdrawWebhookUrl;
@Value("${logging.discord.join-webhook-url}")
private String discordJoinWebhookUrl;

public void sendDiscordWebhookMessage(DiscordWebhookMessage message) {
try {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type", "application/json; utf-8");
HttpEntity<DiscordWebhookMessage> messageEntity = new HttpEntity<>(message, httpHeaders);
String webhookUrl = "";

if (message.type() == REPORT) {
webhookUrl = discordReportWebhookUrl;
} else if (message.type() == WITHDRAW) {
webhookUrl = discordWithdrawWebhookUrl;
} else if (message.type() == JOIN) {
webhookUrl = discordJoinWebhookUrl;
}

RestTemplate template = new RestTemplate();
ResponseEntity<String> response = template.exchange(
message.type() == REPORT ?
discordReportWebhookUrl :
discordWithdrawWebhookUrl,
webhookUrl,
POST,
messageEntity,
String.class
Expand Down

0 comments on commit 06abb33

Please sign in to comment.