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

70 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

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) {
2024-08-25 17:35:46 -04:00
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) {
2024-08-25 17:35:46 -04:00
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) {
2024-08-25 17:35:46 -04:00
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const { voice, chatterId }: { voice: string, chatterId: number } = await req.json();
2024-08-25 17:35:46 -04:00
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)
2024-08-25 17:35:46 -04:00
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
}
});
2024-08-25 17:35:46 -04:00
return NextResponse.json({ message: null, error: null, value: null }, { status: 200 })
} catch (error) {
2024-08-25 17:35:46 -04:00
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}