🔒 评论接口用户鉴权

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.dto.CommentDTO;
import com.rymcu.forest.entity.Comment;
import com.rymcu.forest.web.api.exception.BaseApiException;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@ -26,7 +27,7 @@ public interface CommentService extends Service<Comment> {
* @param request
* @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.service.ArticleService;
import com.rymcu.forest.service.CommentService;
import com.rymcu.forest.util.Html2TextUtil;
import com.rymcu.forest.util.NotificationUtils;
import com.rymcu.forest.util.Utils;
import com.rymcu.forest.util.XssUtils;
import com.rymcu.forest.util.*;
import com.rymcu.forest.web.api.exception.BaseApiException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -65,23 +63,24 @@ public class CommentServiceImpl extends AbstractService<Comment> implements Comm
@Override
@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);
if(comment.getCommentArticleId() == null){
map.put("message","非法访问,文章主键异常!");
if (comment.getCommentArticleId() == null) {
map.put("message", "非法访问,文章主键异常!");
return map;
}
if(comment.getCommentAuthorId() == null){
map.put("message","非法访问,用户未登录!");
if (comment.getCommentAuthorId() == null) {
map.put("message", "非法访问,用户未登录!");
return map;
}
if(StringUtils.isBlank(comment.getCommentContent())){
map.put("message","回帖内容不能为空!");
if (StringUtils.isBlank(comment.getCommentContent())) {
map.put("message", "回帖内容不能为空!");
return map;
}
Article article = articleService.findById(comment.getCommentArticleId().toString());
if (article == null) {
map.put("message","文章不存在!");
map.put("message", "文章不存在!");
return map;
}
String ip = Utils.getIpAddress(request);
@ -95,23 +94,23 @@ public class CommentServiceImpl extends AbstractService<Comment> implements Comm
commentMapper.updateCommentSharpUrl(comment.getIdComment(), commentSharpUrl);
String commentContent = comment.getCommentContent();
if(StringUtils.isNotBlank(commentContent)){
if (StringUtils.isNotBlank(commentContent)) {
Integer length = commentContent.length();
if(length > MAX_PREVIEW){
if (length > MAX_PREVIEW) {
length = 200;
}
String commentPreviewContent = commentContent.substring(0,length);
String commentPreviewContent = commentContent.substring(0, length);
commentContent = Html2TextUtil.getContent(commentPreviewContent);
// 评论者不是作者本人则进行消息通知
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) {
Comment originalComment = commentMapper.selectByPrimaryKey(comment.getCommentOriginalCommentId());
// 回复消息时,评论者不是上级评论作者则进行消息通知
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;
@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);
return GlobalResultGenerator.genSuccessResult(map);
}