Added redemptions & redeemable actions. Fixed a few bugs.
This commit is contained in:
150
app/api/settings/redemptions/actions/route.ts
Normal file
150
app/api/settings/redemptions/actions/route.ts
Normal file
@ -0,0 +1,150 @@
|
||||
import { db } from "@/lib/db"
|
||||
import { NextResponse } from "next/server";
|
||||
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
||||
import { ActionType, Prisma } from "@prisma/client";
|
||||
import { JsonSerializer } from "typescript-json-serializer";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req)
|
||||
if (!user) {
|
||||
return new NextResponse("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
const actions = await db.action.findMany({
|
||||
where: {
|
||||
userId: user.id
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(actions.map(({userId, ...attrs}) => attrs));
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS/ACTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req)
|
||||
if (!user) {
|
||||
return new NextResponse("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
const { name, type, scene_name, scene_item_name, rotation, position_x, position_y, file_path, file_content }: { 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 } = await req.json();
|
||||
if (!name && !type)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
if (type == ActionType.OBS_TRANSFORM && (!scene_name || !scene_item_name || !rotation && !position_x && !position_y))
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
if ((type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) && (!file_path || !file_content))
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
if (type == ActionType.AUDIO_FILE && !file_path)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
|
||||
let data:any = { }
|
||||
if (type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) {
|
||||
data = { file_path, file_content, ...data }
|
||||
} else if (type == ActionType.OBS_TRANSFORM) {
|
||||
data = { scene_name, scene_item_name, ...data }
|
||||
if (!!rotation)
|
||||
data = { rotation, ...data }
|
||||
if (!!position_x)
|
||||
data = { position_x, ...data }
|
||||
if (!!position_y)
|
||||
data = { position_y, ...data }
|
||||
} else if (type == ActionType.AUDIO_FILE) {
|
||||
data = { file_path, ...data }
|
||||
}
|
||||
|
||||
await db.action.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
name,
|
||||
type,
|
||||
data: data as Prisma.JsonObject
|
||||
}
|
||||
});
|
||||
|
||||
return new NextResponse("", { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS/ACTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req)
|
||||
if (!user) {
|
||||
return new NextResponse("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
const { name, type, scene_name, scene_item_name, rotation, position_x, position_y, file_path, file_content }: { 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 } = await req.json();
|
||||
if (!name && !type)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
if (type == ActionType.OBS_TRANSFORM && (!scene_name || !scene_item_name || !rotation && !position_x && !position_y))
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
if ((type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) && (!file_path || !file_content))
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
if (type == ActionType.AUDIO_FILE && !file_path)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
|
||||
let data:any = { }
|
||||
if (type == ActionType.WRITE_TO_FILE || type == ActionType.APPEND_TO_FILE) {
|
||||
data = { file_path, file_content, ...data }
|
||||
} else if (type == ActionType.OBS_TRANSFORM) {
|
||||
data = { scene_name, scene_item_name, ...data }
|
||||
if (!!rotation)
|
||||
data = { rotation, ...data }
|
||||
if (!!position_x)
|
||||
data = { position_x, ...data }
|
||||
if (!!position_y)
|
||||
data = { position_y, ...data }
|
||||
} else if (type == ActionType.AUDIO_FILE) {
|
||||
data = { file_path, ...data }
|
||||
}
|
||||
|
||||
await db.action.update({
|
||||
where: {
|
||||
userId_name: {
|
||||
userId: user.id,
|
||||
name
|
||||
}
|
||||
},
|
||||
data: {
|
||||
type,
|
||||
data: data as Prisma.JsonObject
|
||||
}
|
||||
});
|
||||
|
||||
return new NextResponse("", { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS/ACTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req)
|
||||
if (!user) {
|
||||
return new NextResponse("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(req.url)
|
||||
const name = searchParams.get('action_name') as string
|
||||
const redemptions = await db.action.delete({
|
||||
where: {
|
||||
userId_name: {
|
||||
userId: user.id,
|
||||
name
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(redemptions);
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
}
|
143
app/api/settings/redemptions/route.ts
Normal file
143
app/api/settings/redemptions/route.ts
Normal file
@ -0,0 +1,143 @@
|
||||
import { db } from "@/lib/db"
|
||||
import { NextResponse } from "next/server";
|
||||
import fetchUserWithImpersonation from "@/lib/fetch-user-impersonation";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req)
|
||||
if (!user) {
|
||||
return new NextResponse("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
const redemptions = await db.redemption.findMany({
|
||||
where: {
|
||||
userId: user.id
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(redemptions.map(({userId, ...attrs}) => attrs));
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req)
|
||||
if (!user) {
|
||||
return new NextResponse("Unauthorized", { 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 });
|
||||
|
||||
const action = await db.action.findFirst({
|
||||
where: {
|
||||
name: actionName
|
||||
}
|
||||
})
|
||||
if (!action)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
|
||||
let data:any = {
|
||||
actionName,
|
||||
order,
|
||||
state
|
||||
}
|
||||
|
||||
if (!data.actionName)
|
||||
data = { actionName, ...data }
|
||||
if (!data.order)
|
||||
data = { order, ...data }
|
||||
if (!data.state)
|
||||
data = { state, ...data }
|
||||
|
||||
const res = await db.redemption.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
redemptionId,
|
||||
order,
|
||||
state: true,
|
||||
...data
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json(res, { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req)
|
||||
if (!user) {
|
||||
return new NextResponse("Unauthorized", { 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 });
|
||||
|
||||
const action = await db.action.findFirst({
|
||||
where: {
|
||||
name: actionName
|
||||
}
|
||||
})
|
||||
if (!action)
|
||||
return new NextResponse("Bad Request", { status: 400 });
|
||||
|
||||
let data:any = {
|
||||
actionName,
|
||||
redemptionId,
|
||||
order,
|
||||
state
|
||||
}
|
||||
|
||||
if (!data.actionName)
|
||||
data = { actionName, ...data }
|
||||
if (!data.order)
|
||||
data = { order, ...data }
|
||||
if (!data.state)
|
||||
data = { state, ...data }
|
||||
if (!data.redemptionId)
|
||||
data = { redemptionId, ...data }
|
||||
|
||||
const res = await db.redemption.update({
|
||||
where: {
|
||||
id
|
||||
},
|
||||
data: data
|
||||
});
|
||||
|
||||
return NextResponse.json(res, { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req)
|
||||
if (!user) {
|
||||
return new NextResponse("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(req.url)
|
||||
const id = searchParams.get('id') as string
|
||||
const redemptions = await db.redemption.delete({
|
||||
where: {
|
||||
id
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(redemptions);
|
||||
} catch (error) {
|
||||
console.log("[REDEMPTIONS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
}
|
@ -5,19 +5,16 @@ import voices from "@/data/tts";
|
||||
|
||||
export async function GET(req: Request) {
|
||||
try {
|
||||
if (!voices) {
|
||||
return new NextResponse("Voices not available.", { status: 500 });
|
||||
}
|
||||
|
||||
const user = await fetchUserWithImpersonation(req)
|
||||
if (!user) {
|
||||
return new NextResponse("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
const u = await db.user.findFirst({
|
||||
where: {
|
||||
id: user.id
|
||||
}
|
||||
});
|
||||
|
||||
const voice = voices.find(v => v.value == new String(u?.ttsDefaultVoice))
|
||||
return NextResponse.json(voice);
|
||||
return NextResponse.json(user.ttsDefaultVoice);
|
||||
} catch (error) {
|
||||
console.log("[TTS/FILTER/DEFAULT]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
@ -26,28 +23,32 @@ export async function GET(req: Request) {
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
if (!voices) {
|
||||
return new NextResponse("Voices not available.", { status: 500 });
|
||||
}
|
||||
|
||||
const user = await fetchUserWithImpersonation(req)
|
||||
if (!user) {
|
||||
return new NextResponse("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
const { voice } = await req.json();
|
||||
if (!voice || !voices.map(v => v.label.toLowerCase()).includes(voice.toLowerCase())) return new NextResponse("Bad Request", { status: 400 });
|
||||
if (!voice || !voices.map(v => v.toLowerCase()).includes(voice.toLowerCase())) return new NextResponse("Bad Request", { status: 400 });
|
||||
|
||||
const v = voices.find(v => v.label.toLowerCase() == voice.toLowerCase())
|
||||
const v = voices.find(v => v.toLowerCase() == voice.toLowerCase())
|
||||
|
||||
await db.user.update({
|
||||
where: {
|
||||
id: user.id
|
||||
},
|
||||
data: {
|
||||
ttsDefaultVoice: Number.parseInt(v.value)
|
||||
ttsDefaultVoice: v
|
||||
}
|
||||
});
|
||||
|
||||
return new NextResponse("", { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[TTS/FILTER/DEFAULT]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
@ -31,7 +31,7 @@ export async function POST(req: Request) {
|
||||
|
||||
const { search, replace } = await req.json();
|
||||
if (!search || search.length < 4 || search.length > 200) return new NextResponse("Bad Request", { status: 400 });
|
||||
if (!replace) return new NextResponse("Bad Request", { status: 400 });
|
||||
if (replace == null) return new NextResponse("Bad Request", { status: 400 });
|
||||
|
||||
const filter = await db.ttsWordFilter.create({
|
||||
data: {
|
||||
@ -58,7 +58,7 @@ export async function PUT(req: Request) {
|
||||
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) return new NextResponse("Bad Request", { status: 400 });
|
||||
if (replace == null) return new NextResponse("Bad Request", { status: 400 });
|
||||
|
||||
const filter = await db.ttsWordFilter.update({
|
||||
where: {
|
||||
|
@ -1,7 +1,6 @@
|
||||
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 {
|
||||
@ -10,23 +9,18 @@ export async function GET(req: Request) {
|
||||
return new NextResponse("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
let list : {
|
||||
value: string;
|
||||
label: string;
|
||||
gender: string;
|
||||
language: string;
|
||||
}[] = []
|
||||
const enabled = user.ttsEnabledVoice
|
||||
for (let v of voices) {
|
||||
var n = Number.parseInt(v.value) - 1
|
||||
if ((enabled & (1 << n)) > 0) {
|
||||
list.push(v)
|
||||
}
|
||||
}
|
||||
const voiceStates = await db.ttsVoiceState.findMany({
|
||||
where: {
|
||||
userId: user.id
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json(list);
|
||||
const voiceNames = await db.ttsVoice.findMany();
|
||||
const voiceNamesMapped: { [id: string]: string } = Object.assign({}, ...voiceNames.map(v => ({ [v.id]: v.name })))
|
||||
|
||||
return NextResponse.json(voiceStates.filter(v => v.state).map(v => voiceNamesMapped[v.ttsVoiceId]));
|
||||
} catch (error) {
|
||||
console.log("[TTS/FILTER/USER]", error);
|
||||
console.log("[TTS]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
}
|
||||
@ -38,16 +32,29 @@ export async function POST(req: Request) {
|
||||
return new NextResponse("Unauthorized", { status: 401 });
|
||||
}
|
||||
|
||||
let { voice } = await req.json();
|
||||
voice = voice & ((1 << voices.length) - 1)
|
||||
const { voice, state }: { voice: string, state: boolean } = await req.json();
|
||||
|
||||
await db.user.update({
|
||||
where: {
|
||||
id: user.id
|
||||
},
|
||||
data: {
|
||||
ttsEnabledVoice: voice
|
||||
}
|
||||
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 });
|
||||
}
|
||||
|
||||
await db.ttsVoiceState.upsert({
|
||||
where: {
|
||||
userId_ttsVoiceId: {
|
||||
userId: user.id,
|
||||
ttsVoiceId: voiceIdsMapped[voice.toLowerCase()]
|
||||
}
|
||||
},
|
||||
update: {
|
||||
state
|
||||
},
|
||||
create: {
|
||||
userId: user.id,
|
||||
ttsVoiceId: voiceIdsMapped[voice.toLowerCase()],
|
||||
state
|
||||
}
|
||||
});
|
||||
|
||||
return new NextResponse("", { status: 200 });
|
||||
|
71
app/api/settings/tts/selected/route.ts
Normal file
71
app/api/settings/tts/selected/route.ts
Normal file
@ -0,0 +1,71 @@
|
||||
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 new NextResponse("Unauthorized", { 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) {
|
||||
console.log("[TTS/SELECTED]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(req: Request) {
|
||||
try {
|
||||
const user = await fetchUserWithImpersonation(req)
|
||||
if (!user) {
|
||||
return new NextResponse("Unauthorized", { 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 });
|
||||
|
||||
const v = voices.find(v => v.toLowerCase() == voice.toLowerCase())
|
||||
const voiceData = await db.ttsVoice.findFirst({
|
||||
where: {
|
||||
name: v
|
||||
}
|
||||
})
|
||||
if (!voiceData)
|
||||
return new NextResponse("Bad Request", { 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 new NextResponse("", { status: 200 });
|
||||
} catch (error) {
|
||||
console.log("[TTS/SELECTED]", error);
|
||||
return new NextResponse("Internal Error", { status: 500 });
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user