mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-08-19 17:50:59 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 99644046fc | |||
| 22c9bd7d39 | |||
| 4eed6889d6 | |||
| 959b0f6a48 |
@@ -14,6 +14,8 @@ public enum NotificationType {
|
|||||||
POST_REVIEW_REQUEST,
|
POST_REVIEW_REQUEST,
|
||||||
/** Your post under review was approved or rejected */
|
/** Your post under review was approved or rejected */
|
||||||
POST_REVIEWED,
|
POST_REVIEWED,
|
||||||
|
/** An administrator deleted your post */
|
||||||
|
POST_DELETED,
|
||||||
/** A subscribed post received a new comment */
|
/** A subscribed post received a new comment */
|
||||||
POST_UPDATED,
|
POST_UPDATED,
|
||||||
/** Someone subscribed to your post */
|
/** Someone subscribed to your post */
|
||||||
|
|||||||
@@ -579,7 +579,9 @@ public class PostService {
|
|||||||
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
|
.orElseThrow(() -> new com.openisle.exception.NotFoundException("Post not found"));
|
||||||
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"));
|
||||||
if (!user.getId().equals(post.getAuthor().getId()) && user.getRole() != Role.ADMIN) {
|
User author = post.getAuthor();
|
||||||
|
boolean adminDeleting = !user.getId().equals(author.getId()) && user.getRole() == Role.ADMIN;
|
||||||
|
if (!user.getId().equals(author.getId()) && user.getRole() != Role.ADMIN) {
|
||||||
throw new IllegalArgumentException("Unauthorized");
|
throw new IllegalArgumentException("Unauthorized");
|
||||||
}
|
}
|
||||||
for (Comment c : commentRepository.findByPostAndParentIsNullOrderByCreatedAtAsc(post)) {
|
for (Comment c : commentRepository.findByPostAndParentIsNullOrderByCreatedAtAsc(post)) {
|
||||||
@@ -596,7 +598,12 @@ public class PostService {
|
|||||||
future.cancel(false);
|
future.cancel(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
String title = post.getTitle();
|
||||||
postRepository.delete(post);
|
postRepository.delete(post);
|
||||||
|
if (adminDeleting) {
|
||||||
|
notificationService.createNotification(author, NotificationType.POST_DELETED,
|
||||||
|
null, null, null, user, null, title);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public java.util.List<Post> getPostsByIds(java.util.List<Long> ids) {
|
public java.util.List<Post> getPostsByIds(java.util.List<Long> ids) {
|
||||||
|
|||||||
@@ -61,6 +61,58 @@ class PostServiceTest {
|
|||||||
verify(postRepo).delete(post);
|
verify(postRepo).delete(post);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void deletePostByAdminNotifiesAuthor() {
|
||||||
|
PostRepository postRepo = mock(PostRepository.class);
|
||||||
|
UserRepository userRepo = mock(UserRepository.class);
|
||||||
|
CategoryRepository catRepo = mock(CategoryRepository.class);
|
||||||
|
TagRepository tagRepo = mock(TagRepository.class);
|
||||||
|
LotteryPostRepository lotteryRepo = mock(LotteryPostRepository.class);
|
||||||
|
NotificationService notifService = mock(NotificationService.class);
|
||||||
|
SubscriptionService subService = mock(SubscriptionService.class);
|
||||||
|
CommentService commentService = mock(CommentService.class);
|
||||||
|
CommentRepository commentRepo = mock(CommentRepository.class);
|
||||||
|
ReactionRepository reactionRepo = mock(ReactionRepository.class);
|
||||||
|
PostSubscriptionRepository subRepo = mock(PostSubscriptionRepository.class);
|
||||||
|
NotificationRepository notificationRepo = mock(NotificationRepository.class);
|
||||||
|
PostReadService postReadService = mock(PostReadService.class);
|
||||||
|
ImageUploader imageUploader = mock(ImageUploader.class);
|
||||||
|
TaskScheduler taskScheduler = mock(TaskScheduler.class);
|
||||||
|
EmailSender emailSender = mock(EmailSender.class);
|
||||||
|
ApplicationContext context = mock(ApplicationContext.class);
|
||||||
|
|
||||||
|
PostService service = new PostService(postRepo, userRepo, catRepo, tagRepo, lotteryRepo,
|
||||||
|
notifService, subService, commentService, commentRepo,
|
||||||
|
reactionRepo, subRepo, notificationRepo, postReadService,
|
||||||
|
imageUploader, taskScheduler, emailSender, context, PublishMode.DIRECT);
|
||||||
|
when(context.getBean(PostService.class)).thenReturn(service);
|
||||||
|
|
||||||
|
Post post = new Post();
|
||||||
|
post.setId(1L);
|
||||||
|
post.setTitle("T");
|
||||||
|
post.setContent("");
|
||||||
|
User author = new User();
|
||||||
|
author.setId(2L);
|
||||||
|
author.setRole(Role.USER);
|
||||||
|
post.setAuthor(author);
|
||||||
|
|
||||||
|
User admin = new User();
|
||||||
|
admin.setId(1L);
|
||||||
|
admin.setRole(Role.ADMIN);
|
||||||
|
|
||||||
|
when(postRepo.findById(1L)).thenReturn(Optional.of(post));
|
||||||
|
when(userRepo.findByUsername("admin")).thenReturn(Optional.of(admin));
|
||||||
|
when(commentRepo.findByPostAndParentIsNullOrderByCreatedAtAsc(post)).thenReturn(List.of());
|
||||||
|
when(reactionRepo.findByPost(post)).thenReturn(List.of());
|
||||||
|
when(subRepo.findByPost(post)).thenReturn(List.of());
|
||||||
|
when(notificationRepo.findByPost(post)).thenReturn(List.of());
|
||||||
|
|
||||||
|
service.deletePost(1L, "admin");
|
||||||
|
|
||||||
|
verify(notifService).createNotification(eq(author), eq(NotificationType.POST_DELETED), isNull(),
|
||||||
|
isNull(), isNull(), eq(admin), isNull(), eq("T"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void createPostRespectsRateLimit() {
|
void createPostRespectsRateLimit() {
|
||||||
PostRepository postRepo = mock(PostRepository.class);
|
PostRepository postRepo = mock(PostRepository.class);
|
||||||
|
|||||||
@@ -95,7 +95,6 @@ const closeMilkTeaPopup = () => {
|
|||||||
if (!process.client) return
|
if (!process.client) return
|
||||||
localStorage.setItem('milkTeaActivityPopupShown', 'true')
|
localStorage.setItem('milkTeaActivityPopupShown', 'true')
|
||||||
showMilkTeaPopup.value = false
|
showMilkTeaPopup.value = false
|
||||||
checkNotificationSetting()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const checkNotificationSetting = async () => {
|
const checkNotificationSetting = async () => {
|
||||||
@@ -108,7 +107,6 @@ const closeNotificationPopup = () => {
|
|||||||
if (!process.client) return
|
if (!process.client) return
|
||||||
localStorage.setItem('notificationSettingPopupShown', 'true')
|
localStorage.setItem('notificationSettingPopupShown', 'true')
|
||||||
showNotificationPopup.value = false
|
showNotificationPopup.value = false
|
||||||
checkNewMedals()
|
|
||||||
}
|
}
|
||||||
const checkNewMedals = async () => {
|
const checkNewMedals = async () => {
|
||||||
if (!process.client) return
|
if (!process.client) return
|
||||||
|
|||||||
@@ -495,6 +495,24 @@
|
|||||||
已被管理员拒绝
|
已被管理员拒绝
|
||||||
</NotificationContainer>
|
</NotificationContainer>
|
||||||
</template>
|
</template>
|
||||||
|
<template v-else-if="item.type === 'POST_DELETED'">
|
||||||
|
<NotificationContainer :item="item" :markRead="markRead">
|
||||||
|
管理员
|
||||||
|
<template v-if="item.fromUser">
|
||||||
|
<NuxtLink
|
||||||
|
class="notif-content-text"
|
||||||
|
@click="markRead(item.id)"
|
||||||
|
:to="`/users/${item.fromUser.id}`"
|
||||||
|
>
|
||||||
|
{{ item.fromUser.username }}
|
||||||
|
</NuxtLink>
|
||||||
|
</template>
|
||||||
|
删除了您的帖子
|
||||||
|
<span class="notif-content-text">
|
||||||
|
{{ stripMarkdownLength(item.content, 100) }}
|
||||||
|
</span>
|
||||||
|
</NotificationContainer>
|
||||||
|
</template>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<NotificationContainer :item="item" :markRead="markRead">
|
<NotificationContainer :item="item" :markRead="markRead">
|
||||||
{{ formatType(item.type) }}
|
{{ formatType(item.type) }}
|
||||||
@@ -524,7 +542,7 @@ import {
|
|||||||
fetchNotifications,
|
fetchNotifications,
|
||||||
fetchUnreadCount,
|
fetchUnreadCount,
|
||||||
isLoadingMessage,
|
isLoadingMessage,
|
||||||
markRead as markNotificationRead,
|
markNotificationRead,
|
||||||
notifications,
|
notifications,
|
||||||
markAllRead,
|
markAllRead,
|
||||||
hasMore,
|
hasMore,
|
||||||
@@ -580,7 +598,7 @@ const togglePref = async (pref) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const markRead = async (id) => {
|
const markRead = async (id) => {
|
||||||
await markNotificationRead(id)
|
markNotificationRead(id)
|
||||||
if (selectedTab.value === 'unread') {
|
if (selectedTab.value === 'unread') {
|
||||||
const index = notifications.value.findIndex((n) => n.id === id)
|
const index = notifications.value.findIndex((n) => n.id === id)
|
||||||
if (index !== -1) notifications.value.splice(index, 1)
|
if (index !== -1) notifications.value.splice(index, 1)
|
||||||
@@ -655,6 +673,8 @@ const formatType = (t) => {
|
|||||||
return '抽奖中奖了'
|
return '抽奖中奖了'
|
||||||
case 'LOTTERY_DRAW':
|
case 'LOTTERY_DRAW':
|
||||||
return '抽奖已开奖'
|
return '抽奖已开奖'
|
||||||
|
case 'POST_DELETED':
|
||||||
|
return '帖子被删除'
|
||||||
default:
|
default:
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ const iconMap = {
|
|||||||
LOTTERY_WIN: 'fas fa-trophy',
|
LOTTERY_WIN: 'fas fa-trophy',
|
||||||
LOTTERY_DRAW: 'fas fa-bullhorn',
|
LOTTERY_DRAW: 'fas fa-bullhorn',
|
||||||
MENTION: 'fas fa-at',
|
MENTION: 'fas fa-at',
|
||||||
|
POST_DELETED: 'fas fa-trash',
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchUnreadCount() {
|
export async function fetchUnreadCount() {
|
||||||
@@ -158,7 +159,7 @@ function createFetchNotifications() {
|
|||||||
...n,
|
...n,
|
||||||
src: n.comment.author.avatar,
|
src: n.comment.author.avatar,
|
||||||
iconClick: () => {
|
iconClick: () => {
|
||||||
markRead(n.id)
|
markNotificationRead(n.id)
|
||||||
navigateTo(`/users/${n.comment.author.id}`, { replace: true })
|
navigateTo(`/users/${n.comment.author.id}`, { replace: true })
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -168,7 +169,7 @@ function createFetchNotifications() {
|
|||||||
emoji: reactionEmojiMap[n.reactionType],
|
emoji: reactionEmojiMap[n.reactionType],
|
||||||
iconClick: () => {
|
iconClick: () => {
|
||||||
if (n.fromUser) {
|
if (n.fromUser) {
|
||||||
markRead(n.id)
|
markNotificationRead(n.id)
|
||||||
navigateTo(`/users/${n.fromUser.id}`, { replace: true })
|
navigateTo(`/users/${n.fromUser.id}`, { replace: true })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -180,7 +181,19 @@ function createFetchNotifications() {
|
|||||||
icon: n.fromUser ? undefined : iconMap[n.type],
|
icon: n.fromUser ? undefined : iconMap[n.type],
|
||||||
iconClick: () => {
|
iconClick: () => {
|
||||||
if (n.fromUser) {
|
if (n.fromUser) {
|
||||||
markRead(n.id)
|
markNotificationRead(n.id)
|
||||||
|
navigateTo(`/users/${n.fromUser.id}`, { replace: true })
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
} else if (n.type === 'POST_DELETED') {
|
||||||
|
arr.push({
|
||||||
|
...n,
|
||||||
|
src: n.fromUser ? n.fromUser.avatar : null,
|
||||||
|
icon: n.fromUser ? undefined : iconMap[n.type],
|
||||||
|
iconClick: () => {
|
||||||
|
if (n.fromUser) {
|
||||||
|
markNotificationRead(n.id)
|
||||||
navigateTo(`/users/${n.fromUser.id}`, { replace: true })
|
navigateTo(`/users/${n.fromUser.id}`, { replace: true })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -191,7 +204,7 @@ function createFetchNotifications() {
|
|||||||
icon: iconMap[n.type],
|
icon: iconMap[n.type],
|
||||||
iconClick: () => {
|
iconClick: () => {
|
||||||
if (n.post) {
|
if (n.post) {
|
||||||
markRead(n.id)
|
markNotificationRead(n.id)
|
||||||
navigateTo(`/posts/${n.post.id}`)
|
navigateTo(`/posts/${n.post.id}`)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -201,7 +214,7 @@ function createFetchNotifications() {
|
|||||||
...n,
|
...n,
|
||||||
src: n.comment.author.avatar,
|
src: n.comment.author.avatar,
|
||||||
iconClick: () => {
|
iconClick: () => {
|
||||||
markRead(n.id)
|
markNotificationRead(n.id)
|
||||||
navigateTo(`/users/${n.comment.author.id}`, { replace: true })
|
navigateTo(`/users/${n.comment.author.id}`, { replace: true })
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -211,7 +224,7 @@ function createFetchNotifications() {
|
|||||||
icon: iconMap[n.type],
|
icon: iconMap[n.type],
|
||||||
iconClick: () => {
|
iconClick: () => {
|
||||||
if (n.fromUser) {
|
if (n.fromUser) {
|
||||||
markRead(n.id)
|
markNotificationRead(n.id)
|
||||||
navigateTo(`/users/${n.fromUser.id}`, { replace: true })
|
navigateTo(`/users/${n.fromUser.id}`, { replace: true })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -222,7 +235,7 @@ function createFetchNotifications() {
|
|||||||
icon: iconMap[n.type],
|
icon: iconMap[n.type],
|
||||||
iconClick: () => {
|
iconClick: () => {
|
||||||
if (n.fromUser) {
|
if (n.fromUser) {
|
||||||
markRead(n.id)
|
markNotificationRead(n.id)
|
||||||
navigateTo(`/users/${n.fromUser.id}`, { replace: true })
|
navigateTo(`/users/${n.fromUser.id}`, { replace: true })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -237,7 +250,7 @@ function createFetchNotifications() {
|
|||||||
icon: iconMap[n.type],
|
icon: iconMap[n.type],
|
||||||
iconClick: () => {
|
iconClick: () => {
|
||||||
if (n.post) {
|
if (n.post) {
|
||||||
markRead(n.id)
|
markNotificationRead(n.id)
|
||||||
navigateTo(`/posts/${n.post.id}`, { replace: true })
|
navigateTo(`/posts/${n.post.id}`, { replace: true })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -249,7 +262,7 @@ function createFetchNotifications() {
|
|||||||
icon: n.fromUser ? undefined : iconMap[n.type],
|
icon: n.fromUser ? undefined : iconMap[n.type],
|
||||||
iconClick: () => {
|
iconClick: () => {
|
||||||
if (n.post) {
|
if (n.post) {
|
||||||
markRead(n.id)
|
markNotificationRead(n.id)
|
||||||
navigateTo(`/posts/${n.post.id}`, { replace: true })
|
navigateTo(`/posts/${n.post.id}`, { replace: true })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -277,7 +290,7 @@ function createFetchNotifications() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const markRead = async (id) => {
|
const markNotificationRead = async (id) => {
|
||||||
if (!id) return
|
if (!id) return
|
||||||
const n = notifications.value.find((n) => n.id === id)
|
const n = notifications.value.find((n) => n.id === id)
|
||||||
if (!n || n.read) return
|
if (!n || n.read) return
|
||||||
@@ -319,7 +332,7 @@ function createFetchNotifications() {
|
|||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
fetchNotifications,
|
fetchNotifications,
|
||||||
markRead,
|
markNotificationRead,
|
||||||
notifications,
|
notifications,
|
||||||
isLoadingMessage,
|
isLoadingMessage,
|
||||||
markAllRead,
|
markAllRead,
|
||||||
@@ -329,7 +342,7 @@ function createFetchNotifications() {
|
|||||||
|
|
||||||
export const {
|
export const {
|
||||||
fetchNotifications,
|
fetchNotifications,
|
||||||
markRead,
|
markNotificationRead,
|
||||||
notifications,
|
notifications,
|
||||||
isLoadingMessage,
|
isLoadingMessage,
|
||||||
markAllRead,
|
markAllRead,
|
||||||
|
|||||||
Reference in New Issue
Block a user