Skip to content

Commit

Permalink
feat: realtime reply message update
Browse files Browse the repository at this point in the history
  • Loading branch information
aseerkt committed Jul 30, 2024
1 parent 64f1b11 commit 421b07b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
46 changes: 33 additions & 13 deletions server/src/modules/messages/messages.service.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand All @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion web/src/features/message/components/MessageList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const MessageList = ({
<main className='flex-1 overflow-hidden'>
<div
ref={listRef}
className='flex h-full flex-col-reverse justify-start gap-2 overflow-y-auto py-3'
className='flex h-full flex-col-reverse justify-start gap-2 overflow-y-auto scroll-smooth py-3'
>
{content}
{scrollElement}
Expand Down

0 comments on commit 421b07b

Please sign in to comment.