refactor(ai-moderation): migrate sticker cache SQL queries from ? to $N parameter placeholders
This commit is contained in:
@@ -75,7 +75,7 @@ export async function getStickerFromCache(
|
||||
const key = sanitizeKey(stickerName);
|
||||
try {
|
||||
const row = await executeGet(
|
||||
"SELECT base64, mime_type, size, fetched_at FROM sticker_cache WHERE name = ? AND fetched_at > ?",
|
||||
"SELECT base64, mime_type, size, fetched_at FROM sticker_cache WHERE name = $1 AND fetched_at > $2",
|
||||
[key, Date.now() - TTL_MS],
|
||||
);
|
||||
if (!row) return null;
|
||||
@@ -110,7 +110,7 @@ export async function setStickerInCache(
|
||||
await evictIfNeeded(size);
|
||||
await executeAll(
|
||||
`INSERT INTO sticker_cache (name, base64, mime_type, size, fetched_at)
|
||||
VALUES (?, ?, ?, ?, ?)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (name) DO UPDATE SET
|
||||
base64 = EXCLUDED.base64,
|
||||
mime_type = EXCLUDED.mime_type,
|
||||
|
||||
@@ -269,6 +269,7 @@ export function makeUserModerationCacheKey(
|
||||
export async function getCachedUserModeration(
|
||||
cacheKey: string,
|
||||
): Promise<{
|
||||
status: "clean" | "flagged";
|
||||
flags: string[];
|
||||
score: number;
|
||||
analysis: string;
|
||||
@@ -288,9 +289,19 @@ export async function getCachedUserModeration(
|
||||
if (!row) return null;
|
||||
|
||||
const parsed = JSON.parse(row.flags) as Record<string, unknown>;
|
||||
const flags = (parsed.flags as string[]) ?? [];
|
||||
// Use stored status if available (new entries), otherwise derive from flags (legacy compatibility)
|
||||
const storedStatus = parsed.status as string | undefined;
|
||||
const status: "clean" | "flagged" =
|
||||
storedStatus === "clean" || storedStatus === "flagged"
|
||||
? storedStatus
|
||||
: flags.length === 0
|
||||
? "clean"
|
||||
: "flagged";
|
||||
|
||||
return {
|
||||
flags: (parsed.flags as string[]) ?? [],
|
||||
status,
|
||||
flags,
|
||||
score: (parsed.score as number) ?? 0,
|
||||
analysis: (parsed.analysis as string) ?? "",
|
||||
categories: (parsed.categories as string[]) ?? [],
|
||||
|
||||
Reference in New Issue
Block a user