pollo/app/routes/api.room.get.all.tsx

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-11-21 16:05:53 -07:00
import { getAuth } from "@clerk/remix/ssr.server";
import { LoaderFunctionArgs, json } from "@remix-run/node";
2023-11-21 16:05:53 -07:00
import { eq } from "drizzle-orm";
import { eventStream } from "remix-utils/sse/server";
import { db } from "~/services/db.server";
2023-11-21 16:05:53 -07:00
import { emitter } from "~/services/emitter.server";
import { rooms } from "~/services/schema";
2023-11-21 18:08:19 -07:00
// Get Room List
2023-11-21 16:05:53 -07:00
export async function loader({ context, params, request }: LoaderFunctionArgs) {
const { userId } = await getAuth({ context, params, request });
if (!userId) {
return json("Not Signed In!", {
status: 403,
statusText: "UNAUTHORIZED!",
});
}
2023-11-21 16:05:53 -07:00
return eventStream(request.signal, function setup(send) {
2023-11-21 18:08:19 -07:00
async function handler() {
const roomList = await db.query.rooms.findMany({
where: eq(rooms.userId, userId || ""),
});
send({ event: userId!, data: JSON.stringify(roomList) });
2023-11-21 16:05:53 -07:00
}
2023-11-21 18:08:19 -07:00
// Initial fetch
db.query.rooms
.findMany({
where: eq(rooms.userId, userId || ""),
})
.then((roomList) => {
send({ event: userId!, data: JSON.stringify(roomList) });
2023-11-21 18:08:19 -07:00
});
2023-11-21 16:05:53 -07:00
emitter.on("roomlist", handler);
return function clear() {
emitter.off("roomlist", handler);
};
});
}