feat: completed messagetochannel endpoint
This commit is contained in:
@@ -52,5 +52,5 @@ export type GetChannelsByCategoryIdInput = z.infer<
|
||||
export type UpdateChannelInput = z.infer<typeof updateChannelSchema>;
|
||||
export type DeleteChannelInput = z.infer<typeof deleteChannelSchema>;
|
||||
export type DeleteChannelsByCategoryIdInput = z.infer<
|
||||
typeof deleteChannelsByCategoryIdSchema
|
||||
typeof deleteChannelsByCategoryIdSchema
|
||||
>;
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
import { Hono } from "hono";
|
||||
import { cors } from "hono/cors";
|
||||
import { Server as Engine } from "@socket.io/bun-engine";
|
||||
import { Server } from "socket.io";
|
||||
import routes from "./routes/index";
|
||||
import { Scalar } from "@scalar/hono-api-reference";
|
||||
import { openAPIRouteHandler } from "hono-openapi";
|
||||
|
||||
//initialize socket.io server
|
||||
const io = new Server();
|
||||
|
||||
//initialize bun engine
|
||||
//then bind to socket.io server
|
||||
const engine = new Engine();
|
||||
io.bind(engine);
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
//get userId and clientId from query params
|
||||
const userId = socket.handshake.query.userId;
|
||||
const clientId = socket.handshake.query.clientId;
|
||||
if (!userId || Array.isArray(userId)) {
|
||||
socket.disconnect();
|
||||
throw new Error("Invalid user ID");
|
||||
}
|
||||
|
||||
if (!clientId || Array.isArray(clientId)) {
|
||||
socket.disconnect();
|
||||
throw new Error("Invalid client ID");
|
||||
}
|
||||
|
||||
socket.join(userId);
|
||||
console.log(
|
||||
`User ${userId} connected. Client ID ${clientId} on socket ${socket.id}`,
|
||||
);
|
||||
|
||||
socket.on("disconnect", () => {
|
||||
console.log(`User ${userId} disconnected from socket ${socket.id}`);
|
||||
});
|
||||
});
|
||||
|
||||
const app = new Hono();
|
||||
|
||||
app.use(
|
||||
"*",
|
||||
cors({
|
||||
origin: "http://localhost:5173",
|
||||
allowHeaders: ["Content-Type", "Authorization"],
|
||||
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||
credentials: true,
|
||||
}),
|
||||
);
|
||||
|
||||
app.route("/api", routes);
|
||||
|
||||
app.get(
|
||||
"/openapi",
|
||||
openAPIRouteHandler(app, {
|
||||
documentation: {
|
||||
info: {
|
||||
title: "Hono API",
|
||||
version: "1.0.0",
|
||||
description: "Greeting API",
|
||||
},
|
||||
servers: [{ url: "http://localhost:3000", description: "Local Server" }],
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
app.get("/scalar", Scalar({ url: "/openapi" }));
|
||||
|
||||
export default app;
|
||||
@@ -0,0 +1,16 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const postMessageToChannelSchema = z.object({
|
||||
instanceId: z.uuidv7(),
|
||||
categoryId: z.uuidv7(),
|
||||
channelId: z.uuidv7(),
|
||||
userId: z.uuidv7(),
|
||||
content: z.string().min(1).max(2000),
|
||||
repliedMessageId: z.uuidv7().optional(),
|
||||
token: z.string(),
|
||||
});
|
||||
|
||||
//TODO: add more realtime related validators as needed
|
||||
|
||||
export type PostMessageToChannelInput = z.infer<typeof postMessageToChannelSchema>;
|
||||
//TODO: create more input schemas for other realtime actions
|
||||
Reference in New Issue
Block a user