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

51 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-04-20 04:20:00 -06:00
import { getAuth } from "@clerk/remix/ssr.server";
2023-12-13 14:52:56 -07:00
import { type LoaderFunctionArgs, json } from "@remix-run/node";
2023-04-20 04:20:00 -06:00
import { eq } from "drizzle-orm";
import { eventStream } from "remix-utils/sse/server";
import { db } from "~/services/db.server";
import { emitter } from "~/services/emitter.server";
2023-12-11 22:58:49 -07:00
import { rooms } from "~/services/schema.server";
2023-04-20 04:20:00 -06:00
// Get Room List
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!",
});
}
return eventStream(request.signal, function setup(send) {
async function handler() {
2024-04-09 11:29:47 -06:00
db.query.rooms
.findMany({
where: eq(rooms.userId, userId || ""),
})
.then((roomList) => {
Promise.all([
send({ event: userId!, data: JSON.stringify(roomList) }),
]);
});
2023-04-20 04:20:00 -06:00
}
// Initial fetch
2024-04-09 11:29:47 -06:00
db.query.rooms
.findMany({
where: eq(rooms.userId, userId || ""),
})
.then((roomList) => {
Promise.all([
send({ event: userId!, data: JSON.stringify(roomList) }),
]);
});
2023-04-20 04:20:00 -06:00
emitter.on("roomlist", handler);
return function clear() {
emitter.off("roomlist", handler);
};
});
}