fix: comment with realtime user info

main
wangqing 9 months ago
parent abc55394b6
commit 2abbece9b5

@ -75,6 +75,26 @@
<artifactId>ip2region</artifactId> <artifactId>ip2region</artifactId>
<version>2.7.0</version> <version>2.7.0</version>
</dependency> </dependency>
<dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-spring-boot2-starter</artifactId>
<version>2.8.0-release</version>
</dependency>
<dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-core</artifactId>
<version>2.8.5-release</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> </dependencies>
<build> <build>
<finalName>app</finalName> <finalName>app</finalName>

@ -4,6 +4,7 @@ package com.luoo.comment.controller;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import api.StatusCode; import api.StatusCode;
import com.luoo.comment.dao.PublicationLikeDao;
import com.luoo.comment.pojo.*; import com.luoo.comment.pojo.*;
import com.luoo.comment.service.CommentService; import com.luoo.comment.service.CommentService;
import com.luoo.comment.service.ComplaintService; import com.luoo.comment.service.ComplaintService;
@ -19,6 +20,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import net.oschina.j2cache.CacheChannel;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -32,10 +34,7 @@ import org.springframework.web.bind.annotation.*;
import util.IdWorker; import util.IdWorker;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Date; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -74,6 +73,12 @@ public class CommentController extends BaseController {
@Autowired @Autowired
private IdWorker idWorker; private IdWorker idWorker;
@Autowired
private CacheChannel cacheChannel;
@Autowired
private PublicationLikeDao publicationLikeDao;
@GetMapping @GetMapping
public Result findAll(){ public Result findAll(){
return Result.success(commentService.findAll()); return Result.success(commentService.findAll());
@ -113,35 +118,55 @@ public class CommentController extends BaseController {
@ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)}) @ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)})
@GetMapping("/hot/{journalId}/{page}/{size}") @GetMapping("/hot/{journalId}/{page}/{size}")
public Result findHotByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) { public Result findHotByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) {
// List<CommentResp> list = (List<CommentResp>) cacheChannel.get(Constants.J2CACHE_REGION_JOURNAL_COMMENT_PAGE_HOT+"_"+journalId,page+"_"+size, key->getHotList(journalId,page,size),true).getValue();
List<CommentResp> list = getHotList(journalId,page,size);
//验证是否登录并且拿到ID //验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization); UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) { if (null == userLoginDto) {
Page<Comment> pageList = commentService.findByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().parallel().map(x->getCommentResp(x,"")).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list)); return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
} }
String userId = userLoginDto.getUserId(); String userId = userLoginDto.getUserId();
Page<Comment> pageList = commentService.findHotByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().parallel().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); Set<String> 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<CommentResp>(Long.valueOf(list.size()),list)); return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
} }
private List<CommentResp> getHotList(String journalId,int page,int size) {
Page<Comment> pageList = commentService.findHotByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().parallel().map(x->getCommentResp(x)).collect(Collectors.toList());
return list;
}
private List<CommentResp> getNewList(String journalId,int page,int size) {
Page<Comment> pageList = commentService.findNewByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().parallel().map(x->getCommentResp(x)).collect(Collectors.toList());
return list;
}
private CommentResp updateHaveThumbup(CommentResp commentResp,Set<String> 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字控制到两级") @ApiOperation(value = "根据期刊ID获取分页评论列表最新按时间倒序",notes = "根据期刊ID获取根节点评论分页列表 数据库中保存的有期刊号的都是根节点期刊号为空的代表有父评论且parentID一定有值拿到当然根节点评论列表每个根评论有 commentCount字段页面可以显示有 280个回复然后根据 @GetMapping(\"/comment/{parentId}/{page}/{size}\") 接口分页拿到该评论下的子评论列表页面在请求添加评论的时候当给评论添加子评论的时候父评论parentID有值就无法添加子评论并且不显示回复2字控制到两级")
@ApiImplicitParams({@ApiImplicitParam(name = "journalId", value = "剘刊id", required = true), @ApiImplicitParams({@ApiImplicitParam(name = "journalId", value = "剘刊id", required = true),
@ApiImplicitParam(name = "page", value = "分页: 页码以1开始", required = true), @ApiImplicitParam(name = "page", value = "分页: 页码以1开始", required = true),
@ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)}) @ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)})
@GetMapping("/new/{journalId}/{page}/{size}") @GetMapping("/new/{journalId}/{page}/{size}")
public Result findNewByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) { public Result findNewByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) {
List<CommentResp> list = getNewList(journalId,page,size);
//验证是否登录并且拿到ID //验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization); UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) { if (null == userLoginDto) {
Page<Comment> pageList = commentService.findByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().parallel().map(x->getCommentResp(x,"")).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list)); return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
} }
String userId = userLoginDto.getUserId(); String userId = userLoginDto.getUserId();
Page<Comment> pageList = commentService.findNewByJournalId(journalId,page,size); Set<String> publicationLikeSet = publicationLikeDao.findAllByUserId(userId).parallelStream().map(p->p.getLikedItemId()).collect(Collectors.toSet());
List<CommentResp> list =pageList.stream().parallel().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); list = list.parallelStream().map(c->updateHaveThumbup(c,publicationLikeSet)).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list)); return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
} }
@ -165,7 +190,15 @@ public class CommentController extends BaseController {
CommentResp commentResp = new CommentResp(); CommentResp commentResp = new CommentResp();
BeanUtils.copyProperties(comment,commentResp); BeanUtils.copyProperties(comment,commentResp);
if(comment.getCommentCount()>0){ if(comment.getCommentCount()>0){
Comment topComment = commentService.findByParentId(comment.get_id(),1,1).getContent().get(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.setTopChildrenComment(topComment);
} }
commentResp.setThumbupCountString(comment.getThumbupCount()+""); commentResp.setThumbupCountString(comment.getThumbupCount()+"");
@ -176,6 +209,41 @@ public class CommentController extends BaseController {
return commentResp; 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}") @GetMapping("/{page}/{size}")
public Result search(@PathVariable int page,@PathVariable int size){ public Result search(@PathVariable int page,@PathVariable int size){
Page<Comment> pageData = commentService.search(page,size); Page<Comment> pageData = commentService.search(page,size);
@ -189,7 +257,7 @@ public class CommentController extends BaseController {
UserLoginDto userLoginDto = getUserLoginDto(authorization); UserLoginDto userLoginDto = getUserLoginDto(authorization);
Comment comment = commentService.findById(commentId); Comment comment = commentService.findById(commentId);
if (null == userLoginDto) { if (null == userLoginDto) {
CommentResp commentResp = getCommentResp(comment,""); CommentResp commentResp = getCommentResp(comment);
return Result.success(commentResp); return Result.success(commentResp);
} }
return Result.success(getCommentResp(comment,userLoginDto.getUserId())); return Result.success(getCommentResp(comment,userLoginDto.getUserId()));
@ -249,19 +317,27 @@ public class CommentController extends BaseController {
@GetMapping("/comment/{parentId}/{page}/{size}") @GetMapping("/comment/{parentId}/{page}/{size}")
public Result findByParentId(@PathVariable String parentId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) { public Result findByParentId(@PathVariable String parentId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) {
List<CommentResp> list = getChildrenCommentList(parentId,page,size);
//验证是否登录并且拿到ID //验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization); UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) { if (null == userLoginDto) {
Page<Comment> pageList = commentService.findByParentId(parentId,page,size);
List<CommentResp> list =pageList.stream().parallel().map(x->getCommentResp(x,"")).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list)); return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
} }
String userId = userLoginDto.getUserId(); String userId = userLoginDto.getUserId();
Page<Comment> pageList = commentService.findByParentId(parentId,page,size); Set<String> publicationLikeSet = publicationLikeDao.findAllByUserId(userId).parallelStream().map(p->p.getLikedItemId()).collect(Collectors.toSet());
List<CommentResp> list =pageList.stream().parallel().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); list = list.parallelStream().map(c->updateHaveThumbup(c,publicationLikeSet)).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list)); return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
} }
private List<CommentResp> getChildrenCommentList(String parentId,int page,int size) {
Page<Comment> pageList = commentService.findByParentId(parentId,page,size);
List<CommentResp> list =pageList.stream().parallel().map(x->getCommentResp(x)).collect(Collectors.toList());
return list;
}
@ApiOperation(value = "点赞/取消点赞评论",notes = "返回") @ApiOperation(value = "点赞/取消点赞评论",notes = "返回")
@PutMapping("/thumbup/{commentId}") @PutMapping("/thumbup/{commentId}")
public Result thumbup(@PathVariable String commentId,@RequestHeader(value = "Authorization", required = true) String authorization) { public Result thumbup(@PathVariable String commentId,@RequestHeader(value = "Authorization", required = true) String authorization) {

@ -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<PublicationLike,String> {
public List<PublicationLike> findAllByUserId(String userId);
}

@ -48,11 +48,11 @@ public class CommentResp implements Serializable {
private boolean haveThumbup; private boolean haveThumbup;
// private String rootId; private String rootId;
//
// private String rootJournalId; private String rootJournalId;
//
// private String rootJournalName; private String rootJournalName;
private String replyToUserId; private String replyToUserId;

@ -5,4 +5,35 @@ spring:
profile: dev profile: dev
label: master label: master
uri: http://116.62.145.60:12000 uri: http://116.62.145.60:12000
# uri: http://127.0.0.1:12000 # 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

@ -0,0 +1,3 @@
default: 10000, 1h
journal_query_page: 10000, 6h
user_info: 20000, 6h

@ -12,7 +12,7 @@ spring:
max-request-size: 200MB max-request-size: 200MB
redis: redis:
# 地址, 多个地址使用‘,’逗号分割 # 地址, 多个地址使用‘,’逗号分割
hosts: ${spring.redis.host}:${spring.redis.port} hosts: ${spring.redis.host}
j2cache: j2cache:
openSpringCache: true openSpringCache: true

Loading…
Cancel
Save