fix: 修复代码合并问题

This commit is contained in:
sivdead
2025-09-25 18:17:26 +08:00
parent 76962d6d1c
commit 3da5d24488
3 changed files with 139 additions and 119 deletions

View File

@@ -1,10 +1,17 @@
package com.openisle.service; package com.openisle.service;
import com.openisle.config.CachingConfig; import com.openisle.config.CachingConfig;
import com.openisle.exception.NotFoundException;
import com.openisle.exception.RateLimitException; import com.openisle.exception.RateLimitException;
import com.openisle.model.*; import com.openisle.model.*;
import com.openisle.repository.*; import com.openisle.repository.*;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ScheduledFuture;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@@ -21,36 +28,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import com.openisle.exception.RateLimitException;
import com.openisle.mapper.PostMapper;
import com.openisle.model.*;
import com.openisle.repository.CategoryRepository;
import com.openisle.repository.CommentRepository;
import com.openisle.repository.LotteryPostRepository;
import com.openisle.repository.NotificationRepository;
import com.openisle.repository.PointHistoryRepository;
import com.openisle.repository.PollPostRepository;
import com.openisle.repository.PollVoteRepository;
import com.openisle.repository.PostRepository;
import com.openisle.repository.PostSubscriptionRepository;
import com.openisle.repository.ReactionRepository;
import com.openisle.repository.TagRepository;
import com.openisle.repository.UserRepository;
import com.openisle.service.EmailSender;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ScheduledFuture;
import java.util.stream.Collectors;
@Slf4j @Slf4j
@Service @Service
public class PostService { public class PostService {
@@ -121,6 +98,7 @@ public class PostService {
this.tagRepository = tagRepository; this.tagRepository = tagRepository;
this.lotteryPostRepository = lotteryPostRepository; this.lotteryPostRepository = lotteryPostRepository;
this.pollPostRepository = pollPostRepository; this.pollPostRepository = pollPostRepository;
this.categoryProposalPostRepository = categoryProposalPostRepository;
this.pollVoteRepository = pollVoteRepository; this.pollVoteRepository = pollVoteRepository;
this.notificationService = notificationService; this.notificationService = notificationService;
this.subscriptionService = subscriptionService; this.subscriptionService = subscriptionService;
@@ -165,17 +143,22 @@ public class PostService {
for (PollPost pp : pollPostRepository.findByEndTimeBeforeAndResultAnnouncedFalse(now)) { for (PollPost pp : pollPostRepository.findByEndTimeBeforeAndResultAnnouncedFalse(now)) {
applicationContext.getBean(PostService.class).finalizePoll(pp.getId()); applicationContext.getBean(PostService.class).finalizePoll(pp.getId());
} }
for (CategoryProposalPost cp : categoryProposalPostRepository for (CategoryProposalPost cp : categoryProposalPostRepository.findByEndTimeAfterAndProposalStatus(
.findByEndTimeAfterAndProposalStatus(now, CategoryProposalStatus.PENDING)) { now,
CategoryProposalStatus.PENDING
)) {
if (cp.getEndTime() != null) { if (cp.getEndTime() != null) {
ScheduledFuture<?> future = taskScheduler.schedule( ScheduledFuture<?> future = taskScheduler.schedule(
() -> applicationContext.getBean(PostService.class).finalizeProposal(cp.getId()), () -> applicationContext.getBean(PostService.class).finalizeProposal(cp.getId()),
java.util.Date.from(cp.getEndTime().atZone(ZoneId.systemDefault()).toInstant())); java.util.Date.from(cp.getEndTime().atZone(ZoneId.systemDefault()).toInstant())
);
scheduledFinalizations.put(cp.getId(), future); scheduledFinalizations.put(cp.getId(), future);
} }
} }
for (CategoryProposalPost cp : categoryProposalPostRepository for (CategoryProposalPost cp : categoryProposalPostRepository.findByEndTimeBeforeAndProposalStatus(
.findByEndTimeBeforeAndProposalStatus(now, CategoryProposalStatus.PENDING)) { now,
CategoryProposalStatus.PENDING
)) {
applicationContext.getBean(PostService.class).finalizeProposal(cp.getId()); applicationContext.getBean(PostService.class).finalizeProposal(cp.getId());
} }
} }
@@ -349,7 +332,7 @@ public class PostService {
post.setStatus(publishMode == PublishMode.REVIEW ? PostStatus.PENDING : PostStatus.PUBLISHED); post.setStatus(publishMode == PublishMode.REVIEW ? PostStatus.PENDING : PostStatus.PUBLISHED);
if (post instanceof LotteryPost) { if (post instanceof LotteryPost) {
post = lotteryPostRepository.save((LotteryPost) post); post = lotteryPostRepository.save((LotteryPost) post);
}else if (post instanceof CategoryProposalPost categoryProposalPost) { } else if (post instanceof CategoryProposalPost categoryProposalPost) {
post = categoryProposalPostRepository.save(categoryProposalPost); post = categoryProposalPostRepository.save(categoryProposalPost);
} else if (post instanceof PollPost) { } else if (post instanceof PollPost) {
post = pollPostRepository.save((PollPost) post); post = pollPostRepository.save((PollPost) post);
@@ -408,7 +391,8 @@ public class PostService {
} else if (post instanceof CategoryProposalPost cp && cp.getEndTime() != null) { } else if (post instanceof CategoryProposalPost cp && cp.getEndTime() != null) {
ScheduledFuture<?> future = taskScheduler.schedule( ScheduledFuture<?> future = taskScheduler.schedule(
() -> applicationContext.getBean(PostService.class).finalizeProposal(cp.getId()), () -> applicationContext.getBean(PostService.class).finalizeProposal(cp.getId()),
java.util.Date.from(cp.getEndTime().atZone(ZoneId.systemDefault()).toInstant())); java.util.Date.from(cp.getEndTime().atZone(ZoneId.systemDefault()).toInstant())
);
scheduledFinalizations.put(cp.getId(), future); scheduledFinalizations.put(cp.getId(), future);
} else if (post instanceof PollPost pp && pp.getEndTime() != null) { } else if (post instanceof PollPost pp && pp.getEndTime() != null) {
ScheduledFuture<?> future = taskScheduler.schedule( ScheduledFuture<?> future = taskScheduler.schedule(
@@ -420,13 +404,13 @@ public class PostService {
return post; return post;
} }
@CacheEvict( @CacheEvict(value = CachingConfig.POST_CACHE_NAME, allEntries = true)
value = CachingConfig.POST_CACHE_NAME, allEntries = true
)
@Transactional @Transactional
public void finalizeProposal(Long postId) { public void finalizeProposal(Long postId) {
scheduledFinalizations.remove(postId); scheduledFinalizations.remove(postId);
categoryProposalPostRepository.findById(postId).ifPresent(cp -> { categoryProposalPostRepository
.findById(postId)
.ifPresent(cp -> {
if (cp.getProposalStatus() != CategoryProposalStatus.PENDING) { if (cp.getProposalStatus() != CategoryProposalStatus.PENDING) {
return; return;
} }
@@ -452,13 +436,38 @@ public class PostService {
} }
cp.setRejectReason(reason); cp.setRejectReason(reason);
} }
cp.setResultSnapshot("approveVotes=" + approveVotes + ", totalParticipants=" + totalParticipants + ", approvePercent=" + approvePercent); cp.setResultSnapshot(
"approveVotes=" +
approveVotes +
", totalParticipants=" +
totalParticipants +
", approvePercent=" +
approvePercent
);
categoryProposalPostRepository.save(cp); categoryProposalPostRepository.save(cp);
if (cp.getAuthor() != null) { if (cp.getAuthor() != null) {
notificationService.createNotification(cp.getAuthor(), NotificationType.POLL_RESULT_OWNER, cp, null, null, null, null, null); notificationService.createNotification(
cp.getAuthor(),
NotificationType.POLL_RESULT_OWNER,
cp,
null,
null,
null,
null,
null
);
} }
for (User participant : cp.getParticipants()) { for (User participant : cp.getParticipants()) {
notificationService.createNotification(participant, NotificationType.POLL_RESULT_PARTICIPANT, cp, null, null, null, null, null); notificationService.createNotification(
participant,
NotificationType.POLL_RESULT_PARTICIPANT,
cp,
null,
null,
null,
null,
null
);
} }
postChangeLogService.recordVoteResult(cp); postChangeLogService.recordVoteResult(cp);
}); });

View File

@@ -76,6 +76,15 @@ class PostControllerTest {
@MockBean @MockBean
private MedalService medalService; private MedalService medalService;
@MockBean
private CategoryService categoryService;
@MockBean
private TagService tagService;
@MockBean
private PointService pointService;
@MockBean @MockBean
private com.openisle.repository.PollVoteRepository pollVoteRepository; private com.openisle.repository.PollVoteRepository pollVoteRepository;
@@ -275,6 +284,7 @@ class PostControllerTest {
any(), any(),
any(), any(),
any(), any(),
any(),
any() any()
); );
} }

View File

@@ -46,3 +46,4 @@ app.avatar.base-url=${AVATAR_BASE_URL:https://api.dicebear.com/6.x}
# Web push configuration # Web push configuration
app.webpush.public-key=${WEBPUSH_PUBLIC_KEY:} app.webpush.public-key=${WEBPUSH_PUBLIC_KEY:}
app.webpush.private-key=${WEBPUSH_PRIVATE_KEY:} app.webpush.private-key=${WEBPUSH_PRIVATE_KEY:}
app.snippet-length=${SNIPPET_LENGTH:200}