Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 60 additions & 1 deletion apps/api/src/modules/chat/chat.route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { and, count, desc, eq, ne, notExists } from "drizzle-orm";
import { and, count, desc, eq, ilike, ne, notExists } from "drizzle-orm";
import {
createTypiRouter,
createTypiRoute,
Expand Down Expand Up @@ -305,6 +305,65 @@ const chatRouter = createTypiRouter({
},
}),
}),
"/:chatId/messages/search": createTypiRoute({
get: createTypiRouteHandler("/:chatId/messages/search", {
input: {
query: z.object({
searchTerm: z.string(),
}),
},
middlewares: [authMiddleware],
handler: async (ctx) => {
const chatId = parseInt(ctx.input.path.chatId);
const userId = ctx.data.userId;

const isParticipant = await db.query.chatParticipants.findFirst({
where: and(
eq(chatParticipants.userId, userId),
eq(chatParticipants.chatId, chatId)
),
});
if (!isParticipant)
return ctx.error(
"UNAUTHORIZED",
"You are not a participant of this chat"
);

const chat = await db.query.chats.findFirst({
where: eq(chats.id, chatId),
});
if (!chat) return ctx.error("NOT_FOUND", "Chat not found.");
const chatMessages = await db.query.messages.findMany({
where: and(
eq(messages.chatId, chatId),
ilike(messages.content, `%${ctx.input.query.searchTerm}%`)
),
orderBy: [desc(messages.createdAt)],
with: {
sender: true,
readReceipnts: true,
attachments: true,
},
});

const blockerIds = await getUserBlockers(userId);

const processedMessages = chatMessages.map((message) => {
if (blockerIds.has(message.senderId)) {
return {
...message,
sender: unavailableUserData(),
};
}
return message;
});

return ctx.success({
messages: processedMessages,
});
},
}),
}),
});

export default chatRouter;
Loading