refactor(chat): remove private unlock machine flow

This commit is contained in:
2026-06-26 19:17:29 +08:00
parent b6f18a1ef3
commit 1b73c3ac10
11 changed files with 27 additions and 302 deletions
+9 -116
View File
@@ -2,10 +2,10 @@
* Chat 状态机(XState v5
*
* 鉴权解耦(事件驱动):
* chat 机器不感知鉴权 / 不管 WebSocket —— 由 <ChatAuthSync /> 派生 loginStatus
* chat 机器不感知鉴权 —— 由 <ChatAuthSync /> 派生 loginStatus
* ChatAuthSync 派发登录态生命周期事件:
* - `ChatGuestLogin` → 游客会话(断 WS = 不连)
* - `ChatUserLogin { token }` → 其他登录用户会话(不连 WS
* - `ChatGuestLogin` → 游客会话
* - `ChatUserLogin { token }` → 其他登录用户会话
* - `ChatLogout` → 正式登录用户登出
*
* 登录态流转约束:
@@ -15,8 +15,8 @@
*
* 状态结构(parent state 模式):
* - `idle`:屏没挂 / 登出
* - `guestSession`parent):游客会话 —— 不 invoke WS
* - `userSession`parent):其他登录用户会话 —— 不 invoke WS
* - `guestSession`parent):游客会话
* - `userSession`parent):其他登录用户会话
*
* init 任务:
* - guestSession.initializingloadHistory
@@ -50,7 +50,6 @@ import {
sendMessageHttpActor,
loadMoreHistoryActor,
httpMessageQueueActor,
unlockPrivateMessageActor,
} from "./chat-machine.actors";
const log = new Logger("StoresChatChatMachine");
@@ -73,7 +72,6 @@ export const chatMachine = setup({
sendMessageHttp: sendMessageHttpActor,
loadMoreHistory: loadMoreHistoryActor,
httpMessageQueue: httpMessageQueueActor,
unlockPrivateMessage: unlockPrivateMessageActor,
},
actions: {
enqueueMessage: sendTo("messageQueue", ({ event }) => event),
@@ -195,7 +193,7 @@ export const chatMachine = setup({
};
}),
appendSocketErrorMessage: assign(({ context }) => {
appendQueuedSendErrorMessage: assign(({ context }) => {
const messages = [
...context.messages,
{
@@ -213,69 +211,6 @@ export const chatMachine = setup({
if (event.type !== "ChatQueuedHttpDone") return {};
return applyHttpSendOutput(context, event.output);
}),
setUnlockingPrivateMessage: assign(({ event }) => {
if (event.type !== "ChatUnlockPrivateMessage") return {};
return {
unlockingPrivateMessageId: event.messageId,
upgradePromptVisible: false,
upgradeReason: null,
upgradeHint: null,
upgradeDetail: null,
};
}),
applyUnlockPrivateOutput: assign(({ context, event }) => {
if (!("output" in event)) return {};
const output = event.output as unknown as {
messageId: string;
response: import("@/data/dto/chat").UnlockPrivateResponse;
};
const { messageId, response } = output;
if (response.unlocked && response.content != null) {
return {
messages: context.messages.map((message) =>
message.id === messageId
? {
...message,
content: response.content ?? message.content,
locked: false,
lockedPrivate: false,
privateMessageHint: null,
isPrivate: message.isPrivate ?? true,
}
: message,
),
unlockingPrivateMessageId: null,
upgradePromptVisible: false,
upgradeReason: null,
upgradeHint: null,
upgradeDetail: null,
};
}
if (response.showUpgrade) {
return {
unlockingPrivateMessageId: null,
upgradePromptVisible: true,
upgradeReason: "private_message",
upgradeHint: null,
upgradeDetail: {
usedToday: response.privateUsedToday,
limit: response.privateFreeLimit,
},
};
}
return {
unlockingPrivateMessageId: null,
};
}),
clearUnlockingPrivateMessage: assign({
unlockingPrivateMessageId: null,
}),
},
}).createMachine({
id: "chat",
@@ -312,7 +247,7 @@ export const chatMachine = setup({
actions: "applyQueuedHttpOutput",
},
ChatQueuedSendError: {
actions: "appendSocketErrorMessage",
actions: "appendQueuedSendErrorMessage",
},
},
initial: "initializing",
@@ -354,11 +289,7 @@ export const chatMachine = setup({
actions: "appendGuestUserImage",
target: "sending",
},
ChatUnlockPrivateMessage: {
actions: "setUnlockingPrivateMessage",
target: "unlockingPrivate",
},
// 删除 ChatLoadMoreHistory / WS handlers —— 游客无服务端 history,也不连接 WS
// 游客不支持翻页历史,发送消息统一走 HTTP 队列。
},
},
sending: {
@@ -379,23 +310,6 @@ export const chatMachine = setup({
},
},
},
unlockingPrivate: {
invoke: {
src: "unlockPrivateMessage",
input: ({ event }) => ({
messageId:
event.type === "ChatUnlockPrivateMessage" ? event.messageId : "",
}),
onDone: {
target: "ready",
actions: "applyUnlockPrivateOutput",
},
onError: {
target: "ready",
actions: "clearUnlockingPrivateMessage",
},
},
},
},
},
@@ -421,7 +335,7 @@ export const chatMachine = setup({
actions: "applyQueuedHttpOutput",
},
ChatQueuedSendError: {
actions: "appendSocketErrorMessage",
actions: "appendQueuedSendErrorMessage",
},
},
initial: "initializing",
@@ -460,10 +374,6 @@ export const chatMachine = setup({
ChatLoadMoreHistory: {
target: "loadingMore",
},
ChatUnlockPrivateMessage: {
actions: "setUnlockingPrivateMessage",
target: "unlockingPrivate",
},
},
},
sendingViaHttp: {
@@ -503,23 +413,6 @@ export const chatMachine = setup({
},
},
},
unlockingPrivate: {
invoke: {
src: "unlockPrivateMessage",
input: ({ event }) => ({
messageId:
event.type === "ChatUnlockPrivateMessage" ? event.messageId : "",
}),
onDone: {
target: "ready",
actions: "applyUnlockPrivateOutput",
},
onError: {
target: "ready",
actions: "clearUnlockingPrivateMessage",
},
},
},
},
},
},