CommentController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Copyright (c) 2015-2016, Michael Yang 杨福海 (fuhai999@gmail.com).
  3. *
  4. * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.gnu.org/licenses/lgpl-3.0.txt
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package io.jpress.front.controller;
  17. import io.jpress.Consts;
  18. import io.jpress.core.BaseFrontController;
  19. import io.jpress.core.cache.ActionCacheManager;
  20. import io.jpress.model.Comment;
  21. import io.jpress.model.Content;
  22. import io.jpress.model.User;
  23. import io.jpress.model.query.ContentQuery;
  24. import io.jpress.model.query.OptionQuery;
  25. import io.jpress.model.query.UserQuery;
  26. import io.jpress.router.RouterMapping;
  27. import io.jpress.utils.CookieUtils;
  28. import io.jpress.utils.StringUtils;
  29. import java.math.BigInteger;
  30. import java.util.Date;
  31. @RouterMapping(url = "/comment")
  32. public class CommentController extends BaseFrontController {
  33. public void index() {
  34. renderError(404);
  35. }
  36. public void submit() {
  37. String gotoUrl = getPara("goto");
  38. if (gotoUrl == null) {
  39. gotoUrl = getRequest().getHeader("Referer");
  40. }
  41. String anchor = getPara("anchor");
  42. if (gotoUrl != null && anchor != null) {
  43. gotoUrl += "#" + anchor;
  44. }
  45. // 是否开启验证码功能
  46. Boolean comment_need_captcha = OptionQuery.me().findValueAsBool("comment_need_captcha");
  47. if (comment_need_captcha != null && comment_need_captcha == true) {
  48. if (!validateCaptcha("comment_captcha")) { // 验证码验证失败
  49. renderForCommentError("validate captcha fail", 1);
  50. return;
  51. }
  52. }
  53. BigInteger userId = StringUtils.toBigInteger(CookieUtils.get(this, Consts.COOKIE_LOGINED_USER), null);
  54. BigInteger uuUserId = getParaToBigInteger("uuUserId");
  55. // 允许未登录用户评论
  56. Boolean comment_allow_not_login = OptionQuery.me().findValueAsBool("comment_allow_not_login");
  57. // 允许未登录用户评论
  58. if (comment_allow_not_login == null || comment_allow_not_login == false) {
  59. //在不允许未登录用户评论,uuhelper只能在uuId和uuUserId都为空的情况下不准评论
  60. if (userId == null && uuUserId == null) {
  61. String redirect = Consts.ROUTER_USER_LOGIN;
  62. if (StringUtils.isNotBlank(gotoUrl)) {
  63. redirect += "?goto=" + StringUtils.urlEncode(gotoUrl);
  64. }
  65. redirect(redirect);
  66. return;
  67. }
  68. }
  69. String status = Comment.STATUS_NORMAL;
  70. Boolean comment_must_audited = OptionQuery.me().findValueAsBool("comment_must_audited");
  71. if (comment_must_audited != null && comment_must_audited) {
  72. status = Comment.STATUS_DRAFT;
  73. }
  74. BigInteger contentId = getParaToBigInteger("cid");
  75. if (contentId == null) {
  76. renderForCommentError("comment fail,content id is null.", 1);
  77. return;
  78. }
  79. Content content = ContentQuery.me().findById(contentId);
  80. if (content == null) {
  81. renderForCommentError("find not find the content, maybe it has bean deleted.", 1);
  82. return;
  83. }
  84. if (!content.isCommentEnable()) {
  85. renderForCommentError("the comment function of the content has been closed.", 1);
  86. return;
  87. }
  88. String text = getPara("text");
  89. if (StringUtils.isBlank(text)) {
  90. renderForCommentError("comment fail,text is blank.", 2);
  91. return;
  92. }
  93. String uuUserAvatar = getPara("uu_user_avatar");
  94. String author = getPara("author");
  95. if (userId == null && uuUserId != null) {
  96. author = getPara("uuUserName");
  97. }
  98. String email = getPara("email");
  99. String ip = getIPAddress();
  100. String agent = getUserAgent();
  101. String type = Comment.TYPE_COMMENT;
  102. if (userId != null) {
  103. User user = UserQuery.me().findById(userId);
  104. if (user != null) {
  105. author = StringUtils.isNotBlank(user.getNickname()) ? user.getNickname() : user.getUsername();
  106. }
  107. }
  108. if (StringUtils.isBlank(author)) {
  109. String defautAuthor = OptionQuery.me().findValue("comment_default_nickname");
  110. author = StringUtils.isNotBlank(defautAuthor) ? defautAuthor : "网友";
  111. }
  112. BigInteger parentId = getParaToBigInteger("parent_id");
  113. Comment comment = new Comment();
  114. comment.setContentModule(content.getModule());
  115. comment.setType(Comment.TYPE_COMMENT);
  116. comment.setContentId(content.getId());
  117. comment.setText(text);
  118. comment.setIp(ip);
  119. comment.setAgent(agent);
  120. comment.setAuthor(author);
  121. comment.setEmail(email);
  122. comment.setType(type);
  123. comment.setStatus(status);
  124. comment.setUserId(userId);
  125. comment.setCreated(new Date());
  126. comment.setParentId(parentId);
  127. comment.setUuUserId(uuUserId);
  128. comment.setUuUserAvatar(uuUserAvatar);
  129. if (comment.save()) {
  130. ActionCacheManager.clearCache();
  131. }
  132. if (isAjaxRequest()) {
  133. renderAjaxResultForSuccess();
  134. return;
  135. }
  136. if (gotoUrl != null) {
  137. redirect(gotoUrl);
  138. return;
  139. }
  140. renderText("comment ok");
  141. }
  142. private void renderForCommentError(String message, int errorCode) {
  143. String referer = getRequest().getHeader("Referer");
  144. if (isAjaxRequest()) {
  145. renderAjaxResult(message, errorCode);
  146. } else {
  147. redirect(referer + "#" + getPara("anchor"));
  148. }
  149. }
  150. }