From 2abbece9b58ea48097e638a1bb92b5f0f8fbbd8f Mon Sep 17 00:00:00 2001
From: wangqing <408564126@qq.com>
Date: Sat, 2 Mar 2024 09:42:53 +0800
Subject: [PATCH] fix: comment with realtime user info
---
luoo_comment/pom.xml | 20 ++++
.../comment/controller/CommentController.java | 110 +++++++++++++++---
.../luoo/comment/dao/PublicationLikeDao.java | 12 ++
.../com/luoo/comment/pojo/CommentResp.java | 10 +-
luoo_comment/src/main/resources/bootstrap.yml | 33 +++++-
.../src/main/resources/caffeine.properties | 3 +
luoo_music/src/main/resources/bootstrap.yml | 2 +-
7 files changed, 166 insertions(+), 24 deletions(-)
create mode 100644 luoo_comment/src/main/java/com/luoo/comment/dao/PublicationLikeDao.java
create mode 100644 luoo_comment/src/main/resources/caffeine.properties
diff --git a/luoo_comment/pom.xml b/luoo_comment/pom.xml
index 2cbbacf..6561788 100644
--- a/luoo_comment/pom.xml
+++ b/luoo_comment/pom.xml
@@ -75,6 +75,26 @@
ip2region
2.7.0
+
+ net.oschina.j2cache
+ j2cache-spring-boot2-starter
+ 2.8.0-release
+
+
+ net.oschina.j2cache
+ j2cache-core
+ 2.8.5-release
+
+
+ org.slf4j
+ slf4j-simple
+
+
+ org.slf4j
+ slf4j-api
+
+
+
app
diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java
index 6099c64..14ea27d 100644
--- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java
+++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java
@@ -4,6 +4,7 @@ package com.luoo.comment.controller;
import api.PageResult;
import api.Result;
import api.StatusCode;
+import com.luoo.comment.dao.PublicationLikeDao;
import com.luoo.comment.pojo.*;
import com.luoo.comment.service.CommentService;
import com.luoo.comment.service.ComplaintService;
@@ -19,6 +20,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
+import net.oschina.j2cache.CacheChannel;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
@@ -32,10 +34,7 @@ import org.springframework.web.bind.annotation.*;
import util.IdWorker;
import javax.servlet.http.HttpServletRequest;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -74,6 +73,12 @@ public class CommentController extends BaseController {
@Autowired
private IdWorker idWorker;
+ @Autowired
+ private CacheChannel cacheChannel;
+
+ @Autowired
+ private PublicationLikeDao publicationLikeDao;
+
@GetMapping
public Result findAll(){
return Result.success(commentService.findAll());
@@ -113,35 +118,55 @@ public class CommentController extends BaseController {
@ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)})
@GetMapping("/hot/{journalId}/{page}/{size}")
public Result findHotByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) {
+// List list = (List) cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT+"_"+journalId,page+"_"+size, key->getHotList(journalId,page,size),true).getValue();
+ List list = getHotList(journalId,page,size);
//验证是否登录,并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
- Page pageList = commentService.findByJournalId(journalId,page,size);
- List list =pageList.stream().parallel().map(x->getCommentResp(x,"")).collect(Collectors.toList());
+
+
return Result.success(new PageResult(Long.valueOf(list.size()),list));
}
String userId = userLoginDto.getUserId();
- Page pageList = commentService.findHotByJournalId(journalId,page,size);
- List list =pageList.stream().parallel().map(x->getCommentResp(x,userId)).collect(Collectors.toList());
+
+ Set publicationLikeSet = publicationLikeDao.findAllByUserId(userId).parallelStream().map(p->p.getLikedItemId()).collect(Collectors.toSet());
+ list = list.parallelStream().map(c->updateHaveThumbup(c,publicationLikeSet)).collect(Collectors.toList());
return Result.success(new PageResult(Long.valueOf(list.size()),list));
}
+ private List getHotList(String journalId,int page,int size) {
+ Page pageList = commentService.findHotByJournalId(journalId,page,size);
+ List list =pageList.stream().parallel().map(x->getCommentResp(x)).collect(Collectors.toList());
+ return list;
+ }
+
+ private List getNewList(String journalId,int page,int size) {
+ Page pageList = commentService.findNewByJournalId(journalId,page,size);
+ List list =pageList.stream().parallel().map(x->getCommentResp(x)).collect(Collectors.toList());
+ return list;
+ }
+
+
+ private CommentResp updateHaveThumbup(CommentResp commentResp,Set publicationLikeSet) {
+ commentResp.setHaveThumbup(publicationLikeSet.contains(commentResp.get_id()));
+ return commentResp;
+ }
@ApiOperation(value = "根据期刊ID获取分页评论列表(最新),按时间倒序",notes = "根据期刊ID获取根节点评论分页列表 ,数据库中保存的有期刊号的都是根节点,期刊号为空的代表有父评论且parentID一定有值,拿到当然根节点评论列表,每个根评论有 commentCount字段,页面可以显示有 280个回复,然后根据 @GetMapping(\"/comment/{parentId}/{page}/{size}\") 接口,分页拿到该评论下的子评论列表,页面在请求添加评论的时候,当给评论添加子评论的时候,父评论parentID有值,就无法添加子评论并且不显示回复2字,控制到两级")
@ApiImplicitParams({@ApiImplicitParam(name = "journalId", value = "剘刊id", required = true),
@ApiImplicitParam(name = "page", value = "分页: 页码,以1开始", required = true),
@ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)})
@GetMapping("/new/{journalId}/{page}/{size}")
public Result findNewByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) {
+ List list = getNewList(journalId,page,size);
//验证是否登录,并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
- Page pageList = commentService.findByJournalId(journalId,page,size);
- List list =pageList.stream().parallel().map(x->getCommentResp(x,"")).collect(Collectors.toList());
+
return Result.success(new PageResult(Long.valueOf(list.size()),list));
}
String userId = userLoginDto.getUserId();
- Page pageList = commentService.findNewByJournalId(journalId,page,size);
- List list =pageList.stream().parallel().map(x->getCommentResp(x,userId)).collect(Collectors.toList());
+ Set publicationLikeSet = publicationLikeDao.findAllByUserId(userId).parallelStream().map(p->p.getLikedItemId()).collect(Collectors.toSet());
+ list = list.parallelStream().map(c->updateHaveThumbup(c,publicationLikeSet)).collect(Collectors.toList());
return Result.success(new PageResult(Long.valueOf(list.size()),list));
}
@@ -165,7 +190,15 @@ public class CommentController extends BaseController {
CommentResp commentResp = new CommentResp();
BeanUtils.copyProperties(comment,commentResp);
if(comment.getCommentCount()>0){
+
Comment topComment = commentService.findByParentId(comment.get_id(),1,1).getContent().get(0);
+ UserInfo topCommentUserInfo = userInfoService.findById(topComment.getUserId());
+ topComment.setNickName(topCommentUserInfo.getNickName());
+ topComment.setAvatar(Constants.RESOURCE_PREFIX+topCommentUserInfo.getAvatar());
+ if(StringUtils.isNotEmpty(topComment.getReplyToUserId())){
+ UserInfo topReplyUserInfo = userInfoService.findById(topComment.getReplyToUserId());
+ topComment.setReplyToNickname(topReplyUserInfo.getNickName());
+ }
commentResp.setTopChildrenComment(topComment);
}
commentResp.setThumbupCountString(comment.getThumbupCount()+"");
@@ -176,6 +209,41 @@ public class CommentController extends BaseController {
return commentResp;
}
+
+
+ private CommentResp getCommentResp(Comment comment) {
+
+
+
+ UserInfo commentUserInfo = userInfoService.findById(comment.getUserId());
+ comment.setNickName(commentUserInfo.getNickName());
+ comment.setAvatar(Constants.RESOURCE_PREFIX+commentUserInfo.getAvatar());
+ if(StringUtils.isNotEmpty(comment.getReplyToUserId())){
+ UserInfo replyToUserInfo = userInfoService.findById(comment.getReplyToUserId());
+ comment.setReplyToNickname(replyToUserInfo.getNickName());
+ }
+ CommentResp commentResp = new CommentResp();
+ BeanUtils.copyProperties(comment,commentResp);
+ if(comment.getCommentCount()>0){
+
+ Comment topComment = commentService.findByParentId(comment.get_id(),1,1).getContent().get(0);
+ UserInfo topCommentUserInfo = userInfoService.findById(topComment.getUserId());
+ topComment.setNickName(topCommentUserInfo.getNickName());
+ topComment.setAvatar(Constants.RESOURCE_PREFIX+topCommentUserInfo.getAvatar());
+ if(StringUtils.isNotEmpty(topComment.getReplyToUserId())){
+ UserInfo topReplyUserInfo = userInfoService.findById(topComment.getReplyToUserId());
+ topComment.setReplyToNickname(topReplyUserInfo.getNickName());
+ }
+ commentResp.setTopChildrenComment(topComment);
+ }
+ commentResp.setThumbupCountString(comment.getThumbupCount()+"");
+ commentResp.setThumbupCountInt(comment.getThumbupCount());
+ if(comment.getThumbupCount()>999) {
+ commentResp.setThumbupCountString("999+");
+ }
+ return commentResp;
+ }
+
@GetMapping("/{page}/{size}")
public Result search(@PathVariable int page,@PathVariable int size){
Page pageData = commentService.search(page,size);
@@ -189,7 +257,7 @@ public class CommentController extends BaseController {
UserLoginDto userLoginDto = getUserLoginDto(authorization);
Comment comment = commentService.findById(commentId);
if (null == userLoginDto) {
- CommentResp commentResp = getCommentResp(comment,"");
+ CommentResp commentResp = getCommentResp(comment);
return Result.success(commentResp);
}
return Result.success(getCommentResp(comment,userLoginDto.getUserId()));
@@ -249,19 +317,27 @@ public class CommentController extends BaseController {
@GetMapping("/comment/{parentId}/{page}/{size}")
public Result findByParentId(@PathVariable String parentId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) {
+
+ List list = getChildrenCommentList(parentId,page,size);
//验证是否登录,并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
- Page pageList = commentService.findByParentId(parentId,page,size);
- List list =pageList.stream().parallel().map(x->getCommentResp(x,"")).collect(Collectors.toList());
+
return Result.success(new PageResult(Long.valueOf(list.size()),list));
}
String userId = userLoginDto.getUserId();
- Page pageList = commentService.findByParentId(parentId,page,size);
- List list =pageList.stream().parallel().map(x->getCommentResp(x,userId)).collect(Collectors.toList());
+ Set publicationLikeSet = publicationLikeDao.findAllByUserId(userId).parallelStream().map(p->p.getLikedItemId()).collect(Collectors.toSet());
+ list = list.parallelStream().map(c->updateHaveThumbup(c,publicationLikeSet)).collect(Collectors.toList());
return Result.success(new PageResult(Long.valueOf(list.size()),list));
}
+
+
+ private List getChildrenCommentList(String parentId,int page,int size) {
+ Page pageList = commentService.findByParentId(parentId,page,size);
+ List list =pageList.stream().parallel().map(x->getCommentResp(x)).collect(Collectors.toList());
+ return list;
+ }
@ApiOperation(value = "点赞/取消点赞评论",notes = "返回")
@PutMapping("/thumbup/{commentId}")
public Result thumbup(@PathVariable String commentId,@RequestHeader(value = "Authorization", required = true) String authorization) {
diff --git a/luoo_comment/src/main/java/com/luoo/comment/dao/PublicationLikeDao.java b/luoo_comment/src/main/java/com/luoo/comment/dao/PublicationLikeDao.java
new file mode 100644
index 0000000..b063b67
--- /dev/null
+++ b/luoo_comment/src/main/java/com/luoo/comment/dao/PublicationLikeDao.java
@@ -0,0 +1,12 @@
+package com.luoo.comment.dao;
+
+import com.luoo.comment.pojo.PublicationLike;
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+import java.util.List;
+
+public interface PublicationLikeDao extends MongoRepository {
+
+ public List findAllByUserId(String userId);
+
+}
diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java
index d3ab8fd..dbdc23b 100644
--- a/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java
+++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java
@@ -48,11 +48,11 @@ public class CommentResp implements Serializable {
private boolean haveThumbup;
-// private String rootId;
-//
-// private String rootJournalId;
-//
-// private String rootJournalName;
+ private String rootId;
+
+ private String rootJournalId;
+
+ private String rootJournalName;
private String replyToUserId;
diff --git a/luoo_comment/src/main/resources/bootstrap.yml b/luoo_comment/src/main/resources/bootstrap.yml
index cc9070b..c718333 100644
--- a/luoo_comment/src/main/resources/bootstrap.yml
+++ b/luoo_comment/src/main/resources/bootstrap.yml
@@ -5,4 +5,35 @@ spring:
profile: dev
label: master
uri: http://116.62.145.60:12000
-# uri: http://127.0.0.1:12000
\ No newline at end of file
+# uri: http://127.0.0.1:12000
+
+redis:
+ # 地址, 多个地址使用‘,’逗号分割
+ hosts: ${spring.redis.host}
+
+j2cache:
+ openSpringCache: true
+ # 缓存中不存在时,运行缓存空对象
+ allowNullValues: true
+ redisClient: lettuce
+ l2CacheOpen: true
+ serialization: json
+ sync_ttl_to_redis: true
+ # 一级缓存使用caffeine
+ L1:
+ provider_class: caffeine
+ L2:
+ #使用springRedis替换二级缓存
+ provider_class: net.oschina.j2cache.cache.support.redis.SpringRedisProvider
+ config_section: redis
+ #使用springRedis进行广播通知缓失效
+ broadcast: net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy
+
+# 上面配置的一级缓存为caffeine, 那么这里对一级缓存的配置就必须以这个caffeine开头
+caffeine:
+ properties: caffeine.properties
+---
+spring:
+ cache:
+ # 一级缓存使用caffeine
+ type: caffeine
\ No newline at end of file
diff --git a/luoo_comment/src/main/resources/caffeine.properties b/luoo_comment/src/main/resources/caffeine.properties
new file mode 100644
index 0000000..4fed996
--- /dev/null
+++ b/luoo_comment/src/main/resources/caffeine.properties
@@ -0,0 +1,3 @@
+default: 10000, 1h
+journal_query_page: 10000, 6h
+user_info: 20000, 6h
\ No newline at end of file
diff --git a/luoo_music/src/main/resources/bootstrap.yml b/luoo_music/src/main/resources/bootstrap.yml
index 099afc9..a051078 100644
--- a/luoo_music/src/main/resources/bootstrap.yml
+++ b/luoo_music/src/main/resources/bootstrap.yml
@@ -12,7 +12,7 @@ spring:
max-request-size: 200MB
redis:
# 地址, 多个地址使用‘,’逗号分割
- hosts: ${spring.redis.host}:${spring.redis.port}
+ hosts: ${spring.redis.host}
j2cache:
openSpringCache: true