🔒 评论接口用户鉴权

This commit is contained in:
ronger 2022-05-11 19:50:28 +08:00
parent ac247f8e68
commit da934ff548
3 changed files with 19 additions and 19 deletions

View File

@ -3,6 +3,7 @@ package com.rymcu.forest.service;
import com.rymcu.forest.core.service.Service; import com.rymcu.forest.core.service.Service;
import com.rymcu.forest.dto.CommentDTO; import com.rymcu.forest.dto.CommentDTO;
import com.rymcu.forest.entity.Comment; import com.rymcu.forest.entity.Comment;
import com.rymcu.forest.web.api.exception.BaseApiException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.List; import java.util.List;
@ -26,7 +27,7 @@ public interface CommentService extends Service<Comment> {
* @param request * @param request
* @return * @return
*/ */
Map postComment(Comment comment, HttpServletRequest request); Map postComment(Comment comment, HttpServletRequest request) throws BaseApiException;
/** /**
* 获取评论列表数据 * 获取评论列表数据

View File

@ -9,10 +9,8 @@ import com.rymcu.forest.entity.Comment;
import com.rymcu.forest.mapper.CommentMapper; import com.rymcu.forest.mapper.CommentMapper;
import com.rymcu.forest.service.ArticleService; import com.rymcu.forest.service.ArticleService;
import com.rymcu.forest.service.CommentService; import com.rymcu.forest.service.CommentService;
import com.rymcu.forest.util.Html2TextUtil; import com.rymcu.forest.util.*;
import com.rymcu.forest.util.NotificationUtils; import com.rymcu.forest.web.api.exception.BaseApiException;
import com.rymcu.forest.util.Utils;
import com.rymcu.forest.util.XssUtils;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -65,23 +63,24 @@ public class CommentServiceImpl extends AbstractService<Comment> implements Comm
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Map postComment(Comment comment, HttpServletRequest request) { public Map postComment(Comment comment, HttpServletRequest request) throws BaseApiException {
comment.setCommentAuthorId(Objects.requireNonNull(UserUtils.getCurrentUserByToken()).getIdUser());
Map map = new HashMap(1); Map map = new HashMap(1);
if(comment.getCommentArticleId() == null){ if (comment.getCommentArticleId() == null) {
map.put("message","非法访问,文章主键异常!"); map.put("message", "非法访问,文章主键异常!");
return map; return map;
} }
if(comment.getCommentAuthorId() == null){ if (comment.getCommentAuthorId() == null) {
map.put("message","非法访问,用户未登录!"); map.put("message", "非法访问,用户未登录!");
return map; return map;
} }
if(StringUtils.isBlank(comment.getCommentContent())){ if (StringUtils.isBlank(comment.getCommentContent())) {
map.put("message","回帖内容不能为空!"); map.put("message", "回帖内容不能为空!");
return map; return map;
} }
Article article = articleService.findById(comment.getCommentArticleId().toString()); Article article = articleService.findById(comment.getCommentArticleId().toString());
if (article == null) { if (article == null) {
map.put("message","文章不存在!"); map.put("message", "文章不存在!");
return map; return map;
} }
String ip = Utils.getIpAddress(request); String ip = Utils.getIpAddress(request);
@ -95,23 +94,23 @@ public class CommentServiceImpl extends AbstractService<Comment> implements Comm
commentMapper.updateCommentSharpUrl(comment.getIdComment(), commentSharpUrl); commentMapper.updateCommentSharpUrl(comment.getIdComment(), commentSharpUrl);
String commentContent = comment.getCommentContent(); String commentContent = comment.getCommentContent();
if(StringUtils.isNotBlank(commentContent)){ if (StringUtils.isNotBlank(commentContent)) {
Integer length = commentContent.length(); Integer length = commentContent.length();
if(length > MAX_PREVIEW){ if (length > MAX_PREVIEW) {
length = 200; length = 200;
} }
String commentPreviewContent = commentContent.substring(0,length); String commentPreviewContent = commentContent.substring(0, length);
commentContent = Html2TextUtil.getContent(commentPreviewContent); commentContent = Html2TextUtil.getContent(commentPreviewContent);
// 评论者不是作者本人则进行消息通知 // 评论者不是作者本人则进行消息通知
if (!article.getArticleAuthorId().equals(comment.getCommentAuthorId())) { if (!article.getArticleAuthorId().equals(comment.getCommentAuthorId())) {
NotificationUtils.saveNotification(article.getArticleAuthorId(),comment.getIdComment(), NotificationConstant.Comment, commentContent); NotificationUtils.saveNotification(article.getArticleAuthorId(), comment.getIdComment(), NotificationConstant.Comment, commentContent);
} }
// 判断是否是回复消息 // 判断是否是回复消息
if (comment.getCommentOriginalCommentId() != null && comment.getCommentOriginalCommentId() != 0) { if (comment.getCommentOriginalCommentId() != null && comment.getCommentOriginalCommentId() != 0) {
Comment originalComment = commentMapper.selectByPrimaryKey(comment.getCommentOriginalCommentId()); Comment originalComment = commentMapper.selectByPrimaryKey(comment.getCommentOriginalCommentId());
// 回复消息时,评论者不是上级评论作者则进行消息通知 // 回复消息时,评论者不是上级评论作者则进行消息通知
if (!comment.getCommentAuthorId().equals(originalComment.getCommentAuthorId())) { if (!comment.getCommentAuthorId().equals(originalComment.getCommentAuthorId())) {
NotificationUtils.saveNotification(originalComment.getCommentAuthorId(),comment.getIdComment(), NotificationConstant.Comment, commentContent); NotificationUtils.saveNotification(originalComment.getCommentAuthorId(), comment.getIdComment(), NotificationConstant.Comment, commentContent);
} }
} }
} }

View File

@ -26,7 +26,7 @@ public class CommentController {
private CommentService commentService; private CommentService commentService;
@PostMapping("/post") @PostMapping("/post")
public GlobalResult postComment(@RequestBody Comment comment, HttpServletRequest request) throws BaseApiException, UnsupportedEncodingException { public GlobalResult postComment(@RequestBody Comment comment, HttpServletRequest request) throws BaseApiException {
Map map = commentService.postComment(comment,request); Map map = commentService.postComment(comment,request);
return GlobalResultGenerator.genSuccessResult(map); return GlobalResultGenerator.genSuccessResult(map);
} }