From 520a651a701e265ba6a27e3bc4e63bc7c11a2254 Mon Sep 17 00:00:00 2001 From: "woo-jin.shin" Date: Sat, 4 Dec 2021 01:07:42 +0900 Subject: [PATCH] Apply accessory image --- .../controller/CrawlAPIController.java | 14 ++++ .../processor/domain/PpomppuArticle.java | 7 +- .../processor/dto/PpomppuArticleParser.java | 6 ++ .../dto/PpomppuArticleTransformer.java | 18 +++-- .../MessageSenderAPIController.java | 3 +- .../sender/dto/SlackBaseMessageBlock.java | 1 + .../sender/dto/SlackImageMessageBlock.java | 49 ++++++++++++ .../ppomppu/sender/dto/SlackMessageBlock.java | 3 + .../sender/dto/SlackMessageBlockType.java | 1 + .../ppomppu/sender/dto/SlackMessageDTO.java | 8 +- .../sender/dto/SlackSectionMessageBlock.java | 11 ++- .../ppomppu/support/dto/BlockMessageDTO.java | 77 ++++++++++++------- 12 files changed, 158 insertions(+), 40 deletions(-) create mode 100644 sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackImageMessageBlock.java diff --git a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/controller/CrawlAPIController.java b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/controller/CrawlAPIController.java index 9641e84..8858a36 100644 --- a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/controller/CrawlAPIController.java +++ b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/controller/CrawlAPIController.java @@ -1,10 +1,13 @@ package com.myoa.engineering.crawl.ppomppu.processor.controller; +import java.util.List; + import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import com.myoa.engineering.crawl.ppomppu.processor.domain.PpomppuArticle; import com.myoa.engineering.crawl.ppomppu.processor.dto.FeedParsedResult; import com.myoa.engineering.crawl.ppomppu.processor.service.MessageSenderService; import com.myoa.engineering.crawl.ppomppu.processor.service.PpomppuArticleService; @@ -53,4 +56,15 @@ public class CrawlAPIController { return publishedMessages.then(Mono.just(APIResponse.success(result.done()))); } + @PostMapping("/exploit/boards/{boardName}") + public Mono> crawlBoardDryRun( + @PathVariable("boardName") PpomppuBoardName boardName) { + log.info("got request... {}", boardName); + Mono publishedMessages = + ppomppuRSSFeedService.getArticles(boardName) + .flatMap(messageSenderService::sendBlockMessageToSlack); + + return publishedMessages.map(APIResponse::success); + } + } diff --git a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/domain/PpomppuArticle.java b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/domain/PpomppuArticle.java index 9d88870..ad68260 100644 --- a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/domain/PpomppuArticle.java +++ b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/domain/PpomppuArticle.java @@ -34,6 +34,9 @@ public class PpomppuArticle extends Auditable { @Column private String articleUrl; + @Column + private String thumbnailUrl; + @Column private String title; @@ -48,11 +51,13 @@ public class PpomppuArticle extends Auditable { @Builder public PpomppuArticle(Long id, Long articleId, PpomppuBoardName boardName, String articleUrl, - String title, Integer recommended, Integer hit, Instant registeredAt) { + String thumbnailUrl, String title, Integer recommended, Integer hit, + Instant registeredAt) { this.id = id; this.articleId = articleId; this.boardName = boardName; this.articleUrl = articleUrl; + this.thumbnailUrl = thumbnailUrl; this.title = title; this.recommended = recommended; this.hit = hit; diff --git a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleParser.java b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleParser.java index 4387642..63da010 100644 --- a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleParser.java +++ b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleParser.java @@ -27,6 +27,7 @@ public final class PpomppuArticleParser { final long articleId = PpomppuArticleParser.parseArticleId(articleElement.get(0)); final String title = PpomppuArticleParser.parseTitle(articleElement.get(2)); final String articleUrl = PpomppuArticleParser.parseArticleUrl(articleElement.get(2)); + final String thumbnailUrl = PpomppuArticleParser.parseThumbnailUrl(articleElement.get(3)); final int recommended = PpomppuArticleParser.parseRecommended(articleElement.get(6)); final int hit = PpomppuArticleParser.parseHit(articleElement.get(7)); final Instant registeredAt = PpomppuArticleParser.parseRegisteredAt(articleElement.get(5)); @@ -35,6 +36,7 @@ public final class PpomppuArticleParser { .articleId(articleId) .title(title) .articleUrl(articleUrl) + .thumbnailUrl(thumbnailUrl) .recommended(recommended) .hit(hit) .registeredAt(registeredAt) @@ -53,6 +55,10 @@ public final class PpomppuArticleParser { return PpomppuBoardName.ofViewPageUrl(td.getElementsByTag("a").attr("href")); } + public static String parseThumbnailUrl(Element td) { + return "https:" + td.getElementsByTag("img").get(0).attr("src"); + } + public static Integer parseRecommended(Element td) { final String voteString = td.text(); final int recommended; diff --git a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleTransformer.java b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleTransformer.java index 141af97..b0284b7 100644 --- a/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleTransformer.java +++ b/processor/src/main/java/com/myoa/engineering/crawl/ppomppu/processor/dto/PpomppuArticleTransformer.java @@ -31,7 +31,8 @@ public final class PpomppuArticleTransformer { .requestedAt(Instant.now()) .publishedAt(article.getRegisteredAt()) .title(String.format(MESSAGE_FORMAT_V1, - article.getBoardName().getMenuName(), article.getArticleUrl(), article.getTitle())) + article.getBoardName().getMenuName(), article.getArticleUrl(), + article.getTitle())) .body(article.getArticleUrl()) .build(); @@ -50,14 +51,15 @@ public final class PpomppuArticleTransformer { public static BlockMessageDTO transformToBlockMessage(List articles) { Instant requestedAt = Instant.now(); - List body = articles.stream() - .map(PpomppuArticleTransformer::convertToInlineMessage) - .collect(Collectors.toList()); + List body = articles.stream() + .map(e -> BlockMessageDTO.createBlock(convertToInlineMessage(e), + e.getThumbnailUrl())) + .collect(Collectors.toList()); return BlockMessageDTO.builder() - .requestedAt(requestedAt) - .title(DATE_TIME_FORMATTER.format(requestedAt)) - .blocks(body) - .build(); + .requestedAt(requestedAt) + .title(DATE_TIME_FORMATTER.format(requestedAt)) + .blocks(body) + .build(); } public static String convertToInlineMessage(PpomppuArticle article) { diff --git a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/controller/MessageSenderAPIController.java b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/controller/MessageSenderAPIController.java index becc16b..ef84721 100644 --- a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/controller/MessageSenderAPIController.java +++ b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/controller/MessageSenderAPIController.java @@ -7,7 +7,6 @@ import org.springframework.web.bind.annotation.RestController; import com.myoa.engineering.crawl.ppomppu.sender.dto.SlackBaseMessageBlock; import com.myoa.engineering.crawl.ppomppu.sender.dto.SlackMessageDTO; -import com.myoa.engineering.crawl.ppomppu.sender.dto.SlackSectionMessageBlock; import com.myoa.engineering.crawl.ppomppu.sender.infrastructure.client.MongeShoppingBotSlackMessageSender; import com.myoa.engineering.crawl.ppomppu.support.dto.APIResponse; import com.myoa.engineering.crawl.ppomppu.support.dto.BlockMessageDTO; @@ -46,7 +45,7 @@ public class MessageSenderAPIController { return Mono.just(APIResponse.fail(dto, "empty blocks")); } SlackMessageDTO slackMessageDTO = sender.ofBlockMessageBased(); - dto.getBlocks().forEach(slackMessageDTO::addBlock); + dto.getBlocks().forEach(slackMessageDTO::addSectionBlock); slackMessageDTO.addBlock(SlackBaseMessageBlock.ofDivider()); return sender.sendMessage(slackMessageDTO) diff --git a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackBaseMessageBlock.java b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackBaseMessageBlock.java index 09ee20a..f8968ee 100644 --- a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackBaseMessageBlock.java +++ b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackBaseMessageBlock.java @@ -40,6 +40,7 @@ public class SlackBaseMessageBlock implements SlackMessageBlock { .build(); } + @Override public String getType() { return type.getType(); } diff --git a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackImageMessageBlock.java b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackImageMessageBlock.java new file mode 100644 index 0000000..8377a2f --- /dev/null +++ b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackImageMessageBlock.java @@ -0,0 +1,49 @@ +package com.myoa.engineering.crawl.ppomppu.sender.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * SlackImageMessageBlock + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-11-30 + * + */ +@Getter +@NoArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class SlackImageMessageBlock implements SlackMessageBlock { + private static final long serialVersionUID = 1597984001727808419L; + + private SlackMessageBlockType type; + + @JsonProperty(value = "image_url", required = true) + private String imageUrl; + + @JsonProperty(value = "alt_text", required = true) + private String altText; + + @Builder + private SlackImageMessageBlock(SlackMessageBlockType type, String imageUrl, String altText) { + this.type = type; + this.imageUrl = imageUrl; + this.altText = altText; + } + + public static SlackImageMessageBlock of(String imageUrl, String altText) { + return SlackImageMessageBlock.builder() + .type(SlackMessageBlockType.IMAGE) + .imageUrl(imageUrl) + .altText(altText) + .build(); + } + + @Override + public String getType() { + return type.getType(); + } +} diff --git a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageBlock.java b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageBlock.java index e4c8794..cd94027 100644 --- a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageBlock.java +++ b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageBlock.java @@ -9,4 +9,7 @@ import java.io.Serializable; * */ public interface SlackMessageBlock extends Serializable { + + String getType(); + } diff --git a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageBlockType.java b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageBlockType.java index 2cbfd38..aef8d0d 100644 --- a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageBlockType.java +++ b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageBlockType.java @@ -15,6 +15,7 @@ public enum SlackMessageBlockType { SECTION("section"), MARKDOWN("mrkdwn"), DIVIDER("divider"), + IMAGE("image"), ; private String type; diff --git a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageDTO.java b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageDTO.java index 23326b1..d4bb207 100644 --- a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageDTO.java +++ b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackMessageDTO.java @@ -4,6 +4,8 @@ import java.util.List; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import com.myoa.engineering.crawl.ppomppu.support.dto.BlockMessageDTO; +import com.myoa.engineering.crawl.ppomppu.support.dto.BlockMessageDTO.Block; import lombok.Builder; import lombok.Getter; @@ -44,8 +46,10 @@ public class SlackMessageDTO implements MessageDTO { this.text = text; } - public void addBlock(String blockRawMessage) { - addBlock(SlackSectionMessageBlock.ofMarkDown(blockRawMessage)); + public void addSectionBlock(BlockMessageDTO.Block block) { + SlackSectionMessageBlock slackSectionMessageBlock = SlackSectionMessageBlock.ofMarkDown(block.getText()); + slackSectionMessageBlock.applyImageaccessory(block.getImageUrl(), block.getAltText()); + addBlock(slackSectionMessageBlock); } public void addBlock(SlackMessageBlock block) { diff --git a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackSectionMessageBlock.java b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackSectionMessageBlock.java index cd12e20..aa6fbbe 100644 --- a/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackSectionMessageBlock.java +++ b/sender/src/main/java/com/myoa/engineering/crawl/ppomppu/sender/dto/SlackSectionMessageBlock.java @@ -20,11 +20,14 @@ public class SlackSectionMessageBlock implements SlackMessageBlock { private SlackMessageBlockType type; private SlackBaseMessageBlock text; + private SlackImageMessageBlock accessory; @Builder - private SlackSectionMessageBlock(SlackMessageBlockType type, SlackBaseMessageBlock text) { + private SlackSectionMessageBlock(SlackMessageBlockType type, SlackBaseMessageBlock text, + SlackImageMessageBlock accessory) { this.type = type; this.text = text; + this.accessory = accessory; } public static SlackSectionMessageBlock ofMarkDown(String message) { @@ -34,6 +37,12 @@ public class SlackSectionMessageBlock implements SlackMessageBlock { .build(); } + public SlackSectionMessageBlock applyImageaccessory(String imageUrl, String altText) { + this.accessory = SlackImageMessageBlock.of(imageUrl, altText); + return this; + } + + @Override public String getType() { return type.getType(); } diff --git a/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/BlockMessageDTO.java b/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/BlockMessageDTO.java index 812e9b8..30a74d1 100644 --- a/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/BlockMessageDTO.java +++ b/support/src/main/java/com/myoa/engineering/crawl/ppomppu/support/dto/BlockMessageDTO.java @@ -1,4 +1,4 @@ - package com.myoa.engineering.crawl.ppomppu.support.dto; +package com.myoa.engineering.crawl.ppomppu.support.dto; import java.io.Serializable; import java.time.Instant; @@ -8,31 +8,56 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; - /** - * SimpleMessageDTO - * @author Shin Woo-jin (woo-jin.shin@linecorp.com) - * @since 2021-11-21 - * - */ - @Getter - @NoArgsConstructor - public class BlockMessageDTO implements Serializable { +/** + * SimpleMessageDTO + * @author Shin Woo-jin (woo-jin.shin@linecorp.com) + * @since 2021-11-21 + * + */ +@Getter +@NoArgsConstructor +public class BlockMessageDTO implements Serializable { - private static final long serialVersionUID = -6992039884035135523L; + private static final long serialVersionUID = -6992039884035135523L; - private String title; - private List blocks; - private String url; - private Instant publishedAt; - private Instant requestedAt; + private String title; + private List blocks; + private String url; + private Instant publishedAt; + private Instant requestedAt; - @Builder - public BlockMessageDTO(String title, List blocks, String url, Instant publishedAt, - Instant requestedAt) { - this.title = title; - this.blocks = blocks; - this.url = url; - this.publishedAt = publishedAt; - this.requestedAt = requestedAt; - } - } + @Builder + public BlockMessageDTO(String title, List blocks, String url, Instant publishedAt, + Instant requestedAt) { + this.title = title; + this.blocks = blocks; + this.url = url; + this.publishedAt = publishedAt; + this.requestedAt = requestedAt; + } + + @Getter + @NoArgsConstructor + public static class Block implements Serializable { + private static final long serialVersionUID = 3633781631892663709L; + + private String text; + private String imageUrl; + private String altText; + + public Block(String text, String imageUrl, String altText) { + this.text = text; + this.imageUrl = imageUrl; + this.altText = altText; + } + } + + public static Block createBlock(String text, String imageUrl) { + return new Block(text, imageUrl, ""); + } + + public static Block createBlock(String text, String imageUrl, String altText) { + return new Block(text, imageUrl, altText); + } + +}