From 421b07b01fca0b61635c8919b56c5e12801c7f4c Mon Sep 17 00:00:00 2001 From: Aseer KT Date: Tue, 30 Jul 2024 10:22:39 +0530 Subject: [PATCH] feat: realtime reply message update --- .../src/modules/messages/messages.service.ts | 46 +++++++++++++------ .../message/components/MessageList.tsx | 2 +- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/server/src/modules/messages/messages.service.ts b/server/src/modules/messages/messages.service.ts index 7c9ebe8..b7273c9 100644 --- a/server/src/modules/messages/messages.service.ts +++ b/server/src/modules/messages/messages.service.ts @@ -1,5 +1,5 @@ import { db } from '@/database' -import { and, eq, isNull } from 'drizzle-orm' +import { and, eq, getTableColumns, isNull } from 'drizzle-orm' import { groupsTable } from '../groups/groups.schema' import { checkPermission } from '../members/members.service' import { usersTable } from '../users/users.schema' @@ -18,12 +18,27 @@ export const insertMessage = async ({ senderId: number parentMessageId?: number }) => { - let chatName = '' if (groupId) { const { isAllowed } = await checkPermission(groupId, senderId, 'member') if (!isAllowed) { throw new Error('createMessage: Not authorized') } + } + + const [message] = await db + .insert(messagesTable) + .values({ + groupId, + receiverId, + content, + senderId, + parentMessageId, + }) + .returning() + + let chatName = '' + + if (groupId) { const [group] = await db .select({ name: groupsTable.name }) .from(groupsTable) @@ -39,17 +54,22 @@ export const insertMessage = async ({ chatName = receiver.username } - const [message] = await db - .insert(messagesTable) - .values({ - groupId, - receiverId, - content, - senderId, - parentMessageId, - }) - .returning() - return { ...message, chatName } + let parentMessage + + if (message.parentMessageId) { + const result = await db + .select({ + ...getTableColumns(messagesTable), + username: usersTable.username, + }) + .from(messagesTable) + .where(eq(messagesTable.id, message.parentMessageId)) + .innerJoin(usersTable, eq(usersTable.id, messagesTable.senderId)) + .limit(1) + parentMessage = result[0] + } + + return { ...message, chatName, parentMessage } } export const markMessageAsRead = async ( diff --git a/web/src/features/message/components/MessageList.tsx b/web/src/features/message/components/MessageList.tsx index 99ed39c..3083897 100644 --- a/web/src/features/message/components/MessageList.tsx +++ b/web/src/features/message/components/MessageList.tsx @@ -98,7 +98,7 @@ export const MessageList = ({
{content} {scrollElement}