Added basic validation for requests
This commit is contained in:
@@ -6,7 +6,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 connection = await db.twitchConnection.deleteMany({
|
||||
@@ -18,6 +18,6 @@ export async function POST(req: Request) {
|
||||
return NextResponse.json(connection);
|
||||
} catch (error) {
|
||||
console.log("[CONNECTION/TWITCH]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -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 { searchParams } = new URL(req.url)
|
||||
@@ -27,6 +27,6 @@ export async function GET(req: Request) {
|
||||
return NextResponse.json(connection);
|
||||
} catch (error) {
|
||||
console.log("[CONNECTION/TWITCH]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,13 @@ import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
||||
import axios from "axios";
|
||||
import { env } from "process";
|
||||
import { TwitchUpdateAuthorization } from "@/lib/twitch";
|
||||
import { z } from "zod"
|
||||
|
||||
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 { searchParams } = new URL(req.url)
|
||||
const groupId = searchParams.get('groupId') as string
|
||||
@@ -17,7 +18,7 @@ export async function GET(req: Request) {
|
||||
const search = searchParams.get('search') as string
|
||||
|
||||
if (!groupId && search != 'all')
|
||||
return new NextResponse("Bad Request", { status: 400 })
|
||||
return NextResponse.json({ message: 'Something went wrong', error: null, value: null }, { status: 400 })
|
||||
|
||||
let page = parseInt(pageString)
|
||||
if (isNaN(page) || page === undefined || page === null)
|
||||
@@ -40,18 +41,15 @@ export async function GET(req: Request) {
|
||||
})
|
||||
|
||||
const paginated = search == 'all' ? chatters : chatters.slice(page * 50, (page + 1) * 50)
|
||||
if (!paginated || paginated.length == 0) {
|
||||
console.log('No users returned from db')
|
||||
if (!paginated || paginated.length == 0)
|
||||
return NextResponse.json([])
|
||||
}
|
||||
|
||||
const ids = chatters.map(c => c.chatterId)
|
||||
const idsString = 'id=' + ids.map(i => i.toString()).reduce((a, b) => a + '&id=' + b)
|
||||
|
||||
const auth = await TwitchUpdateAuthorization(user.id)
|
||||
if (!auth) {
|
||||
return new NextResponse("", { status: 403 })
|
||||
}
|
||||
if (!auth)
|
||||
return NextResponse.json({ message: 'Unauthorized', error: null, value: null }, { status: 403 })
|
||||
|
||||
const users = await axios.get("https://api.twitch.tv/helix/users?" + idsString, {
|
||||
headers: {
|
||||
@@ -60,31 +58,38 @@ export async function GET(req: Request) {
|
||||
}
|
||||
})
|
||||
|
||||
if (!users) {
|
||||
return new NextResponse("", { status: 400 })
|
||||
}
|
||||
if (!users)
|
||||
return NextResponse.json({ message: 'No users found', error: null, value: null }, { status: 400 })
|
||||
|
||||
if (users.data.data.length == 0) {
|
||||
console.log('No users returned from twitch')
|
||||
if (users.data.data.length == 0)
|
||||
return NextResponse.json([])
|
||||
}
|
||||
|
||||
return NextResponse.json(users.data.data.map((u: any) => ({ id: u.id, username: u.login })));
|
||||
} catch (error) {
|
||||
console.log("[GROUPS/USERS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Failed to get', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
const groupIdSchema = z.string({
|
||||
required_error: "Group ID should be available.",
|
||||
invalid_type_error: "Group ID must be a string"
|
||||
}).regex(/^[\w\-\=]{1,32}$/, "Group ID must contain only letters, numbers, dashes, underscores & equal signs.")
|
||||
|
||||
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 { groupId, users }: { groupId: string, users: { id: number, username: string }[] } = await req.json();
|
||||
if (!groupId || !users)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
if (!groupId)
|
||||
return NextResponse.json({ message: 'groupId must be set.', error: null, value: null }, { status: 400 });
|
||||
if (!users)
|
||||
return NextResponse.json({ message: 'users must be set.', error: null, value: null }, { status: 400 });
|
||||
|
||||
const groupIdValidation = await groupIdSchema.safeParseAsync(groupId)
|
||||
if (!groupIdValidation.success)
|
||||
return NextResponse.json({ message: 'groupId does not meet requirements.', error: JSON.parse(groupIdValidation.error['message'])[0], value: null }, { status: 400 })
|
||||
|
||||
const chatters = await db.chatterGroup.createMany({
|
||||
data: users.map(u => ({ userId: user.id, chatterId: u.id, groupId, chatterLabel: u.username }))
|
||||
@@ -92,8 +97,7 @@ export async function POST(req: Request) {
|
||||
|
||||
return NextResponse.json(chatters, { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[GROUPS/USERS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Failed to create', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,13 +105,20 @@ 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 groupId = searchParams.get('groupId') as string
|
||||
const ids = searchParams.get('ids') as string
|
||||
if (!groupId || !ids)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
if (!groupId)
|
||||
return NextResponse.json({ message: 'groupId must be set.', error: null, value: null }, { status: 400 });
|
||||
|
||||
if (!ids)
|
||||
return NextResponse.json({ message: 'ids must be set.', error: null, value: null }, { status: 400 });
|
||||
|
||||
const groupIdValidation = await groupIdSchema.safeParseAsync(groupId)
|
||||
if (!groupIdValidation.success)
|
||||
return NextResponse.json({ message: 'groupId does not meet requirements.', error: JSON.parse(groupIdValidation.error['message'])[0], value: null }, { status: 400 })
|
||||
|
||||
const chatters = await db.chatterGroup.deleteMany({
|
||||
where: {
|
||||
@@ -121,7 +132,6 @@ export async function DELETE(req: Request) {
|
||||
|
||||
return NextResponse.json(chatters);
|
||||
} catch (error) {
|
||||
console.log("[GROUPS/USERS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Failed to delete.', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
import { db } from "@/lib/db"
|
||||
import { NextResponse } from "next/server";
|
||||
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
||||
import { ActionType, Prisma } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
|
||||
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 commands = await db.groupPermission.findMany({
|
||||
where: {
|
||||
@@ -17,20 +17,31 @@ export async function GET(req: Request) {
|
||||
|
||||
return NextResponse.json(commands.map(({userId, ...attrs}) => attrs));
|
||||
} catch (error) {
|
||||
console.log("[GROUPS/PERMISSIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
const permissionPathSchema = z.string({
|
||||
required_error: "Permission path should be available.",
|
||||
invalid_type_error: "Permission path must be a string"
|
||||
}).regex(/^[\w\-\.]{1,64}$/, "Permission path must contain only letters, numbers, dashes, periods.")
|
||||
|
||||
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 { path, allow, groupId }: { path: string, allow: boolean, groupId: string } = await req.json();
|
||||
if (!path)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: 'path does not exist.', error: null, value: null }, { status: 400 });
|
||||
const permissionPathValidation = permissionPathSchema.safeParse(path)
|
||||
if (!permissionPathValidation.success)
|
||||
return NextResponse.json({ message: 'path must meet certain requirements.', error: JSON.parse(permissionPathValidation.error['message'])[0], value: null }, { status: 400 });
|
||||
if (!groupId)
|
||||
return NextResponse.json({ message: 'groupId does not exist.', error: null, value: null }, { status: 400 });
|
||||
if (groupId.length > 64)
|
||||
return NextResponse.json({ message: 'groupId is too long.', error: null, value: null }, { status: 400 });
|
||||
|
||||
const permission = await db.groupPermission.create({
|
||||
data: {
|
||||
@@ -43,8 +54,7 @@ export async function POST(req: Request) {
|
||||
|
||||
return NextResponse.json(permission, { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[GROUPS/PERMISSIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,30 +62,30 @@ export async function PUT(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 { id, path, allow }: { id: string, path: string, allow: boolean|null } = await req.json();
|
||||
if (!id)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: 'id does not exist.', error: null, value: null }, { status: 400 });
|
||||
if (!path)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
|
||||
let data: any = {}
|
||||
if (!!path)
|
||||
data = { ...data, path }
|
||||
data = { ...data, allow }
|
||||
return NextResponse.json({ message: 'path does not exist.', error: null, value: null }, { status: 400 });
|
||||
const permissionPathValidation = permissionPathSchema.safeParse(path)
|
||||
if (!permissionPathValidation.success)
|
||||
return NextResponse.json({ message: 'path must meet certain requirements.', error: JSON.parse(permissionPathValidation.error['message'])[0], value: null }, { status: 400 });
|
||||
|
||||
const permission = await db.groupPermission.update({
|
||||
where: {
|
||||
id
|
||||
},
|
||||
data: data
|
||||
data: {
|
||||
path,
|
||||
allow
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json(permission, { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[GROUPS/PERMISSIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +93,7 @@ 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
|
||||
@@ -95,7 +105,6 @@ export async function DELETE(req: Request) {
|
||||
|
||||
return NextResponse.json(permission);
|
||||
} catch (error) {
|
||||
console.log("[GROUPS/PERMISSIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,13 @@
|
||||
import { db } from "@/lib/db"
|
||||
import { NextResponse } from "next/server";
|
||||
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
||||
import { ActionType, Prisma } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
|
||||
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 actions = await db.group.findMany({
|
||||
@@ -16,23 +16,30 @@ export async function GET(req: Request) {
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(actions.map(({userId, ...attrs}) => attrs));
|
||||
return NextResponse.json(actions.map(({ userId, ...attrs }) => attrs));
|
||||
} catch (error) {
|
||||
console.log("[GROUPS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
const groupNameSchema = z.string({
|
||||
required_error: "Group name is required.",
|
||||
invalid_type_error: "Group name must be a string"
|
||||
}).regex(/^[\w\-\s]{1,20}$/, "Group name must contain only letters, numbers, spaces, dashes, and underscores.")
|
||||
|
||||
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 { name, priority }: { name: string, priority: number } = await req.json();
|
||||
if (!name)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: 'name does not exist.', error: null, value: null }, { status: 400 });
|
||||
const groupNameValidation = await groupNameSchema.safeParseAsync(name)
|
||||
if (!groupNameValidation.success)
|
||||
return NextResponse.json({ message: 'name does not meet requirements.', error: JSON.parse(groupNameValidation.error['message'])[0], value: null }, { status: 400 })
|
||||
|
||||
const group = await db.group.create({
|
||||
data: {
|
||||
@@ -44,8 +51,7 @@ export async function POST(req: Request) {
|
||||
|
||||
return NextResponse.json(group, { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[GROUPS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,19 +59,24 @@ export async function PUT(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 { id, name, priority }: { id: string, name: string, priority: number } = await req.json();
|
||||
if (!id)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: 'id does not exist.', error: null, value: null }, { status: 400 });
|
||||
if (!name && !priority)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: 'Either name or priority must not be null.', error: null, value: null }, { status: 400 });
|
||||
if (name) {
|
||||
const groupNameValidation = await groupNameSchema.safeParseAsync(name)
|
||||
if (!groupNameValidation.success)
|
||||
return NextResponse.json({ message: 'name does not meet requirements.', error: JSON.parse(groupNameValidation.error['message'])[0], value: null }, { status: 400 })
|
||||
}
|
||||
|
||||
let data: any = {}
|
||||
if (!!name)
|
||||
if (name)
|
||||
data = { ...data, name: name.toLowerCase() }
|
||||
if (!!priority)
|
||||
if (priority)
|
||||
data = { ...data, priority }
|
||||
|
||||
const group = await db.group.update({
|
||||
@@ -77,8 +88,7 @@ export async function PUT(req: Request) {
|
||||
|
||||
return NextResponse.json(group, { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[GROUPS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,7 +96,7 @@ 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)
|
||||
@@ -99,7 +109,6 @@ export async function DELETE(req: Request) {
|
||||
|
||||
return NextResponse.json(group);
|
||||
} catch (error) {
|
||||
console.log("[GROUPS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -8,13 +8,13 @@ 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 { searchParams } = new URL(req.url)
|
||||
const logins = (searchParams.get('logins') as string)?.split(',').reduce((a,b) => a + ',&login=' + b)
|
||||
const ids = (searchParams.get('ids') as string)?.split(',').reduce((a,b) => a + ',&id=' + b)
|
||||
if (!logins && !ids) {
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: 'Either logins or ids must not be null.', error: null, value: null }, { status: 400 });
|
||||
}
|
||||
|
||||
let suffix = ""
|
||||
@@ -27,7 +27,7 @@ export async function GET(req: Request) {
|
||||
|
||||
const auth = await TwitchUpdateAuthorization(user.id)
|
||||
if (!auth) {
|
||||
return new NextResponse("", { status: 403 })
|
||||
return NextResponse.json({ message: 'You do not have permissions for this.', error: null, value: null }, { status: 403 });
|
||||
}
|
||||
|
||||
console.log('TWITCH URL:', 'https://api.twitch.tv/helix/users' + suffix)
|
||||
@@ -39,17 +39,11 @@ export async function GET(req: Request) {
|
||||
}
|
||||
})
|
||||
|
||||
if (!users || !users.data) {
|
||||
return new NextResponse("", { status: 400 })
|
||||
}
|
||||
if (!users?.data?.data)
|
||||
return NextResponse.json([], { status: 200 });
|
||||
|
||||
if (users.data.data.length == 0) {
|
||||
return NextResponse.json([])
|
||||
}
|
||||
|
||||
return NextResponse.json(users.data.data.map((u: any) => ({ id: u.id, username: u.login })));
|
||||
return NextResponse.json(users.data.data.map((u: any) => ({ id: u.id, username: u.login })), { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[GROUPS/USERS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,31 @@
|
||||
import { db } from "@/lib/db"
|
||||
import { NextResponse } from "next/server";
|
||||
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
||||
import axios from "axios";
|
||||
import { env } from "process";
|
||||
import { TwitchUpdateAuthorization } from "@/lib/twitch";
|
||||
import { z } from "zod";
|
||||
|
||||
const groupIdSchema = z.string({
|
||||
required_error: "Group ID should be available.",
|
||||
invalid_type_error: "Group ID must be a string"
|
||||
}).regex(/^[\w\-\=]{1,32}$/, "Group ID must contain only letters, numbers, dashes, underscores & equal signs.")
|
||||
|
||||
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 { searchParams } = new URL(req.url)
|
||||
const groupId = searchParams.get('groupId') as string
|
||||
|
||||
if (groupId) {
|
||||
const groupIdValidation = await groupIdSchema.safeParseAsync(groupId)
|
||||
if (!groupIdValidation.success)
|
||||
return NextResponse.json({ message: 'groupId does not meet requirements.', error: JSON.parse(groupIdValidation.error['message'])[0], value: null }, { status: 400 })
|
||||
}
|
||||
|
||||
let chatters: { userId: string, groupId: string, chatterId: bigint, chatterLabel: string }[]
|
||||
|
||||
if (!!groupId)
|
||||
if (groupId)
|
||||
chatters = await db.chatterGroup.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
@@ -31,10 +40,8 @@ export async function GET(req: Request) {
|
||||
})
|
||||
|
||||
return NextResponse.json(chatters.map(u => ({ ...u, chatterId: Number(u.chatterId) }))
|
||||
.map(({userId, chatterLabel, ...attrs}) => attrs))
|
||||
|
||||
.map(({ userId, chatterLabel, ...attrs }) => attrs))
|
||||
} catch (error) {
|
||||
console.log("[GROUPS/USERS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Failed to get groups', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,13 @@ import { db } from "@/lib/db"
|
||||
import { NextResponse } from "next/server";
|
||||
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
||||
import { ActionType, Prisma } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
|
||||
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 actions = await db.action.findMany({
|
||||
@@ -17,17 +18,21 @@ export async function GET(req: Request) {
|
||||
})
|
||||
|
||||
return NextResponse.json(actions.map(({ userId, ...attrs }) => attrs));
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS/ACTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
} catch (error: any) {
|
||||
return NextResponse.json({ message: null, error: error, value: null }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
const nameSchema = z.string({
|
||||
required_error: "Name is required.",
|
||||
invalid_type_error: "Name must be a string"
|
||||
}).regex(/^[\w\-\s]{1,32}$/, "Name must contain only letters, numbers, spaces, dashes, and underscores.")
|
||||
|
||||
async function common(req: Request, action: (id: string, name: string, type: ActionType, data: any) => void) {
|
||||
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 { name, type, scene_name, scene_item_name, rotation, position_x, position_y, file_path, file_content, tts_voice, obs_visible, obs_index, sleep, oauth_name, oauth_type }:
|
||||
@@ -35,16 +40,21 @@ async function common(req: Request, action: (id: string, name: string, type: Act
|
||||
name: string, type: ActionType, scene_name: string, scene_item_name: string, rotation: string, position_x: string, position_y: string, file_path: string, file_content: string, tts_voice: string, obs_visible: boolean, obs_index: number, sleep: number,
|
||||
oauth_name: string, oauth_type: string
|
||||
} = await req.json();
|
||||
if (!name && !type)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
if (!name)
|
||||
return NextResponse.json({ message: 'name is required.', error: null, value: null }, { status: 400 });
|
||||
const nameValidation = nameSchema.safeParse(name)
|
||||
if (!nameValidation.success)
|
||||
return NextResponse.json({ message: 'name must follow some requirements.', error: nameValidation.error, value: null }, { status: 400 });
|
||||
if (!type)
|
||||
return NextResponse.json({ message: 'type is required', error: null, value: null }, { status: 400 });
|
||||
if (type == ActionType.OBS_TRANSFORM && (!scene_name || !scene_item_name || !rotation && !position_x && !position_y))
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: '"scene_name", "scene_item_name" and one of "rotation", "position_x", "position_y" are required.', error: null, value: null }, { status: 400 });
|
||||
if ((type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) && (!file_path || !file_content))
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: '"scene_name", "scene_item_name", "file_path" & "file_content" are required.', error: null, value: null }, { status: 400 });
|
||||
if (type == ActionType.AUDIO_FILE && !file_path)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: '"scene_name", "scene_item_name" & "file_path" are required.', error: null, value: null }, { status: 400 });
|
||||
if ([ActionType.OAUTH, ActionType.NIGHTBOT_PLAY, ActionType.NIGHTBOT_PAUSE, ActionType.NIGHTBOT_SKIP, ActionType.NIGHTBOT_CLEAR_PLAYLIST, ActionType.NIGHTBOT_CLEAR_QUEUE, ActionType.TWITCH_OAUTH].some(t => t == type) && (!oauth_name || !oauth_type))
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: '"oauth_name" & "oauth_type" are required.', error: null, value: null }, { status: 400 });
|
||||
|
||||
let data: any = {}
|
||||
if (type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) {
|
||||
@@ -78,10 +88,9 @@ async function common(req: Request, action: (id: string, name: string, type: Act
|
||||
|
||||
action(user.id, name, type, data)
|
||||
|
||||
return new NextResponse("", { status: 200 });
|
||||
return NextResponse.json({ message: null, error: null, value: null }, { status: 200 });
|
||||
} catch (error: any) {
|
||||
//console.log("[REDEMPTIONS/ACTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: null, error: error, value: null }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +129,7 @@ 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)
|
||||
@@ -135,8 +144,7 @@ export async function DELETE(req: Request) {
|
||||
})
|
||||
|
||||
return NextResponse.json(redemptions);
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
} catch (error: any) {
|
||||
return NextResponse.json({ message: null, error: error, value: null }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -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 redemptions = await db.redemption.findMany({
|
||||
@@ -17,8 +17,7 @@ export async function GET(req: Request) {
|
||||
|
||||
return NextResponse.json(redemptions.map(({userId, ...attrs}) => attrs));
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,12 +25,15 @@ 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 { actionName, redemptionId, order, state }: { actionName: string, redemptionId: string, order: number, state: boolean } = await req.json();
|
||||
if (!redemptionId || !actionName && !order && !state)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
if (!redemptionId && !actionName && !order)
|
||||
return NextResponse.json({ message: 'One of the following fields must exist: redemptionId, actionName, order, state.', error: null, value: null }, { status: 400 })
|
||||
|
||||
if (actionName && actionName.length > 32)
|
||||
return NextResponse.json({ message: 'actionName cannot be longer than 32 characters.', error: null, value: null }, { status: 400 })
|
||||
|
||||
const action = await db.action.findFirst({
|
||||
where: {
|
||||
@@ -39,7 +41,7 @@ export async function POST(req: Request) {
|
||||
}
|
||||
})
|
||||
if (!action)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: 'Action does not exist.', error: null, value: null }, { status: 400 })
|
||||
|
||||
let data:any = {
|
||||
actionName,
|
||||
@@ -66,8 +68,7 @@ export async function POST(req: Request) {
|
||||
|
||||
return NextResponse.json(res, { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,12 +76,15 @@ export async function PUT(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 { id, actionName, redemptionId, order, state }: { id: string, actionName: string, redemptionId: string, order: number, state: boolean } = await req.json();
|
||||
if (!redemptionId || !actionName && !order && !state)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
if (!redemptionId && !actionName && !order)
|
||||
return NextResponse.json({ message: 'One of the following fields must exist: redemptionId, actionName, order, state.', error: null, value: null }, { status: 400 })
|
||||
|
||||
if (actionName && actionName.length > 32)
|
||||
return NextResponse.json({ message: 'actionName cannot be longer than 32 characters.', error: null, value: null }, { status: 400 })
|
||||
|
||||
const action = await db.action.findFirst({
|
||||
where: {
|
||||
@@ -88,7 +92,7 @@ export async function PUT(req: Request) {
|
||||
}
|
||||
})
|
||||
if (!action)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
return NextResponse.json({ message: 'Action does not exist.', error: null, value: null }, { status: 400 })
|
||||
|
||||
let data:any = {
|
||||
actionName,
|
||||
@@ -115,8 +119,7 @@ export async function PUT(req: Request) {
|
||||
|
||||
return NextResponse.json(res, { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,7 +127,7 @@ 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)
|
||||
@@ -137,7 +140,6 @@ export async function DELETE(req: Request) {
|
||||
|
||||
return NextResponse.json(redemptions);
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
return NextResponse.json({ message: 'Something went wrong', error: error, value: null }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user