brutalism design
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
|
||||
import { Type } from 'typebox'
|
||||
import { ChatMessageSchema, NewChatMessagePayloadSchema } from '../plugins/schemas/chat.ts'
|
||||
import { TypeboxRef } from '../utils/typebox-ref.ts'
|
||||
|
||||
const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
fastify.post(
|
||||
@@ -10,9 +11,9 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
summary: 'Send message',
|
||||
tags: ['Chat'],
|
||||
operationId: 'chat.send',
|
||||
body: NewChatMessagePayloadSchema,
|
||||
body: TypeboxRef(NewChatMessagePayloadSchema),
|
||||
response: {
|
||||
200: ChatMessageSchema,
|
||||
200: TypeboxRef(ChatMessageSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -22,7 +23,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
const message = await fastify.prisma.message.create({
|
||||
data: {
|
||||
text: req.body.text,
|
||||
senderId: user.id,
|
||||
senderUsername: user.username,
|
||||
attachments: {
|
||||
create: (req.body.attachments ?? []).map((attachmentId) => {
|
||||
return {
|
||||
@@ -35,6 +36,13 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
}),
|
||||
},
|
||||
},
|
||||
include: {
|
||||
attachments: {
|
||||
include: {
|
||||
attachment: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!message) {
|
||||
@@ -43,11 +51,11 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
|
||||
const response = {
|
||||
id: message.id,
|
||||
senderId: user.id,
|
||||
senderUsername: user.username,
|
||||
text: message.text,
|
||||
createdAt: message.createdAt.toISOString(),
|
||||
updatedAt: message.updatedAt.toISOString(),
|
||||
attachments: req.body.attachments ?? [],
|
||||
attachments: message.attachments.map(({ attachment }) => attachment),
|
||||
}
|
||||
|
||||
fastify.bus.emit('chat:new-message', response)
|
||||
@@ -69,7 +77,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
}),
|
||||
response: {
|
||||
200: Type.Object({
|
||||
messages: Type.Array(ChatMessageSchema),
|
||||
messages: Type.Array(TypeboxRef(ChatMessageSchema)),
|
||||
nextCursor: Type.Optional(Type.String({ format: 'uuid', description: 'Cursor to last message' })),
|
||||
}),
|
||||
},
|
||||
@@ -81,7 +89,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
const messages = await fastify.prisma.message.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
take: req.query.limit + 1,
|
||||
include: { attachments: true },
|
||||
include: { attachments: { include: { attachment: true } } },
|
||||
...(req.query.cursor && {
|
||||
cursor: {
|
||||
id: req.query.cursor,
|
||||
@@ -99,7 +107,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
...message,
|
||||
createdAt: message.createdAt.toISOString(),
|
||||
updatedAt: message.updatedAt.toISOString(),
|
||||
attachments: message.attachments.map(({ attachmentId }) => attachmentId),
|
||||
attachments: message.attachments.map(({ attachment }) => attachment),
|
||||
}
|
||||
}),
|
||||
nextCursor: cursorMessage?.id,
|
||||
|
||||
Reference in New Issue
Block a user