feat: completed messagetochannel endpoint

This commit is contained in:
PrimarchPaul
2025-09-28 01:45:43 -04:00
parent db9a9a5d13
commit 6ef53fd964
7 changed files with 129 additions and 161 deletions

View File

@@ -103,7 +103,7 @@ model Message {
User User @relation(fields: [userId], references: [id]) User User @relation(fields: [userId], references: [id])
userId String userId String
deleted Boolean deleted Boolean
text String text String @db.VarChar(2000)
createdAt DateTime @default(now()) createdAt DateTime @default(now())
updatedAt DateTime @updatedAt updatedAt DateTime @updatedAt
replies Reply? @relation("MessageToReply") replies Reply? @relation("MessageToReply")

View File

@@ -1,68 +1,32 @@
import { Context } from "hono"; import { Context } from "hono";
import { import {
sendMessageToChannel, sendMessageToChannelEvent,
removeMessageFromChannel, removeMessageFromChannel,
} from "../services/realtime.js"; } from "../services/realtime.js";
import { success } from "zod"; import { success } from "zod";
import { PostMessageToChannelInput } from "../validators/realtimeValidator.js";
import { sendMessage } from "./messageController.js";
export async function postMessageToChannel(io: any, c: Context) { export async function postMessageToChannel(io: any, c: Context, data: PostMessageToChannelInput) {
try { const instanceId = data.instanceId;
io = c.get("io"); const categoryId = data.categoryId;
const channelId = data.channelId;
const instanceId = c.req.param("instanceId"); const userId = data.userId;
const categoryId = c.req.param("categoryId"); const content = data.content;
const channelId = c.req.param("channelId"); const token = data.token;
const message = await c.req.json(); const repliedMessageId = data.repliedMessageId ?? null;
const event = "new_channel_message";
const result = await sendMessageToChannel( return sendMessageToChannelEvent(
instanceId, instanceId,
categoryId, categoryId,
channelId, channelId,
message, userId,
"new_channel_message", content,
io, token,
); repliedMessageId,
event,
if (result === "Event not implemented") { io
console.log( );
"controller::realtime::postMessageToChannel - Failed to send message",
);
return c.json({
success: false,
message: "Event not implemented or recognized",
status: 400,
});
}
if (result === "no acknowledgment") {
console.log(
"controller::realtime::postMessageToChannel - No acknowledgment received from client",
);
return c.json({
success: false,
message: "No acknowledgment received from client",
status: 500,
});
}
if (!result) {
throw new Error("failed to send message");
}
return c.json({
success: true,
message: "Message sent successfully",
status: 200,
});
} catch (err) {
const errMessage = err as Error;
console.log("controller::realtime::postMessageToChannel - ", errMessage);
return c.json({
success: false,
message: errMessage.message,
status: 500,
});
}
} }
export async function deleteMessageFromChannel(io: any, c: Context) { export async function deleteMessageFromChannel(io: any, c: Context) {

View File

@@ -1,29 +0,0 @@
import { Hono } from "hono";
import { zValidator } from "@hono/zod-validator";
import { describeRoute, resolver } from "hono-openapi";
import {
postMessageToChannel,
deleteMessageFromChannel,
} from "../controller/realtime";
const app = new Hono();
app.post(
"message/",
zValidator({
body: z.object({
content: z.string().min(1).max(500),
}),
}),
async (c) => {
const { instanceId, categoryId, channelId } = c.req.params;
const { content } = c.req.body;
return postMessageToChannel(c.get("io"), {
instanceId,
categoryId,
channelId,
content,
});
},
);

View File

@@ -0,0 +1,88 @@
import { Hono } from "hono";
import { zValidator } from "@hono/zod-validator";
import { describeRoute, resolver } from "hono-openapi";
import {
postMessageToChannelSchema
} from "../validators/realtimeValidator.js";
import {
postMessageToChannel,
deleteMessageFromChannel,
} from "../controller/realtimeController";
const realtimeRoutes = new Hono();
realtimeRoutes.post(
"message/:instanceId/:categoryId/:channelId",
describeRoute({
description: "Post a message to a channel",
responses: {
200: {
description: "Message posted successfully",
content: {
"application/json": { schema: resolver(postMessageToChannelSchema) },
},
},
400: {
description: "Bad Request - Invalid input data",
content: {
"application/json": { schema: resolver(postMessageToChannelSchema) },
},
},
401: {
description: "Unauthorized - Invalid token",
content: {
"application/json": { schema: resolver(postMessageToChannelSchema) },
},
},
404: {
description: "Instance, Category, Channel, or User not found",
content: {
"application/json": { schema: resolver(postMessageToChannelSchema) },
},
},
500: {
description: "Server error",
content: {
"application/json": { schema: resolver(postMessageToChannelSchema) },
},
},
},
}),
zValidator("json", postMessageToChannelSchema),
async (c) => {
const instanceId = c.req.param("instanceId");
const categoryId = c.req.param("categoryId");
const channelId = c.req.param("channelId");
const { userId, content, repliedMessageId, token } = await c.req.json();
const ioServer = (c as any).get("io");
if (!ioServer) {
return c.json({ success: false, error: "Realtime server not available" }, 500);
}
const result = await postMessageToChannel(ioServer, c, {
instanceId,
categoryId,
channelId,
userId,
content,
token,
repliedMessageId: repliedMessageId ?? null,
})
if (result === "event not implemented") {
return c.json({ success: false, message: "Event not implemented or recognized" }, 400);
}
if (result === "no acknowledgment") {
return c.json({ success: false, message: "No acknowledgment received from client" }, 500);
}
if (!result) {
return c.json({ success: false, message: "Failed to post message" }, 500);
}
return c.json({ success: true, result }, 200);
}
);

View File

@@ -52,5 +52,5 @@ export type GetChannelsByCategoryIdInput = z.infer<
export type UpdateChannelInput = z.infer<typeof updateChannelSchema>; export type UpdateChannelInput = z.infer<typeof updateChannelSchema>;
export type DeleteChannelInput = z.infer<typeof deleteChannelSchema>; export type DeleteChannelInput = z.infer<typeof deleteChannelSchema>;
export type DeleteChannelsByCategoryIdInput = z.infer< export type DeleteChannelsByCategoryIdInput = z.infer<
typeof deleteChannelsByCategoryIdSchema typeof deleteChannelsByCategoryIdSchema
>; >;

View File

@@ -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;

View File

@@ -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