2023-04-20 04:20:00 -06:00
|
|
|
import { getAuth } from "@clerk/remix/ssr.server";
|
|
|
|
import { ActionFunctionArgs, json } from "@remix-run/node";
|
|
|
|
import { createId } from "@paralleldrive/cuid2";
|
|
|
|
import { db } from "~/services/db.server";
|
|
|
|
import { emitter } from "~/services/emitter.server";
|
|
|
|
import { rooms } from "~/services/schema";
|
2023-12-01 14:57:31 -07:00
|
|
|
import { invalidateCache } from "~/services/redis.server";
|
2023-04-20 04:20:00 -06:00
|
|
|
|
|
|
|
export async function action({ request, params, context }: ActionFunctionArgs) {
|
|
|
|
const { userId } = await getAuth({ context, params, request });
|
|
|
|
|
|
|
|
if (!userId) {
|
|
|
|
return json("Not Signed In!", {
|
|
|
|
status: 403,
|
|
|
|
statusText: "UNAUTHORIZED!",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await request.json();
|
|
|
|
|
|
|
|
const room = await db
|
|
|
|
.insert(rooms)
|
|
|
|
.values({
|
|
|
|
id: `room_${createId()}`,
|
|
|
|
created_at: Date.now().toString(),
|
|
|
|
userId: userId || "",
|
|
|
|
roomName: data.name,
|
|
|
|
storyName: "First Story!",
|
|
|
|
scale: "0.5,1,2,3,5,8",
|
|
|
|
visible: 0,
|
|
|
|
})
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
const success = room.length > 0;
|
|
|
|
|
|
|
|
if (success) {
|
2023-12-01 16:15:21 -07:00
|
|
|
await invalidateCache(`kv_roomlist_${userId}`, "sp");
|
2023-12-01 14:57:31 -07:00
|
|
|
emitter.emit("nodes", "roomlist");
|
2023-04-20 04:20:00 -06:00
|
|
|
|
|
|
|
return json(room, {
|
|
|
|
status: 200,
|
|
|
|
statusText: "SUCCESS",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|