Browse Source

更新 search-service 中的 comment 模块

reghao 1 week ago
parent
commit
26bfe0eb58

+ 6 - 5
search/search-service/src/main/java/cn/reghao/tnb/search/app/comment/controller/CommentController.java

@@ -3,13 +3,14 @@ package cn.reghao.tnb.search.app.comment.controller;
 import cn.reghao.jutil.jdk.web.db.PageList;
 import cn.reghao.jutil.jdk.web.db.PageList;
 import cn.reghao.tnb.common.web.WebResult;
 import cn.reghao.tnb.common.web.WebResult;
 import cn.reghao.tnb.common.auth.AuthUser;
 import cn.reghao.tnb.common.auth.AuthUser;
-import cn.reghao.tnb.content.api.dto.comment.CommentSbtDto;
-import cn.reghao.tnb.content.api.dto.comment.UserReply;
+import cn.reghao.tnb.search.app.comment.model.dto.CommentSbtDto;
+import cn.reghao.tnb.search.app.comment.model.vo.UserReply;
 import cn.reghao.tnb.search.app.comment.service.CommentService;
 import cn.reghao.tnb.search.app.comment.service.CommentService;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Operation;
 import lombok.extern.slf4j.Slf4j;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.MediaType;
 import org.springframework.http.MediaType;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.bind.annotation.*;
 
 
 /**
 /**
@@ -30,21 +31,21 @@ public class CommentController {
     @AuthUser
     @AuthUser
     @Operation(summary = "发送视频评论", description = "N")
     @Operation(summary = "发送视频评论", description = "N")
     @PostMapping("/video")
     @PostMapping("/video")
-    public String submitComment(@RequestBody CommentSbtDto commentSbtDto) {
+    public String submitComment(@RequestBody @Validated CommentSbtDto commentSbtDto) {
         commentService.saveComment(commentSbtDto);
         commentService.saveComment(commentSbtDto);
         return WebResult.success();
         return WebResult.success();
     }
     }
 
 
     @Operation(summary = "获取视频评论", description = "N")
     @Operation(summary = "获取视频评论", description = "N")
     @GetMapping(value = "/video", produces = MediaType.APPLICATION_JSON_VALUE)
     @GetMapping(value = "/video", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String getUserComment(@RequestParam("videoId") String videoId, @RequestParam("pageNumber") int pageNumber) {
+    public String getUserComment(@RequestParam("videoId") String videoId, @RequestParam("pn") int pageNumber) {
         PageList<UserReply> pageList = commentService.getUserComment(videoId, pageNumber);
         PageList<UserReply> pageList = commentService.getUserComment(videoId, pageNumber);
         return WebResult.success(pageList);
         return WebResult.success(pageList);
     }
     }
 
 
     @Operation(summary = "获取评论的子评论", description = "N")
     @Operation(summary = "获取评论的子评论", description = "N")
     @GetMapping(value = "/video/child", produces = MediaType.APPLICATION_JSON_VALUE)
     @GetMapping(value = "/video/child", produces = MediaType.APPLICATION_JSON_VALUE)
-    public String getChildComment(@RequestParam("commentId") long commentId, @RequestParam("pageNumber") int pageNumber) {
+    public String getChildComment(@RequestParam("commentId") long commentId, @RequestParam("pn") int pageNumber) {
         PageList<UserReply> pageList = commentService.getChildComment(commentId, pageNumber);
         PageList<UserReply> pageList = commentService.getChildComment(commentId, pageNumber);
         return WebResult.success(pageList);
         return WebResult.success(pageList);
     }
     }

+ 2 - 1
search/search-service/src/main/java/cn/reghao/tnb/search/app/comment/controller/DanmakuController.java

@@ -7,6 +7,7 @@ import cn.reghao.tnb.search.app.comment.service.DanmakuService;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Operation;
 import org.springframework.http.MediaType;
 import org.springframework.http.MediaType;
+import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.bind.annotation.*;
 
 
 import java.util.List;
 import java.util.List;
@@ -27,7 +28,7 @@ public class DanmakuController {
 
 
     @Operation(summary = "发送视频弹幕", description = "N")
     @Operation(summary = "发送视频弹幕", description = "N")
     @PostMapping("/v3/")
     @PostMapping("/v3/")
-    public String submitDanmaku(@RequestBody DanmakuSbtDto danmakuSbtDto) {
+    public String submitDanmaku(@RequestBody @Validated DanmakuSbtDto danmakuSbtDto) {
         Result result = danmakuService.submit(danmakuSbtDto);
         Result result = danmakuService.submit(danmakuSbtDto);
         return WebResult.result(result);
         return WebResult.result(result);
     }
     }

+ 14 - 13
content/content-api/src/main/java/cn/reghao/tnb/content/api/dto/comment/CommentSbtDto.java → search/search-service/src/main/java/cn/reghao/tnb/search/app/comment/model/dto/CommentSbtDto.java

@@ -1,4 +1,4 @@
-package cn.reghao.tnb.content.api.dto.comment;
+package cn.reghao.tnb.search.app.comment.model.dto;
 
 
 import lombok.Getter;
 import lombok.Getter;
 import lombok.Setter;
 import lombok.Setter;
@@ -6,8 +6,6 @@ import lombok.Setter;
 import jakarta.validation.constraints.NotBlank;
 import jakarta.validation.constraints.NotBlank;
 import jakarta.validation.constraints.NotNull;
 import jakarta.validation.constraints.NotNull;
 import java.io.Serializable;
 import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
 
 
 /**
 /**
  * @author reghao
  * @author reghao
@@ -18,22 +16,25 @@ import java.util.List;
 public class CommentSbtDto implements Serializable {
 public class CommentSbtDto implements Serializable {
     private static final long serialVersionUID = 1L;
     private static final long serialVersionUID = 1L;
 
 
-    private CommentData newComment;
-    private CommentData parent;
-
-    @Deprecated
+    @NotNull
+    private Long parentId;
+    @NotBlank
+    private String content;
+    @NotBlank
     private String videoId;
     private String videoId;
+    private Long targetId;
+    private String targetUsername;
+
+    //private Long commentId;
+    /*private CommentData newComment;
+    private CommentData parent;
     @Deprecated
     @Deprecated
     private Long avId;
     private Long avId;
     @Deprecated
     @Deprecated
     private Long userId;
     private Long userId;
-    private Long parentId;
-    private Long commentId;
-    @NotBlank
-    private String content;
+    private String postId;
     private String imgSrc;
     private String imgSrc;
-    @NotNull
     private Long createAt;
     private Long createAt;
     private Integer likes;
     private Integer likes;
-    private List<String> commentImages = new ArrayList<>();
+    private List<String> commentImages = new ArrayList<>();*/
 }
 }

