Commit 2e131795 authored by Liang Ding's avatar Liang Ding

🔨 #12246

parent 8fae6ef1
......@@ -29,13 +29,12 @@ import org.b3log.latke.servlet.annotation.RequestProcessor;
import org.b3log.latke.servlet.renderer.JSONRenderer;
import org.b3log.latke.util.Requests;
import org.b3log.latke.util.freemarker.Templates;
import org.b3log.solo.model.Article;
import org.b3log.solo.model.Comment;
import org.b3log.solo.model.Common;
import org.b3log.solo.model.Page;
import org.b3log.solo.model.*;
import org.b3log.solo.service.CommentMgmtService;
import org.b3log.solo.service.PreferenceQueryService;
import org.b3log.solo.service.UserMgmtService;
import org.b3log.solo.service.UserQueryService;
import org.b3log.solo.util.Skins;
import org.json.JSONObject;
import javax.inject.Inject;
......@@ -54,7 +53,7 @@ import java.util.Map;
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author ArmstrongCN
* @version 1.3.2.12, Feb 17, 2017
* @version 1.3.2.13, Feb 18, 2017
* @since 0.3.1
*/
@RequestProcessor
......@@ -89,6 +88,12 @@ public class CommentProcessor {
@Inject
private UserMgmtService userMgmtService;
/**
* Preference query service.
*/
@Inject
private PreferenceQueryService preferenceQueryService;
/**
* Adds a comment to a page.
* <p>
......@@ -122,15 +127,12 @@ public class CommentProcessor {
final HttpServletResponse httpServletResponse = context.getResponse();
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(httpServletRequest, httpServletResponse);
requestJSONObject.put(Common.TYPE, Page.PAGE);
fillCommenter(requestJSONObject, httpServletRequest, httpServletResponse);
final JSONObject jsonObject = commentMgmtService.checkAddCommentRequest(requestJSONObject);
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
renderer.setJSONObject(jsonObject);
......@@ -149,11 +151,9 @@ public class CommentProcessor {
}
final String storedCaptcha = (String) session.getAttribute(CaptchaProcessor.CAPTCHA);
session.removeAttribute(CaptchaProcessor.CAPTCHA);
if (!userQueryService.isLoggedIn(httpServletRequest, httpServletResponse)) {
final String captcha = requestJSONObject.optString(CaptchaProcessor.CAPTCHA);
if (null == storedCaptcha || !storedCaptcha.equals(captcha)) {
......@@ -162,7 +162,6 @@ public class CommentProcessor {
return;
}
}
try {
......@@ -171,13 +170,26 @@ public class CommentProcessor {
final Map<String, Object> dataModel = new HashMap<>();
dataModel.put(Comment.COMMENT, addResult);
final String skinDirName = (String) httpServletRequest.getAttribute(Keys.TEMAPLTE_DIR_NAME);
final Template template = Templates.MAIN_CFG.getTemplate("common-comment.ftl");
final StringWriter stringWriter = new StringWriter();
template.process(dataModel, stringWriter);
stringWriter.close();
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) httpServletRequest.getAttribute(Keys.TEMAPLTE_DIR_NAME);
final Template template = Templates.MAIN_CFG.getTemplate("common-comment.ftl");
final JSONObject preference = preferenceQueryService.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();
addResult.put("cmtTpl", stringWriter.toString());
} catch (final Exception e) {
// 1.9.0 向后兼容
}
addResult.put("cmtTpl", stringWriter.toString());
addResult.put(Keys.STATUS_CODE, true);
renderer.setJSONObject(addResult);
......@@ -223,15 +235,12 @@ public class CommentProcessor {
final HttpServletResponse httpServletResponse = context.getResponse();
final JSONObject requestJSONObject = Requests.parseRequestJSONObject(httpServletRequest, httpServletResponse);
requestJSONObject.put(Common.TYPE, Article.ARTICLE);
fillCommenter(requestJSONObject, httpServletRequest, httpServletResponse);
final JSONObject jsonObject = commentMgmtService.checkAddCommentRequest(requestJSONObject);
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
renderer.setJSONObject(jsonObject);
......@@ -241,7 +250,6 @@ public class CommentProcessor {
}
final HttpSession session = httpServletRequest.getSession(false);
if (null == session) {
jsonObject.put(Keys.STATUS_CODE, false);
jsonObject.put(Keys.MSG, langPropsService.get("captchaErrorLabel"));
......@@ -250,20 +258,16 @@ public class CommentProcessor {
}
final String storedCaptcha = (String) session.getAttribute(CaptchaProcessor.CAPTCHA);
session.removeAttribute(CaptchaProcessor.CAPTCHA);
if (!userQueryService.isLoggedIn(httpServletRequest, httpServletResponse)) {
final String captcha = requestJSONObject.optString(CaptchaProcessor.CAPTCHA);
if (null == storedCaptcha || !storedCaptcha.equals(captcha)) {
jsonObject.put(Keys.STATUS_CODE, false);
jsonObject.put(Keys.MSG, langPropsService.get("captchaErrorLabel"));
return;
}
}
try {
......@@ -271,14 +275,26 @@ public class CommentProcessor {
final Map<String, Object> dataModel = new HashMap<>();
dataModel.put(Comment.COMMENT, addResult);
final JSONObject article = addResult.optJSONObject(Article.ARTICLE);
article.put(Common.COMMENTABLE, addResult.opt(Common.COMMENTABLE));
article.put(Common.PERMALINK, addResult.opt(Common.PERMALINK));
dataModel.put(Article.ARTICLE, article);
// https://github.com/b3log/solo/issues/12246
try {
final String skinDirName = (String) httpServletRequest.getAttribute(Keys.TEMAPLTE_DIR_NAME);
final Template template = Templates.MAIN_CFG.getTemplate("common-comment.ftl");
final JSONObject preference = preferenceQueryService.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();
addResult.put("cmtTpl", stringWriter.toString());
} catch (final Exception e) {
// 1.9.0 向后兼容
}
final String skinDirName = (String) httpServletRequest.getAttribute(Keys.TEMAPLTE_DIR_NAME);
final Template template = Templates.MAIN_CFG.getTemplate("common-comment.ftl");
final StringWriter stringWriter = new StringWriter();
template.process(dataModel, stringWriter);
stringWriter.close();
addResult.put("cmtTpl", stringWriter.toString());
addResult.put(Keys.STATUS_CODE, true);
renderer.setJSONObject(addResult);
......@@ -302,7 +318,6 @@ public class CommentProcessor {
userMgmtService.tryLogInWithCookie(httpServletRequest, httpServletResponse);
final JSONObject currentUser = userQueryService.getCurrentUser(httpServletRequest);
if (null == currentUser) {
return;
}
......
......@@ -57,7 +57,7 @@ import org.jsoup.safety.Whitelist;
* Comment management service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.3.2.9, Feb 17, 2017
* @version 1.3.2.10, Feb 18, 2017
* @since 0.3.5
*/
@Service
......@@ -400,7 +400,12 @@ public class CommentMgmtService {
* "commentSharpURL": "",
* "commentContent": "", // processed XSS HTML
* "commentName": "", // processed XSS
* "commentURL": "" // optional
* "commentURL": "", // optional
* "isReply": boolean,
* "page": {},
* "commentOriginalCommentId": "" // optional
* "commentable": boolean,
* "permalink": "" // page.pagePermalink
* }
* </pre>
*
......@@ -408,18 +413,21 @@ public class CommentMgmtService {
*/
public JSONObject addPageComment(final JSONObject requestJSONObject) throws ServiceException {
final JSONObject ret = new JSONObject();
ret.put(Common.IS_REPLY, false);
final Transaction transaction = commentRepository.beginTransaction();
try {
final String pageId = requestJSONObject.getString(Keys.OBJECT_ID);
final JSONObject page = pageRepository.get(pageId);
ret.put(Page.PAGE, page);
final String commentName = requestJSONObject.getString(Comment.COMMENT_NAME);
final String commentEmail = requestJSONObject.getString(Comment.COMMENT_EMAIL).trim().toLowerCase();
final String commentURL = requestJSONObject.optString(Comment.COMMENT_URL);
final String commentContent = requestJSONObject.getString(Comment.COMMENT_CONTENT);
final String originalCommentId = requestJSONObject.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID);
ret.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, originalCommentId);
// Step 1: Add comment
final JSONObject comment = new JSONObject();
......@@ -437,6 +445,11 @@ public class CommentMgmtService {
comment.put(Comment.COMMENT_DATE, date);
ret.put(Comment.COMMENT_DATE, DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
ret.put("commentDate2", date);
ret.put(Common.COMMENTABLE, preference.getBoolean(Option.ID_C_COMMENTABLE) && page.getBoolean(Page.PAGE_COMMENTABLE));
ret.put(Common.PERMALINK, page.getString(Page.PAGE_PERMALINK));
if (!Strings.isEmptyOrNull(originalCommentId)) {
originalComment = commentRepository.get(originalCommentId);
if (null != originalComment) {
......@@ -445,6 +458,8 @@ public class CommentMgmtService {
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, originalCommentName);
ret.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, originalCommentName);
ret.put(Common.IS_REPLY, true);
} else {
LOGGER.log(Level.WARN, "Not found orginal comment[id={0}] of reply[name={1}, content={2}]", originalCommentId,
commentName, commentContent);
......@@ -522,7 +537,12 @@ public class CommentMgmtService {
* "commentSharpURL": "",
* "commentContent": "", // processed XSS HTML
* "commentName": "", // processed XSS
* "commentURL": "" // optional
* "commentURL": "", // optional
* "isReply": boolean,
* "article": {},
* "commentOriginalCommentId": "", // optional
* "commentable": boolean,
* "permalink": "" // article.articlePermalink
* }
* </pre>
*
......@@ -530,18 +550,21 @@ public class CommentMgmtService {
*/
public JSONObject addArticleComment(final JSONObject requestJSONObject) throws ServiceException {
final JSONObject ret = new JSONObject();
ret.put(Common.IS_REPLY, false);
final Transaction transaction = commentRepository.beginTransaction();
try {
final String articleId = requestJSONObject.getString(Keys.OBJECT_ID);
final JSONObject article = articleRepository.get(articleId);
ret.put(Article.ARTICLE, article);
final String commentName = requestJSONObject.getString(Comment.COMMENT_NAME);
final String commentEmail = requestJSONObject.getString(Comment.COMMENT_EMAIL).trim().toLowerCase();
final String commentURL = requestJSONObject.optString(Comment.COMMENT_URL);
final String commentContent = requestJSONObject.getString(Comment.COMMENT_CONTENT);
final String originalCommentId = requestJSONObject.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID);
ret.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, originalCommentId);
// Step 1: Add comment
final JSONObject comment = new JSONObject();
......@@ -561,6 +584,10 @@ public class CommentMgmtService {
comment.put(Comment.COMMENT_DATE, date);
ret.put(Comment.COMMENT_DATE, DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
ret.put("commentDate2", date);
ret.put(Common.COMMENTABLE, preference.getBoolean(Option.ID_C_COMMENTABLE) && article.getBoolean(Article.ARTICLE_COMMENTABLE));
ret.put(Common.PERMALINK, article.getString(Article.ARTICLE_PERMALINK));
ret.put(Comment.COMMENT_NAME, commentName);
ret.put(Comment.COMMENT_CONTENT, commentContent);
......@@ -574,6 +601,8 @@ public class CommentMgmtService {
comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, originalCommentName);
ret.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, originalCommentName);
ret.put(Common.IS_REPLY, true);
} else {
LOGGER.log(Level.WARN, "Not found orginal comment[id={0}] of reply[name={1}, content={2}]",
new String[]{originalCommentId, commentName, commentContent});
......
......@@ -49,7 +49,7 @@ import org.json.JSONObject;
* Comment query service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.3.0.8, Jun 28, 2016
* @version 1.3.0.9, Feb 18, 2017
* @since 0.3.5
*/
@Service
......@@ -234,6 +234,7 @@ public class CommentQueryService {
for (final JSONObject comment : comments) {
comment.put(Comment.COMMENT_TIME, ((Date) comment.get(Comment.COMMENT_DATE)).getTime());
comment.put("commentDate2", comment.get(Comment.COMMENT_DATE)); // 1.9.0 向后兼容
comment.put(Comment.COMMENT_NAME, comment.getString(Comment.COMMENT_NAME));
String url = comment.getString(Comment.COMMENT_URL);
if (StringUtils.contains(url, "<")) { // legacy issue https://github.com/b3log/solo/issues/12091
......
......@@ -9,17 +9,16 @@
<#else>
<a class="user-name" href="${comment.commentURL}" target="_blank">${comment.commentName}</a>
</#if>
<#if false> comment.isReply
@
<a class="user-name" href="${servePath}${article.permalink}#${comment.commentOriginalCommentId}"
<#if comment.isReply>
@<a class="user-name" href="${servePath}${article.permalink}#${comment.commentOriginalCommentId}"
onmouseover="page.showComment(this, '${comment.commentOriginalCommentId}', 23);"
onmouseout="page.hideComment('${comment.commentOriginalCommentId}')"
>${comment.commentOriginalCommentName}</a>
</#if>
<time class="ft-gray">{comment.commentDate?string("yyyy-MM-dd HH:mm")}</time>
<time class="ft-gray">${comment.commentDate2?string("yyyy-MM-dd HH:mm")}</time>
<#if true> article.commentable
<a class="reply-btn" href="javascript:replyTo('${comment.oId}')">{replyLabel}</a>
<#if article.commentable>
<a class="reply-btn" href="javascript:replyTo('${comment.oId}')">${replyLabel}</a>
</#if>
</div>
<div class="content-reset">
......
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