From 91669c634f782dae39e2e6d45be974b331423826 Mon Sep 17 00:00:00 2001 From: heavyrian2012 Date: Sun, 22 May 2022 11:26:01 +0800 Subject: [PATCH] add send articles message builder --- .../ArticlesContentBuilder.java | 83 +++++++++++++++++++ .../cn/wildfirechat/proto/ProtoConstants.java | 1 + 2 files changed, 84 insertions(+) create mode 100644 common/src/main/java/cn/wildfirechat/messagecontentbuilder/ArticlesContentBuilder.java diff --git a/common/src/main/java/cn/wildfirechat/messagecontentbuilder/ArticlesContentBuilder.java b/common/src/main/java/cn/wildfirechat/messagecontentbuilder/ArticlesContentBuilder.java new file mode 100644 index 000000000..5a32eef63 --- /dev/null +++ b/common/src/main/java/cn/wildfirechat/messagecontentbuilder/ArticlesContentBuilder.java @@ -0,0 +1,83 @@ +package cn.wildfirechat.messagecontentbuilder; + +import cn.wildfirechat.pojos.MessagePayload; +import cn.wildfirechat.proto.ProtoConstants; +import io.netty.util.internal.StringUtil; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; +import java.util.function.Consumer; + +public class ArticlesContentBuilder extends MessageContentBuilder{ + public static class Article { + public String id; + public String cover; + public String title; + public String url; + public boolean readReport; + } + public Article topArticle; + public List
subArticles; + + public static ArticlesContentBuilder newBuilder(String id, String cover, String title, String url, boolean readReport) { + ArticlesContentBuilder builder = new ArticlesContentBuilder(); + builder.topArticle = new Article(); + builder.topArticle.id = id; + builder.topArticle.cover = cover; + builder.topArticle.title = title; + builder.topArticle.url = url; + builder.topArticle.readReport = readReport; + return builder; + } + + public ArticlesContentBuilder addSubArticle(String id, String cover, String title, String url, boolean readReport) { + Article article = new Article(); + article.id = id; + article.cover = cover; + article.title = title; + article.url = url; + article.readReport = readReport; + if(subArticles == null) { + subArticles = new ArrayList<>(); + } + subArticles.add(article); + return this; + } + + private static JSONObject toJSON(Article article) { + JSONObject top = new JSONObject(); + if(!StringUtil.isNullOrEmpty(article.id)) + top.put("id", article.id); + if(!StringUtil.isNullOrEmpty(article.cover)) + top.put("cover", article.cover); + if(!StringUtil.isNullOrEmpty(article.title)) + top.put("title", article.title); + if(!StringUtil.isNullOrEmpty(article.url)) + top.put("url", article.url); + if(article.readReport) + top.put("rr", article.readReport); + return top; + } + + @Override + public MessagePayload build() { + MessagePayload payload = encodeBase(); + payload.setType(ProtoConstants.ContentType.Articles); + payload.setPersistFlag(ProtoConstants.PersistFlag.Persist_And_Count); + payload.setSearchableContent(topArticle.title); + JSONObject jsonObject = new JSONObject(); + jsonObject.put("top", toJSON(topArticle)); + if(subArticles != null && !subArticles.isEmpty()) { + JSONArray jsonArray = new JSONArray(); + subArticles.forEach(article -> jsonArray.add(toJSON(article))); + jsonObject.put("subArticles", jsonArray); + } + + payload.setBase64edData(Base64.getEncoder().encodeToString(jsonObject.toJSONString().getBytes(StandardCharsets.UTF_8))); + return payload; + } +} diff --git a/common/src/main/java/cn/wildfirechat/proto/ProtoConstants.java b/common/src/main/java/cn/wildfirechat/proto/ProtoConstants.java index e42fd3e49..e17d3e15b 100644 --- a/common/src/main/java/cn/wildfirechat/proto/ProtoConstants.java +++ b/common/src/main/java/cn/wildfirechat/proto/ProtoConstants.java @@ -89,6 +89,7 @@ public interface ContentType { int Name_Card = 10; int Composited = 11; int Rich_Notification = 12; + int Articles = 13; int Enter_Channel_Chat = 71; int Leave_Channel_Chat = 72; int Recall = 80;