From 10b6fdd1cbedf5485b1a8d078fc953030b8261ec Mon Sep 17 00:00:00 2001 From: tim Date: Tue, 19 Aug 2025 18:24:49 +0800 Subject: [PATCH] Revert "feat: add paginated notifications and unread endpoint" This reverts commit 73168c1859b0ee9e68046676925ee71a134d0fe5. --- .../controller/NotificationController.java | 11 +------ .../repository/NotificationRepository.java | 4 --- .../openisle/service/NotificationService.java | 16 +++------- .../NotificationControllerTest.java | 30 ++---------------- .../service/NotificationServiceTest.java | 6 ++-- frontend_nuxt/pages/message.vue | 31 ++++++------------- frontend_nuxt/utils/notification.js | 24 ++++---------- 7 files changed, 26 insertions(+), 96 deletions(-) diff --git a/backend/src/main/java/com/openisle/controller/NotificationController.java b/backend/src/main/java/com/openisle/controller/NotificationController.java index f732043ff..d25d2a808 100644 --- a/backend/src/main/java/com/openisle/controller/NotificationController.java +++ b/backend/src/main/java/com/openisle/controller/NotificationController.java @@ -24,17 +24,8 @@ public class NotificationController { @GetMapping public List list(@RequestParam(value = "read", required = false) Boolean read, - @RequestParam(value = "page", defaultValue = "0") int page, Authentication auth) { - return notificationService.listNotifications(auth.getName(), read, page, 50).stream() - .map(notificationMapper::toDto) - .collect(Collectors.toList()); - } - - @GetMapping("/unread") - public List listUnread(@RequestParam(value = "page", defaultValue = "0") int page, - Authentication auth) { - return notificationService.listNotifications(auth.getName(), false, page, 50).stream() + return notificationService.listNotifications(auth.getName(), read).stream() .map(notificationMapper::toDto) .collect(Collectors.toList()); } diff --git a/backend/src/main/java/com/openisle/repository/NotificationRepository.java b/backend/src/main/java/com/openisle/repository/NotificationRepository.java index df2fae2d7..1e897b3a0 100644 --- a/backend/src/main/java/com/openisle/repository/NotificationRepository.java +++ b/backend/src/main/java/com/openisle/repository/NotificationRepository.java @@ -6,8 +6,6 @@ import com.openisle.model.Post; import com.openisle.model.Comment; import com.openisle.model.NotificationType; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; import java.util.List; @@ -15,8 +13,6 @@ import java.util.List; public interface NotificationRepository extends JpaRepository { List findByUserOrderByCreatedAtDesc(User user); List findByUserAndReadOrderByCreatedAtDesc(User user, boolean read); - Page findByUser(User user, Pageable pageable); - Page findByUserAndRead(User user, boolean read, Pageable pageable); long countByUserAndRead(User user, boolean read); List findByPost(Post post); List findByComment(Comment comment); diff --git a/backend/src/main/java/com/openisle/service/NotificationService.java b/backend/src/main/java/com/openisle/service/NotificationService.java index 26761902f..6ecf571e8 100644 --- a/backend/src/main/java/com/openisle/service/NotificationService.java +++ b/backend/src/main/java/com/openisle/service/NotificationService.java @@ -24,10 +24,6 @@ import java.util.List; import java.util.ArrayList; import java.util.concurrent.Executor; import java.util.stream.Collectors; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Sort; /** Service for creating and retrieving notifications. */ @Service @@ -184,19 +180,17 @@ public class NotificationService { userRepository.save(user); } - public List listNotifications(String username, Boolean read, int page, int size) { + public List listNotifications(String username, Boolean read) { User user = userRepository.findByUsername(username) .orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found")); Set disabled = user.getDisabledNotificationTypes(); - Pageable pageable = PageRequest.of(page, size, - Sort.by(Sort.Direction.DESC, "createdAt")); - Page pg; + List list; if (read == null) { - pg = notificationRepository.findByUser(user, pageable); + list = notificationRepository.findByUserOrderByCreatedAtDesc(user); } else { - pg = notificationRepository.findByUserAndRead(user, read, pageable); + list = notificationRepository.findByUserAndReadOrderByCreatedAtDesc(user, read); } - return pg.stream().filter(n -> !disabled.contains(n.getType())).collect(Collectors.toList()); + return list.stream().filter(n -> !disabled.contains(n.getType())).collect(Collectors.toList()); } public void markRead(String username, List ids) { diff --git a/backend/src/test/java/com/openisle/controller/NotificationControllerTest.java b/backend/src/test/java/com/openisle/controller/NotificationControllerTest.java index e9a69ef3d..6b74440a1 100644 --- a/backend/src/test/java/com/openisle/controller/NotificationControllerTest.java +++ b/backend/src/test/java/com/openisle/controller/NotificationControllerTest.java @@ -45,7 +45,7 @@ class NotificationControllerTest { p.setId(2L); n.setPost(p); n.setCreatedAt(LocalDateTime.now()); - when(notificationService.listNotifications("alice", null, 0, 50)) + when(notificationService.listNotifications("alice", null)) .thenReturn(List.of(n)); NotificationDto dto = new NotificationDto(); @@ -55,33 +55,7 @@ class NotificationControllerTest { dto.setPost(ps); when(notificationMapper.toDto(n)).thenReturn(dto); - mockMvc.perform(get("/api/notifications?page=0") - .principal(new UsernamePasswordAuthenticationToken("alice","p"))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$[0].id").value(1)) - .andExpect(jsonPath("$[0].post.id").value(2)); - } - - @Test - void listUnreadNotifications() throws Exception { - Notification n = new Notification(); - n.setId(1L); - n.setType(NotificationType.POST_VIEWED); - Post p = new Post(); - p.setId(2L); - n.setPost(p); - n.setCreatedAt(LocalDateTime.now()); - when(notificationService.listNotifications("alice", false, 0, 50)) - .thenReturn(List.of(n)); - - NotificationDto dto = new NotificationDto(); - dto.setId(1L); - PostSummaryDto ps = new PostSummaryDto(); - ps.setId(2L); - dto.setPost(ps); - when(notificationMapper.toDto(n)).thenReturn(dto); - - mockMvc.perform(get("/api/notifications/unread?page=0") + mockMvc.perform(get("/api/notifications") .principal(new UsernamePasswordAuthenticationToken("alice","p"))) .andExpect(status().isOk()) .andExpect(jsonPath("$[0].id").value(1)) diff --git a/backend/src/test/java/com/openisle/service/NotificationServiceTest.java b/backend/src/test/java/com/openisle/service/NotificationServiceTest.java index 2b1d7e512..a01d3c95b 100644 --- a/backend/src/test/java/com/openisle/service/NotificationServiceTest.java +++ b/backend/src/test/java/com/openisle/service/NotificationServiceTest.java @@ -65,12 +65,12 @@ class NotificationServiceTest { when(uRepo.findByUsername("bob")).thenReturn(Optional.of(user)); Notification n = new Notification(); - when(nRepo.findByUser(eq(user), any())).thenReturn(new org.springframework.data.domain.PageImpl<>(List.of(n))); + when(nRepo.findByUserOrderByCreatedAtDesc(user)).thenReturn(List.of(n)); - List list = service.listNotifications("bob", null, 0, 50); + List list = service.listNotifications("bob", null); assertEquals(1, list.size()); - verify(nRepo).findByUser(eq(user), any()); + verify(nRepo).findByUserOrderByCreatedAtDesc(user); } @Test diff --git a/frontend_nuxt/pages/message.vue b/frontend_nuxt/pages/message.vue index 58d9953bb..061eedd37 100644 --- a/frontend_nuxt/pages/message.vue +++ b/frontend_nuxt/pages/message.vue @@ -53,13 +53,13 @@ -
- +
+ -
diff --git a/frontend_nuxt/utils/notification.js b/frontend_nuxt/utils/notification.js index 8ce55f111..7c0245b94 100644 --- a/frontend_nuxt/utils/notification.js +++ b/frontend_nuxt/utils/notification.js @@ -118,9 +118,7 @@ export async function updateNotificationPreference(type, enabled) { function createFetchNotifications() { const notifications = ref([]) const isLoadingMessage = ref(false) - const page = ref(0) - const currentUnread = ref(false) - const fetchNotifications = async ({ unread = false, reset = false } = {}) => { + const fetchNotifications = async () => { const config = useRuntimeConfig() const API_BASE_URL = config.public.apiBaseUrl if (isLoadingMessage && notifications && markRead) { @@ -128,18 +126,11 @@ function createFetchNotifications() { const token = getToken() if (!token) { toast.error('请先登录') - return false - } - if (reset) { - notifications.value = [] - page.value = 0 - currentUnread.value = unread + return } isLoadingMessage.value = true - const endpoint = currentUnread.value - ? `/api/notifications/unread?page=${page.value}` - : `/api/notifications?page=${page.value}` - const res = await fetch(`${API_BASE_URL}${endpoint}`, { + notifications.value = [] + const res = await fetch(`${API_BASE_URL}/api/notifications`, { headers: { Authorization: `Bearer ${token}`, }, @@ -147,7 +138,7 @@ function createFetchNotifications() { isLoadingMessage.value = false if (!res.ok) { toast.error('获取通知失败') - return true + return } const data = await res.json() @@ -293,14 +284,10 @@ function createFetchNotifications() { }) } } - page.value++ - return data.length < 50 } catch (e) { console.error(e) - return true } } - return true } const markRead = async (id) => { @@ -348,6 +335,7 @@ function createFetchNotifications() { markRead, notifications, isLoadingMessage, + markRead, markAllRead, } }