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

๐Ÿ”€ :: (#230) ํ”ผ๋“œ ์‹ ๊ณ  Slack ์—ฐ๋™ #114

Merged
merged 14 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
f4bb995
โšก๏ธ :: webhook ์˜์กด์„ฑ ์ถ”๊ฐ€
jeongho1209 Sep 6, 2023
0d75b2a
๐Ÿ“‘ :: AsyncConfig ์ถ”๊ฐ€
jeongho1209 Sep 6, 2023
6afbf8d
โ™ป๏ธ :: ์ฝ”๋“œ ์ •๋ ฌ
jeongho1209 Sep 6, 2023
bd52ec0
โšก๏ธ :: webhook url ์ถ”๊ฐ€
jeongho1209 Sep 6, 2023
d38a82a
๐Ÿ“‘ :: SlackReport ์ถ”๊ฐ€
jeongho1209 Sep 6, 2023
5b0ca08
โšก๏ธ :: ํ”ผ๋“œ ์‹ ๊ณ ์‹œ slack ์•Œ๋žŒ ๊ธฐ๋Šฅ ์ถ”๊ฐ€
jeongho1209 Sep 6, 2023
2454e6d
โšก๏ธ :: ์•„์ด๋””๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒ API ์ถ”๊ฐ€
jeongho1209 Sep 7, 2023
324da78
๐Ÿ“‘ :: UserNotFoundException ์ถ”๊ฐ€
jeongho1209 Sep 7, 2023
ec14fc7
โšก๏ธ :: ์•„์ด๋””๋กœ ์‚ฌ์šฉ์ž ์ •๋ณด ์กฐํšŒ API ์ถ”๊ฐ€
jeongho1209 Sep 7, 2023
93990a0
โ™ป๏ธ :: ๋‹จ์ผ ์•„์ด๋””๋กœ ์ •๋ณด ์กฐํšŒํ•˜๋„๋ก ์ˆ˜์ •
jeongho1209 Sep 7, 2023
c8899c6
โ™ป๏ธ :: ํ•„์š”์—†๋Š” ๊ณต๋ฐฑ ์ œ๊ฑฐ
jeongho1209 Sep 7, 2023
288f386
โ™ป๏ธ :: ํ•„์š”์—†๋Š” ์ค„ ์ œ๊ฑฐ
jeongho1209 Sep 9, 2023
aa7511d
โ™ป๏ธ :: EventListener ๊ตฌ์กฐ๋กœ ์ˆ˜์ •
jeongho1209 Sep 9, 2023
0f88bfe
โ™ป๏ธ :: import ์ •๋ฆฌ
jeongho1209 Sep 9, 2023
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 @@ -56,13 +56,13 @@ public void saveComment(CreateCommentDomainRequest request) {
.build()
);

if(!feed.getUserId().equals(userId)) {
if (!feed.getUserId().equals(userId)) {
sendNotification(feed);
}
}

private void sendNotification(Feed feed) {
if(CategoryEnum.NOTICE.getName().equals(categorySpi.queryCategoryById(feed.getCategoryId()).getName())) {
if (CategoryEnum.NOTICE.getName().equals(categorySpi.queryCategoryById(feed.getCategoryId()).getName())) {
commentNotificationUtil.sendNotification(feed, FEED_NOTICE_COMMENT);
} else {
commentNotificationUtil.sendNotification(feed, FEED_BAMBOO_COMMENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,51 @@

import com.xquare.v1servicefeed.annotation.DomainService;
import com.xquare.v1servicefeed.configuration.spi.SecuritySpi;
import com.xquare.v1servicefeed.feed.Feed;
import com.xquare.v1servicefeed.feed.spi.QueryFeedSpi;
import com.xquare.v1servicefeed.report.Report;
import com.xquare.v1servicefeed.report.api.ReportApi;
import com.xquare.v1servicefeed.report.api.dto.CreateReportDomainRequest;
import com.xquare.v1servicefeed.report.exception.CannotReportMyFeedException;
import com.xquare.v1servicefeed.report.spi.CommandReportSpi;
import com.xquare.v1servicefeed.feed.Feed;
import com.xquare.v1servicefeed.feed.spi.QueryFeedSpi;
import com.xquare.v1servicefeed.user.User;
import com.xquare.v1servicefeed.user.exception.UserNotFoundException;
import com.xquare.v1servicefeed.user.spi.FeedUserSpi;
import com.xquare.v1servicefeed.webhook.SlackReport;
import com.xquare.v1servicefeed.webhook.spi.SendWebhookSpi;
import lombok.RequiredArgsConstructor;

import java.util.UUID;

@RequiredArgsConstructor
@DomainService
public class ReportApiImpl implements ReportApi {

private final QueryFeedSpi queryFeedSpi;
private final CommandReportSpi commandReportSpi;
private final SecuritySpi securitySpi;
private final SendWebhookSpi sendWebhookSpi;
private final FeedUserSpi feedUserSpi;

@Override
public void saveReport(CreateReportDomainRequest request) {
Feed feed = queryFeedSpi.queryFeedById(request.getFeedId());
UUID userId = securitySpi.getCurrentUserId();
User user = feedUserSpi.queryUserById(securitySpi.getCurrentUserId())
.orElseThrow(() -> UserNotFoundException.EXCEPTION);

if(userId.equals(feed.getUserId())) {
if (user.getId().equals(feed.getUserId())) {
throw CannotReportMyFeedException.EXCEPTION;
}

commandReportSpi.saveReport(
Report.builder()
.userId(userId)
.userId(user.getId())
.reportUserId(feed.getUserId())
.feedId(feed.getId())
.content(request.getContent())
.build()
);

sendWebhookSpi.sendReportToSlack(
new SlackReport(feed.getContent(), user.getName(), request.getContent())
);
Comment on lines +48 to +50
Copy link
Member

Choose a reason for hiding this comment

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

Spring EventListener ์•Œ์•„๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š” :)

Copy link
Member Author

Choose a reason for hiding this comment

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

๋„ต!

Copy link
Member

Choose a reason for hiding this comment

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

์•Œ์•„๋ดค๋‚˜์œ ? ์–ด๋–ค๊ฒƒ๊ฐ™์•„์š”??

Copy link
Member Author

Choose a reason for hiding this comment

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

์ œ๊ฐ€ ์ „์— report๋ผ๋Š” ๋ ˆํฌ์ง€ํ† ๋ฆฌ์—์„œ๋„ ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋˜ ๊ฒฝํ—˜์ด ์žˆ์–ด์„œ ๊ทธ๋•Œ๋„ Event Listener๋ฅผ ์‚ฌ์šฉํ• ๊นŒ ๊ณ ๋ฏผํ–ˆ์—ˆ๋Š”๋ฐ
api ์™„๋ฃŒ๋˜๋Š”๋ฐ๊นŒ์ง€ ์‹œ๊ฐ„๋งŒ ์ตœ์ ํ™”ํ•˜๋ ค๋ฉด @async๋งŒ ์‚ฌ์šฉํ•ด๋„ ๊ดœ์ฐฎ์„๊ฑฐ ๊ฐ™๋‹ค ์ƒ๊ฐํ–ˆ์—ˆ๋Š”๋ฐ ํ˜น์‹œ ์–ด๋–ป๊ฒŒ ์ƒ๊ฐํ•˜์‹œ๋‚˜์š”?

Copy link
Member

Choose a reason for hiding this comment

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

์—ฌ๊ธฐ์„œ ๋‚ด๊ฐ€ ๋งŒ์•ฝ EventListener๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์‹œ๊ฐ„ ์ตœ์ ํ™”๋ฅผ ์ด์œ ๋กœ ์‚ฌ์šฉํ•˜์ง€๋Š” ์•Š์„ ๊ฒƒ ๊ฐ™์Œ
๋– ์˜ค๋ฅด๋Š”๊ฑฐ ์—†์Œ?

Copy link
Member

Choose a reason for hiding this comment

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

์‹ ๊ณ  ์ €์žฅ ์‹คํŒจํ•˜๋ฉด ๊ธฐ์กด๋„ Slack์— ์•Œ๋žŒ ์•ˆ๊ฐˆ๊ฑฐ๊ฐ™์€๋””?

Copy link
Member

Choose a reason for hiding this comment

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

๋‚˜๋Š” ์˜์กด์„ฑ ์ด์•ผ๊ธฐํ•œ๋ถ€๋ถ„์ด๊ธด ํ•จ
์‹ ๊ณ ๋ฅผ ์ €์žฅํ•˜๋Š” ์• ๋Š” ์Šฌ๋ž™์„ ๋ณด๋‚ด๋Š”์• ๋ฅผ ๊ตณ์ด ์„ธ๋ถ€์ ์œผ๋กœ ์•Œ์•„์•ผํ•  ์ด์œ ๊ฐ€ ์—†๋Š”๋ฐ, ์˜์กดํ•˜๊ณ ์žˆ์œผ๋‹ˆ EventListener๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์˜์กด์„ฑ์„ ์—†์•จ ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ(Dto ๋นผ๊ณ ) ์ง€๊ธˆ๊ฐ™์€ ๊ตฌ์กฐ๋Š” ๊ดœ์ฐฎ์€๋ฐ, ๋‹จ๋ฐฉํ–ฅ ์˜์กด์œผ๋กœ ๋ชจ๋“ˆ๋ณ„ ์˜์กด์ด ์žˆ์„ ๋•Œ ๊ทธ ํ๋ฆ„์„ ์—ญํ–‰ํ•ด์„œ ๋ญ”๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด์•ผํ•  ๋•Œ๊ฐ€ ์žˆ๋Š”๋ฐ, ๊ทธ๋Ÿด ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐœ๊ฟ€์ž„

Copy link
Member Author

Choose a reason for hiding this comment

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

๊ทธ๋Ÿฌ๋ฉด ํ•œ ๋ฒˆ ์ ์šฉํ•ด์„œ ์ˆ˜์ •ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!

Copy link
Member

Choose a reason for hiding this comment

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

๋‚œ ์ข€ ๋‹ค๋ฅธ ๊ด€์ ์ด๊ธด ํ•จ
slack ์žฅ์• ์‹œ์— ์‹ ๊ณ  ์„œ๋น„์Šค์— ์˜ํ–ฅ์„ ์ค˜์•ผํ•˜๋‚˜? ์ง€๊ธˆ ์ƒํƒœ๋Œ€๋กœ๋ฉด ์Šฌ๋ž™ ์žฅ์• ์‹œ์— ์‹ ๊ณ  ๊ธฐ๋Šฅ์ด ๋จนํ†ต์ผํ…๋ฐ ๊ดœ์ฐฎ์€๊ฐ€?๋„ ๊ณ ๋ฏผํ•ด๋ณด๋ฉด ์ข‹์„๋“ฏ

Copy link
Member Author

Choose a reason for hiding this comment

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

๋„ต!

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public enum UserErrorCode implements ExceptionProperty {

FORBIDDEN_USER(403, "Forbidden User"),

INVALID_ROLE(403, "Invalid Role");
INVALID_ROLE(403, "Invalid Role"),

USER_NOT_FOUND(404, "User Not Found");

private final int status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.xquare.v1servicefeed.user.exception;

import com.xquare.v1servicefeed.error.FeedException;
import com.xquare.v1servicefeed.user.error.UserErrorCode;

public class UserNotFoundException extends FeedException {

public static final FeedException EXCEPTION =
new UserNotFoundException();

private UserNotFoundException() {
super(UserErrorCode.USER_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import com.xquare.v1servicefeed.user.role.UserRole;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Spi
public interface FeedUserSpi {
List<User> queryUserByIds(List<UUID> ids);
void validateUserId(UUID userId, UUID currentUserId);
List<User> queryAllUserByRole(String role);

Optional<User> queryUserById(UUID id);
jeongho1209 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.xquare.v1servicefeed.webhook;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class SlackReport {

private final String feedContent;

private final String userName;

private final String reportContent;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.xquare.v1servicefeed.webhook.spi;

import com.xquare.v1servicefeed.annotation.Spi;
import com.xquare.v1servicefeed.webhook.SlackReport;

@Spi
public interface SendWebhookSpi {

void sendReportToSlack(SlackReport slackReport);
}
3 changes: 3 additions & 0 deletions feed-infrastructure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ dependencies {
// message
implementation 'org.springframework.cloud:spring-cloud-aws-messaging:2.2.6.RELEASE'

// Webhook
implementation 'net.gpedro.integrations.slack:slack-webhook:1.4.0'

implementation(project(":feed-domain"))

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.xquare.v1servicefeed.configuration.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableAsync
@Configuration
public class AsyncConfig {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.xquare.v1servicefeed.feign.client;

import com.xquare.v1servicefeed.feign.client.dto.response.UserInfoElement;
import com.xquare.v1servicefeed.feign.client.dto.response.UserInfoResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
Expand All @@ -16,4 +18,7 @@ public interface UserClient {

@GetMapping("/users/role")
UserInfoResponse getStudent(@RequestParam(value = "roleName", required = false) String role);

@GetMapping("/users/id/{userId}")
UserInfoElement getUserInfoByUserId(@PathVariable("userId") UUID userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@RequiredArgsConstructor
Expand Down Expand Up @@ -69,4 +70,23 @@ public List<User> queryAllUserByRole(String role) {
.build())
.toList();
}

@Override
public Optional<User> queryUserById(UUID id) {
UserInfoElement userInfoElement = userClient.getUserInfoByUserId(id);

return Optional.of(
User.builder()
.id(userInfoElement.getId())
.accountId(userInfoElement.getAccountId())
.name(userInfoElement.getName())
.password(userInfoElement.getPassword())
.birthDay(userInfoElement.getBirthDay())
.grade(userInfoElement.getGrade())
.classNum(userInfoElement.getClassNum())
.num(userInfoElement.getNum())
.profileFileName(userInfoElement.getProfileFileName())
.build()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.xquare.v1servicefeed.webhook;

import com.xquare.v1servicefeed.webhook.spi.SendWebhookSpi;
import lombok.RequiredArgsConstructor;
import net.gpedro.integrations.slack.SlackApi;
import net.gpedro.integrations.slack.SlackAttachment;
import net.gpedro.integrations.slack.SlackMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class SendWebhookAdapter implements SendWebhookSpi {

private static final String REPORT_MESSAGE = "ํ”ผ๋“œ ์‹ ๊ณ  ๋ฐœ์ƒ";
private static final String REPORT_USER_NAME = "์‹ ๊ณ ์ž";
private static final String REPORT_CONTENT = "์‹ ๊ณ  ๋‚ด์šฉ";
private static final String REPORT_FEED_CONTENT = "์‹ ๊ณ ํ•œ ๊ฒŒ์‹œ๊ธ€ ๋‚ด์šฉ";
private static final String MESSAGE_COLOR = "#9650FA";
private static final String FALLBACK = "Required plain-text summary of the attachment";

@Value("${webhook.url}")
private String webhookUrl;

@Override
@Async
public void sendReportToSlack(SlackReport slackReport) {
SlackAttachment slackAttachment = createSlackAttachment(slackReport);
SlackMessage slackMessage = new SlackMessage("").addAttachments(slackAttachment);
new SlackApi(webhookUrl).call(slackMessage);
}

private SlackAttachment createSlackAttachment(SlackReport slackReport) {
SlackAttachment slackAttachment = new SlackAttachment();
slackAttachment.setTitle(REPORT_MESSAGE);
slackAttachment.setText(getErrorReason(slackReport));
slackAttachment.setColor(MESSAGE_COLOR);
slackAttachment.setFallback(FALLBACK);
return slackAttachment;
}

private String getErrorReason(SlackReport slackReport) {
return REPORT_USER_NAME + " : " + slackReport.getUserName()
+ "\n" + REPORT_FEED_CONTENT + " : " + slackReport.getFeedContent()
+ "\n" + REPORT_CONTENT + " : " + slackReport.getReportContent();
}
}
3 changes: 3 additions & 0 deletions feed-infrastructure/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ springdoc:
display-request-duration: true
cache:
disabled: true

webhook:
url: ${webhook.url}
Loading