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

[✨ feat/#144] Discord 회원가입 알림 구현 #151

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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 @@ -14,4 +14,5 @@ public interface UserRepository extends JpaRepository<User, Long> {

Optional<User> findByAuthIdAndAuthType(String authId, AuthType authType);


}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class AuthServiceImpl implements AuthService {
private final ValueConfig valueConfig;
private final UserRepository userRepository;
private final FilterRepository filterRepository;
private final WebhookService webhookService;

@Override
@Transactional
Expand All @@ -62,6 +63,8 @@ public SignUpResponseDto signUp(String authId, SignUpRequestDto request) {

Token token = getFullToken(user);

webhookService.sendDiscordNotification(user); // 디스코드에 회원가입 알림 전송

return createSignUpResponseDto(token, user);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.terning.terningserver.service;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.terning.terningserver.domain.User;
import org.terning.terningserver.repository.user.UserRepository;

import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@Service
@Transactional
public class WebhookService {

// 디스코드 웹훅 URL 주입
@Value("${discord.webhook.url}")
Copy link
Member

@JungYoonShin JungYoonShin Sep 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 yml 파일에 해당 코드 관련해서 설정 추가해야하는거 맞을까요?!

private String discordWebhookUrl;

private final UserRepository userRepository;

// 알림을 보내는 메서드
public void sendDiscordNotification(User user) {

// REST 요청을 처리하기 위한 RestTemplate 객체 생성
RestTemplate restTemplate = new RestTemplate();

// 회원 수를 기존 DB에서 조회하여 총 회원 수 계산
Long totalMembers = userRepository.count();

// 알림 메시지 생성
String message = String.format("가입자명 : %s\n[%d] 번째 유저가 회원가입했습니다!", user.getName(), totalMembers);

// HTTP 요청을 위한 헤더 설정
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

// HTTP 요청 바디에 전송할 메시지 설정
Map<String, String> body = new HashMap<>();
body.put("content", message);

// HTTP 요청 엔터티 생성
HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(body, headers);

// 디스코드 웹훅 URL로 POST 요청을 보내어 알림 전송
restTemplate.postForEntity(discordWebhookUrl, requestEntity, String.class);
}
}
Loading