refactor(community): extract comment store and post/comment API adapters

This commit is contained in:
zetaloop
2026-02-23 11:04:40 +08:00
parent 8e62b15403
commit 77d23d0c9d
9 changed files with 201 additions and 63 deletions
+30
View File
@@ -1,3 +1,6 @@
import { allow, deny } from "@/lib/policy/assert"
import { addNotification } from "@/lib/api/notifications"
import { useAuthStore } from "@/store/auth"
import { usePostStore } from "@/store/posts"
export function listPosts() {
@@ -11,3 +14,30 @@ export function getPostById(postId: string) {
export function listPostsByAuthor(userId: string) {
return usePostStore.getState().posts.filter((post) => post.author.id === userId)
}
export function togglePostLike(postId: string) {
const user = useAuthStore.getState().user
if (!user) {
return deny("AUTH_REQUIRED", "请先登录")
}
const post = usePostStore.getState().posts.find((item) => item.id === postId)
if (!post) {
return deny("NOT_FOUND", "帖子不存在")
}
const shouldNotify = !post.liked
usePostStore.getState().togglePostLike(postId)
if (shouldNotify) {
addNotification({
type: "community",
title: "帖子收到点赞",
content: `${post.title}》有新的点赞`,
link: `/post/${post.id}`,
})
}
return allow()
}