refactor(errors): migrate decisions to {code,msg}
This commit is contained in:
+11
-11
@@ -1,4 +1,4 @@
|
||||
import { allow, deny } from "@/lib/policy/assert"
|
||||
import { allow, deny } from "@/lib/decision"
|
||||
import { useAuthStore } from "@/store/auth"
|
||||
import { useChatStore } from "@/store/chat"
|
||||
|
||||
@@ -16,18 +16,18 @@ export function listChatMessages(sessionId: string) {
|
||||
|
||||
export function sendTextMessage(sessionId: string, content: string) {
|
||||
const userId = useAuthStore.getState().user?.id
|
||||
if (!userId) return deny("AUTH_REQUIRED", "请先登录")
|
||||
if (!userId) return deny(401, "请先登录")
|
||||
|
||||
const chatState = useChatStore.getState()
|
||||
const session = chatState.sessions.find((item) => item.id === sessionId)
|
||||
if (!session) return deny("NOT_FOUND", "会话不存在")
|
||||
if (session.readonly) return deny("INVALID_STATUS", "当前会话只读")
|
||||
if (!session) return deny(404, "会话不存在")
|
||||
if (session.readonly) return deny(400, "当前会话只读")
|
||||
if (!session.participants.some((participant) => participant.id === userId)) {
|
||||
return deny("NOT_PARTICIPANT", "仅会话参与方可发送消息")
|
||||
return deny(403, "仅会话参与方可发送消息")
|
||||
}
|
||||
|
||||
if (!content.trim()) {
|
||||
return deny("VALIDATION_FAILED", "消息不能为空")
|
||||
return deny(400, "消息不能为空")
|
||||
}
|
||||
|
||||
chatState.sendTextMessage(sessionId, userId, content)
|
||||
@@ -36,18 +36,18 @@ export function sendTextMessage(sessionId: string, content: string) {
|
||||
|
||||
export function sendImageMessage(sessionId: string, imageUrl: string) {
|
||||
const userId = useAuthStore.getState().user?.id
|
||||
if (!userId) return deny("AUTH_REQUIRED", "请先登录")
|
||||
if (!userId) return deny(401, "请先登录")
|
||||
|
||||
const chatState = useChatStore.getState()
|
||||
const session = chatState.sessions.find((item) => item.id === sessionId)
|
||||
if (!session) return deny("NOT_FOUND", "会话不存在")
|
||||
if (session.readonly) return deny("INVALID_STATUS", "当前会话只读")
|
||||
if (!session) return deny(404, "会话不存在")
|
||||
if (session.readonly) return deny(400, "当前会话只读")
|
||||
if (!session.participants.some((participant) => participant.id === userId)) {
|
||||
return deny("NOT_PARTICIPANT", "仅会话参与方可发送消息")
|
||||
return deny(403, "仅会话参与方可发送消息")
|
||||
}
|
||||
|
||||
if (!imageUrl.trim()) {
|
||||
return deny("VALIDATION_FAILED", "图片地址无效")
|
||||
return deny(400, "图片地址无效")
|
||||
}
|
||||
|
||||
chatState.sendImageMessage(sessionId, userId, imageUrl)
|
||||
|
||||
Reference in New Issue
Block a user