hermes-web/app/api/settings/tts/selected/route.ts

71 lines
2.3 KiB
TypeScript

import { db } from "@/lib/db"
import { NextResponse } from "next/server";
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
import voices from "@/data/tts";
export async function GET(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
}
const selected = await db.ttsChatVoice.findMany({
where: {
userId: user.id
}
})
const voices = await db.ttsVoice.findMany();
const voiceNamesMapped: { [id: string]: string } = Object.assign({}, ...voices.map(v => ({ [v.id]: v.name })))
const data = selected.map(s => ({ chatter_id: new Number(s.chatterId), voice: voiceNamesMapped[s.ttsVoiceId] }))
return NextResponse.json(data);
} catch (error) {
console.log("[TTS/SELECTED]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
export async function POST(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
}
const { voice, chatterId }: { voice: string, chatterId: number } = await req.json();
if (!voice || !voices.map(v => v.toLowerCase()).includes(voice.toLowerCase())) return new NextResponse("Bad Request", { status: 400 });
const v = voices.find(v => v.toLowerCase() == voice.toLowerCase())
const voiceData = await db.ttsVoice.findFirst({
where: {
name: v
}
})
if (!voiceData)
return new NextResponse("Bad Request", { status: 400 });
await db.ttsChatVoice.upsert({
where: {
userId_chatterId: {
userId: user.id,
chatterId
}
},
create: {
userId: user.id,
chatterId,
ttsVoiceId: voiceData.id
},
update: {
ttsVoiceId: voiceData.id
}
});
return new NextResponse("", { status: 200 });
} catch (error) {
console.log("[TTS/SELECTED]", error);
return new NextResponse("Internal Error", { status: 500 });
}
}