Skip to content

Commit

Permalink
feat(Slack): 새 게시글 알림 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
limehee committed Jun 21, 2024
1 parent 33cae77 commit 4575174
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +44,8 @@ public class BoardService {

private final ValidationService validationService;

private final SlackService slackService;

private final BoardRepository boardRepository;

private final BoardLikeRepository boardLikeRepository;
Expand All @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/page/clab/api/domain/board/domain/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class Board extends BaseEntity {

private String imageUrl;

@Getter
@Column(nullable = false)
private boolean wantAnonymous;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -106,6 +112,11 @@ public List<LayoutBlock> 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:
Expand Down Expand Up @@ -194,6 +205,20 @@ private List<LayoutBlock> createApplicationBlocks(ApplicationRequestDto requestD
return blocks;
}

private List<LayoutBlock> createBoardBlocks(Board board) {
List<LayoutBlock> 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<LayoutBlock> createServerStartBlocks() {
String osInfo = System.getProperty("os.name") + " " + System.getProperty("os.version");
String jdkVersion = System.getProperty("java.version");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down

0 comments on commit 4575174

Please sign in to comment.