Commit 0fa0d09f authored by Van's avatar Van

Merge remote-tracking branch 'origin/3.6.0-dev' into 3.6.0-dev

parents 5948b363 4ff35443
......@@ -58,7 +58,7 @@ import javax.servlet.http.HttpSessionEvent;
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="http://vanessa.b3log.org">Vanessa</a>
* @version 1.11.0.14, Mar 31, 2019
* @version 1.11.0.15, Apr 18, 2019
* @since 0.3.1
*/
public final class SoloServletListener extends AbstractServletListener {
......@@ -361,11 +361,9 @@ public final class SoloServletListener extends AbstractServletListener {
DispatcherServlet.get("/console/categories/{page}/{pageSize}/{windowSize}", categoryConsole::getCategories);
final CommentConsole commentConsole = beanManager.getReference(CommentConsole.class);
DispatcherServlet.delete("/console/page/comment/{id}", commentConsole::removePageComment);
DispatcherServlet.delete("/console/article/comment/{id}", commentConsole::removeArticleComment);
DispatcherServlet.get("/console/comments/{page}/{pageSize}/{windowSize}", commentConsole::getComments);
DispatcherServlet.get("/console/comments/article/{id}", commentConsole::getArticleComments);
DispatcherServlet.get("/console/comments/page/{id}", commentConsole::getPageComments);
final LinkConsole linkConsole = beanManager.getReference(LinkConsole.class);
DispatcherServlet.delete("/console/link/{id}", linkConsole::removeLink);
......
......@@ -21,7 +21,7 @@ package org.b3log.solo.model;
* This class defines all page model relevant keys.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.1, Feb 6, 2019
* @version 1.1.0.2, Apr 18, 2019
* @since 0.3.1
*/
public final class Page {
......@@ -82,6 +82,8 @@ public final class Page {
* A normal customized page.
* </ul>
* </p>
* <p>
* "page" 类型已经废除 https://github.com/b3log/solo/issues/12764
*/
public static final String PAGE_TYPE = "pageType";
......
......@@ -29,12 +29,14 @@ import org.b3log.latke.servlet.RequestContext;
import org.b3log.latke.servlet.annotation.RequestProcessing;
import org.b3log.latke.servlet.annotation.RequestProcessor;
import org.b3log.latke.servlet.renderer.JsonRenderer;
import org.b3log.solo.model.*;
import org.b3log.solo.model.Article;
import org.b3log.solo.model.Comment;
import org.b3log.solo.model.Common;
import org.b3log.solo.model.Option;
import org.b3log.solo.service.CommentMgmtService;
import org.b3log.solo.service.OptionQueryService;
import org.b3log.solo.service.UserMgmtService;
import org.b3log.solo.service.UserQueryService;
import org.b3log.solo.util.Emotions;
import org.b3log.solo.util.Skins;
import org.b3log.solo.util.Solos;
import org.json.JSONObject;
......@@ -48,7 +50,7 @@ import java.util.Map;
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="https://hacpai.com/member/armstrong">ArmstrongCN</a>
* @version 1.3.3.7, Mar 17, 2019
* @version 1.4.0.0, Apr 18, 2019
* @since 0.3.1
*/
@RequestProcessor
......@@ -89,102 +91,6 @@ public class CommentProcessor {
@Inject
private OptionQueryService optionQueryService;
/**
* Adds a comment to a page.
*
* <p>
* Request json:
* <pre>
* {
* "captcha": "",
* "oId": pageId,
* "commentName": "",
* "commentURL": "",
* "commentContent": "",
* "commentOriginalCommentId": "" // optional, if exists this key, the comment is an reply
* }
* </pre>
* </p>
* <p>
* Renders the response with a json object, for example,
* <pre>
* {
* "oId": generatedCommentId,
* "sc": "COMMENT_PAGE_SUCC"
* "commentDate": "", // yyyy/MM/dd HH:mm:ss
* "commentSharpURL": "",
* "commentThumbnailURL": "",
* "commentOriginalCommentName": "" // if exists this key, the comment is an reply
* }
* </pre>
* </p>
*
* @param context the specified context
*/
@RequestProcessing(value = "/page/comments", method = HttpMethod.POST)
public void addPageComment(final RequestContext context) {
final JSONObject requestJSONObject = context.requestJSON();
requestJSONObject.put(Common.TYPE, Page.PAGE);
fillCommenter(requestJSONObject, context);
final JSONObject jsonObject = commentMgmtService.checkAddCommentRequest(requestJSONObject);
final JsonRenderer renderer = new JsonRenderer();
context.setRenderer(renderer);
renderer.setJSONObject(jsonObject);
if (!jsonObject.optBoolean(Keys.STATUS_CODE)) {
LOGGER.log(Level.WARN, "Can't add comment[msg={0}]", jsonObject.optString(Keys.MSG));
return;
}
if (!Solos.isLoggedIn(context)) {
jsonObject.put(Keys.STATUS_CODE, false);
jsonObject.put(Keys.MSG, "Need login");
return;
}
try {
final JSONObject addResult = commentMgmtService.addPageComment(requestJSONObject);
final Map<String, Object> dataModel = new HashMap<>();
dataModel.put(Comment.COMMENT, addResult);
final JSONObject page = addResult.optJSONObject(Page.PAGE);
page.put(Common.COMMENTABLE, addResult.opt(Common.COMMENTABLE));
page.put(Common.PERMALINK, addResult.opt(Common.PERMALINK));
dataModel.put(Article.ARTICLE, page);
// 添加评论优化 https://github.com/b3log/solo/issues/12246
try {
final String skinDirName = (String) context.attr(Keys.TEMAPLTE_DIR_NAME);
final Template template = Skins.getSkinTemplate(context, "common-comment.ftl");
final JSONObject preference = optionQueryService.getPreference();
Skins.fillLangs(preference.optString(Option.ID_C_LOCALE_STRING), skinDirName, dataModel);
Keys.fillServer(dataModel);
final StringWriter stringWriter = new StringWriter();
template.process(dataModel, stringWriter);
stringWriter.close();
String cmtTpl = stringWriter.toString();
cmtTpl = Emotions.convert(cmtTpl);
addResult.put("cmtTpl", cmtTpl);
} catch (final Exception e) {
// 1.9.0 向后兼容
}
addResult.put(Keys.STATUS_CODE, true);
renderer.setJSONObject(addResult);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Can not add comment on page", e);
jsonObject.put(Keys.STATUS_CODE, false);
jsonObject.put(Keys.MSG, langPropsService.get("addFailLabel"));
}
}
/**
* Adds a comment to an article.
*
......
......@@ -39,7 +39,7 @@ import java.util.List;
* Comment console request processing.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.5, Dec 11, 2018
* @version 1.1.0.0, Apr 18, 2019
* @since 0.4.0
*/
@RequestProcessor
......@@ -69,48 +69,6 @@ public class CommentConsole {
@Inject
private LangPropsService langPropsService;
/**
* Removes a comment of an article by the specified request.
* <p>
* Renders the response with a json object, for example,
* <pre>
* {
* "sc": boolean,
* "msg": ""
* }
* </pre>
* </p>
*
* @param context the specified request context
*/
public void removePageComment(final RequestContext context) {
final JsonRenderer renderer = new JsonRenderer();
context.setRenderer(renderer);
final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret);
try {
final String commentId = context.pathVar("id");
final JSONObject currentUser = Solos.getCurrentUser(context.getRequest(), context.getResponse());
if (!commentQueryService.canAccessComment(commentId, currentUser)) {
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langPropsService.get("forbiddenLabel"));
return;
}
commentMgmtService.removePageComment(commentId);
ret.put(Keys.STATUS_CODE, true);
ret.put(Keys.MSG, langPropsService.get("removeSuccLabel"));
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
ret.put(Keys.STATUS_CODE, false);
ret.put(Keys.MSG, langPropsService.get("removeFailLabel"));
}
}
/**
* Removes a comment of an article by the specified request.
* <p>
......@@ -251,48 +209,4 @@ public class CommentConsole {
jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
}
}
/**
* Gets comments of a page specified by the article id for administrator.
* <p>
* Renders the response with a json object, for example,
* <pre>
* {
* "sc": boolean,
* "comments": [{
* "oId": "",
* "commentName": "",
* "thumbnailUrl": "",
* "commentURL": "",
* "commentContent": "",
* "commentTime": long,
* "commentSharpURL": "",
* "isReply": boolean
* }, ....]
* }
* </pre>
* </p>
*
* @param context the specified request context
*/
public void getPageComments(final RequestContext context) {
final JsonRenderer renderer = new JsonRenderer();
context.setRenderer(renderer);
final JSONObject ret = new JSONObject();
renderer.setJSONObject(ret);
try {
final String pageId = context.pathVar("id");
final List<JSONObject> comments = commentQueryService.getComments(pageId);
ret.put(Comment.COMMENTS, comments);
ret.put(Keys.STATUS_CODE, true);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
final JSONObject jsonObject = new JSONObject().put(Keys.STATUS_CODE, false);
renderer.setJSONObject(jsonObject);
jsonObject.put(Keys.MSG, langPropsService.get("getFailLabel"));
}
}
}
......@@ -346,7 +346,7 @@ public class ArticleMgmtService {
* "articleTags": "tag1,tag2,tag3",
* "articleStatus": int, // 0: published, 1: draft
* "articlePermalink": "", // optional
* "postToCommunity": boolean, // optional, default is true
* "postToCommunity": boolean, // optional
* "articleSignId": "" // optional, default is "0",
* "articleCommentable": boolean,
* "articleViewPwd": "",
......
......@@ -27,7 +27,7 @@ import org.b3log.solo.repository.OptionRepository;
import org.json.JSONObject;
/**
* Upgrade script from v3.3.0 to v3.4.0.
* Upgrade script from v3.4.0 to v3.5.0.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.1, Mar 31, 2019
......@@ -41,7 +41,7 @@ public final class V340_350 {
private static final Logger LOGGER = Logger.getLogger(V340_350.class);
/**
* Performs upgrade from v3.3.0 to v3.4.0.
* Performs upgrade from v3.4.0 to v3.5.0.
*
* @throws Exception upgrade fails
*/
......
/*
* Solo - A small and beautiful blogging system written in Java.
* Copyright (c) 2010-present, b3log.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.b3log.solo.upgrade;
import org.b3log.latke.Keys;
import org.b3log.latke.ioc.BeanManager;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.repository.FilterOperator;
import org.b3log.latke.repository.PropertyFilter;
import org.b3log.latke.repository.Query;
import org.b3log.latke.repository.Transaction;
import org.b3log.solo.model.*;
import org.b3log.solo.repository.CommentRepository;
import org.b3log.solo.repository.OptionRepository;
import org.b3log.solo.repository.PageRepository;
import org.b3log.solo.repository.UserRepository;
import org.b3log.solo.service.ArticleMgmtService;
import org.json.JSONObject;
import java.util.List;
/**
* Upgrade script from v3.5.0 to v3.6.0.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Apr 18, 2019
* @since 3.6.0
*/
public final class V350_360 {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(V350_360.class);
/**
* Performs upgrade from v3.5.0 to v3.6.0.
*
* @throws Exception upgrade fails
*/
public static void perform() throws Exception {
final String fromVer = "3.5.0";
final String toVer = "3.6.0";
LOGGER.log(Level.INFO, "Upgrading from version [" + fromVer + "] to version [" + toVer + "]....");
final BeanManager beanManager = BeanManager.getInstance();
final OptionRepository optionRepository = beanManager.getReference(OptionRepository.class);
try {
convertPagesToArticles();
final Transaction transaction = optionRepository.beginTransaction();
final JSONObject versionOpt = optionRepository.get(Option.ID_C_VERSION);
versionOpt.put(Option.OPTION_VALUE, toVer);
optionRepository.update(Option.ID_C_VERSION, versionOpt);
transaction.commit();
LOGGER.log(Level.INFO, "Upgraded from version [" + fromVer + "] to version [" + toVer + "] successfully");
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Upgrade failed!", e);
throw new Exception("Upgrade failed from version [" + fromVer + "] to version [" + toVer + "]");
}
}
/**
* 去掉自定义页面 https://github.com/b3log/solo/issues/12764
*/
private static void convertPagesToArticles() throws Exception {
final BeanManager beanManager = BeanManager.getInstance();
final PageRepository pageRepository = beanManager.getReference(PageRepository.class);
final CommentRepository commentRepository = beanManager.getReference(CommentRepository.class);
final ArticleMgmtService articleMgmtService = beanManager.getReference(ArticleMgmtService.class);
final UserRepository userRepository = beanManager.getReference(UserRepository.class);
final JSONObject admin = userRepository.getAdmin();
final List<JSONObject> pages = pageRepository.getList(new Query().
setFilter(new PropertyFilter(Page.PAGE_TYPE, FilterOperator.EQUAL, Page.PAGE)));
for (final JSONObject page : pages) {
final String title = page.optString(Page.PAGE_TITLE);
final String content = page.optString(Page.PAGE_CONTENT);
final int commentCnt = page.optInt(Page.PAGE_COMMENT_COUNT);
final String permalink = page.optString(Page.PAGE_PERMALINK);
final boolean commentable = page.optBoolean(Page.PAGE_COMMENTABLE);
final JSONObject article = new JSONObject();
article.put(Article.ARTICLE_AUTHOR_ID, admin.optString(Keys.OBJECT_ID));
article.put(Article.ARTICLE_TITLE, title);
article.put(Article.ARTICLE_ABSTRACT, Article.getAbstractText(content));
article.put(Article.ARTICLE_COMMENT_COUNT, commentCnt);
if ("/my-github-repos".equals(permalink)) {
article.put(Article.ARTICLE_TAGS_REF, "开源,GitHub");
}
article.put(Article.ARTICLE_PERMALINK, permalink);
article.put(Article.ARTICLE_COMMENTABLE, commentable);
article.put(Article.ARTICLE_CONTENT, content);
article.put(Article.ARTICLE_VIEW_PWD, "");
article.put(Article.ARTICLE_STATUS, Article.ARTICLE_STATUS_C_PUBLISHED);
article.put(Common.POST_TO_COMMUNITY, false);
final JSONObject addArticleReq = new JSONObject();
addArticleReq.put(Article.ARTICLE, article);
final String articleId = articleMgmtService.addArticle(addArticleReq);
final String pageId = page.optString(Keys.OBJECT_ID);
final List<JSONObject> comments = commentRepository.getList(new Query().setFilter(new PropertyFilter(Comment.COMMENT_ON_ID, FilterOperator.EQUAL, pageId)));
final Transaction transaction = pageRepository.beginTransaction();
for (final JSONObject comment : comments) {
comment.put(Comment.COMMENT_ON_ID, articleId);
comment.put(Comment.COMMENT_ON_TYPE, Article.ARTICLE);
final String commentId = comment.optString(Keys.OBJECT_ID);
commentRepository.update(commentId, comment);
}
page.put(Page.PAGE_TYPE, "link");
pageRepository.update(pageId, page);
transaction.commit();
}
}
}
......@@ -18,12 +18,10 @@
package org.b3log.solo.service;
import org.b3log.latke.Keys;
import org.b3log.latke.util.Requests;
import org.b3log.solo.AbstractTestCase;
import org.b3log.solo.model.Comment;
import org.b3log.solo.model.Page;
import org.b3log.solo.util.Solos;
import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.Test;
......@@ -34,7 +32,7 @@ import java.util.List;
* {@link CommentMgmtService} test case.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.3, Sep 16, 2018
* @version 1.0.0.4, Apr 18, 2019
*/
@Test(suiteName = "service")
public class CommentMgmtServiceTestCase extends AbstractTestCase {
......@@ -91,59 +89,6 @@ public class CommentMgmtServiceTestCase extends AbstractTestCase {
Assert.assertEquals(result.getJSONArray(Comment.COMMENTS).length(), 2);
}
/**
* Add Page Comment.
*
* @throws Exception exception
*/
@Test(dependsOnMethods = "addArticleComment")
public void addPageComment() throws Exception {
addPage();
final PageQueryService pageQueryService = getPageQueryService();
final JSONObject paginationRequest = Solos.buildPaginationRequest("1/10/20");
JSONObject result = pageQueryService.getPages(paginationRequest);
Assert.assertNotNull(result);
Assert.assertEquals(result.getJSONArray(Page.PAGES).length(), 1);
final JSONArray pages = result.getJSONArray(Page.PAGES);
final CommentQueryService commentQueryService = getCommentQueryService();
result = commentQueryService.getComments(paginationRequest);
Assert.assertNotNull(result);
Assert.assertEquals(result.getJSONArray(Comment.COMMENTS).length(),
2); // 2 article comments
final CommentMgmtService commentMgmtService = getCommentMgmtService();
final JSONObject requestJSONObject = new JSONObject();
final String pageId = pages.getJSONObject(0).getString(Keys.OBJECT_ID);
requestJSONObject.put(Keys.OBJECT_ID, pageId);
requestJSONObject.put(Comment.COMMENT_NAME, "Solo");
requestJSONObject.put(Comment.COMMENT_URL, "comment URL");
requestJSONObject.put(Comment.COMMENT_CONTENT, "comment content");
final JSONObject addResult = commentMgmtService.addPageComment(requestJSONObject);
Assert.assertNotNull(addResult);
Assert.assertNotNull(addResult.getString(Keys.OBJECT_ID));
Assert.assertNotNull(addResult.getString(Comment.COMMENT_T_DATE));
Assert.assertNotNull(addResult.getString(Comment.COMMENT_THUMBNAIL_URL));
Assert.assertNotNull(addResult.getString(Comment.COMMENT_SHARP_URL));
result = commentQueryService.getComments(paginationRequest);
Assert.assertNotNull(result);
Assert.assertEquals(result.getJSONArray(Comment.COMMENTS).length(),
3); // 2 article comments + 1 page comment
final List<JSONObject> pageComments = commentQueryService.getComments(pageId);
Assert.assertNotNull(pageComments);
Assert.assertEquals(pageComments.size(), 1);
}
/**
* Adds a page.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment