diff --git a/shopping-crawler/build.gradle b/shopping-crawler/build.gradle index 1aa8ad3..3c2eca3 100644 --- a/shopping-crawler/build.gradle +++ b/shopping-crawler/build.gradle @@ -22,6 +22,7 @@ dependencies { implementation "org.springframework.cloud:spring-cloud-starter-openfeign" implementation "io.github.openfeign:feign-hc5" implementation 'org.ahocorasick:ahocorasick:0.6.3' + implementation "com.slack.api:slack-api-client:1.39.1" annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' annotationProcessor 'org.projectlombok:lombok' diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/slack/SlackConfiguration.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/slack/SlackConfiguration.java new file mode 100644 index 0000000..918eb9e --- /dev/null +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/configuration/slack/SlackConfiguration.java @@ -0,0 +1,20 @@ +package com.myoa.engineering.crawl.shopping.configuration.slack; + +import com.myoa.engineering.crawl.shopping.configuration.slack.properties.SlackSecretProperties; +import com.slack.api.Slack; +import com.slack.api.methods.MethodsClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class SlackConfiguration { + + private static final String DEFAULT_BOT_UNIT_NAME = "shopping-crawler"; + + @Bean + public MethodsClient slackMethodsClient(SlackSecretProperties slackSecretProperties) { + String token = slackSecretProperties.find(DEFAULT_BOT_UNIT_NAME).getToken(); + return Slack.getInstance().methods(token); + } + +} diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/AppUser.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/AppUser.java index a5d0959..4f55324 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/AppUser.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/AppUser.java @@ -12,7 +12,7 @@ import lombok.NoArgsConstructor; @NoArgsConstructor @AllArgsConstructor @Entity -@Table +@Table(name = "app_user") public class AppUser extends Auditable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/Article.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/Article.java index 4021435..a32e102 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/Article.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/Article.java @@ -15,7 +15,7 @@ import java.time.ZonedDateTime; @NoArgsConstructor @AllArgsConstructor @Entity -@Table +@Table(name = "article") public class Article extends Auditable { @Id diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/SubscribedKeyword.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/SubscribedKeyword.java index b3b2969..cf10524 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/SubscribedKeyword.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/entity/v2/SubscribedKeyword.java @@ -13,7 +13,7 @@ import lombok.NoArgsConstructor; @NoArgsConstructor @AllArgsConstructor @Entity -@Table +@Table(name = "subscribed_keyword") public class SubscribedKeyword extends Auditable { @Id diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/model/UserNotifyModel.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/model/UserNotifyModel.java index 36a261a..bc1bcd7 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/model/UserNotifyModel.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/domain/model/UserNotifyModel.java @@ -4,6 +4,7 @@ import com.myoa.engineering.crawl.shopping.domain.model.v2.ArticleModel; import lombok.*; import java.util.List; +import java.util.stream.Collectors; @ToString @@ -12,13 +13,30 @@ import java.util.List; @NoArgsConstructor @AllArgsConstructor public class UserNotifyModel { - private String userId; + private String slackId; private List articles; - public static UserNotifyModel of(String userId, List articles) { + public static UserNotifyModel of(String slackId, List articles) { return UserNotifyModel.builder() - .userId(userId) + .slackId(slackId) .articles(articles) .build(); } + + public String toCompositedMessage() { + String compositedMessage = wrapUserId() + "\n" + + articles.stream() + .map(this::convertArticletoMessage) + .collect(Collectors.joining("\n")); + // article -> builder.append(convertArticletoMessage.toMessage()).append("\n");i + return compositedMessage; + } + + private String wrapUserId() { + return "<@" + slackId + ">"; + } + + private String convertArticletoMessage(ArticleModel article) { + return "<" + article.getArticleUrl() + "|" + article.getTitle() + ">"; + } } diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/FeedParsedResult.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/feed/v1/FeedParsedResult.java similarity index 95% rename from shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/FeedParsedResult.java rename to shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/feed/v1/FeedParsedResult.java index f169a3e..04cac9c 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/FeedParsedResult.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/feed/v1/FeedParsedResult.java @@ -1,4 +1,4 @@ -package com.myoa.engineering.crawl.shopping.dto; +package com.myoa.engineering.crawl.shopping.dto.feed.v1; import com.myoa.engineering.crawl.shopping.support.dto.constant.PpomppuBoardName; import lombok.Builder; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/PpomppuArticleParseDTO.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/feed/v1/PpomppuArticleParseDTO.java similarity index 97% rename from shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/PpomppuArticleParseDTO.java rename to shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/feed/v1/PpomppuArticleParseDTO.java index 497d746..8258bb1 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/PpomppuArticleParseDTO.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/feed/v1/PpomppuArticleParseDTO.java @@ -1,4 +1,4 @@ -package com.myoa.engineering.crawl.shopping.dto; +package com.myoa.engineering.crawl.shopping.dto.feed.v1; import com.myoa.engineering.crawl.shopping.domain.entity.v1.PpomppuArticle; import com.myoa.engineering.crawl.shopping.support.util.DateUtil; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/PpomppuArticleTransformer.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/feed/v1/PpomppuArticleTransformer.java similarity index 98% rename from shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/PpomppuArticleTransformer.java rename to shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/feed/v1/PpomppuArticleTransformer.java index 504b036..9002042 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/PpomppuArticleTransformer.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/feed/v1/PpomppuArticleTransformer.java @@ -1,4 +1,4 @@ -package com.myoa.engineering.crawl.shopping.dto; +package com.myoa.engineering.crawl.shopping.dto.feed.v1; import com.myoa.engineering.crawl.shopping.domain.entity.v1.PpomppuArticle; import com.myoa.engineering.crawl.shopping.support.dto.BlockMessageDTO; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/MessageDTO.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/MessageDTO.java similarity index 75% rename from shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/MessageDTO.java rename to shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/MessageDTO.java index 018a1fd..c1464ee 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/MessageDTO.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/MessageDTO.java @@ -1,4 +1,4 @@ -package com.myoa.engineering.crawl.shopping.dto.slack; +package com.myoa.engineering.crawl.shopping.dto.slack.v1; import java.io.Serializable; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackBaseMessageBlock.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackBaseMessageBlock.java similarity index 95% rename from shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackBaseMessageBlock.java rename to shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackBaseMessageBlock.java index 4f41cba..d2579f8 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackBaseMessageBlock.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackBaseMessageBlock.java @@ -1,4 +1,4 @@ -package com.myoa.engineering.crawl.shopping.dto.slack; +package com.myoa.engineering.crawl.shopping.dto.slack.v1; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackImageMessageBlock.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackImageMessageBlock.java similarity index 95% rename from shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackImageMessageBlock.java rename to shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackImageMessageBlock.java index 8ec1e51..8c74591 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackImageMessageBlock.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackImageMessageBlock.java @@ -1,4 +1,4 @@ -package com.myoa.engineering.crawl.shopping.dto.slack; +package com.myoa.engineering.crawl.shopping.dto.slack.v1; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackMessageBlock.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackMessageBlock.java similarity index 79% rename from shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackMessageBlock.java rename to shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackMessageBlock.java index 59c9748..a13322c 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackMessageBlock.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackMessageBlock.java @@ -1,4 +1,4 @@ -package com.myoa.engineering.crawl.shopping.dto.slack; +package com.myoa.engineering.crawl.shopping.dto.slack.v1; import java.io.Serializable; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackMessageBlockType.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackMessageBlockType.java similarity index 85% rename from shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackMessageBlockType.java rename to shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackMessageBlockType.java index a3cdc1b..a2a4e9a 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackMessageBlockType.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackMessageBlockType.java @@ -1,4 +1,4 @@ -package com.myoa.engineering.crawl.shopping.dto.slack; +package com.myoa.engineering.crawl.shopping.dto.slack.v1; import lombok.AllArgsConstructor; import lombok.Getter; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackMessageDTO.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackMessageDTO.java similarity index 96% rename from shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackMessageDTO.java rename to shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackMessageDTO.java index ba79116..05af9d8 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackMessageDTO.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackMessageDTO.java @@ -1,4 +1,4 @@ -package com.myoa.engineering.crawl.shopping.dto.slack; +package com.myoa.engineering.crawl.shopping.dto.slack.v1; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackSectionMessageBlock.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackSectionMessageBlock.java similarity index 96% rename from shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackSectionMessageBlock.java rename to shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackSectionMessageBlock.java index 651d77f..573ee58 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/SlackSectionMessageBlock.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/dto/slack/v1/SlackSectionMessageBlock.java @@ -1,4 +1,4 @@ -package com.myoa.engineering.crawl.shopping.dto.slack; +package com.myoa.engineering.crawl.shopping.dto.slack.v1; import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Builder; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/event/handler/ArticleUpsertEventListener.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/event/handler/ArticleUpsertEventListener.java index f0ac31c..af261e5 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/event/handler/ArticleUpsertEventListener.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/event/handler/ArticleUpsertEventListener.java @@ -45,7 +45,7 @@ public class ArticleUpsertEventListener { .map(user -> { List filteredArticles = handleAhoCorasick(articleMap) .apply(subscribedKeywordCacheService.getSubscribedKeywordsCached(user.getName())); - return UserNotifyModel.of(user.getName(), filteredArticles); + return UserNotifyModel.of(user.getSlackId(), filteredArticles); }) .forEach(this::notifyMessage); @@ -53,16 +53,12 @@ public class ArticleUpsertEventListener { private Function, List> handleAhoCorasick( Map> articleMap) { - return userTrieModel -> { - return userTrieModel - .entrySet() - .stream().filter(e -> articleMap.containsKey(e.getKey())) - .map((entry) -> filterAhocorasick(articleMap.get(entry.getKey()), entry.getValue())) - .flatMap(List::stream) - .toList(); -// return UserNotifyModel.of(userTrieModel.values().stream().findFirst().get().getUserId(), -// filteredArticle); - }; + return userTrieModel -> userTrieModel + .entrySet() + .stream().filter(e -> articleMap.containsKey(e.getKey())) + .map((entry) -> filterAhocorasick(articleMap.get(entry.getKey()), entry.getValue())) + .flatMap(List::stream) + .toList(); } private List filterAhocorasick(List articles, @@ -75,12 +71,12 @@ public class ArticleUpsertEventListener { //ArticleUpsertEventListener::printArticle } - private void notifyMessage(UserNotifyModel article) { - System.out.println("article = " + article); - if (article.getArticles().isEmpty()){ + private void notifyMessage(UserNotifyModel userNotifyModel) { + System.out.println("article = " + userNotifyModel); + if (userNotifyModel.getArticles().isEmpty()) { return; } - userNotifyService.notify("안녕 " + article.getUserId() + "\n" + article.getArticles()); + userNotifyService.notify(userNotifyModel.toCompositedMessage()); } } diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/infra/client/slack/SlackAPIClient.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/infra/client/slack/SlackAPIClient.java index 1d1ed89..21a87e3 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/infra/client/slack/SlackAPIClient.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/infra/client/slack/SlackAPIClient.java @@ -1,7 +1,7 @@ package com.myoa.engineering.crawl.shopping.infra.client.slack; import com.myoa.engineering.crawl.shopping.configuration.FeignDefaultConfig; -import com.myoa.engineering.crawl.shopping.dto.slack.SlackMessageDTO; +import com.myoa.engineering.crawl.shopping.dto.slack.v1.SlackMessageDTO; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; diff --git a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/service/UserNotifyService.java b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/service/UserNotifyService.java index 13486ee..c31a589 100644 --- a/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/service/UserNotifyService.java +++ b/shopping-crawler/src/main/java/com/myoa/engineering/crawl/shopping/service/UserNotifyService.java @@ -1,7 +1,7 @@ package com.myoa.engineering.crawl.shopping.service; import com.myoa.engineering.crawl.shopping.configuration.slack.properties.SlackSecretProperties; -import com.myoa.engineering.crawl.shopping.dto.slack.SlackMessageDTO; +import com.myoa.engineering.crawl.shopping.dto.slack.v1.SlackMessageDTO; import com.myoa.engineering.crawl.shopping.infra.client.slack.SlackAPIClient; import org.springframework.stereotype.Service;