123 lines
3.5 KiB
TypeScript
123 lines
3.5 KiB
TypeScript
import type { Static } from '@sinclair/typebox'
|
|
import { Type } from '@sinclair/typebox'
|
|
import type { FastifyPluginAsync, FastifySchema } from 'fastify'
|
|
|
|
import prisma from '#src/tools/database/prisma.js'
|
|
import { fastifyErrors } from '#src/models/utils.js'
|
|
import authenticateUser from '#src/tools/plugins/authenticateUser.js'
|
|
import { messageSchema } from '#src/models/Message.js'
|
|
import { memberSchema } from '#src/models/Member.js'
|
|
import { userPublicWithoutSettingsSchema } from '#src/models/User.js'
|
|
import {
|
|
getPaginationOptions,
|
|
queryPaginationObjectSchema
|
|
} from '#src/tools/database/pagination.js'
|
|
import { channelSchema } from '#src/models/Channel.js'
|
|
|
|
type QuerySchemaType = Static<typeof queryPaginationObjectSchema>
|
|
|
|
const parametersSchema = Type.Object({
|
|
channelId: channelSchema.id
|
|
})
|
|
|
|
type Parameters = Static<typeof parametersSchema>
|
|
|
|
const getServiceSchema: FastifySchema = {
|
|
description: 'GET all the messages of a channel by its id.',
|
|
tags: ['messages'] as string[],
|
|
security: [
|
|
{
|
|
bearerAuth: []
|
|
}
|
|
] as Array<{ [key: string]: [] }>,
|
|
params: parametersSchema,
|
|
querystring: queryPaginationObjectSchema,
|
|
response: {
|
|
200: Type.Array(
|
|
Type.Object({
|
|
...messageSchema,
|
|
member: Type.Object({
|
|
...memberSchema,
|
|
user: Type.Object(userPublicWithoutSettingsSchema)
|
|
})
|
|
})
|
|
),
|
|
400: fastifyErrors[400],
|
|
401: fastifyErrors[401],
|
|
403: fastifyErrors[403],
|
|
404: fastifyErrors[404],
|
|
500: fastifyErrors[500]
|
|
}
|
|
} as const
|
|
|
|
export const getMessagesByChannelIdService: FastifyPluginAsync = async (
|
|
fastify
|
|
) => {
|
|
await fastify.register(authenticateUser)
|
|
|
|
fastify.route<{
|
|
Params: Parameters
|
|
Querystring: QuerySchemaType
|
|
}>({
|
|
method: 'GET',
|
|
url: '/channels/:channelId/messages',
|
|
schema: getServiceSchema,
|
|
handler: async (request, reply) => {
|
|
if (request.user == null) {
|
|
throw fastify.httpErrors.forbidden()
|
|
}
|
|
const { channelId } = request.params
|
|
const channel = await prisma.channel.findUnique({
|
|
where: { id: channelId }
|
|
})
|
|
if (channel == null) {
|
|
throw fastify.httpErrors.notFound('Channel not found')
|
|
}
|
|
const memberCheck = await prisma.member.findFirst({
|
|
where: { guildId: channel.guildId, userId: request.user.current.id }
|
|
})
|
|
if (memberCheck == null) {
|
|
throw fastify.httpErrors.notFound('Channel not found')
|
|
}
|
|
const messagesRequest = await prisma.message.findMany({
|
|
...getPaginationOptions(request.query),
|
|
orderBy: { createdAt: 'desc' },
|
|
where: { channelId }
|
|
})
|
|
const messages = await Promise.all(
|
|
messagesRequest.reverse().map(async (message) => {
|
|
const member = await prisma.member.findFirst({
|
|
where: { id: message.memberId },
|
|
include: {
|
|
user: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
logo: true,
|
|
status: true,
|
|
biography: true,
|
|
website: true,
|
|
createdAt: true,
|
|
updatedAt: true
|
|
}
|
|
}
|
|
}
|
|
})
|
|
return {
|
|
...message,
|
|
member: {
|
|
...member,
|
|
user: {
|
|
...member?.user,
|
|
email: null
|
|
}
|
|
}
|
|
}
|
|
})
|
|
)
|
|
reply.statusCode = 200
|
|
return messages
|
|
}
|
|
})
|
|
}
|