+ 18 - 25
search/search-service/src/main/java/cn/reghao/tnb/search/app/comment/model/po/UserComment.java

@@ -2,12 +2,13 @@ package cn.reghao.tnb.search.app.comment.model.po;
 
 
 import cn.reghao.jutil.jdk.web.db.BaseObject;
 import cn.reghao.jutil.jdk.web.db.BaseObject;
 import cn.reghao.tnb.common.auth.UserContext;
 import cn.reghao.tnb.common.auth.UserContext;
-import cn.reghao.tnb.content.api.dto.comment.CommentData;
+import cn.reghao.tnb.search.app.comment.model.dto.CommentSbtDto;
 import lombok.EqualsAndHashCode;
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.NoArgsConstructor;
 import lombok.Setter;
 import lombok.Setter;
 
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 
 
 /**
 /**
@@ -22,40 +23,32 @@ public class UserComment extends BaseObject<String> {
     private Long pCommentId;
     private Long pCommentId;
     private Long commentId;
     private Long commentId;
     private String content;
     private String content;
-    private String imgSrc;
     private List<String> commentImages;
     private List<String> commentImages;
     private Integer likes;
     private Integer likes;
-    private Boolean liked;
-    private Integer postType;
-    private String postId;
-    private Long avId;
     private String location;
     private String location;
+    private String postId;
     private Long publishAt;
     private Long publishAt;
     private Long publishBy;
     private Long publishBy;
+    private Long avId;
+    private Long targetId;
+    private String targetUsername;
 
 
-    public UserComment(long parentId, long commentId, CommentData commentData) {
-        this.commentId = commentId;
-        this.postId = commentData.getPostId();
-        this.pCommentId = parentId;
-        this.content = commentData.getContent();
-        this.commentImages = commentData.getImageUrls();
-        this.likes = commentData.getLikes();
-        this.liked = false;
-        this.location = commentData.getLocation();
-        this.publishAt = commentData.getCreateAt();
-        this.publishBy = Long.parseLong(commentData.getUser().getUserId());
-    }
+    private Boolean liked;
+    private Integer postType;
+    private String imgSrc;
 
 
-    public UserComment(long commentId, CommentData commentData, long parentId) {
-        this.postId = commentData.getPostId();
-        this.avId = 0L;
-        this.pCommentId = parentId;
+    public UserComment(long commentId, CommentSbtDto commentSbtDto) {
+        this.pCommentId = commentSbtDto.getParentId();
         this.commentId = commentId;
         this.commentId = commentId;
-        this.content = commentData.getContent();
-        this.commentImages = commentData.getImageUrls();
+        this.content = commentSbtDto.getContent();
+        this.commentImages = new ArrayList<>();
         this.likes = 0;
         this.likes = 0;
-        this.liked = false;
+        this.location = "";
+        this.postId = commentSbtDto.getVideoId();
         this.publishAt = System.currentTimeMillis();
         this.publishAt = System.currentTimeMillis();
         this.publishBy = UserContext.getUserId();
         this.publishBy = UserContext.getUserId();
+        this.avId = 0L;
+        this.targetId = commentSbtDto.getTargetId();
+        this.targetUsername = commentSbtDto.getTargetUsername();
     }
     }
 }
 }

+ 1 - 1
content/content-api/src/main/java/cn/reghao/tnb/content/api/dto/comment/ReplyUser.java → search/search-service/src/main/java/cn/reghao/tnb/search/app/comment/model/vo/ReplyUser.java

@@ -1,4 +1,4 @@
-package cn.reghao.tnb.content.api.dto.comment;
+package cn.reghao.tnb.search.app.comment.model.vo;
 
 
 import lombok.Getter;
 import lombok.Getter;
 import lombok.Setter;
 import lombok.Setter;

+ 15 - 5
content/content-api/src/main/java/cn/reghao/tnb/content/api/dto/comment/UserReply.java → search/search-service/src/main/java/cn/reghao/tnb/search/app/comment/model/vo/UserReply.java

@@ -1,5 +1,6 @@
-package cn.reghao.tnb.content.api.dto.comment;
+package cn.reghao.tnb.search.app.comment.model.vo;
 
 
+import cn.reghao.tnb.search.app.comment.model.po.UserComment;
 import lombok.Getter;
 import lombok.Getter;
 import lombok.Setter;
 import lombok.Setter;
 
 
@@ -18,17 +19,26 @@ public class UserReply implements Serializable {
 
 
     private Long id;
     private Long id;
     private String content;
     private String content;
-    private String imgSrc;
     private Integer likes;
     private Integer likes;
-    private Boolean liked;
-    private String reply;
     private Long createAt;
     private Long createAt;
     private List<UserReply> children;
     private List<UserReply> children;
     private Long total;
     private Long total;
     private ReplyUser user;
     private ReplyUser user;
+    private Long targetId;
+    private String targetUsername;
+
+    private String reply;
+    private String imgSrc;
+    private Boolean liked;
 
 
-    public UserReply() {
+    public UserReply(UserComment userComment) {
+        this.id = userComment.getCommentId();
+        this.content = userComment.getContent();
+        this.likes = userComment.getLikes();
+        this.createAt = userComment.getPublishAt();
         this.children = new ArrayList<>();
         this.children = new ArrayList<>();
         this.total = 0L;
         this.total = 0L;
+        this.targetId = userComment.getTargetId();
+        this.targetUsername = userComment.getTargetUsername();
     }
     }
 }
 }

+ 33 - 64
search/search-service/src/main/java/cn/reghao/tnb/search/app/comment/service/CommentService.java

@@ -4,14 +4,13 @@ import cn.reghao.jutil.jdk.web.db.PageList;
 import cn.reghao.jutil.jdk.string.SnowFlake;
 import cn.reghao.jutil.jdk.string.SnowFlake;
 import cn.reghao.tnb.auth.api.iface.AccountQuery;
 import cn.reghao.tnb.auth.api.iface.AccountQuery;
 import cn.reghao.tnb.common.auth.UserContext;
 import cn.reghao.tnb.common.auth.UserContext;
-import cn.reghao.tnb.content.api.dto.comment.CommentData;
 import cn.reghao.tnb.search.app.comment.db.mongo.UserCommentMongo;
 import cn.reghao.tnb.search.app.comment.db.mongo.UserCommentMongo;
 import cn.reghao.tnb.search.app.comment.model.po.UserComment;
 import cn.reghao.tnb.search.app.comment.model.po.UserComment;
 import cn.reghao.tnb.user.api.dto.UserCard;
 import cn.reghao.tnb.user.api.dto.UserCard;
 import cn.reghao.tnb.user.api.iface.UserService;
 import cn.reghao.tnb.user.api.iface.UserService;
-import cn.reghao.tnb.content.api.dto.comment.CommentSbtDto;
-import cn.reghao.tnb.content.api.dto.comment.ReplyUser;
-import cn.reghao.tnb.content.api.dto.comment.UserReply;
+import cn.reghao.tnb.search.app.comment.model.dto.CommentSbtDto;
+import cn.reghao.tnb.search.app.comment.model.vo.ReplyUser;
+import cn.reghao.tnb.search.app.comment.model.vo.UserReply;
 import org.apache.dubbo.config.annotation.DubboReference;
 import org.apache.dubbo.config.annotation.DubboReference;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
@@ -29,6 +28,7 @@ public class CommentService {
     @DubboReference(check = false, retries = 0, timeout = 60_000)
     @DubboReference(check = false, retries = 0, timeout = 60_000)
     private AccountQuery accountQuery;
     private AccountQuery accountQuery;
 
 
+    private int pageSize = 10;
     private final SnowFlake idGenerator;
     private final SnowFlake idGenerator;
     private final UserCommentMongo userCommentMongo;
     private final UserCommentMongo userCommentMongo;
 
 
@@ -39,25 +39,8 @@ public class CommentService {
 
 
     public void saveComment(CommentSbtDto commentSbtDto) {
     public void saveComment(CommentSbtDto commentSbtDto) {
         long commentId = idGenerator.nextId();
         long commentId = idGenerator.nextId();
-        CommentData commentData = commentSbtDto.getNewComment();
-        String userIdStr = commentData.getUser().getUserId();
-        long userId = accountQuery.getUserIdLong(userIdStr);
-        String postId = commentData.getPostId();
-
-        CommentData parent = commentSbtDto.getParent();
-        long parentId = 0;
-        if (parent != null) {
-            parentId = parent.getId();
-        }
-
-        String imageUrl = commentData.getImageUrl();
-        if (imageUrl != null) {
-            commentData.getImageUrls().add(imageUrl);
-        }
-
-        UserComment userComment = new UserComment(commentId, commentData, parentId);
+        UserComment userComment = new UserComment(commentId, commentSbtDto);
         userCommentMongo.save(userComment);
         userCommentMongo.save(userComment);
-
         /*VideoPost videoPost = videoPostMapper.findByVideoId(postId);
         /*VideoPost videoPost = videoPostMapper.findByVideoId(postId);
         if (videoPost != null) {
         if (videoPost != null) {
             long publishBy = videoPost.getPublishBy();
             long publishBy = videoPost.getPublishBy();
@@ -71,7 +54,7 @@ public class CommentService {
 
 
     @Deprecated
     @Deprecated
     public void saveBiliComment(CommentSbtDto commentSbtDto) {
     public void saveBiliComment(CommentSbtDto commentSbtDto) {
-        long commentId = commentSbtDto.getNewComment().getId();
+        /*long commentId = commentSbtDto.getNewComment().getId();
         UserComment userComment = userCommentMongo.findByCommentId(commentId);
         UserComment userComment = userCommentMongo.findByCommentId(commentId);
         if (userComment != null) {
         if (userComment != null) {
             return;
             return;
@@ -80,38 +63,20 @@ public class CommentService {
         CommentData commentData = commentSbtDto.getNewComment();
         CommentData commentData = commentSbtDto.getNewComment();
         long parentId = commentSbtDto.getParent().getId();
         long parentId = commentSbtDto.getParent().getId();
         userComment = new UserComment(parentId, commentId, commentData);
         userComment = new UserComment(parentId, commentId, commentData);
-        userCommentMongo.save(userComment);
+        userCommentMongo.save(userComment);*/
     }
     }
 
 
     public PageList<UserReply> getUserComment(String videoId, int pageNumber) {
     public PageList<UserReply> getUserComment(String videoId, int pageNumber) {
         long total = userCommentMongo.countByPostId(videoId);
         long total = userCommentMongo.countByPostId(videoId);
         int pageSize = 10;
         int pageSize = 10;
         List<UserReply> list = userCommentMongo.findByPostId(videoId, pageNumber).stream()
         List<UserReply> list = userCommentMongo.findByPostId(videoId, pageNumber).stream()
-                .map(userComment -> {
-                    long commentId = userComment.getCommentId();
-                    UserReply commentVo = getCommentVo(userComment);
-                    long total1 = userCommentMongo.countChildComment(commentId);
-                    commentVo.setTotal(total1);
-                    List<UserReply> childComments = userCommentMongo.findByParentId(commentId, 1, pageSize).stream()
-                            .map(this::getCommentVo)
-                            .collect(Collectors.toList());
-                    commentVo.setChildren(childComments);
-                    return commentVo;
-                })
+                .map(this::getCommentVo)
                 .collect(Collectors.toList());
                 .collect(Collectors.toList());
         return PageList.pageList(pageNumber, pageSize, (int) total, list);
         return PageList.pageList(pageNumber, pageSize, (int) total, list);
     }
     }
 
 
