Skip to content

Commit

Permalink
피드 신고시 discord 알림 기능 구현 (#290)
Browse files Browse the repository at this point in the history
* feature: 피드 신고시 discord 알림 기능 구현

* fix: properties사용하는방법으로 수정

* fix: comment기능 merge
  • Loading branch information
dbscks97 authored Oct 2, 2024
1 parent 5b85d1e commit b7608cb
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.depromeet.stonebed.domain.discord.application;

import com.depromeet.stonebed.global.error.ErrorCode;
import com.depromeet.stonebed.global.error.exception.CustomException;
import com.depromeet.stonebed.infra.properties.DiscordProperties;
import java.util.HashMap;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.RestClient;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class DiscordNotificationService {

private final DiscordProperties discordProperties;
private final RestClient restClient;

public void sendDiscordMessage(String message) {
Map<String, String> payload = new HashMap<>();
payload.put("content", message);

try {
String discordWebhookUrl = discordProperties.url();
log.info("Sending Discord notification to URL: {}", discordWebhookUrl);

restClient
.post()
.uri(discordWebhookUrl)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(payload)
.exchange(
(request, response) -> {
if (!response.getStatusCode().is2xxSuccessful()) {
throw new CustomException(
ErrorCode.DISCORD_NOTIFICATION_FAILED);
}
log.info("Discord 알림 전송 성공: {}", message);
return response.bodyTo(String.class);
});

} catch (Exception e) {
log.error("Discord 알림 전송 중 예외 발생: {}", message, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.depromeet.stonebed.domain.report.application;

import com.depromeet.stonebed.domain.discord.application.DiscordNotificationService;
import com.depromeet.stonebed.domain.member.domain.Member;
import com.depromeet.stonebed.domain.missionRecord.dao.MissionRecordRepository;
import com.depromeet.stonebed.domain.missionRecord.domain.MissionRecord;
Expand All @@ -9,6 +10,7 @@
import com.depromeet.stonebed.global.error.ErrorCode;
import com.depromeet.stonebed.global.error.exception.CustomException;
import com.depromeet.stonebed.global.util.MemberUtil;
import java.time.format.DateTimeFormatter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -17,22 +19,64 @@
@RequiredArgsConstructor
@Transactional
public class ReportService {
private static final DateTimeFormatter DATE_TIME_FORMATTER =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private final ReportRepository reportRepository;
private final MissionRecordRepository missionRecordRepository;
private final MemberUtil memberUtil;
private final DiscordNotificationService discordNotificationService;

public void reportFeed(ReportRequest reportRequest) {
final Member member = memberUtil.getCurrentMember();
final Member reporter = memberUtil.getCurrentMember();

MissionRecord missionRecord =
missionRecordRepository
.findById(reportRequest.recordId())
.orElseThrow(() -> new CustomException(ErrorCode.MISSION_RECORD_NOT_FOUND));

Member reportedMember = missionRecord.getMember();

Report report =
Report.createReport(
missionRecord, member, reportRequest.reason(), reportRequest.details());
missionRecord, reporter, reportRequest.reason(), reportRequest.details());

reportRepository.save(report);

sendReportNotificationToDiscord(reporter, reportedMember, missionRecord, reportRequest);
}

private void sendReportNotificationToDiscord(
Member reporter,
Member reportedMember,
MissionRecord missionRecord,
ReportRequest reportRequest) {
String reportTime = java.time.LocalDateTime.now().format(DATE_TIME_FORMATTER);

String message =
String.format(
"🚨 **신고 접수 알림** 🚨\n\n"
+ "**-- 신고자 정보 --**\n"
+ "**닉네임**: %s\n"
+ "**신고 시간**: %s\n\n"
+ "**-- 신고 상세 내용 --**\n"
+ "**신고 사유**: %s\n"
+ "**신고 내용**: %s\n\n"
+ "**-- 신고 대상 정보 --**\n"
+ "**닉네임**: %s\n"
+ "**게시글 ID**: %d\n"
+ "**게시글 이미지 URL**: %s\n"
+ "**게시글 내용**: %s",
reporter.getProfile().getNickname(),
reportTime,
reportRequest.reason(),
reportRequest.details(),
reportedMember.getProfile().getNickname(),
missionRecord.getId(),
missionRecord.getImageUrl() != null
? missionRecord.getImageUrl()
: "이미지 없음",
missionRecord.getContent() != null ? missionRecord.getContent() : "내용 없음");

discordNotificationService.sendDiscordMessage(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ public enum ErrorCode {
// report
INVALID_REPORT_REASON(HttpStatus.NOT_FOUND, "해당 신고 사유를 찾을 수 없습니다."),

DISCORD_NOTIFICATION_FAILED(HttpStatus.BAD_REQUEST, "디스코드 알림 전송이 실패했습니다."),

// comment
COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 댓글을 찾을 수 없습니다."),
;
COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 댓글을 찾을 수 없습니다.");

private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.depromeet.stonebed.infra.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "discord")
public record DiscordProperties(String url) {}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
JwtProperties.class,
AppleProperties.class,
SwaggerProperties.class,
SqsProperties.class
SqsProperties.class,
DiscordProperties.class
})
@Configuration
public class PropertiesConfig {}
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ logging:
com.depromeet.stonebed.*.*.application.*: debug
org.hibernate.SQL: debug
org.hibernate.type: trace

discord:
url: ${DISCORD_WEBHOOK_URL}

0 comments on commit b7608cb

Please sign in to comment.