feat: add paginated notifications with infinite scroll

This commit is contained in:
Tim
2025-08-19 13:56:36 +08:00
parent b06815cc59
commit 640fbd6ea4
5 changed files with 217 additions and 174 deletions

View File

@@ -24,8 +24,10 @@ public class NotificationController {
@GetMapping
public List<NotificationDto> list(@RequestParam(value = "read", required = false) Boolean read,
@RequestParam(value = "page", defaultValue = "0") int page,
@RequestParam(value = "pageSize", defaultValue = "50") int pageSize,
Authentication auth) {
return notificationService.listNotifications(auth.getName(), read).stream()
return notificationService.listNotifications(auth.getName(), read, page, pageSize).stream()
.map(notificationMapper::toDto)
.collect(Collectors.toList());
}

View File

@@ -5,6 +5,7 @@ import com.openisle.model.User;
import com.openisle.model.Post;
import com.openisle.model.Comment;
import com.openisle.model.NotificationType;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
@@ -13,6 +14,9 @@ import java.util.List;
public interface NotificationRepository extends JpaRepository<Notification, Long> {
List<Notification> findByUserOrderByCreatedAtDesc(User user);
List<Notification> findByUserAndReadOrderByCreatedAtDesc(User user, boolean read);
List<Notification> findByUserOrderByCreatedAtDesc(User user, Pageable pageable);
List<Notification> findByUserAndReadOrderByCreatedAtDesc(User user, boolean read, Pageable pageable);
long countByUserAndRead(User user, boolean read);
List<Notification> findByPost(Post post);
List<Notification> findByComment(Comment comment);

View File

@@ -8,6 +8,8 @@ import com.openisle.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import com.openisle.service.EmailSender;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.transaction.annotation.Transactional;
@@ -180,15 +182,16 @@ public class NotificationService {
userRepository.save(user);
}
public List<Notification> listNotifications(String username, Boolean read) {
public List<Notification> listNotifications(String username, Boolean read, int page, int pageSize) {
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
Set<NotificationType> disabled = user.getDisabledNotificationTypes();
Pageable pageable = PageRequest.of(page, pageSize);
List<Notification> list;
if (read == null) {
list = notificationRepository.findByUserOrderByCreatedAtDesc(user);
list = notificationRepository.findByUserOrderByCreatedAtDesc(user, pageable);
} else {
list = notificationRepository.findByUserAndReadOrderByCreatedAtDesc(user, read);
list = notificationRepository.findByUserAndReadOrderByCreatedAtDesc(user, read, pageable);
}
return list.stream().filter(n -> !disabled.contains(n.getType())).collect(Collectors.toList());
}