From 2b9b664aa221dc4572d5dc71263741cf53f8747f Mon Sep 17 00:00:00 2001
From: Tim <135014430+nagisa77@users.noreply.github.com>
Date: Thu, 10 Jul 2025 16:03:02 +0800
Subject: [PATCH] feat: show post participants
---
open-isle-cli/src/views/HomePageView.vue | 15 ++++++++++-----
.../com/openisle/controller/PostController.java | 4 ++++
.../openisle/repository/CommentRepository.java | 3 +++
.../java/com/openisle/service/CommentService.java | 10 ++++++++++
.../openisle/controller/PostControllerTest.java | 5 +++++
5 files changed, 32 insertions(+), 5 deletions(-)
diff --git a/open-isle-cli/src/views/HomePageView.vue b/open-isle-cli/src/views/HomePageView.vue
index 1afad9644..a1be8d9d2 100644
--- a/open-isle-cli/src/views/HomePageView.vue
+++ b/open-isle-cli/src/views/HomePageView.vue
@@ -66,9 +66,14 @@
{{ article.comments }}
@@ -216,7 +221,7 @@ export default {
description: p.content,
category: p.category,
tags: p.tags || [],
- members: [],
+ members: (p.participants || []).map(m => ({ id: m.id, avatar: m.avatar })),
comments: (p.comments || []).length,
views: p.views,
time: TimeManager.format(p.createdAt)
@@ -252,7 +257,7 @@ export default {
description: p.content,
category: p.category,
tags: p.tags || [],
- members: [],
+ members: (p.participants || []).map(m => ({ id: m.id, avatar: m.avatar })),
comments: (p.comments || []).length,
views: p.views,
time: TimeManager.format(p.createdAt)
diff --git a/src/main/java/com/openisle/controller/PostController.java b/src/main/java/com/openisle/controller/PostController.java
index 8a420a1d0..25304957e 100644
--- a/src/main/java/com/openisle/controller/PostController.java
+++ b/src/main/java/com/openisle/controller/PostController.java
@@ -128,6 +128,9 @@ public class PostController {
.collect(Collectors.toList());
dto.setComments(comments);
+ java.util.List
participants = commentService.getParticipants(post.getId(), 5);
+ dto.setParticipants(participants.stream().map(this::toAuthorDto).collect(Collectors.toList()));
+
return dto;
}
@@ -219,6 +222,7 @@ public class PostController {
private long views;
private List comments;
private List reactions;
+ private java.util.List participants;
}
@Data
diff --git a/src/main/java/com/openisle/repository/CommentRepository.java b/src/main/java/com/openisle/repository/CommentRepository.java
index 70a219d7e..f76a7e099 100644
--- a/src/main/java/com/openisle/repository/CommentRepository.java
+++ b/src/main/java/com/openisle/repository/CommentRepository.java
@@ -13,4 +13,7 @@ public interface CommentRepository extends JpaRepository {
List findByParentOrderByCreatedAtAsc(Comment parent);
List findByAuthorOrderByCreatedAtDesc(User author, Pageable pageable);
List findByContentContainingIgnoreCase(String keyword);
+
+ @org.springframework.data.jpa.repository.Query("SELECT DISTINCT c.author FROM Comment c WHERE c.post = :post")
+ java.util.List findDistinctAuthorsByPost(@org.springframework.data.repository.query.Param("post") Post post);
}
diff --git a/src/main/java/com/openisle/service/CommentService.java b/src/main/java/com/openisle/service/CommentService.java
index 04ea14178..c929bf5d2 100644
--- a/src/main/java/com/openisle/service/CommentService.java
+++ b/src/main/java/com/openisle/service/CommentService.java
@@ -102,6 +102,16 @@ public class CommentService {
return commentRepository.findByAuthorOrderByCreatedAtDesc(user, pageable);
}
+ public java.util.List getParticipants(Long postId, int limit) {
+ Post post = postRepository.findById(postId)
+ .orElseThrow(() -> new IllegalArgumentException("Post not found"));
+ java.util.LinkedHashSet set = new java.util.LinkedHashSet<>();
+ set.add(post.getAuthor());
+ set.addAll(commentRepository.findDistinctAuthorsByPost(post));
+ java.util.List list = new java.util.ArrayList<>(set);
+ return list.subList(0, Math.min(limit, list.size()));
+ }
+
public java.util.List getCommentsByIds(java.util.List ids) {
return commentRepository.findAllById(ids);
}
diff --git a/src/test/java/com/openisle/controller/PostControllerTest.java b/src/test/java/com/openisle/controller/PostControllerTest.java
index 46e0e93ed..7f221bb5e 100644
--- a/src/test/java/com/openisle/controller/PostControllerTest.java
+++ b/src/test/java/com/openisle/controller/PostControllerTest.java
@@ -73,6 +73,7 @@ class PostControllerTest {
post.setAuthor(user);
post.setCategory(cat);
post.setTags(java.util.Set.of(tag));
+ Mockito.when(commentService.getParticipants(Mockito.anyLong(), Mockito.anyInt())).thenReturn(java.util.List.of());
Mockito.when(postService.createPost(eq("alice"), eq(1L), eq("t"), eq("c"), eq(java.util.List.of(1L)))).thenReturn(post);
Mockito.when(postService.viewPost(eq(1L), Mockito.isNull())).thenReturn(post);
@@ -110,6 +111,7 @@ class PostControllerTest {
post.setAuthor(user);
post.setCategory(cat);
post.setTags(java.util.Set.of(tag));
+ Mockito.when(commentService.getParticipants(Mockito.anyLong(), Mockito.anyInt())).thenReturn(java.util.List.of());
Mockito.when(postService.listPostsByCategories(Mockito.isNull(), Mockito.isNull(), Mockito.isNull()))
.thenReturn(List.of(post));
@@ -182,6 +184,7 @@ class PostControllerTest {
Mockito.when(commentService.getCommentsForPost(1L)).thenReturn(List.of(comment));
Mockito.when(commentService.getReplies(2L)).thenReturn(List.of(reply));
Mockito.when(commentService.getReplies(3L)).thenReturn(List.of());
+ Mockito.when(commentService.getParticipants(Mockito.anyLong(), Mockito.anyInt())).thenReturn(java.util.List.of());
Mockito.when(reactionService.getReactionsForPost(1L)).thenReturn(List.of(pr));
Mockito.when(reactionService.getReactionsForComment(2L)).thenReturn(List.of(cr));
Mockito.when(reactionService.getReactionsForComment(3L)).thenReturn(List.of());
@@ -209,6 +212,7 @@ class PostControllerTest {
post.setAuthor(user);
post.setCategory(cat);
post.setTags(java.util.Set.of(tag));
+ Mockito.when(commentService.getParticipants(Mockito.anyLong(), Mockito.anyInt())).thenReturn(java.util.List.of());
Mockito.when(postService.listPostsByCategoriesAndTags(eq(java.util.List.of(1L)), eq(java.util.List.of(1L, 2L)), eq(0), eq(5)))
.thenReturn(List.of(post));
@@ -241,6 +245,7 @@ class PostControllerTest {
post.setAuthor(user);
post.setCategory(cat);
post.setTags(java.util.Set.of(tag));
+ Mockito.when(commentService.getParticipants(Mockito.anyLong(), Mockito.anyInt())).thenReturn(java.util.List.of());
Mockito.when(postService.listPostsByViews(eq(java.util.List.of(1L)), eq(java.util.List.of(1L, 2L)), eq(0), eq(5)))
.thenReturn(List.of(post));