From 6ab748a1f912dbff6f34332dbde72a146f35be3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E8=BE=89?= <1101635162@qq.com> Date: Wed, 6 Nov 2024 13:52:21 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=84=E8=AE=BAapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../symphony/processor/ArticleProcessor.java | 131 ++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/src/main/java/org/b3log/symphony/processor/ArticleProcessor.java b/src/main/java/org/b3log/symphony/processor/ArticleProcessor.java index 54f5e90b..b6715bbb 100644 --- a/src/main/java/org/b3log/symphony/processor/ArticleProcessor.java +++ b/src/main/java/org/b3log/symphony/processor/ArticleProcessor.java @@ -252,6 +252,7 @@ public static void register() { Dispatcher.get("/api/articles/domain/{domainURI}", articleProcessor::getDomainArticles, loginCheck::handle); Dispatcher.get("/api/article/{id}", articleProcessor::showArticleApi, loginCheck::handle); Dispatcher.get("/api/article/heat/{articleId}", articleProcessor::getArticleHeat); + Dispatcher.get("/api/comment/{id}", articleProcessor::showCommentApi, loginCheck::handle); } public void getArticleHeat(final RequestContext context) { @@ -275,6 +276,136 @@ private AbstractResponseRenderer buildJsonRenderer() { return renderer; } + public void showCommentApi(final RequestContext context) { + final AbstractResponseRenderer renderer = buildJsonRenderer(); + context.setRenderer(renderer); + + final Map dataModel = new HashMap<>(); + final String articleId = context.pathVar("id"); + final Request request = context.getRequest(); + + final JSONObject article = articleQueryService.getArticleById(articleId); + if (null == article) { + context.renderCodeMsg(404, "帖子不存在!"); + return; + } + + final int cmtViewMode = 0; + JSONObject currentUser = Sessions.getUser(); + String currentUserId = currentUser.optString(Keys.OBJECT_ID); + + int pageNum = Paginator.getPage(request); + final int pageSize = Symphonys.ARTICLE_COMMENTS_CNT; + final int windowSize = Symphonys.ARTICLE_COMMENTS_WIN_SIZE; + final int commentCnt = article.getInt(Article.ARTICLE_COMMENT_CNT); + final int pageCount = (int) Math.ceil((double) commentCnt / (double) pageSize); + // 回帖分页 SEO https://github.com/b3log/symphony/issues/813 + if (UserExt.USER_COMMENT_VIEW_MODE_C_TRADITIONAL == cmtViewMode) { + if (0 < pageCount && pageNum > pageCount) { + pageNum = pageCount; + } + } else { + if (pageNum > pageCount) { + pageNum = 1; + } + } + final List pageNums = Paginator.paginate(pageNum, pageSize, pageCount, windowSize); + + JSONObject pagination = new JSONObject(); + pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount); + pagination.put(Pagination.PAGINATION_PAGE_NUMS, pageNums); + dataModel.put("pagination", pagination); + + articleQueryService.processArticleContent(article); + + JSONObject result = new JSONObject(); + + if (!article.optBoolean(Common.DISCUSSION_VIEWABLE)) { + result.put(Article.ARTICLE_T_COMMENTS, (Object) Collections.emptyList()); + result.put(Article.ARTICLE_T_NICE_COMMENTS, (Object) Collections.emptyList()); + return; + } + + List niceComments = new ArrayList<>(); + if (pageNum == 1) { + niceComments = commentQueryService.getNiceComments(cmtViewMode, articleId, 3); + result.put(Article.ARTICLE_T_NICE_COMMENTS, (Object) niceComments); + } + + // Load comments + final List articleComments = commentQueryService.getArticleComments(articleId, pageNum, pageSize, cmtViewMode); + result.put(Article.ARTICLE_T_COMMENTS, (Object) articleComments); + + final String articleAuthorId = article.optString(Article.ARTICLE_AUTHOR_ID); + + for (final JSONObject comment : niceComments) { + comment.remove("commentUA"); + comment.remove("commentIP"); + final JSONObject commenter = comment.optJSONObject("commenter"); + commenter.remove("userPassword"); + commenter.remove("userLatestLoginIP"); + commenter.remove("userPhone"); + commenter.remove("userQQ"); + commenter.remove("userCity"); + commenter.remove("userCountry"); + commenter.remove("userEmail"); + commenter.remove("secret2fa"); + + String thankTemplate = langPropsService.get("thankConfirmLabel"); + thankTemplate = thankTemplate.replace("{point}", String.valueOf(Symphonys.POINT_THANK_COMMENT)) + .replace("{user}", comment.optJSONObject(Comment.COMMENT_T_COMMENTER).optString(User.USER_NAME)); + comment.put(Comment.COMMENT_T_THANK_LABEL, thankTemplate); + + final String commentId = comment.optString(Keys.OBJECT_ID); + + comment.put(Common.REWARDED, rewardQueryService.isRewarded(currentUserId, commentId, Reward.TYPE_C_COMMENT)); + final int commentVote = voteQueryService.isVoted(currentUserId, commentId); + comment.put(Comment.COMMENT_T_VOTE, commentVote); + + comment.put(Common.REWARED_COUNT, comment.optInt(Comment.COMMENT_THANK_CNT)); + + // https://github.com/b3log/symphony/issues/682 + if (Comment.COMMENT_VISIBLE_C_AUTHOR == comment.optInt(Comment.COMMENT_VISIBLE)) { + final String commentAuthorId = comment.optString(Comment.COMMENT_AUTHOR_ID); + if ((!StringUtils.equals(currentUserId, commentAuthorId) && !StringUtils.equals(currentUserId, articleAuthorId))) { + comment.put(Comment.COMMENT_CONTENT, langPropsService.get("onlySelfAndArticleAuthorVisibleLabel")); + } + } + } + + for (final JSONObject comment : articleComments) { + comment.remove("commentUA"); + comment.remove("commentIP"); + final JSONObject commenter = comment.optJSONObject("commenter"); + commenter.remove("userPassword"); + commenter.remove("userLatestLoginIP"); + commenter.remove("userPhone"); + commenter.remove("userQQ"); + commenter.remove("userCity"); + commenter.remove("userCountry"); + commenter.remove("userEmail"); + commenter.remove("secret2fa"); + + final String commentId = comment.optString(Keys.OBJECT_ID); + + comment.put(Common.REWARDED, + rewardQueryService.isRewarded(currentUserId, commentId, Reward.TYPE_C_COMMENT)); + final int commentVote = voteQueryService.isVoted(currentUserId, commentId); + comment.put(Comment.COMMENT_T_VOTE, commentVote); + comment.put(Common.REWARED_COUNT, comment.optInt(Comment.COMMENT_THANK_CNT)); + + // https://github.com/b3log/symphony/issues/682 + if (Comment.COMMENT_VISIBLE_C_AUTHOR == comment.optInt(Comment.COMMENT_VISIBLE)) { + final String commentAuthorId = comment.optString(Comment.COMMENT_AUTHOR_ID); + if ((!StringUtils.equals(currentUserId, commentAuthorId) && !StringUtils.equals(currentUserId, articleAuthorId))) { + comment.put(Comment.COMMENT_CONTENT, langPropsService.get("onlySelfAndArticleAuthorVisibleLabel")); + } + } + } + + context.renderData(result).renderCode(StatusCodes.SUCC).renderMsg(""); + } + /** * api for get article details *