refactor(chat): simplify paid message lock state
This commit is contained in:
@@ -15,11 +15,7 @@ describe("UnlockPrivateResponse", () => {
|
||||
shortfallCredits: 0,
|
||||
lockDetail: {
|
||||
locked: false,
|
||||
showContent: true,
|
||||
showUpgrade: false,
|
||||
reason: null,
|
||||
hint: null,
|
||||
detail: null,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -49,8 +45,7 @@ describe("UnlockPrivateResponse", () => {
|
||||
},
|
||||
lockDetail: {
|
||||
locked: false,
|
||||
showContent: true,
|
||||
showUpgrade: false,
|
||||
reason: null,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -73,13 +68,7 @@ describe("UnlockPrivateResponse", () => {
|
||||
shortfallCredits: 5,
|
||||
lockDetail: {
|
||||
locked: true,
|
||||
showContent: false,
|
||||
showUpgrade: true,
|
||||
reason: "insufficient_balance",
|
||||
hint: "Insufficient credits.",
|
||||
detail: {
|
||||
messageId: "msg_001",
|
||||
},
|
||||
reason: "voice_message",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -89,7 +78,10 @@ describe("UnlockPrivateResponse", () => {
|
||||
expect(response.creditsCharged).toBe(0);
|
||||
expect(response.requiredCredits).toBe(10);
|
||||
expect(response.shortfallCredits).toBe(5);
|
||||
expect(response.lockDetail.showUpgrade).toBe(true);
|
||||
expect(response.lockDetail).toEqual({
|
||||
locked: true,
|
||||
reason: "voice_message",
|
||||
});
|
||||
});
|
||||
|
||||
it("defaults nullable content to an empty string", () => {
|
||||
@@ -100,11 +92,7 @@ describe("UnlockPrivateResponse", () => {
|
||||
reason: "insufficient_balance",
|
||||
lockDetail: {
|
||||
locked: true,
|
||||
showContent: false,
|
||||
showUpgrade: true,
|
||||
reason: "insufficient_balance",
|
||||
hint: null,
|
||||
detail: null,
|
||||
reason: "private_message",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -123,11 +111,7 @@ describe("UnlockPrivateResponse", () => {
|
||||
shortfallCredits: null,
|
||||
lockDetail: {
|
||||
locked: true,
|
||||
showContent: false,
|
||||
showUpgrade: true,
|
||||
reason: "insufficient_balance",
|
||||
hint: null,
|
||||
detail: null,
|
||||
reason: "image_paywall",
|
||||
},
|
||||
});
|
||||
|
||||
@@ -147,11 +131,26 @@ describe("UnlockPrivateResponse", () => {
|
||||
|
||||
expect(response.lockDetail).toEqual({
|
||||
locked: false,
|
||||
showContent: true,
|
||||
showUpgrade: false,
|
||||
reason: null,
|
||||
hint: null,
|
||||
detail: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("strips legacy lock detail fields while preserving lock state and type", () => {
|
||||
const response = UnlockPrivateResponse.fromJson({
|
||||
unlocked: false,
|
||||
lockDetail: {
|
||||
locked: true,
|
||||
reason: "private_message",
|
||||
showContent: false,
|
||||
showUpgrade: true,
|
||||
hint: "Legacy hint",
|
||||
detail: { messageId: "legacy-message" },
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.lockDetail).toEqual({
|
||||
locked: true,
|
||||
reason: "private_message",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,7 +17,6 @@ export class ChatSendResponse {
|
||||
declare readonly image: ChatImageData;
|
||||
declare readonly lockDetail: ChatLockDetailData;
|
||||
declare readonly canSendMessage: boolean;
|
||||
// declare readonly cannotSendReason: string | null;
|
||||
declare readonly creditBalance: number;
|
||||
declare readonly creditsCharged: number;
|
||||
declare readonly requiredCredits: number;
|
||||
|
||||
@@ -23,11 +23,7 @@ function makeMessage(
|
||||
image: { type: null, url: null },
|
||||
lockDetail: {
|
||||
locked: false,
|
||||
showContent: true,
|
||||
showUpgrade: false,
|
||||
reason: null,
|
||||
hint: null,
|
||||
detail: null,
|
||||
},
|
||||
...overrides,
|
||||
});
|
||||
@@ -45,11 +41,7 @@ function makeResponse(
|
||||
image: { type: null, url: null },
|
||||
lockDetail: {
|
||||
locked: false,
|
||||
showContent: true,
|
||||
showUpgrade: false,
|
||||
reason: null,
|
||||
hint: null,
|
||||
detail: null,
|
||||
},
|
||||
...overrides,
|
||||
});
|
||||
|
||||
@@ -28,13 +28,8 @@ export class ChatLocalMessageStore {
|
||||
audioUrl: normalizeUnlockedAudioUrl(patch.audioUrl, message.audioUrl),
|
||||
image: patch.image ?? message.image,
|
||||
lockDetail: patch.lockDetail ?? {
|
||||
...message.lockDetail,
|
||||
locked: false,
|
||||
showContent: true,
|
||||
showUpgrade: false,
|
||||
reason: null,
|
||||
hint: null,
|
||||
detail: null,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,21 +5,15 @@ import { z } from "zod";
|
||||
|
||||
import {
|
||||
booleanOrFalse,
|
||||
booleanOrTrue,
|
||||
schemaOr,
|
||||
stringOrNull,
|
||||
unknownRecordOrNull,
|
||||
} from "../nullable-defaults";
|
||||
|
||||
const CHAT_IMAGE_DEFAULTS = { type: null, url: null } as const;
|
||||
|
||||
const CHAT_LOCK_DETAIL_DEFAULTS = {
|
||||
locked: false,
|
||||
showContent: true,
|
||||
showUpgrade: false,
|
||||
reason: null,
|
||||
hint: null,
|
||||
detail: null,
|
||||
} as const;
|
||||
|
||||
export const ChatImageSchema = schemaOr(
|
||||
@@ -33,11 +27,7 @@ export const ChatImageSchema = schemaOr(
|
||||
export const ChatLockDetailSchema = schemaOr(
|
||||
z.object({
|
||||
locked: booleanOrFalse,
|
||||
showContent: booleanOrTrue,
|
||||
showUpgrade: booleanOrFalse,
|
||||
reason: stringOrNull,
|
||||
hint: stringOrNull,
|
||||
detail: unknownRecordOrNull,
|
||||
}),
|
||||
CHAT_LOCK_DETAIL_DEFAULTS,
|
||||
);
|
||||
|
||||
@@ -9,7 +9,6 @@ import {
|
||||
numberOrLazy,
|
||||
numberOrZero,
|
||||
stringOrEmpty,
|
||||
stringOrNull,
|
||||
} from "../nullable-defaults";
|
||||
import { ChatImageSchema, ChatLockDetailSchema } from "./chat_payloads";
|
||||
|
||||
@@ -23,7 +22,6 @@ export const ChatSendResponseSchema = z.object({
|
||||
image: ChatImageSchema,
|
||||
lockDetail: ChatLockDetailSchema,
|
||||
canSendMessage: booleanOrTrue,
|
||||
cannotSendReason: stringOrNull,
|
||||
creditBalance: numberOrZero,
|
||||
creditsCharged: numberOrZero,
|
||||
requiredCredits: numberOrZero,
|
||||
|
||||
Reference in New Issue
Block a user