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

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