From e7a1e1d15903b627d02a813a0694c1f23459af8f Mon Sep 17 00:00:00 2001 From: tim Date: Tue, 19 Aug 2025 19:33:13 +0800 Subject: [PATCH] Revert "feat: add paginated notification endpoints" This reverts commit cc525c1c27498822e545fae208c0a4f187f2c415. --- .../controller/NotificationController.java | 14 +- .../repository/NotificationRepository.java | 4 - .../openisle/service/NotificationService.java | 11 +- .../NotificationControllerTest.java | 20 +- .../service/NotificationServiceTest.java | 9 +- frontend_nuxt/pages/message.vue | 46 +-- frontend_nuxt/utils/notification.js | 323 +++++++++--------- 7 files changed, 186 insertions(+), 241 deletions(-) diff --git a/backend/src/main/java/com/openisle/controller/NotificationController.java b/backend/src/main/java/com/openisle/controller/NotificationController.java index b9cb282ba..d25d2a808 100644 --- a/backend/src/main/java/com/openisle/controller/NotificationController.java +++ b/backend/src/main/java/com/openisle/controller/NotificationController.java @@ -23,19 +23,9 @@ public class NotificationController { private final NotificationMapper notificationMapper; @GetMapping - public List list(@RequestParam(value = "page", defaultValue = "0") int page, - @RequestParam(value = "size", defaultValue = "30") int size, + public List list(@RequestParam(value = "read", required = false) Boolean read, Authentication auth) { - return notificationService.listNotifications(auth.getName(), null, page, size).stream() - .map(notificationMapper::toDto) - .collect(Collectors.toList()); - } - - @GetMapping("/unread") - public List listUnread(@RequestParam(value = "page", defaultValue = "0") int page, - @RequestParam(value = "size", defaultValue = "30") int size, - Authentication auth) { - return notificationService.listNotifications(auth.getName(), false, page, size).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 90c5d9463..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 findByUserOrderByCreatedAtDesc(User user, Pageable pageable); - Page findByUserAndReadOrderByCreatedAtDesc(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 ffd2078e8..6ecf571e8 100644 --- a/backend/src/main/java/com/openisle/service/NotificationService.java +++ b/backend/src/main/java/com/openisle/service/NotificationService.java @@ -180,18 +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(); - org.springframework.data.domain.Pageable pageable = org.springframework.data.domain.PageRequest.of(page, size); - org.springframework.data.domain.Page result; + List list; if (read == null) { - result = notificationRepository.findByUserOrderByCreatedAtDesc(user, pageable); + list = notificationRepository.findByUserOrderByCreatedAtDesc(user); } else { - result = notificationRepository.findByUserAndReadOrderByCreatedAtDesc(user, read, pageable); + list = notificationRepository.findByUserAndReadOrderByCreatedAtDesc(user, read); } - return result.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 c3cea2d30..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, 30)) + when(notificationService.listNotifications("alice", null)) .thenReturn(List.of(n)); NotificationDto dto = new NotificationDto(); @@ -62,24 +62,6 @@ class NotificationControllerTest { .andExpect(jsonPath("$[0].post.id").value(2)); } - @Test - void listUnreadNotifications() throws Exception { - Notification n = new Notification(); - n.setId(5L); - n.setType(NotificationType.POST_VIEWED); - when(notificationService.listNotifications("alice", false, 0, 30)) - .thenReturn(List.of(n)); - - NotificationDto dto = new NotificationDto(); - dto.setId(5L); - when(notificationMapper.toDto(n)).thenReturn(dto); - - mockMvc.perform(get("/api/notifications/unread") - .principal(new UsernamePasswordAuthenticationToken("alice","p"))) - .andExpect(status().isOk()) - .andExpect(jsonPath("$[0].id").value(5)); - } - @Test void markReadEndpoint() throws Exception { mockMvc.perform(post("/api/notifications/read") diff --git a/backend/src/test/java/com/openisle/service/NotificationServiceTest.java b/backend/src/test/java/com/openisle/service/NotificationServiceTest.java index 4e632920e..a01d3c95b 100644 --- a/backend/src/test/java/com/openisle/service/NotificationServiceTest.java +++ b/backend/src/test/java/com/openisle/service/NotificationServiceTest.java @@ -11,8 +11,6 @@ import org.mockito.Mockito; import java.util.List; import java.util.Optional; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.Pageable; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -67,13 +65,12 @@ class NotificationServiceTest { when(uRepo.findByUsername("bob")).thenReturn(Optional.of(user)); Notification n = new Notification(); - when(nRepo.findByUserOrderByCreatedAtDesc(eq(user), any(Pageable.class))) - .thenReturn(new PageImpl<>(List.of(n))); + when(nRepo.findByUserOrderByCreatedAtDesc(user)).thenReturn(List.of(n)); - List list = service.listNotifications("bob", null, 0, 10); + List list = service.listNotifications("bob", null); assertEquals(1, list.size()); - verify(nRepo).findByUserOrderByCreatedAtDesc(eq(user), any(Pageable.class)); + verify(nRepo).findByUserOrderByCreatedAtDesc(user); } @Test diff --git a/frontend_nuxt/pages/message.vue b/frontend_nuxt/pages/message.vue index 4b89b6ec5..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 512b4267a..7c0245b94 100644 --- a/frontend_nuxt/utils/notification.js +++ b/frontend_nuxt/utils/notification.js @@ -118,162 +118,175 @@ export async function updateNotificationPreference(type, enabled) { function createFetchNotifications() { const notifications = ref([]) const isLoadingMessage = ref(false) - const hasMore = ref(true) - - const fetchNotifications = async ({ - page = 0, - size = 30, - unread = false, - append = false, - } = {}) => { + const fetchNotifications = async () => { const config = useRuntimeConfig() const API_BASE_URL = config.public.apiBaseUrl - try { - const token = getToken() - if (!token) { - toast.error('请先登录') - return - } - if (!append) notifications.value = [] - isLoadingMessage.value = true - const res = await fetch( - `${API_BASE_URL}/api/notifications${unread ? '/unread' : ''}?page=${page}&size=${size}`, - { + if (isLoadingMessage && notifications && markRead) { + try { + const token = getToken() + if (!token) { + toast.error('请先登录') + return + } + isLoadingMessage.value = true + notifications.value = [] + const res = await fetch(`${API_BASE_URL}/api/notifications`, { headers: { Authorization: `Bearer ${token}`, }, - }, - ) - isLoadingMessage.value = false - if (!res.ok) { - toast.error('获取通知失败') - return - } - const data = await res.json() - const arr = [] - - for (const n of data) { - if (n.type === 'COMMENT_REPLY') { - arr.push({ - ...n, - src: n.comment.author.avatar, - iconClick: () => { - markRead(n.id) - navigateTo(`/users/${n.comment.author.id}`, { replace: true }) - }, - }) - } else if (n.type === 'REACTION') { - arr.push({ - ...n, - emoji: reactionEmojiMap[n.reactionType], - iconClick: () => { - if (n.fromUser) { - markRead(n.id) - navigateTo(`/users/${n.fromUser.id}`, { replace: true }) - } - }, - }) - } else if (n.type === 'POST_VIEWED') { - arr.push({ - ...n, - src: n.fromUser ? n.fromUser.avatar : null, - icon: n.fromUser ? undefined : iconMap[n.type], - iconClick: () => { - if (n.fromUser) { - markRead(n.id) - navigateTo(`/users/${n.fromUser.id}`, { replace: true }) - } - }, - }) - } else if (n.type === 'LOTTERY_WIN' || n.type === 'LOTTERY_DRAW') { - arr.push({ - ...n, - icon: iconMap[n.type], - iconClick: () => { - if (n.post) { - markRead(n.id) - navigateTo(`/posts/${n.post.id}`) - } - }, - }) - } else if (n.type === 'POST_UPDATED' || n.type === 'USER_ACTIVITY') { - arr.push({ - ...n, - src: n.comment.author.avatar, - iconClick: () => { - markRead(n.id) - navigateTo(`/users/${n.comment.author.id}`, { replace: true }) - }, - }) - } else if (n.type === 'MENTION') { - arr.push({ - ...n, - icon: iconMap[n.type], - iconClick: () => { - if (n.fromUser) { - markRead(n.id) - navigateTo(`/users/${n.fromUser.id}`, { replace: true }) - } - }, - }) - } else if (n.type === 'USER_FOLLOWED' || n.type === 'USER_UNFOLLOWED') { - arr.push({ - ...n, - icon: iconMap[n.type], - iconClick: () => { - if (n.fromUser) { - markRead(n.id) - navigateTo(`/users/${n.fromUser.id}`, { replace: true }) - } - }, - }) - } else if ( - n.type === 'FOLLOWED_POST' || - n.type === 'POST_SUBSCRIBED' || - n.type === 'POST_UNSUBSCRIBED' - ) { - arr.push({ - ...n, - icon: iconMap[n.type], - iconClick: () => { - if (n.post) { - markRead(n.id) - navigateTo(`/posts/${n.post.id}`, { replace: true }) - } - }, - }) - } else if (n.type === 'POST_REVIEW_REQUEST') { - arr.push({ - ...n, - src: n.fromUser ? n.fromUser.avatar : null, - icon: n.fromUser ? undefined : iconMap[n.type], - iconClick: () => { - if (n.post) { - markRead(n.id) - navigateTo(`/posts/${n.post.id}`, { replace: true }) - } - }, - }) - } else if (n.type === 'REGISTER_REQUEST') { - arr.push({ - ...n, - icon: iconMap[n.type], - iconClick: () => {}, - }) - } else { - arr.push({ - ...n, - icon: iconMap[n.type], - }) + }) + isLoadingMessage.value = false + if (!res.ok) { + toast.error('获取通知失败') + return } - } + const data = await res.json() - if (append) notifications.value.push(...arr) - else notifications.value = arr - hasMore.value = data.length === size - } catch (e) { - console.error(e) - isLoadingMessage.value = false + for (const n of data) { + if (n.type === 'COMMENT_REPLY') { + notifications.value.push({ + ...n, + src: n.comment.author.avatar, + iconClick: () => { + markRead(n.id) + navigateTo(`/users/${n.comment.author.id}`, { replace: true }) + }, + }) + } else if (n.type === 'REACTION') { + notifications.value.push({ + ...n, + emoji: reactionEmojiMap[n.reactionType], + iconClick: () => { + if (n.fromUser) { + markRead(n.id) + navigateTo(`/users/${n.fromUser.id}`, { replace: true }) + } + }, + }) + } else if (n.type === 'POST_VIEWED') { + notifications.value.push({ + ...n, + src: n.fromUser ? n.fromUser.avatar : null, + icon: n.fromUser ? undefined : iconMap[n.type], + iconClick: () => { + if (n.fromUser) { + markRead(n.id) + navigateTo(`/users/${n.fromUser.id}`, { replace: true }) + } + }, + }) + } else if (n.type === 'LOTTERY_WIN') { + notifications.value.push({ + ...n, + icon: iconMap[n.type], + iconClick: () => { + if (n.post) { + markRead(n.id) + router.push(`/posts/${n.post.id}`) + } + }, + }) + } else if (n.type === 'LOTTERY_DRAW') { + notifications.value.push({ + ...n, + icon: iconMap[n.type], + iconClick: () => { + if (n.post) { + markRead(n.id) + router.push(`/posts/${n.post.id}`) + } + }, + }) + } else if (n.type === 'POST_UPDATED') { + notifications.value.push({ + ...n, + src: n.comment.author.avatar, + iconClick: () => { + markRead(n.id) + navigateTo(`/users/${n.comment.author.id}`, { replace: true }) + }, + }) + } else if (n.type === 'USER_ACTIVITY') { + notifications.value.push({ + ...n, + src: n.comment.author.avatar, + iconClick: () => { + markRead(n.id) + navigateTo(`/users/${n.comment.author.id}`, { replace: true }) + }, + }) + } else if (n.type === 'MENTION') { + notifications.value.push({ + ...n, + icon: iconMap[n.type], + iconClick: () => { + if (n.fromUser) { + markRead(n.id) + navigateTo(`/users/${n.fromUser.id}`, { replace: true }) + } + }, + }) + } else if (n.type === 'USER_FOLLOWED' || n.type === 'USER_UNFOLLOWED') { + notifications.value.push({ + ...n, + icon: iconMap[n.type], + iconClick: () => { + if (n.fromUser) { + markRead(n.id) + navigateTo(`/users/${n.fromUser.id}`, { replace: true }) + } + }, + }) + } else if (n.type === 'FOLLOWED_POST') { + notifications.value.push({ + ...n, + icon: iconMap[n.type], + iconClick: () => { + if (n.post) { + markRead(n.id) + navigateTo(`/posts/${n.post.id}`, { replace: true }) + } + }, + }) + } else if (n.type === 'POST_SUBSCRIBED' || n.type === 'POST_UNSUBSCRIBED') { + notifications.value.push({ + ...n, + icon: iconMap[n.type], + iconClick: () => { + if (n.post) { + markRead(n.id) + navigateTo(`/posts/${n.post.id}`, { replace: true }) + } + }, + }) + } else if (n.type === 'POST_REVIEW_REQUEST') { + notifications.value.push({ + ...n, + src: n.fromUser ? n.fromUser.avatar : null, + icon: n.fromUser ? undefined : iconMap[n.type], + iconClick: () => { + if (n.post) { + markRead(n.id) + navigateTo(`/posts/${n.post.id}`, { replace: true }) + } + }, + }) + } else if (n.type === 'REGISTER_REQUEST') { + notifications.value.push({ + ...n, + icon: iconMap[n.type], + iconClick: () => {}, + }) + } else { + notifications.value.push({ + ...n, + icon: iconMap[n.type], + }) + } + } + } catch (e) { + console.error(e) + } } } @@ -322,16 +335,10 @@ function createFetchNotifications() { markRead, notifications, isLoadingMessage, + markRead, markAllRead, - hasMore, } } -export const { - fetchNotifications, - markRead, - notifications, - isLoadingMessage, - markAllRead, - hasMore, -} = createFetchNotifications() +export const { fetchNotifications, markRead, notifications, isLoadingMessage, markAllRead } = + createFetchNotifications()