-    private UserReply getCommentVo(UserComment userComment) {
-        Long commentId = userComment.getCommentId();
-        Long userId = userComment.getPublishBy();
+    private ReplyUser getReplyUser(long userId) {
         String userIdStr = accountQuery.getUserIdStr(userId);
         String userIdStr = accountQuery.getUserIdStr(userId);
-        String postId = userComment.getPostId();
-        String content = userComment.getContent();
-        Long createAt = userComment.getPublishAt();
-        String imgSrc = userComment.getImgSrc();
-        List<String> images = userComment.getCommentImages();
-
         ReplyUser commentUser = new ReplyUser();
         ReplyUser commentUser = new ReplyUser();
         commentUser.setUserId(userIdStr);
         commentUser.setUserId(userIdStr);
         UserCard userCard = userService.getUserAvatar(userId);
         UserCard userCard = userService.getUserAvatar(userId);
@@ -128,34 +93,38 @@ public class CommentService {
             commentUser.setAuthor(true);
             commentUser.setAuthor(true);
         }
         }
 
 
-        UserReply commentVo = new UserReply();
-        /*commentVo.setVideoId(postId);
-        commentVo.setCommentId(commentId);*/
-        commentVo.setId(commentId);
-        commentVo.setContent(content);
-        commentVo.setImgSrc(imgSrc);
-        commentVo.setCreateAt(createAt);
-        commentVo.setUser(commentUser);
-        commentVo.setLikes(userComment.getLikes());
-        commentVo.setLiked(false);
+        return commentUser;
+    }
 
 
-        if (images != null && !images.isEmpty()) {
-            commentVo.setImgSrc(images.get(0));
+    private UserReply getCommentVo(UserComment userComment) {
+        Long userId = userComment.getPublishBy();
+        ReplyUser commentUser = getReplyUser(userId);
+        UserReply userReply = new UserReply(userComment);
+        userReply.setUser(commentUser);
+
+        long commentId = userComment.getCommentId();
+        long total = userCommentMongo.countChildComment(commentId);
+        if (total > 0) {
+            List<UserReply> childComments = userCommentMongo.findByParentId(commentId, 1, 3).stream()
+                    .map(userComment1 -> {
+                        Long userId1 = userComment1.getPublishBy();
+                        ReplyUser commentUser1 = getReplyUser(userId1);
+                        UserReply userReply1 = new UserReply(userComment1);
+                        userReply1.setUser(commentUser1);
+                        return userReply1;
+                    })
+                    .collect(Collectors.toList());
+            userReply.setChildren(childComments);
+            userReply.setTotal(total);
         }
         }
-
-        return commentVo;
+        return userReply;
     }
     }
 
 
     public PageList<UserReply> getChildComment(long commentId, int pageNumber) {
     public PageList<UserReply> getChildComment(long commentId, int pageNumber) {
-        int pageSize = 10;
         Long total = userCommentMongo.countChildComment(commentId);
         Long total = userCommentMongo.countChildComment(commentId);
         List<UserReply> list = userCommentMongo.findByParentId(commentId, pageNumber, pageSize).stream()
         List<UserReply> list = userCommentMongo.findByParentId(commentId, pageNumber, pageSize).stream()
                 .map(this::getCommentVo)
                 .map(this::getCommentVo)
                 .collect(Collectors.toList());
                 .collect(Collectors.toList());
-        return PageList.pageList(pageNumber, 10, total.intValue(), list);
-    }
-
-    public long countPostComment(String videoId) {
-        return userCommentMongo.countByPostId(videoId);
+        return PageList.pageList(pageNumber, pageSize, total.intValue(), list);
     }
     }
 }
 }