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,68 +1,32 @@
import { Context } from "hono";
import {
sendMessageToChannel,
sendMessageToChannelEvent,
removeMessageFromChannel,
} from "../services/realtime.js";
import { success } from "zod";
import { PostMessageToChannelInput } from "../validators/realtimeValidator.js";
import { sendMessage } from "./messageController.js";
export async function postMessageToChannel(io: any, c: Context) {
try {
io = c.get("io");
const instanceId = c.req.param("instanceId");
const categoryId = c.req.param("categoryId");
const channelId = c.req.param("channelId");
const message = await c.req.json();
const result = await sendMessageToChannel(
instanceId,
categoryId,
channelId,
message,
"new_channel_message",
io,
);
if (result === "Event not implemented") {
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 postMessageToChannel(io: any, c: Context, data: PostMessageToChannelInput) {
const instanceId = data.instanceId;
const categoryId = data.categoryId;
const channelId = data.channelId;
const userId = data.userId;
const content = data.content;
const token = data.token;
const repliedMessageId = data.repliedMessageId ?? null;
const event = "new_channel_message";
return sendMessageToChannelEvent(
instanceId,
categoryId,
channelId,
userId,
content,
token,
repliedMessageId,
event,
io
);
}
export async function deleteMessageFromChannel(io: any, c: Context) {