Added basic validation for requests

This commit is contained in:
Tom
2024-08-25 21:35:46 +00:00
parent 2d40d6fe09
commit 624b3fa63b
42 changed files with 608 additions and 492 deletions

View File

@ -6,49 +6,48 @@ import voices from "@/data/tts";
export async function GET(req: Request) {
try {
if (!voices) {
return new NextResponse("Voices not available.", { status: 500 });
return NextResponse.json({ message: 'Failed to load voices', error: null, value: null }, { status: 500 })
}
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
return NextResponse.json(user.ttsDefaultVoice);
} catch (error) {
console.log("[TTS/FILTER/DEFAULT]", error);
return new NextResponse("Internal Error", { status: 500 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}
export async function POST(req: Request) {
try {
if (!voices) {
return new NextResponse("Voices not available.", { status: 500 });
return NextResponse.json({ message: 'Failed to load voices', error: null, value: null }, { status: 500 })
}
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const { voice } = await req.json();
if (!voice || !voices.map(v => v.toLowerCase()).includes(voice.toLowerCase())) return new NextResponse("Bad Request", { status: 400 });
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())
await db.user.update({
where: {
id: user.id
},
data: {
ttsDefaultVoice: v
}
where: {
id: user.id
},
data: {
ttsDefaultVoice: v
}
});
return new NextResponse("", { status: 200 });
return NextResponse.json({ message: null, error: null, value: null }, { status: 200 })
} catch (error) {
console.log("[TTS/FILTER/DEFAULT]", error);
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
return new NextResponse("Internal Error", { status: 500 });
}

View File

@ -6,7 +6,7 @@ export async function GET(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const filters = await db.ttsUsernameFilter.findMany({
@ -18,7 +18,7 @@ export async function GET(req: Request) {
return NextResponse.json(filters);
} catch (error) {
console.log("[TTS/FILTER/USERS]", error);
return new NextResponse("Internal Error", { status: 500 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}
@ -26,12 +26,14 @@ export async function POST(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const { username, tag } = await req.json();
if (!username || username.length < 4 || username.length > 25) return new NextResponse("Bad Request", { status: 400 });
if (!tag || !["blacklisted", "priority"].includes(tag)) return new NextResponse("Bad Request", { status: 400 });
if (!username || username.length < 4 || username.length > 25)
return NextResponse.json({ message: 'Username does not exist.', error: null, value: null }, { status: 400 })
if (!tag || !["blacklisted", "priority"].includes(tag))
return NextResponse.json({ message: 'Tag does not exist.', error: null, value: null }, { status: 400 })
const filter = await db.ttsUsernameFilter.upsert({
where: {
@ -52,8 +54,7 @@ export async function POST(req: Request) {
return NextResponse.json(filter);
} catch (error) {
console.log("[TTS/FILTER/USERS]", error);
return new NextResponse("Internal Error", { status: 500 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}
@ -61,12 +62,13 @@ export async function DELETE(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const { searchParams } = new URL(req.url)
const username = searchParams.get('username') as string
if (!username || username.length < 4 || username.length > 25) return new NextResponse("Bad Request", { status: 400 });
if (!username || username.length < 4 || username.length > 25)
return NextResponse.json({ message: 'Username does not exist.', error: null, value: null }, { status: 400 })
const filter = await db.ttsUsernameFilter.delete({
where: {
@ -79,7 +81,6 @@ export async function DELETE(req: Request) {
return NextResponse.json(filter)
} catch (error) {
console.log("[TTS/FILTER/USERS]", error);
return new NextResponse("Internal Error", { status: 500 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}

View File

@ -6,7 +6,7 @@ export async function GET(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const filters = await db.ttsWordFilter.findMany({
@ -18,7 +18,7 @@ export async function GET(req: Request) {
return NextResponse.json(filters);
} catch (error) {
console.log("[TTS/FILTER/WORDS]", error);
return new NextResponse("Internal Error", { status: 500 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}
@ -26,97 +26,102 @@ export async function POST(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const { search, replace } = await req.json();
if (!search || search.length < 4 || search.length > 200) return new NextResponse("Bad Request", { status: 400 });
if (replace == null) return new NextResponse("Bad Request", { status: 400 });
if (!search || search.length < 3 || search.length > 127)
return NextResponse.json({ message: 'search must be between 3 to 127 characters.', error: null, value: null }, { status: 400 })
if (replace == null)
return NextResponse.json({ message: 'replace must not be null', error: null, value: null }, { status: 400 })
const filter = await db.ttsWordFilter.create({
data: {
search,
replace,
userId: user.id
}
data: {
search,
replace,
userId: user.id
}
});
return NextResponse.json(filter);
} catch (error) {
console.log("[TTS/FILTER/WORDS]", error);
return new NextResponse("Internal Error", { status: 500 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}
export async function PUT(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
}
const { id, search, replace } = await req.json();
if (!id || id.length < 1) return new NextResponse("Bad Request", { status: 400 });
if (!search || search.length < 4 || search.length > 200) return new NextResponse("Bad Request", { status: 400 });
if (replace == null) return new NextResponse("Bad Request", { status: 400 });
const filter = await db.ttsWordFilter.update({
where: {
id
},
data: {
search,
replace,
userId: user.id
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
});
return NextResponse.json(filter);
} catch (error) {
console.log("[TTS/FILTER/WORDS]", error);
return new NextResponse("Internal Error", { status: 500 });
}
const { id, search, replace } = await req.json();
if (!id || id.length < 1)
return NextResponse.json({ message: 'id does not exist.', error: null, value: null }, { status: 400 })
if (!search || search.length < 3 || search.length > 127)
return NextResponse.json({ message: 'search must be between 3 to 127 characters.', error: null, value: null }, { status: 400 })
if (replace == null)
return NextResponse.json({ message: 'replace must not be null.', error: null, value: null }, { status: 400 })
const filter = await db.ttsWordFilter.update({
where: {
id
},
data: {
search,
replace,
userId: user.id
}
});
return NextResponse.json(filter);
} catch (error) {
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}
export async function DELETE(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const { searchParams } = new URL(req.url)
const id = searchParams.get('id') as string
const search = searchParams.get('search') as string
if (!id && !search) return new NextResponse("Bad Request", { status: 400 });
if (search) {
if (search.length < 4 || search.length > 200) return new NextResponse("Bad Request", { status: 400 });
if (!search || search.length < 3 || search.length > 127)
return NextResponse.json({ message: 'search must be between 3 to 127 characters.', error: null, value: null }, { status: 400 })
const filter = await db.ttsWordFilter.delete({
where: {
userId_search: {
userId: user.id,
search
}
}
});
const filter = await db.ttsWordFilter.delete({
where: {
userId_search: {
userId: user.id,
search
}
}
});
return NextResponse.json(filter)
return NextResponse.json(filter)
} else if (id) {
if (id.length < 1) return new NextResponse("Bad Request", { status: 400 });
if (!id || id.length < 1)
return NextResponse.json({ message: 'id does not exist.', error: null, value: null }, { status: 400 })
const filter = await db.ttsWordFilter.delete({
where: {
id: id
}
});
const filter = await db.ttsWordFilter.delete({
where: {
id: id
}
});
return NextResponse.json(filter)
return NextResponse.json(filter)
}
return new NextResponse("Bad Request", { status: 400 });
return NextResponse.json({ message: 'Either id or search must not be null.', error: null, value: null }, { status: 400 })
} catch (error) {
console.log("[TTS/FILTER/WORDS]", error);
return new NextResponse("Internal Error" + error, { status: 500 });
return NextResponse.json({ message: 'Something went wrong.', error: null, value: null }, { status: 500 })
}
}

View File

@ -6,7 +6,7 @@ export async function GET(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const voiceStates = await db.ttsVoiceState.findMany({
@ -20,8 +20,7 @@ export async function GET(req: Request) {
return NextResponse.json(voiceStates.filter(v => v.state).map(v => voiceNamesMapped[v.ttsVoiceId]));
} catch (error) {
console.log("[TTS]", error);
return new NextResponse("Internal Error", { status: 500 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}
@ -29,7 +28,7 @@ export async function POST(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const { voice, state }: { voice: string, state: boolean } = await req.json();
@ -37,7 +36,7 @@ export async function POST(req: Request) {
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 });
return NextResponse.json({ message: 'Voice does not exist.', error: null, value: null }, { status: 400 });
}
await db.ttsVoiceState.upsert({
@ -57,9 +56,8 @@ export async function POST(req: Request) {
}
});
return new NextResponse("", { status: 200 });
return NextResponse.json({ message: null, error: null, value: null }, { status: 200 })
} catch (error) {
console.log("[TTS/FILTER/USER]", error);
return new NextResponse("Internal Error", { status: 500 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}

View File

@ -7,7 +7,7 @@ export async function GET(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { status: 401 });
}
const selected = await db.ttsChatVoice.findMany({
@ -22,8 +22,7 @@ export async function GET(req: Request) {
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 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}
@ -31,11 +30,12 @@ export async function POST(req: Request) {
try {
const user = await fetchUserWithImpersonation(req)
if (!user) {
return new NextResponse("Unauthorized", { status: 401 });
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 new NextResponse("Bad Request", { status: 400 });
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({
@ -44,7 +44,7 @@ export async function POST(req: Request) {
}
})
if (!voiceData)
return new NextResponse("Bad Request", { status: 400 });
return NextResponse.json({ message: 'Voice does not exist.', error: null, value: null }, { status: 400 })
await db.ttsChatVoice.upsert({
where: {
@ -63,9 +63,8 @@ export async function POST(req: Request) {
}
});
return new NextResponse("", { status: 200 });
return NextResponse.json({ message: null, error: null, value: null }, { status: 200 })
} catch (error) {
console.log("[TTS/SELECTED]", error);
return new NextResponse("Internal Error", { status: 500 });
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
}
}