70 lines
2.5 KiB
TypeScript
70 lines
2.5 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 NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { 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) {
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
const user = await fetchUserWithImpersonation(req)
|
|
if (!user) {
|
|
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
|
|
}
|
|
|
|
const { voice, chatterId }: { voice: string, chatterId: number } = await req.json();
|
|
if (!voice || !voices.map(v => v.toLowerCase()).includes(voice.toLowerCase()))
|
|
return NextResponse.json({ message: 'Voice does not exist.', error: null, value: null }, { status: 400 })
|
|
|
|
const v = voices.find(v => v.toLowerCase() == voice.toLowerCase())
|
|
const voiceData = await db.ttsVoice.findFirst({
|
|
where: {
|
|
name: v
|
|
}
|
|
})
|
|
if (!voiceData)
|
|
return NextResponse.json({ message: 'Voice does not exist.', error: null, value: null }, { 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 NextResponse.json({ message: null, error: null, value: null }, { status: 200 })
|
|
} catch (error) {
|
|
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
|
}
|
|
} |