Get up to 50 messages before ISO 8601 timestamp

This commit is contained in:
Kevin Puig
2025-09-27 19:37:56 -04:00
parent 74d0c502f3
commit 9b46852e20
4 changed files with 117 additions and 3 deletions

View File

@@ -1,9 +1,13 @@
import { getMessageInformation, sendMessageToChannel } from "../services/messageService"; import { getMessageInformation, getMessagesBefore, sendMessageToChannel } from "../services/messageService";
export async function fetchMessageData(id:string) { export async function fetchMessageData(id:string) {
return await getMessageInformation(id); return await getMessageInformation(id);
} }
export async function fetchMessagesBefore(date:string, channelId:string) {
return getMessagesBefore(date, channelId);
}
export async function sendMessage( export async function sendMessage(
channelId: string, channelId: string,
userId: string, userId: string,

View File

@@ -1,8 +1,8 @@
import { Hono } from "hono"; import { Hono } from "hono";
import { describeResponse, describeRoute, resolver } from "hono-openapi"; import { describeResponse, describeRoute, resolver } from "hono-openapi";
import { getMessageByIdSchema, sendMessageSchema } from "../validators/messageValidator"; import { getMessageByIdSchema, getMessagesBeforeDate, sendMessageSchema } from "../validators/messageValidator";
import { zValidator } from "@hono/zod-validator"; import { zValidator } from "@hono/zod-validator";
import { fetchMessageData, sendMessage } from "../controller/messageController"; import { fetchMessageData, fetchMessagesBefore, sendMessage } from "../controller/messageController";
const messageRoutes = new Hono(); const messageRoutes = new Hono();
@@ -38,6 +38,41 @@ messageRoutes.get(
} }
) )
messageRoutes.get(
"",
describeRoute({
description: "Get up to 50 messages prior to given datetime",
responses: {
200: {
description: "Success getting up to 50 messages",
content: {
"application/json": { schema: resolver(getMessagesBeforeDate) }
}
}
}
}),
zValidator("query", getMessagesBeforeDate),
async (c) => {
const date = c.req.query("date");
if (!date) {
return c.json({ error: "date not provided" }, 400);
}
const channelId = c.req.query("channelId");
if (!channelId) {
return c.json({ error: "channelId not provided" }, 400);
}
const messagesArr = await fetchMessagesBefore(date, channelId);
if (messagesArr) {
return c.json(messagesArr, 200);
} else {
return c.json({ error: "Failed to fetch messages" }, 500);
}
}
)
messageRoutes.post( messageRoutes.post(
"", "",
describeRoute({ describeRoute({

View File

@@ -85,6 +85,74 @@ export async function getMessageInformation(id:string): Promise<{
} }
} }
export async function getMessagesBefore(date:string, channelId:string) {
try {
if (!date || !channelId) {
throw new Error("missing date or channelId");
}
const messages = await prisma.message.findMany({
where: {
channelId: channelId,
createdAt: {
lt: new Date(date),
},
},
orderBy: {
createdAt: "desc",
},
take: 50,
});
const messageInformationPromises = messages.map(async (message) => {
const replyData = await prisma.reply.findFirst({
where: {
messageId: message.id,
},
});
let originalMessage = null;
if (replyData) {
originalMessage = await prisma.message.findUnique({
where: {
id: replyData.repliesToId,
},
});
}
return {
id: message.id,
channelId: message.channelId!,
userId: message.userId!,
text: message.text,
deleted: message.deleted,
replies: originalMessage
? {
messageId: message.id,
repliesToId: originalMessage.id,
repliesToText: originalMessage.text,
}
: null,
};
});
return Promise.all(messageInformationPromises);
} catch (err) {
const errMessage = err as Error;
if (errMessage.message === "missing date or channelId") {
console.log("services::actions::getMessagesBefore - missing date or channelId");
return null;
}
console.log(
"services::actions::getMessagesBefore - unknown error",
errMessage
);
return null;
}
}
export async function sendMessageToChannel( export async function sendMessageToChannel(
channelId: string, channelId: string,
userId: string, userId: string,

View File

@@ -4,6 +4,13 @@ export const getMessageByIdSchema = z.object({
id: z.uuidv7() id: z.uuidv7()
}) })
export const getMessagesBeforeDate = z.object({
date: z.string().refine((val) => !isNaN(Date.parse(val)), {
message: "Invalid date string format"
}),
channelId: z.uuidv7()
})
export const sendMessageSchema = z.object({ export const sendMessageSchema = z.object({
channelId: z.uuidv7(), channelId: z.uuidv7(),
userId: z.uuidv7(), userId: z.uuidv7(),