From 4575174a8a1398898802d6d1390d1ba6869b2dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=95=9C=EA=B4=80=ED=9D=AC?= Date: Fri, 21 Jun 2024 15:33:32 +0900 Subject: [PATCH] =?UTF-8?q?feat(Slack):=20=EC=83=88=20=EA=B2=8C=EC=8B=9C?= =?UTF-8?q?=EA=B8=80=20=EC=95=8C=EB=A6=BC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/application/BoardService.java | 4 +++ .../clab/api/domain/board/domain/Board.java | 1 + .../slack/application/SlackService.java | 5 ++++ .../slack/application/SlackServiceHelper.java | 25 +++++++++++++++++++ .../common/slack/domain/GeneralAlertType.java | 1 + 5 files changed, 36 insertions(+) diff --git a/src/main/java/page/clab/api/domain/board/application/BoardService.java b/src/main/java/page/clab/api/domain/board/application/BoardService.java index 5462632f4..4c9427fda 100644 --- a/src/main/java/page/clab/api/domain/board/application/BoardService.java +++ b/src/main/java/page/clab/api/domain/board/application/BoardService.java @@ -24,6 +24,7 @@ import page.clab.api.global.common.dto.PagedResponseDto; import page.clab.api.global.common.file.application.UploadedFileService; import page.clab.api.global.common.file.domain.UploadedFile; +import page.clab.api.global.common.slack.application.SlackService; import page.clab.api.global.exception.NotFoundException; import page.clab.api.global.exception.PermissionDeniedException; import page.clab.api.global.validation.ValidationService; @@ -43,6 +44,8 @@ public class BoardService { private final ValidationService validationService; + private final SlackService slackService; + private final BoardRepository boardRepository; private final BoardLikeRepository boardLikeRepository; @@ -59,6 +62,7 @@ public String createBoard(BoardRequestDto requestDto) throws PermissionDeniedExc if (board.shouldNotifyForNewBoard()) { notificationService.sendNotificationToMember(currentMember, "[" + board.getTitle() + "] 새로운 공지사항이 등록되었습니다."); } + slackService.sendNewBoardNotification(board); return boardRepository.save(board).getCategory().getKey(); } diff --git a/src/main/java/page/clab/api/domain/board/domain/Board.java b/src/main/java/page/clab/api/domain/board/domain/Board.java index 99847c588..44db8437b 100644 --- a/src/main/java/page/clab/api/domain/board/domain/Board.java +++ b/src/main/java/page/clab/api/domain/board/domain/Board.java @@ -69,6 +69,7 @@ public class Board extends BaseEntity { private String imageUrl; + @Getter @Column(nullable = false) private boolean wantAnonymous; diff --git a/src/main/java/page/clab/api/global/common/slack/application/SlackService.java b/src/main/java/page/clab/api/global/common/slack/application/SlackService.java index f7d2d949c..8963b6f54 100644 --- a/src/main/java/page/clab/api/global/common/slack/application/SlackService.java +++ b/src/main/java/page/clab/api/global/common/slack/application/SlackService.java @@ -8,6 +8,7 @@ import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; import page.clab.api.domain.application.dto.request.ApplicationRequestDto; +import page.clab.api.domain.board.domain.Board; import page.clab.api.domain.member.domain.Member; import page.clab.api.global.common.slack.domain.GeneralAlertType; import page.clab.api.global.common.slack.domain.SecurityAlertType; @@ -36,6 +37,10 @@ public void sendNewApplicationNotification(ApplicationRequestDto applicationRequ eventPublisher.publishEvent(new NotificationEvent(this, GeneralAlertType.APPLICATION_CREATED, null, applicationRequestDto)); } + public void sendNewBoardNotification(Board board) { + eventPublisher.publishEvent(new NotificationEvent(this, GeneralAlertType.BOARD_CREATED, null, board)); + } + @EventListener(ContextRefreshedEvent.class) public void sendServerStartNotification() { eventPublisher.publishEvent(new NotificationEvent(this, GeneralAlertType.SERVER_START, null, null)); diff --git a/src/main/java/page/clab/api/global/common/slack/application/SlackServiceHelper.java b/src/main/java/page/clab/api/global/common/slack/application/SlackServiceHelper.java index 7a9c7cefe..6dc40941b 100644 --- a/src/main/java/page/clab/api/global/common/slack/application/SlackServiceHelper.java +++ b/src/main/java/page/clab/api/global/common/slack/application/SlackServiceHelper.java @@ -21,6 +21,7 @@ import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import page.clab.api.domain.application.dto.request.ApplicationRequestDto; +import page.clab.api.domain.board.domain.Board; import page.clab.api.domain.member.domain.Member; import page.clab.api.global.common.slack.domain.AlertType; import page.clab.api.global.common.slack.domain.GeneralAlertType; @@ -46,10 +47,15 @@ public class SlackServiceHelper { private final Slack slack; + private final String webhookUrl; + private final String webUrl; + private final String apiUrl; + private final String color; + private final Environment environment; private final AttributeStrategy attributeStrategy; @@ -106,6 +112,11 @@ public List createBlocks(AlertType alertType, HttpServletRequest re return createApplicationBlocks((ApplicationRequestDto) additionalData); } break; + case BOARD_CREATED: + if (additionalData instanceof Board) { + return createBoardBlocks((Board) additionalData); + } + break; case SERVER_START: return createServerStartBlocks(); case SERVER_ERROR: @@ -194,6 +205,20 @@ private List createApplicationBlocks(ApplicationRequestDto requestD return blocks; } + private List createBoardBlocks(Board board) { + List blocks = new ArrayList<>(); + String username = board.isWantAnonymous() ? + board.getNickname() : board.getMember().getId() + " " + board.getMember().getName(); + + blocks.add(section(section -> section.text(markdownText(":mag: *New Board*")))); + blocks.add(section(section -> section.fields(Arrays.asList( + markdownText("*Title:*\n" + board.getTitle()), + markdownText("*Category:*\n" + board.getCategory().getDescription()), + markdownText("*User:*\n" + username) + )))); + return blocks; + } + private List createServerStartBlocks() { String osInfo = System.getProperty("os.name") + " " + System.getProperty("os.version"); String jdkVersion = System.getProperty("java.version"); diff --git a/src/main/java/page/clab/api/global/common/slack/domain/GeneralAlertType.java b/src/main/java/page/clab/api/global/common/slack/domain/GeneralAlertType.java index d653e3eea..42444236e 100644 --- a/src/main/java/page/clab/api/global/common/slack/domain/GeneralAlertType.java +++ b/src/main/java/page/clab/api/global/common/slack/domain/GeneralAlertType.java @@ -9,6 +9,7 @@ public enum GeneralAlertType implements AlertType { ADMIN_LOGIN("관리자 로그인", "Admin login."), APPLICATION_CREATED("새 지원서", "New application has been submitted."), + BOARD_CREATED("새 게시글", "New board has been created."), SERVER_START("서버 시작", "Server has been started."), SERVER_ERROR("서버 에러", "Server error occurred.");