mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-21 22:41:05 +08:00
Compare commits
9 Commits
pr-971
...
codex/impl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1fd31184a7 | ||
|
|
d46420ef81 | ||
|
|
b36b5b59dc | ||
|
|
cf96806f80 | ||
|
|
3d0d0496b6 | ||
|
|
f67e220894 | ||
|
|
9306e35b84 | ||
|
|
d2268a1944 | ||
|
|
1a21ba8935 |
@@ -67,7 +67,6 @@ public class PostMapper {
|
|||||||
dto.setCategory(categoryMapper.toDto(post.getCategory()));
|
dto.setCategory(categoryMapper.toDto(post.getCategory()));
|
||||||
dto.setTags(post.getTags().stream().map(tagMapper::toDto).collect(Collectors.toList()));
|
dto.setTags(post.getTags().stream().map(tagMapper::toDto).collect(Collectors.toList()));
|
||||||
dto.setViews(post.getViews());
|
dto.setViews(post.getViews());
|
||||||
dto.setCommentCount(commentService.countComments(post.getId()));
|
|
||||||
dto.setStatus(post.getStatus());
|
dto.setStatus(post.getStatus());
|
||||||
dto.setPinnedAt(post.getPinnedAt());
|
dto.setPinnedAt(post.getPinnedAt());
|
||||||
dto.setRssExcluded(post.getRssExcluded() == null || post.getRssExcluded());
|
dto.setRssExcluded(post.getRssExcluded() == null || post.getRssExcluded());
|
||||||
@@ -82,8 +81,12 @@ public class PostMapper {
|
|||||||
List<User> participants = commentService.getParticipants(post.getId(), 5);
|
List<User> participants = commentService.getParticipants(post.getId(), 5);
|
||||||
dto.setParticipants(participants.stream().map(userMapper::toAuthorDto).collect(Collectors.toList()));
|
dto.setParticipants(participants.stream().map(userMapper::toAuthorDto).collect(Collectors.toList()));
|
||||||
|
|
||||||
LocalDateTime last = commentService.getLastCommentTime(post.getId());
|
LocalDateTime last = post.getLastReplyAt();
|
||||||
dto.setLastReplyAt(last != null ? last : post.getCreatedAt());
|
if (last == null) {
|
||||||
|
commentService.updatePostCommentStats(post);
|
||||||
|
}
|
||||||
|
dto.setCommentCount(post.getCommentCount());
|
||||||
|
dto.setLastReplyAt(post.getLastReplyAt());
|
||||||
dto.setReward(0);
|
dto.setReward(0);
|
||||||
dto.setSubscribed(false);
|
dto.setSubscribed(false);
|
||||||
dto.setType(post.getType());
|
dto.setType(post.getType());
|
||||||
|
|||||||
@@ -72,4 +72,10 @@ public class Post {
|
|||||||
|
|
||||||
@Column(nullable = true)
|
@Column(nullable = true)
|
||||||
private Boolean rssExcluded = true;
|
private Boolean rssExcluded = true;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
|
private long commentCount = 0;
|
||||||
|
|
||||||
|
@Column(nullable = true)
|
||||||
|
private LocalDateTime lastReplyAt;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,6 +76,10 @@ public class CommentService {
|
|||||||
comment.setContent(content);
|
comment.setContent(content);
|
||||||
comment = commentRepository.save(comment);
|
comment = commentRepository.save(comment);
|
||||||
log.debug("Comment {} saved for post {}", comment.getId(), postId);
|
log.debug("Comment {} saved for post {}", comment.getId(), postId);
|
||||||
|
|
||||||
|
// Update post comment statistics
|
||||||
|
updatePostCommentStats(post);
|
||||||
|
|
||||||
imageUploader.addReferences(imageUploader.extractUrls(content));
|
imageUploader.addReferences(imageUploader.extractUrls(content));
|
||||||
if (!author.getId().equals(post.getAuthor().getId())) {
|
if (!author.getId().equals(post.getAuthor().getId())) {
|
||||||
notificationService.createNotification(post.getAuthor(), NotificationType.COMMENT_REPLY, post, comment,
|
notificationService.createNotification(post.getAuthor(), NotificationType.COMMENT_REPLY, post, comment,
|
||||||
@@ -129,6 +133,10 @@ public class CommentService {
|
|||||||
comment.setContent(content);
|
comment.setContent(content);
|
||||||
comment = commentRepository.save(comment);
|
comment = commentRepository.save(comment);
|
||||||
log.debug("Reply {} saved for parent {}", comment.getId(), parentId);
|
log.debug("Reply {} saved for parent {}", comment.getId(), parentId);
|
||||||
|
|
||||||
|
// Update post comment statistics
|
||||||
|
updatePostCommentStats(parent.getPost());
|
||||||
|
|
||||||
imageUploader.addReferences(imageUploader.extractUrls(content));
|
imageUploader.addReferences(imageUploader.extractUrls(content));
|
||||||
if (!author.getId().equals(parent.getAuthor().getId())) {
|
if (!author.getId().equals(parent.getAuthor().getId())) {
|
||||||
notificationService.createNotification(parent.getAuthor(), NotificationType.COMMENT_REPLY, parent.getPost(),
|
notificationService.createNotification(parent.getAuthor(), NotificationType.COMMENT_REPLY, parent.getPost(),
|
||||||
@@ -282,9 +290,13 @@ public class CommentService {
|
|||||||
imageUploader.removeReferences(imageUploader.extractUrls(comment.getContent()));
|
imageUploader.removeReferences(imageUploader.extractUrls(comment.getContent()));
|
||||||
|
|
||||||
// 逻辑删除评论
|
// 逻辑删除评论
|
||||||
|
Post post = comment.getPost();
|
||||||
commentRepository.delete(comment);
|
commentRepository.delete(comment);
|
||||||
// 删除积分历史
|
// 删除积分历史
|
||||||
pointHistoryRepository.deleteAll(pointHistories);
|
pointHistoryRepository.deleteAll(pointHistories);
|
||||||
|
|
||||||
|
// Update post comment statistics
|
||||||
|
updatePostCommentStats(post);
|
||||||
|
|
||||||
// 重新计算受影响用户的积分
|
// 重新计算受影响用户的积分
|
||||||
if (!usersToRecalculate.isEmpty()) {
|
if (!usersToRecalculate.isEmpty()) {
|
||||||
@@ -330,4 +342,23 @@ public class CommentService {
|
|||||||
int replies = commentRepository.findByParentOrderByCreatedAtAsc(comment).size();
|
int replies = commentRepository.findByParentOrderByCreatedAtAsc(comment).size();
|
||||||
return reactions + replies;
|
return reactions + replies;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update post comment statistics (comment count and last reply time)
|
||||||
|
*/
|
||||||
|
public void updatePostCommentStats(Post post) {
|
||||||
|
long commentCount = commentRepository.countByPostId(post.getId());
|
||||||
|
post.setCommentCount(commentCount);
|
||||||
|
|
||||||
|
LocalDateTime lastReplyAt = commentRepository.findLastCommentTime(post);
|
||||||
|
if (lastReplyAt == null) {
|
||||||
|
post.setLastReplyAt(post.getCreatedAt());
|
||||||
|
} else {
|
||||||
|
post.setLastReplyAt(lastReplyAt);
|
||||||
|
}
|
||||||
|
postRepository.save(post);
|
||||||
|
|
||||||
|
log.debug("Updated post {} stats: commentCount={}, lastReplyAt={}",
|
||||||
|
post.getId(), commentCount, lastReplyAt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
-- Add comment count and last reply time fields to posts table for performance optimization
|
||||||
|
ALTER TABLE posts ADD COLUMN comment_count BIGINT NOT NULL DEFAULT 0;
|
||||||
|
ALTER TABLE posts ADD COLUMN last_reply_at DATETIME(6) NULL;
|
||||||
|
|
||||||
|
-- Add index on last_reply_at for sorting by latest reply
|
||||||
|
CREATE INDEX idx_posts_last_reply_at ON posts(last_reply_at);
|
||||||
|
|
||||||
|
-- Initialize comment_count and last_reply_at with existing data
|
||||||
|
UPDATE posts p SET
|
||||||
|
comment_count = (
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM comments c
|
||||||
|
WHERE c.post_id = p.id AND c.deleted_at IS NULL
|
||||||
|
),
|
||||||
|
last_reply_at = (
|
||||||
|
SELECT MAX(c.created_at)
|
||||||
|
FROM comments c
|
||||||
|
WHERE c.post_id = p.id AND c.deleted_at IS NULL
|
||||||
|
);
|
||||||
@@ -55,6 +55,10 @@ class PostControllerTest {
|
|||||||
private UserVisitService userVisitService;
|
private UserVisitService userVisitService;
|
||||||
@MockBean
|
@MockBean
|
||||||
private PostReadService postReadService;
|
private PostReadService postReadService;
|
||||||
|
@MockBean
|
||||||
|
private MedalService medalService;
|
||||||
|
@MockBean
|
||||||
|
private com.openisle.repository.PollVoteRepository pollVoteRepository;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createAndGetPost() throws Exception {
|
void createAndGetPost() throws Exception {
|
||||||
@@ -63,9 +67,13 @@ class PostControllerTest {
|
|||||||
Category cat = new Category();
|
Category cat = new Category();
|
||||||
cat.setId(1L);
|
cat.setId(1L);
|
||||||
cat.setName("tech");
|
cat.setName("tech");
|
||||||
|
cat.setDescription("Technology category");
|
||||||
|
cat.setIcon("tech-icon");
|
||||||
Tag tag = new Tag();
|
Tag tag = new Tag();
|
||||||
tag.setId(1L);
|
tag.setId(1L);
|
||||||
tag.setName("java");
|
tag.setName("java");
|
||||||
|
tag.setDescription("Java programming language");
|
||||||
|
tag.setIcon("java-icon");
|
||||||
Post post = new Post();
|
Post post = new Post();
|
||||||
post.setId(1L);
|
post.setId(1L);
|
||||||
post.setTitle("t");
|
post.setTitle("t");
|
||||||
@@ -111,9 +119,13 @@ class PostControllerTest {
|
|||||||
Category cat = new Category();
|
Category cat = new Category();
|
||||||
cat.setId(1L);
|
cat.setId(1L);
|
||||||
cat.setName("tech");
|
cat.setName("tech");
|
||||||
|
cat.setDescription("Technology category");
|
||||||
|
cat.setIcon("tech-icon");
|
||||||
Tag tag = new Tag();
|
Tag tag = new Tag();
|
||||||
tag.setId(1L);
|
tag.setId(1L);
|
||||||
tag.setName("java");
|
tag.setName("java");
|
||||||
|
tag.setDescription("Java programming language");
|
||||||
|
tag.setIcon("java-icon");
|
||||||
Post post = new Post();
|
Post post = new Post();
|
||||||
post.setId(1L);
|
post.setId(1L);
|
||||||
post.setTitle("t2");
|
post.setTitle("t2");
|
||||||
@@ -147,9 +159,13 @@ class PostControllerTest {
|
|||||||
Category cat = new Category();
|
Category cat = new Category();
|
||||||
cat.setId(1L);
|
cat.setId(1L);
|
||||||
cat.setName("tech");
|
cat.setName("tech");
|
||||||
|
cat.setDescription("Technology category");
|
||||||
|
cat.setIcon("tech-icon");
|
||||||
Tag tag = new Tag();
|
Tag tag = new Tag();
|
||||||
tag.setId(1L);
|
tag.setId(1L);
|
||||||
tag.setName("java");
|
tag.setName("java");
|
||||||
|
tag.setDescription("Java programming language");
|
||||||
|
tag.setIcon("java-icon");
|
||||||
Post post = new Post();
|
Post post = new Post();
|
||||||
post.setId(2L);
|
post.setId(2L);
|
||||||
post.setTitle("hello");
|
post.setTitle("hello");
|
||||||
@@ -197,9 +213,13 @@ class PostControllerTest {
|
|||||||
Category cat = new Category();
|
Category cat = new Category();
|
||||||
cat.setId(1L);
|
cat.setId(1L);
|
||||||
cat.setName("tech");
|
cat.setName("tech");
|
||||||
|
cat.setDescription("Technology category");
|
||||||
|
cat.setIcon("tech-icon");
|
||||||
Tag tag = new Tag();
|
Tag tag = new Tag();
|
||||||
tag.setId(1L);
|
tag.setId(1L);
|
||||||
tag.setName("java");
|
tag.setName("java");
|
||||||
|
tag.setDescription("Java programming language");
|
||||||
|
tag.setIcon("java-icon");
|
||||||
Post post = new Post();
|
Post post = new Post();
|
||||||
post.setId(1L);
|
post.setId(1L);
|
||||||
post.setTitle("t");
|
post.setTitle("t");
|
||||||
@@ -262,6 +282,8 @@ class PostControllerTest {
|
|||||||
Category cat = new Category();
|
Category cat = new Category();
|
||||||
cat.setId(1L);
|
cat.setId(1L);
|
||||||
cat.setName("tech");
|
cat.setName("tech");
|
||||||
|
cat.setDescription("Technology category");
|
||||||
|
cat.setIcon("tech-icon");
|
||||||
Post post = new Post();
|
Post post = new Post();
|
||||||
post.setId(1L);
|
post.setId(1L);
|
||||||
post.setTitle("t");
|
post.setTitle("t");
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package com.openisle.service;
|
||||||
|
|
||||||
|
import com.openisle.model.*;
|
||||||
|
import com.openisle.repository.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.TestPropertySource;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
@SpringBootTest
|
||||||
|
@TestPropertySource(locations = "classpath:application.properties")
|
||||||
|
@Transactional
|
||||||
|
public class PostCommentStatsTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PostRepository postRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CategoryRepository categoryRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TagRepository tagRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CommentService commentService;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPostCommentStatsUpdate() {
|
||||||
|
// Create test user
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername("testuser");
|
||||||
|
user.setEmail("test@example.com");
|
||||||
|
user.setPassword("hash");
|
||||||
|
user = userRepository.save(user);
|
||||||
|
|
||||||
|
// Create test category
|
||||||
|
Category category = new Category();
|
||||||
|
category.setName("Test Category");
|
||||||
|
category.setDescription("Test Category Description");
|
||||||
|
category.setIcon("test-icon");
|
||||||
|
category = categoryRepository.save(category);
|
||||||
|
|
||||||
|
// Create test tag
|
||||||
|
Tag tag = new Tag();
|
||||||
|
tag.setName("Test Tag");
|
||||||
|
tag.setDescription("Test Tag Description");
|
||||||
|
tag.setIcon("test-tag-icon");
|
||||||
|
tag = tagRepository.save(tag);
|
||||||
|
|
||||||
|
// Create test post
|
||||||
|
Post post = new Post();
|
||||||
|
post.setTitle("Test Post");
|
||||||
|
post.setContent("Test content");
|
||||||
|
post.setAuthor(user);
|
||||||
|
post.setCategory(category);
|
||||||
|
post.getTags().add(tag);
|
||||||
|
post.setStatus(PostStatus.PUBLISHED);
|
||||||
|
post.setCommentCount(0L);
|
||||||
|
post = postRepository.save(post);
|
||||||
|
|
||||||
|
// Verify initial state
|
||||||
|
assertEquals(0L, post.getCommentCount());
|
||||||
|
assertNull(post.getLastReplyAt());
|
||||||
|
|
||||||
|
// Add a comment
|
||||||
|
commentService.addComment("testuser", post.getId(), "Test comment");
|
||||||
|
|
||||||
|
// Refresh post from database
|
||||||
|
post = postRepository.findById(post.getId()).orElseThrow();
|
||||||
|
|
||||||
|
// Verify comment count and last reply time are updated
|
||||||
|
assertEquals(1L, post.getCommentCount());
|
||||||
|
assertNotNull(post.getLastReplyAt());
|
||||||
|
|
||||||
|
// Add another comment
|
||||||
|
commentService.addComment("testuser", post.getId(), "Another comment");
|
||||||
|
|
||||||
|
// Refresh post again
|
||||||
|
post = postRepository.findById(post.getId()).orElseThrow();
|
||||||
|
|
||||||
|
// Verify comment count is updated
|
||||||
|
assertEquals(2L, post.getCommentCount());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -268,12 +268,14 @@ class PostServiceTest {
|
|||||||
ApplicationContext context = mock(ApplicationContext.class);
|
ApplicationContext context = mock(ApplicationContext.class);
|
||||||
PointService pointService = mock(PointService.class);
|
PointService pointService = mock(PointService.class);
|
||||||
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
PostChangeLogService postChangeLogService = mock(PostChangeLogService.class);
|
||||||
|
PointHistoryRepository pointHistoryRepository = mock(PointHistoryRepository.class);
|
||||||
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
RedisTemplate redisTemplate = mock(RedisTemplate.class);
|
||||||
|
|
||||||
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
||||||
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
pollPostRepo, pollVoteRepo, notifService, subService, commentService, commentRepo,
|
||||||
reactionRepo, subRepo, notificationRepo, postReadService,
|
reactionRepo, subRepo, notificationRepo, postReadService,
|
||||||
imageUploader, taskScheduler, emailSender, context, pointService, postChangeLogService, PublishMode.DIRECT, redisTemplate);
|
imageUploader, taskScheduler, emailSender, context, pointService, postChangeLogService,
|
||||||
|
pointHistoryRepository, PublishMode.DIRECT, redisTemplate);
|
||||||
when(context.getBean(PostService.class)).thenReturn(service);
|
when(context.getBean(PostService.class)).thenReturn(service);
|
||||||
|
|
||||||
User author = new User();
|
User author = new User();
|
||||||
|
|||||||
@@ -4,7 +4,18 @@ spring.datasource.username=sa
|
|||||||
spring.datasource.password=
|
spring.datasource.password=
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
spring.jpa.hibernate.ddl-auto=create-drop
|
||||||
|
|
||||||
|
springdoc.info.title=openisle
|
||||||
|
springdoc.info.description=Test API documentation
|
||||||
|
springdoc.info.version=1.0.0
|
||||||
|
springdoc.info.scheme=Bearer
|
||||||
|
springdoc.info.header=Authorization
|
||||||
|
|
||||||
|
|
||||||
|
rabbitmq.queue.durable=true
|
||||||
|
rabbitmq.sharding.enabled=true
|
||||||
|
|
||||||
resend.api.key=dummy
|
resend.api.key=dummy
|
||||||
|
resend.from.email=dummy@example.com
|
||||||
cos.base-url=http://test.example.com
|
cos.base-url=http://test.example.com
|
||||||
cos.secret-id=dummy
|
cos.secret-id=dummy
|
||||||
cos.secret-key=dummy
|
cos.secret-key=dummy
|
||||||
@@ -18,6 +29,7 @@ app.upload.max-size=1048576
|
|||||||
app.jwt.secret=TestSecret
|
app.jwt.secret=TestSecret
|
||||||
app.jwt.reason-secret=TestReasonSecret
|
app.jwt.reason-secret=TestReasonSecret
|
||||||
app.jwt.reset-secret=TestResetSecret
|
app.jwt.reset-secret=TestResetSecret
|
||||||
|
app.jwt.invite-secret=TestInviteSecret
|
||||||
app.jwt.expiration=3600000
|
app.jwt.expiration=3600000
|
||||||
|
|
||||||
# Default publish mode for tests
|
# Default publish mode for tests
|
||||||
|
|||||||
@@ -107,11 +107,33 @@ const likeCount = computed(() => counts.value['LIKE'] || 0)
|
|||||||
const userReacted = (type) =>
|
const userReacted = (type) =>
|
||||||
reactions.value.some((r) => r.type === type && r.user === authState.username)
|
reactions.value.some((r) => r.type === type && r.user === authState.username)
|
||||||
|
|
||||||
|
const defaultOrder = computed(() => {
|
||||||
|
if (reactionTypes.value && reactionTypes.value.length) {
|
||||||
|
return reactionTypes.value
|
||||||
|
}
|
||||||
|
const seen = new Set()
|
||||||
|
const order = []
|
||||||
|
for (const reaction of reactions.value) {
|
||||||
|
if (!seen.has(reaction.type)) {
|
||||||
|
seen.add(reaction.type)
|
||||||
|
order.push(reaction.type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return order
|
||||||
|
})
|
||||||
|
|
||||||
const displayedReactions = computed(() => {
|
const displayedReactions = computed(() => {
|
||||||
|
const orderIndex = new Map(defaultOrder.value.map((type, index) => [type, index]))
|
||||||
return Object.entries(counts.value)
|
return Object.entries(counts.value)
|
||||||
.sort((a, b) => b[1] - a[1])
|
.map(([type, count]) => ({ type, count }))
|
||||||
|
.sort((a, b) => {
|
||||||
|
if (b.count !== a.count) return b.count - a.count
|
||||||
|
const indexA = orderIndex.has(a.type) ? orderIndex.get(a.type) : Number.MAX_SAFE_INTEGER
|
||||||
|
const indexB = orderIndex.has(b.type) ? orderIndex.get(b.type) : Number.MAX_SAFE_INTEGER
|
||||||
|
return indexA - indexB
|
||||||
|
})
|
||||||
.slice(0, 3)
|
.slice(0, 3)
|
||||||
.map(([type]) => ({ type }))
|
.map(({ type }) => ({ type }))
|
||||||
})
|
})
|
||||||
|
|
||||||
const panelTypes = computed(() => reactionTypes.value.filter((t) => t !== 'LIKE'))
|
const panelTypes = computed(() => reactionTypes.value.filter((t) => t !== 'LIKE'))
|
||||||
|
|||||||
Reference in New Issue
Block a user