fix(chat): apply unlocked voice audio url

This commit is contained in:
2026-07-10 11:15:59 +08:00
parent 9a54e61517
commit 8312d08653
12 changed files with 76 additions and 25 deletions
@@ -7,6 +7,7 @@ describe("UnlockPrivateResponse", () => {
const response = UnlockPrivateResponse.from({
unlocked: true,
content: "完整消息内容",
audioUrl: "https://example.com/unlocked-voice.mp3",
reason: "ok",
creditBalance: 9180,
creditsCharged: 10,
@@ -24,6 +25,7 @@ describe("UnlockPrivateResponse", () => {
expect(response.unlocked).toBe(true);
expect(response.content).toBe("完整消息内容");
expect(response.audioUrl).toBe("https://example.com/unlocked-voice.mp3");
expect(response.reason).toBe("ok");
expect(response.creditBalance).toBe(9180);
expect(response.creditsCharged).toBe(10);
@@ -66,6 +68,7 @@ describe("UnlockPrivateResponse", () => {
const response = UnlockPrivateResponse.from({
unlocked: false,
content: null,
audioUrl: null,
reason: "insufficient_balance",
lockDetail: {
locked: true,
@@ -78,6 +81,7 @@ describe("UnlockPrivateResponse", () => {
});
expect(response.content).toBe("");
expect(response.audioUrl).toBe("");
});
it("defaults nullable credit fields to zero", () => {
@@ -12,6 +12,7 @@ import type { ChatLockDetailData } from "@/data/schemas/chat";
export class UnlockPrivateResponse {
declare readonly unlocked: boolean;
declare readonly content: string;
declare readonly audioUrl: string;
declare readonly reason: UnlockPrivateReason;
declare readonly creditBalance: number;
declare readonly creditsCharged: number;
@@ -1,7 +1,7 @@
"use client";
import type { ChatLockDetailData } from "@/data/schemas/chat";
import { ChatMessage } from "@/data/dto/chat";
import type { UnlockedPrivateMessageLocalPatch } from "@/data/repositories/interfaces";
import { LocalChatStorage, LocalMessage } from "@/data/storage/chat";
import { Result } from "@/utils";
@@ -10,7 +10,7 @@ export class ChatLocalMessageStore {
async markMessageUnlocked(
messageId: string,
lockDetail?: ChatLockDetailData,
patch: UnlockedPrivateMessageLocalPatch = {},
): Promise<Result<void>> {
return Result.wrap(async () => {
const localResult = await this.getMessages();
@@ -24,7 +24,9 @@ export class ChatLocalMessageStore {
changed = true;
return ChatMessage.from({
...message.toJson(),
lockDetail: lockDetail ?? {
content: normalizeUnlockedContent(patch.content, message.content),
audioUrl: normalizeUnlockedAudioUrl(patch.audioUrl, message.audioUrl),
lockDetail: patch.lockDetail ?? {
...message.lockDetail,
locked: false,
showContent: true,
@@ -105,6 +107,22 @@ export class ChatLocalMessageStore {
}
}
function normalizeUnlockedContent(
nextContent: string | undefined,
currentContent: string,
): string {
if (nextContent == null || nextContent.length === 0) return currentContent;
return nextContent;
}
function normalizeUnlockedAudioUrl(
nextAudioUrl: string | null | undefined,
currentAudioUrl: string | null,
): string | null {
if (nextAudioUrl == null || nextAudioUrl.length === 0) return currentAudioUrl;
return nextAudioUrl;
}
function shouldMarkMessageUnlocked(
message: ChatMessage,
messageId: string,
+3 -3
View File
@@ -7,7 +7,6 @@ import type {
UnlockHistoryResponse,
UnlockPrivateResponse,
} from "@/data/dto/chat";
import type { ChatLockDetailData } from "@/data/schemas/chat";
import { chatApi } from "@/data/services/api";
import {
LocalChatMediaStorage,
@@ -18,6 +17,7 @@ import type {
CacheRemoteChatMediaInput,
ChatMediaLookupInput,
IChatRepository,
UnlockedPrivateMessageLocalPatch,
} from "@/data/repositories/interfaces";
import type { Result } from "@/utils";
@@ -66,11 +66,11 @@ export class ChatRepository implements IChatRepository {
/** 把本地缓存中的单条锁定消息标记为已解锁。 */
async markPrivateMessageUnlockedInLocal(
messageId: string,
lockDetail?: ChatLockDetailData,
patch?: UnlockedPrivateMessageLocalPatch,
): Promise<Result<void>> {
return this.localMessages.markMessageUnlocked(
messageId,
lockDetail,
patch,
);
}
@@ -32,6 +32,12 @@ export interface CacheRemoteChatMediaInput extends ChatMediaLookupInput {
remoteUrl: string;
}
export interface UnlockedPrivateMessageLocalPatch {
lockDetail?: ChatLockDetailData;
audioUrl?: string | null;
content?: string;
}
export interface IChatRepository {
/** 发送一条消息。 */
sendMessage(
@@ -51,7 +57,7 @@ export interface IChatRepository {
/** 把本地缓存中的单条锁定消息标记为已解锁。 */
markPrivateMessageUnlockedInLocal(
messageId: string,
lockDetail?: ChatLockDetailData,
patch?: UnlockedPrivateMessageLocalPatch,
): Promise<Result<void>>;
/** 把一条消息写入本地存储。 */
@@ -11,6 +11,7 @@ export const UnlockPrivateReasonSchema = stringOrEmpty;
export const UnlockPrivateResponseSchema = z.object({
unlocked: booleanOrFalse,
content: stringOrEmpty,
audioUrl: stringOrEmpty,
reason: UnlockPrivateReasonSchema,
creditBalance: numberOrZero,
creditsCharged: numberOrZero,