mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-03-16 09:00:47 +08:00
Compare commits
16 Commits
codex/add-
...
codex/fix-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a3b28eafe4 | ||
|
|
805a8df7d3 | ||
|
|
02be045f55 | ||
|
|
ac3c7b7bec | ||
|
|
c344b5b4ae | ||
|
|
e7a1e1d159 | ||
|
|
30b56e54cf | ||
|
|
cc525c1c27 | ||
|
|
3f2829cd37 | ||
|
|
3258a42b44 | ||
|
|
a64fd71bbe | ||
|
|
1a12bec7b1 | ||
|
|
fbca19791a | ||
|
|
10b6fdd1cb | ||
|
|
7dd1f1b3d0 | ||
|
|
df92ff664c |
@@ -23,18 +23,19 @@ public class NotificationController {
|
|||||||
private final NotificationMapper notificationMapper;
|
private final NotificationMapper notificationMapper;
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<NotificationDto> list(@RequestParam(value = "read", required = false) Boolean read,
|
public List<NotificationDto> list(@RequestParam(value = "page", defaultValue = "0") int page,
|
||||||
@RequestParam(value = "page", defaultValue = "0") int page,
|
@RequestParam(value = "size", defaultValue = "30") int size,
|
||||||
Authentication auth) {
|
Authentication auth) {
|
||||||
return notificationService.listNotifications(auth.getName(), read, page, 50).stream()
|
return notificationService.listNotifications(auth.getName(), null, page, size).stream()
|
||||||
.map(notificationMapper::toDto)
|
.map(notificationMapper::toDto)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/unread")
|
@GetMapping("/unread")
|
||||||
public List<NotificationDto> listUnread(@RequestParam(value = "page", defaultValue = "0") int page,
|
public List<NotificationDto> listUnread(@RequestParam(value = "page", defaultValue = "0") int page,
|
||||||
|
@RequestParam(value = "size", defaultValue = "30") int size,
|
||||||
Authentication auth) {
|
Authentication auth) {
|
||||||
return notificationService.listNotifications(auth.getName(), false, page, 50).stream()
|
return notificationService.listNotifications(auth.getName(), false, page, size).stream()
|
||||||
.map(notificationMapper::toDto)
|
.map(notificationMapper::toDto)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,12 @@ import java.util.List;
|
|||||||
public interface NotificationRepository extends JpaRepository<Notification, Long> {
|
public interface NotificationRepository extends JpaRepository<Notification, Long> {
|
||||||
List<Notification> findByUserOrderByCreatedAtDesc(User user);
|
List<Notification> findByUserOrderByCreatedAtDesc(User user);
|
||||||
List<Notification> findByUserAndReadOrderByCreatedAtDesc(User user, boolean read);
|
List<Notification> findByUserAndReadOrderByCreatedAtDesc(User user, boolean read);
|
||||||
Page<Notification> findByUser(User user, Pageable pageable);
|
Page<Notification> findByUserOrderByCreatedAtDesc(User user, Pageable pageable);
|
||||||
Page<Notification> findByUserAndRead(User user, boolean read, Pageable pageable);
|
Page<Notification> findByUserAndReadOrderByCreatedAtDesc(User user, boolean read, Pageable pageable);
|
||||||
|
Page<Notification> findByUserAndTypeNotInOrderByCreatedAtDesc(User user, java.util.Collection<NotificationType> types, Pageable pageable);
|
||||||
|
Page<Notification> findByUserAndReadAndTypeNotInOrderByCreatedAtDesc(User user, boolean read, java.util.Collection<NotificationType> types, Pageable pageable);
|
||||||
long countByUserAndRead(User user, boolean read);
|
long countByUserAndRead(User user, boolean read);
|
||||||
|
long countByUserAndReadAndTypeNotIn(User user, boolean read, java.util.Collection<NotificationType> types);
|
||||||
List<Notification> findByPost(Post post);
|
List<Notification> findByPost(Post post);
|
||||||
List<Notification> findByComment(Comment comment);
|
List<Notification> findByComment(Comment comment);
|
||||||
|
|
||||||
|
|||||||
@@ -23,11 +23,6 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.concurrent.Executor;
|
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 for creating and retrieving notifications. */
|
||||||
@Service
|
@Service
|
||||||
@@ -188,15 +183,22 @@ public class NotificationService {
|
|||||||
User user = userRepository.findByUsername(username)
|
User user = userRepository.findByUsername(username)
|
||||||
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
|
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
|
||||||
Set<NotificationType> disabled = user.getDisabledNotificationTypes();
|
Set<NotificationType> disabled = user.getDisabledNotificationTypes();
|
||||||
Pageable pageable = PageRequest.of(page, size,
|
org.springframework.data.domain.Pageable pageable = org.springframework.data.domain.PageRequest.of(page, size);
|
||||||
Sort.by(Sort.Direction.DESC, "createdAt"));
|
org.springframework.data.domain.Page<Notification> result;
|
||||||
Page<Notification> pg;
|
|
||||||
if (read == null) {
|
if (read == null) {
|
||||||
pg = notificationRepository.findByUser(user, pageable);
|
if (disabled.isEmpty()) {
|
||||||
|
result = notificationRepository.findByUserOrderByCreatedAtDesc(user, pageable);
|
||||||
|
} else {
|
||||||
|
result = notificationRepository.findByUserAndTypeNotInOrderByCreatedAtDesc(user, disabled, pageable);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
pg = notificationRepository.findByUserAndRead(user, read, pageable);
|
if (disabled.isEmpty()) {
|
||||||
|
result = notificationRepository.findByUserAndReadOrderByCreatedAtDesc(user, read, pageable);
|
||||||
|
} else {
|
||||||
|
result = notificationRepository.findByUserAndReadAndTypeNotInOrderByCreatedAtDesc(user, read, disabled, pageable);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return pg.stream().filter(n -> !disabled.contains(n.getType())).collect(Collectors.toList());
|
return result.getContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void markRead(String username, List<Long> ids) {
|
public void markRead(String username, List<Long> ids) {
|
||||||
@@ -215,8 +217,10 @@ public class NotificationService {
|
|||||||
User user = userRepository.findByUsername(username)
|
User user = userRepository.findByUsername(username)
|
||||||
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
|
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
|
||||||
Set<NotificationType> disabled = user.getDisabledNotificationTypes();
|
Set<NotificationType> disabled = user.getDisabledNotificationTypes();
|
||||||
return notificationRepository.findByUserAndReadOrderByCreatedAtDesc(user, false).stream()
|
if (disabled.isEmpty()) {
|
||||||
.filter(n -> !disabled.contains(n.getType())).count();
|
return notificationRepository.countByUserAndRead(user, false);
|
||||||
|
}
|
||||||
|
return notificationRepository.countByUserAndReadAndTypeNotIn(user, false, disabled);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void notifyMentions(String content, User fromUser, Post post, Comment comment) {
|
public void notifyMentions(String content, User fromUser, Post post, Comment comment) {
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class NotificationControllerTest {
|
|||||||
p.setId(2L);
|
p.setId(2L);
|
||||||
n.setPost(p);
|
n.setPost(p);
|
||||||
n.setCreatedAt(LocalDateTime.now());
|
n.setCreatedAt(LocalDateTime.now());
|
||||||
when(notificationService.listNotifications("alice", null, 0, 50))
|
when(notificationService.listNotifications("alice", null, 0, 30))
|
||||||
.thenReturn(List.of(n));
|
.thenReturn(List.of(n));
|
||||||
|
|
||||||
NotificationDto dto = new NotificationDto();
|
NotificationDto dto = new NotificationDto();
|
||||||
@@ -55,7 +55,7 @@ class NotificationControllerTest {
|
|||||||
dto.setPost(ps);
|
dto.setPost(ps);
|
||||||
when(notificationMapper.toDto(n)).thenReturn(dto);
|
when(notificationMapper.toDto(n)).thenReturn(dto);
|
||||||
|
|
||||||
mockMvc.perform(get("/api/notifications?page=0")
|
mockMvc.perform(get("/api/notifications")
|
||||||
.principal(new UsernamePasswordAuthenticationToken("alice","p")))
|
.principal(new UsernamePasswordAuthenticationToken("alice","p")))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$[0].id").value(1))
|
.andExpect(jsonPath("$[0].id").value(1))
|
||||||
@@ -65,27 +65,19 @@ class NotificationControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
void listUnreadNotifications() throws Exception {
|
void listUnreadNotifications() throws Exception {
|
||||||
Notification n = new Notification();
|
Notification n = new Notification();
|
||||||
n.setId(1L);
|
n.setId(5L);
|
||||||
n.setType(NotificationType.POST_VIEWED);
|
n.setType(NotificationType.POST_VIEWED);
|
||||||
Post p = new Post();
|
when(notificationService.listNotifications("alice", false, 0, 30))
|
||||||
p.setId(2L);
|
|
||||||
n.setPost(p);
|
|
||||||
n.setCreatedAt(LocalDateTime.now());
|
|
||||||
when(notificationService.listNotifications("alice", false, 0, 50))
|
|
||||||
.thenReturn(List.of(n));
|
.thenReturn(List.of(n));
|
||||||
|
|
||||||
NotificationDto dto = new NotificationDto();
|
NotificationDto dto = new NotificationDto();
|
||||||
dto.setId(1L);
|
dto.setId(5L);
|
||||||
PostSummaryDto ps = new PostSummaryDto();
|
|
||||||
ps.setId(2L);
|
|
||||||
dto.setPost(ps);
|
|
||||||
when(notificationMapper.toDto(n)).thenReturn(dto);
|
when(notificationMapper.toDto(n)).thenReturn(dto);
|
||||||
|
|
||||||
mockMvc.perform(get("/api/notifications/unread?page=0")
|
mockMvc.perform(get("/api/notifications/unread")
|
||||||
.principal(new UsernamePasswordAuthenticationToken("alice","p")))
|
.principal(new UsernamePasswordAuthenticationToken("alice","p")))
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andExpect(jsonPath("$[0].id").value(1))
|
.andExpect(jsonPath("$[0].id").value(5));
|
||||||
.andExpect(jsonPath("$[0].post.id").value(2));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ import org.mockito.Mockito;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import org.springframework.data.domain.PageImpl;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
@@ -62,15 +65,17 @@ class NotificationServiceTest {
|
|||||||
User user = new User();
|
User user = new User();
|
||||||
user.setId(2L);
|
user.setId(2L);
|
||||||
user.setUsername("bob");
|
user.setUsername("bob");
|
||||||
|
user.setDisabledNotificationTypes(new HashSet<>());
|
||||||
when(uRepo.findByUsername("bob")).thenReturn(Optional.of(user));
|
when(uRepo.findByUsername("bob")).thenReturn(Optional.of(user));
|
||||||
|
|
||||||
Notification n = new Notification();
|
Notification n = new Notification();
|
||||||
when(nRepo.findByUser(eq(user), any())).thenReturn(new org.springframework.data.domain.PageImpl<>(List.of(n)));
|
when(nRepo.findByUserOrderByCreatedAtDesc(eq(user), any(Pageable.class)))
|
||||||
|
.thenReturn(new PageImpl<>(List.of(n)));
|
||||||
|
|
||||||
List<Notification> list = service.listNotifications("bob", null, 0, 50);
|
List<Notification> list = service.listNotifications("bob", null, 0, 10);
|
||||||
|
|
||||||
assertEquals(1, list.size());
|
assertEquals(1, list.size());
|
||||||
verify(nRepo).findByUser(eq(user), any());
|
verify(nRepo).findByUserOrderByCreatedAtDesc(eq(user), any(Pageable.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -87,6 +92,7 @@ class NotificationServiceTest {
|
|||||||
User user = new User();
|
User user = new User();
|
||||||
user.setId(3L);
|
user.setId(3L);
|
||||||
user.setUsername("carl");
|
user.setUsername("carl");
|
||||||
|
user.setDisabledNotificationTypes(new HashSet<>());
|
||||||
when(uRepo.findByUsername("carl")).thenReturn(Optional.of(user));
|
when(uRepo.findByUsername("carl")).thenReturn(Optional.of(user));
|
||||||
when(nRepo.countByUserAndRead(user, false)).thenReturn(5L);
|
when(nRepo.countByUserAndRead(user, false)).thenReturn(5L);
|
||||||
|
|
||||||
@@ -96,6 +102,56 @@ class NotificationServiceTest {
|
|||||||
verify(nRepo).countByUserAndRead(user, false);
|
verify(nRepo).countByUserAndRead(user, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void listNotificationsFiltersDisabledTypes() {
|
||||||
|
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||||
|
UserRepository uRepo = mock(UserRepository.class);
|
||||||
|
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||||
|
EmailSender email = mock(EmailSender.class);
|
||||||
|
PushNotificationService push = mock(PushNotificationService.class);
|
||||||
|
Executor executor = Runnable::run;
|
||||||
|
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||||
|
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||||
|
|
||||||
|
User user = new User();
|
||||||
|
user.setId(4L);
|
||||||
|
user.setUsername("dana");
|
||||||
|
when(uRepo.findByUsername("dana")).thenReturn(Optional.of(user));
|
||||||
|
|
||||||
|
Notification n = new Notification();
|
||||||
|
when(nRepo.findByUserAndTypeNotInOrderByCreatedAtDesc(eq(user), eq(user.getDisabledNotificationTypes()), any(Pageable.class)))
|
||||||
|
.thenReturn(new PageImpl<>(List.of(n)));
|
||||||
|
|
||||||
|
List<Notification> list = service.listNotifications("dana", null, 0, 10);
|
||||||
|
|
||||||
|
assertEquals(1, list.size());
|
||||||
|
verify(nRepo).findByUserAndTypeNotInOrderByCreatedAtDesc(eq(user), eq(user.getDisabledNotificationTypes()), any(Pageable.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void countUnreadFiltersDisabledTypes() {
|
||||||
|
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||||
|
UserRepository uRepo = mock(UserRepository.class);
|
||||||
|
ReactionRepository rRepo = mock(ReactionRepository.class);
|
||||||
|
EmailSender email = mock(EmailSender.class);
|
||||||
|
PushNotificationService push = mock(PushNotificationService.class);
|
||||||
|
Executor executor = Runnable::run;
|
||||||
|
NotificationService service = new NotificationService(nRepo, uRepo, email, push, rRepo, executor);
|
||||||
|
org.springframework.test.util.ReflectionTestUtils.setField(service, "websiteUrl", "https://ex.com");
|
||||||
|
|
||||||
|
User user = new User();
|
||||||
|
user.setId(5L);
|
||||||
|
user.setUsername("erin");
|
||||||
|
when(uRepo.findByUsername("erin")).thenReturn(Optional.of(user));
|
||||||
|
when(nRepo.countByUserAndReadAndTypeNotIn(eq(user), eq(false), eq(user.getDisabledNotificationTypes())))
|
||||||
|
.thenReturn(2L);
|
||||||
|
|
||||||
|
long count = service.countUnread("erin");
|
||||||
|
|
||||||
|
assertEquals(2L, count);
|
||||||
|
verify(nRepo).countByUserAndReadAndTypeNotIn(eq(user), eq(false), eq(user.getDisabledNotificationTypes()));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createRegisterRequestNotificationsDeletesOldOnes() {
|
void createRegisterRequestNotificationsDeletesOldOnes() {
|
||||||
NotificationRepository nRepo = mock(NotificationRepository.class);
|
NotificationRepository nRepo = mock(NotificationRepository.class);
|
||||||
|
|||||||
@@ -505,19 +505,14 @@
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</BaseTimeline>
|
</BaseTimeline>
|
||||||
<InfiniteLoadMore
|
<InfiniteLoadMore :key="selectedTab" :on-load="loadMore" :pause="isLoadingMessage" />
|
||||||
:key="ioKey"
|
|
||||||
:on-load="fetchNextPage"
|
|
||||||
:pause="isLoadingMessage"
|
|
||||||
root-margin="200px 0px"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { computed, onActivated, ref, watch } from 'vue'
|
import { ref, watch, onActivated } from 'vue'
|
||||||
import BasePlaceholder from '~/components/BasePlaceholder.vue'
|
import BasePlaceholder from '~/components/BasePlaceholder.vue'
|
||||||
import BaseTimeline from '~/components/BaseTimeline.vue'
|
import BaseTimeline from '~/components/BaseTimeline.vue'
|
||||||
import NotificationContainer from '~/components/NotificationContainer.vue'
|
import NotificationContainer from '~/components/NotificationContainer.vue'
|
||||||
@@ -532,6 +527,9 @@ import {
|
|||||||
markRead,
|
markRead,
|
||||||
notifications,
|
notifications,
|
||||||
markAllRead,
|
markAllRead,
|
||||||
|
hasMore,
|
||||||
|
fetchNotificationPreferences,
|
||||||
|
updateNotificationPreference,
|
||||||
} from '~/utils/notification'
|
} from '~/utils/notification'
|
||||||
import TimeManager from '~/utils/time'
|
import TimeManager from '~/utils/time'
|
||||||
|
|
||||||
@@ -542,11 +540,25 @@ const selectedTab = ref(
|
|||||||
['all', 'unread', 'control'].includes(route.query.tab) ? route.query.tab : 'unread',
|
['all', 'unread', 'control'].includes(route.query.tab) ? route.query.tab : 'unread',
|
||||||
)
|
)
|
||||||
const notificationPrefs = ref([])
|
const notificationPrefs = ref([])
|
||||||
const ioKey = computed(() => selectedTab.value)
|
const page = ref(0)
|
||||||
const loadFirstPage = async () => {
|
const pageSize = 30
|
||||||
await fetchNotifications({ unread: selectedTab.value === 'unread', reset: true })
|
|
||||||
|
const loadMore = async () => {
|
||||||
|
if (!hasMore.value) return true
|
||||||
|
page.value++
|
||||||
|
await fetchNotifications({
|
||||||
|
page: page.value,
|
||||||
|
size: pageSize,
|
||||||
|
unread: selectedTab.value === 'unread',
|
||||||
|
append: true,
|
||||||
|
})
|
||||||
|
return !hasMore.value
|
||||||
}
|
}
|
||||||
const fetchNextPage = async () => fetchNotifications()
|
|
||||||
|
watch(selectedTab, async (tab) => {
|
||||||
|
page.value = 0
|
||||||
|
await fetchNotifications({ page: 0, size: pageSize, unread: tab === 'unread' })
|
||||||
|
})
|
||||||
|
|
||||||
const fetchPrefs = async () => {
|
const fetchPrefs = async () => {
|
||||||
notificationPrefs.value = await fetchNotificationPreferences()
|
notificationPrefs.value = await fetchNotificationPreferences()
|
||||||
@@ -556,7 +568,11 @@ const togglePref = async (pref) => {
|
|||||||
const ok = await updateNotificationPreference(pref.type, !pref.enabled)
|
const ok = await updateNotificationPreference(pref.type, !pref.enabled)
|
||||||
if (ok) {
|
if (ok) {
|
||||||
pref.enabled = !pref.enabled
|
pref.enabled = !pref.enabled
|
||||||
await fetchNotifications({ unread: selectedTab.value === 'unread', reset: true })
|
await fetchNotifications({
|
||||||
|
page: page.value,
|
||||||
|
size: pageSize,
|
||||||
|
unread: selectedTab.value === 'unread',
|
||||||
|
})
|
||||||
await fetchUnreadCount()
|
await fetchUnreadCount()
|
||||||
} else {
|
} else {
|
||||||
toast.error('操作失败')
|
toast.error('操作失败')
|
||||||
@@ -636,12 +652,9 @@ const formatType = (t) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(selectedTab, (val) => {
|
onActivated(async () => {
|
||||||
if (val !== 'control') loadFirstPage()
|
page.value = 0
|
||||||
})
|
await fetchNotifications({ page: 0, size: pageSize, unread: selectedTab.value === 'unread' })
|
||||||
|
|
||||||
onActivated(() => {
|
|
||||||
if (selectedTab.value !== 'control') loadFirstPage()
|
|
||||||
fetchPrefs()
|
fetchPrefs()
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -118,189 +118,163 @@ export async function updateNotificationPreference(type, enabled) {
|
|||||||
function createFetchNotifications() {
|
function createFetchNotifications() {
|
||||||
const notifications = ref([])
|
const notifications = ref([])
|
||||||
const isLoadingMessage = ref(false)
|
const isLoadingMessage = ref(false)
|
||||||
const page = ref(0)
|
const hasMore = ref(true)
|
||||||
const currentUnread = ref(false)
|
|
||||||
const fetchNotifications = async ({ unread = false, reset = false } = {}) => {
|
const fetchNotifications = async ({
|
||||||
|
page = 0,
|
||||||
|
size = 30,
|
||||||
|
unread = false,
|
||||||
|
append = false,
|
||||||
|
} = {}) => {
|
||||||
const config = useRuntimeConfig()
|
const config = useRuntimeConfig()
|
||||||
const API_BASE_URL = config.public.apiBaseUrl
|
const API_BASE_URL = config.public.apiBaseUrl
|
||||||
if (isLoadingMessage && notifications && markRead) {
|
try {
|
||||||
try {
|
const token = getToken()
|
||||||
const token = getToken()
|
if (!token) {
|
||||||
if (!token) {
|
toast.error('请先登录')
|
||||||
toast.error('请先登录')
|
return
|
||||||
return false
|
}
|
||||||
}
|
if (!append) notifications.value = []
|
||||||
if (reset) {
|
isLoadingMessage.value = true
|
||||||
notifications.value = []
|
const res = await fetch(
|
||||||
page.value = 0
|
`${API_BASE_URL}/api/notifications${unread ? '/unread' : ''}?page=${page}&size=${size}`,
|
||||||
currentUnread.value = unread
|
{
|
||||||
}
|
|
||||||
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}`, {
|
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
},
|
},
|
||||||
})
|
},
|
||||||
isLoadingMessage.value = false
|
)
|
||||||
if (!res.ok) {
|
isLoadingMessage.value = false
|
||||||
toast.error('获取通知失败')
|
if (!res.ok) {
|
||||||
return true
|
toast.error('获取通知失败')
|
||||||
}
|
return
|
||||||
const data = await res.json()
|
|
||||||
|
|
||||||
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],
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
page.value++
|
|
||||||
return data.length < 50
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
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],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (append) notifications.value.push(...arr)
|
||||||
|
else notifications.value = arr
|
||||||
|
hasMore.value = data.length === size
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e)
|
||||||
|
isLoadingMessage.value = false
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const markRead = async (id) => {
|
const markRead = async (id) => {
|
||||||
@@ -349,8 +323,15 @@ function createFetchNotifications() {
|
|||||||
notifications,
|
notifications,
|
||||||
isLoadingMessage,
|
isLoadingMessage,
|
||||||
markAllRead,
|
markAllRead,
|
||||||
|
hasMore,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const { fetchNotifications, markRead, notifications, isLoadingMessage, markAllRead } =
|
export const {
|
||||||
createFetchNotifications()
|
fetchNotifications,
|
||||||
|
markRead,
|
||||||
|
notifications,
|
||||||
|
isLoadingMessage,
|
||||||
|
markAllRead,
|
||||||
|
hasMore,
|
||||||
|
} = createFetchNotifications()
|
||||||
|
|||||||
Reference in New Issue
Block a user