2024-01-04 02:14:11 -05:00
|
|
|
import { db } from "@/lib/db"
|
|
|
|
import { NextResponse } from "next/server";
|
2024-01-04 16:57:32 -05:00
|
|
|
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
2024-01-04 02:14:11 -05:00
|
|
|
|
|
|
|
export async function GET(req: Request) {
|
|
|
|
try {
|
2024-01-04 16:57:32 -05:00
|
|
|
const user = await fetchUserWithImpersonation(req)
|
2024-01-04 02:14:11 -05:00
|
|
|
if (!user) {
|
|
|
|
return new NextResponse("Unauthorized", { status: 401 });
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
const voiceStates = await db.ttsVoiceState.findMany({
|
|
|
|
where: {
|
|
|
|
userId: user.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const voiceNames = await db.ttsVoice.findMany();
|
|
|
|
const voiceNamesMapped: { [id: string]: string } = Object.assign({}, ...voiceNames.map(v => ({ [v.id]: v.name })))
|
2024-01-04 02:14:11 -05:00
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
return NextResponse.json(voiceStates.filter(v => v.state).map(v => voiceNamesMapped[v.ttsVoiceId]));
|
2024-01-04 02:14:11 -05:00
|
|
|
} catch (error) {
|
2024-06-24 18:16:55 -04:00
|
|
|
console.log("[TTS]", error);
|
2024-01-04 02:14:11 -05:00
|
|
|
return new NextResponse("Internal Error", { status: 500 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function POST(req: Request) {
|
|
|
|
try {
|
2024-01-04 16:57:32 -05:00
|
|
|
const user = await fetchUserWithImpersonation(req)
|
2024-01-04 02:14:11 -05:00
|
|
|
if (!user) {
|
|
|
|
return new NextResponse("Unauthorized", { status: 401 });
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
const { voice, state }: { voice: string, state: boolean } = await req.json();
|
|
|
|
|
|
|
|
const voiceIds = await db.ttsVoice.findMany();
|
|
|
|
const voiceIdsMapped: { [voice: string]: string } = Object.assign({}, ...voiceIds.map(v => ({ [v.name.toLowerCase()]: v.id })));
|
|
|
|
if (!voiceIdsMapped[voice.toLowerCase()]) {
|
|
|
|
return new NextResponse("Bad Request", { status: 400 });
|
|
|
|
}
|
2024-01-04 02:14:11 -05:00
|
|
|
|
2024-06-24 18:16:55 -04:00
|
|
|
await db.ttsVoiceState.upsert({
|
|
|
|
where: {
|
|
|
|
userId_ttsVoiceId: {
|
|
|
|
userId: user.id,
|
|
|
|
ttsVoiceId: voiceIdsMapped[voice.toLowerCase()]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
state
|
|
|
|
},
|
|
|
|
create: {
|
|
|
|
userId: user.id,
|
|
|
|
ttsVoiceId: voiceIdsMapped[voice.toLowerCase()],
|
|
|
|
state
|
|
|
|
}
|
2024-01-04 02:14:11 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
return new NextResponse("", { status: 200 });
|
|
|
|
} catch (error) {
|
|
|
|
console.log("[TTS/FILTER/USER]", error);
|
|
|
|
return new NextResponse("Internal Error", { status: 500 });
|
|
|
|
}
|
|
|
|
}
|