Fixed a few things

This commit is contained in:
Tom 2025-01-07 15:52:47 +00:00
parent 624b3fa63b
commit dd8530b589
4 changed files with 38 additions and 36 deletions

View File

@ -73,7 +73,7 @@ export async function GET(req: Request) {
const groupIdSchema = z.string({ const groupIdSchema = z.string({
required_error: "Group ID should be available.", required_error: "Group ID should be available.",
invalid_type_error: "Group ID must be a string" invalid_type_error: "Group ID must be a string"
}).regex(/^[\w\-\=]{1,32}$/, "Group ID must contain only letters, numbers, dashes, underscores & equal signs.") }).regex(/^[\w\-\=]{1,40}$/, "Group ID must contain only letters, numbers, dashes, underscores & equal signs.")
export async function POST(req: Request) { export async function POST(req: Request) {
try { try {

View File

@ -6,7 +6,7 @@ import { z } from "zod";
const groupIdSchema = z.string({ const groupIdSchema = z.string({
required_error: "Group ID should be available.", required_error: "Group ID should be available.",
invalid_type_error: "Group ID must be a string" invalid_type_error: "Group ID must be a string"
}).regex(/^[\w\-\=]{1,32}$/, "Group ID must contain only letters, numbers, dashes, underscores & equal signs.") }).regex(/^[\w\-\=]{1,40}$/, "Group ID must contain only letters, numbers, dashes, underscores & equal signs.")
export async function GET(req: Request) { export async function GET(req: Request) {
try { try {

View File

@ -3,6 +3,7 @@ import { NextResponse } from "next/server";
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation"; import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
import { ActionType, Prisma } from "@prisma/client"; import { ActionType, Prisma } from "@prisma/client";
import { z } from "zod"; import { z } from "zod";
import { redirect } from "next/dist/server/api-utils";
export async function GET(req: Request) { export async function GET(req: Request) {
try { try {
@ -28,18 +29,15 @@ const nameSchema = z.string({
invalid_type_error: "Name must be a string" invalid_type_error: "Name must be a string"
}).regex(/^[\w\-\s]{1,32}$/, "Name must contain only letters, numbers, spaces, dashes, and underscores.") }).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) { async function common(req: Request, action: (id: string, name: string, type: ActionType, values: any) => void) {
try { try {
const user = await fetchUserWithImpersonation(req) const user = await fetchUserWithImpersonation(req)
if (!user) { if (!user) {
return NextResponse.json({ message: 'Unauthorized.', error: null, value: null }, { 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 }: const { name, type, data }: { name: string, type: ActionType, data: any } = await req.json();
{
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) if (!name)
return NextResponse.json({ message: 'name is required.', error: null, value: null }, { status: 400 }); return NextResponse.json({ message: 'name is required.', error: null, value: null }, { status: 400 });
const nameValidation = nameSchema.safeParse(name) const nameValidation = nameSchema.safeParse(name)
@ -47,48 +45,51 @@ async function common(req: Request, action: (id: string, name: string, type: Act
return NextResponse.json({ message: 'name must follow some requirements.', error: nameValidation.error, value: null }, { status: 400 }); return NextResponse.json({ message: 'name must follow some requirements.', error: nameValidation.error, value: null }, { status: 400 });
if (!type) if (!type)
return NextResponse.json({ message: 'type is required', error: null, value: null }, { status: 400 }); 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)) if (type == ActionType.OBS_TRANSFORM && (!data.scene_name || !data.scene_item_name || !data.rotation && !data.position_x && !data.position_y))
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 }); 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)) if ((type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) && (!data.file_path || !data.file_content))
return NextResponse.json({ message: '"scene_name", "scene_item_name", "file_path" & "file_content" are required.', error: null, value: null }, { 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) if (type == ActionType.AUDIO_FILE && !data.file_path)
return NextResponse.json({ message: '"scene_name", "scene_item_name" & "file_path" are required.', error: null, value: null }, { 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)) 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) && (!data.oauth_name || !data.oauth_type))
return NextResponse.json({ message: '"oauth_name" & "oauth_type" are required.', error: null, value: null }, { status: 400 }); return NextResponse.json({ message: '"oauth_name" & "oauth_type" are required.', error: null, value: null }, { status: 400 });
if ([ActionType.VEADOTUBE_POP_STATE, ActionType.VEADOTUBE_PUSH_STATE, ActionType.VEADOTUBE_SET_STATE].some(t => t == type) && !data.state)
return NextResponse.json({ message: '"state" is required.', error: null, value: null }, { status: 400 });
let data: any = {} let d: any = {}
if (type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) { if (type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) {
data = { file_path, file_content } d = { file_path: data.file_path, file_content: data.file_content }
} else if (type == ActionType.OBS_TRANSFORM) { } else if (type == ActionType.OBS_TRANSFORM) {
data = { scene_name, scene_item_name } d = { scene_name: data.scene_name, scene_item_name: data.scene_item_name }
if (!!rotation) if (!!data.rotation)
data = { rotation, ...data } d = { rotation: data.rotation, ...data }
if (!!position_x) if (!!data.position_x)
data = { position_x, ...data } d = { position_x: data.position_x, ...data }
if (!!position_y) if (!!data.position_y)
data = { position_y, ...data } d = { position_y: data.position_y, ...data }
} else if (type == ActionType.AUDIO_FILE) { } else if (type == ActionType.AUDIO_FILE) {
data = { file_path } d = { file_path: data.file_path }
} else if (type == ActionType.SPECIFIC_TTS_VOICE) { } else if (type == ActionType.SPECIFIC_TTS_VOICE) {
data = { tts_voice } d = { tts_voice: data.tts_voice }
} else if (type == ActionType.TOGGLE_OBS_VISIBILITY) { } else if (type == ActionType.TOGGLE_OBS_VISIBILITY) {
data = { scene_name, scene_item_name } d = { scene_name: data.scene_name, scene_item_name: data.scene_item_name }
} else if (type == ActionType.SPECIFIC_OBS_VISIBILITY) { } else if (type == ActionType.SPECIFIC_OBS_VISIBILITY) {
data = { scene_name, scene_item_name, obs_visible } d = { scene_name: data.scene_name, scene_item_name: data.scene_item_name, obs_visible: data.obs_visible }
} else if (type == ActionType.SPECIFIC_OBS_INDEX) { } else if (type == ActionType.SPECIFIC_OBS_INDEX) {
data = { scene_name, scene_item_name, obs_index } d = { scene_name: data.scene_name, scene_item_name: data.scene_item_name, obs_index: data.obs_index }
} else if (type == ActionType.SLEEP) { } else if (type == ActionType.SLEEP) {
data = { sleep } d = { sleep: data.sleep }
} else 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)) { } else 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)) {
data = { d = {
oauth_name, oauth_name: data.oauth_name,
oauth_type oauth_type: data.oauth_type
} }
} else if ([ActionType.VEADOTUBE_POP_STATE, ActionType.VEADOTUBE_PUSH_STATE, ActionType.VEADOTUBE_SET_STATE].some(t => t == type)) {
d = { state: data.state }
} }
action(user.id, name, type, data) const dd = action(user.id, name, type, d)
return NextResponse.json({ message: null, error: null, value: dd }, { status: 200 });
return NextResponse.json({ message: null, error: null, value: null }, { status: 200 });
} catch (error: any) { } catch (error: any) {
return NextResponse.json({ message: null, error: error, value: null }, { status: 500 }); return NextResponse.json({ message: null, error: error, value: null }, { status: 500 });
} }
@ -96,7 +97,7 @@ async function common(req: Request, action: (id: string, name: string, type: Act
export async function POST(req: Request) { export async function POST(req: Request) {
return common(req, async (id, name, type, data) => { return common(req, async (id, name, type, data) => {
await db.action.create({ return await db.action.create({
data: { data: {
userId: id, userId: id,
name, name,
@ -109,7 +110,7 @@ export async function POST(req: Request) {
export async function PUT(req: Request) { export async function PUT(req: Request) {
return common(req, async (id, name, type, data) => { return common(req, async (id, name, type, data) => {
await db.action.update({ return await db.action.update({
where: { where: {
userId_name: { userId_name: {
userId: id, userId: id,
@ -117,7 +118,6 @@ export async function PUT(req: Request) {
} }
}, },
data: { data: {
name,
type, type,
data: data as Prisma.JsonObject data: data as Prisma.JsonObject
} }

View File

@ -8,6 +8,7 @@ export async function updateTwitchToken(userId: string) {
} }
}) })
if (!connection) { if (!connection) {
console.log('Connections do not exist.')
return null return null
} }
@ -48,6 +49,7 @@ export async function updateTwitchToken(userId: string) {
const { access_token, expires_in, refresh_token, token_type } = token const { access_token, expires_in, refresh_token, token_type } = token
if (!access_token || !refresh_token || token_type !== "bearer") { if (!access_token || !refresh_token || token_type !== "bearer") {
console.log('Failed to grab token.')
return null return null
} }