feat(services): add POST /channels/[channelId]/messages/uploads
This commit is contained in:
@ -22,7 +22,8 @@ const bodyPostServiceSchema = Type.Object({
|
||||
type BodyPostServiceSchemaType = Static<typeof bodyPostServiceSchema>
|
||||
|
||||
const postServiceSchema: FastifySchema = {
|
||||
description: 'POST a new message in a specific channel using its channelId.',
|
||||
description:
|
||||
'POST a new message (text) in a specific channel using its channelId.',
|
||||
tags: ['messages'] as string[],
|
||||
security: [
|
||||
{
|
||||
@ -43,6 +44,7 @@ const postServiceSchema: FastifySchema = {
|
||||
401: fastifyErrors[401],
|
||||
403: fastifyErrors[403],
|
||||
404: fastifyErrors[404],
|
||||
431: fastifyErrors[431],
|
||||
500: fastifyErrors[500]
|
||||
}
|
||||
} as const
|
||||
|
121
src/services/channels/[channelId]/messages/uploads/post.ts
Normal file
121
src/services/channels/[channelId]/messages/uploads/post.ts
Normal file
@ -0,0 +1,121 @@
|
||||
import { Type, Static } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import fastifyMultipart from 'fastify-multipart'
|
||||
|
||||
import prisma from '../../../../../tools/database/prisma.js'
|
||||
import { fastifyErrors } from '../../../../../models/utils.js'
|
||||
import authenticateUser from '../../../../../tools/plugins/authenticateUser.js'
|
||||
import { messageSchema } from '../../../../../models/Message.js'
|
||||
import { memberSchema } from '../../../../../models/Member.js'
|
||||
import { userPublicWithoutSettingsSchema } from '../../../../../models/User.js'
|
||||
import { channelSchema } from '../../../../../models/Channel.js'
|
||||
import { uploadFile } from '../../../../../tools/utils/uploadFile.js'
|
||||
import { maximumFileSize } from '../../../../../tools/configurations/index.js'
|
||||
|
||||
const parametersSchema = Type.Object({
|
||||
channelId: channelSchema.id
|
||||
})
|
||||
|
||||
type Parameters = Static<typeof parametersSchema>
|
||||
|
||||
const postServiceSchema: FastifySchema = {
|
||||
description:
|
||||
'POST a new message (file) in a specific channel using its channelId.',
|
||||
tags: ['messages'] as string[],
|
||||
consumes: ['multipart/form-data'] as string[],
|
||||
produces: ['application/json'] as string[],
|
||||
security: [
|
||||
{
|
||||
bearerAuth: []
|
||||
}
|
||||
] as Array<{ [key: string]: [] }>,
|
||||
params: parametersSchema,
|
||||
response: {
|
||||
200: 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 postMessageUploadsByChannelIdService: FastifyPluginAsync = async (
|
||||
fastify
|
||||
) => {
|
||||
await fastify.register(authenticateUser)
|
||||
|
||||
await fastify.register(fastifyMultipart)
|
||||
|
||||
fastify.route<{
|
||||
Params: Parameters
|
||||
}>({
|
||||
method: 'POST',
|
||||
url: '/channels/:channelId/messages/uploads',
|
||||
schema: postServiceSchema,
|
||||
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 },
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
logo: true,
|
||||
status: true,
|
||||
biography: true,
|
||||
website: true,
|
||||
createdAt: true,
|
||||
updatedAt: true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
if (memberCheck == null) {
|
||||
throw fastify.httpErrors.notFound('Channel not found')
|
||||
}
|
||||
const file = await uploadFile({
|
||||
fastify,
|
||||
request,
|
||||
folderInUploadsFolder: 'messages',
|
||||
maximumFileSize: maximumFileSize
|
||||
})
|
||||
const message = await prisma.message.create({
|
||||
data: {
|
||||
value: file.pathToStoreInDatabase,
|
||||
type: 'file',
|
||||
mimetype: file.mimetype,
|
||||
channelId,
|
||||
memberId: memberCheck.id
|
||||
}
|
||||
})
|
||||
reply.statusCode = 201
|
||||
return {
|
||||
...message,
|
||||
member: {
|
||||
...memberCheck,
|
||||
user: {
|
||||
...memberCheck.user,
|
||||
email: null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
@ -3,9 +3,11 @@ import { FastifyPluginAsync } from 'fastify'
|
||||
import { getChannelByIdService } from './[channelId]/get.js'
|
||||
import { getMessagesByChannelIdService } from './[channelId]/messages/get.js'
|
||||
import { postMessageByChannelIdService } from './[channelId]/messages/post.js'
|
||||
import { postMessageUploadsByChannelIdService } from './[channelId]/messages/uploads/post.js'
|
||||
|
||||
export const channelsService: FastifyPluginAsync = async (fastify) => {
|
||||
await fastify.register(getChannelByIdService)
|
||||
await fastify.register(getMessagesByChannelIdService)
|
||||
await fastify.register(postMessageByChannelIdService)
|
||||
await fastify.register(postMessageUploadsByChannelIdService)
|
||||
}
|
||||
|
@ -5,8 +5,12 @@ import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
import fastifyMultipart from 'fastify-multipart'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { uploadImage } from '../../../../tools/utils/uploadImage.js'
|
||||
import { uploadFile } from '../../../../tools/utils/uploadFile.js'
|
||||
import { guildSchema } from '../../../../models/Guild.js'
|
||||
import {
|
||||
maximumImageSize,
|
||||
supportedImageMimetype
|
||||
} from '../../../../tools/configurations/index.js'
|
||||
|
||||
const parametersSchema = Type.Object({
|
||||
guildId: guildSchema.id
|
||||
@ -60,19 +64,21 @@ export const putGuildIconById: FastifyPluginAsync = async (fastify) => {
|
||||
if (guild == null) {
|
||||
throw fastify.httpErrors.notFound()
|
||||
}
|
||||
const icon = await uploadImage({
|
||||
const file = await uploadFile({
|
||||
fastify,
|
||||
request,
|
||||
folderInUploadsFolder: 'guilds'
|
||||
folderInUploadsFolder: 'guilds',
|
||||
maximumFileSize: maximumImageSize,
|
||||
supportedFileMimetype: supportedImageMimetype
|
||||
})
|
||||
await prisma.guild.update({
|
||||
where: { id: guildId },
|
||||
data: { icon }
|
||||
data: { icon: file.pathToStoreInDatabase }
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return {
|
||||
guild: {
|
||||
icon
|
||||
icon: file.pathToStoreInDatabase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ import path from 'node:path'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
|
||||
import { fastifyErrors } from '../../models/utils'
|
||||
|
||||
const parametersUploadsSchema = Type.Object({
|
||||
image: Type.String()
|
||||
})
|
||||
@ -11,7 +13,16 @@ type ParametersUploadsSchemaType = Static<typeof parametersUploadsSchema>
|
||||
|
||||
const getUploadsSchema: FastifySchema = {
|
||||
tags: ['uploads'] as string[],
|
||||
params: parametersUploadsSchema
|
||||
params: parametersUploadsSchema,
|
||||
response: {
|
||||
200: {
|
||||
type: 'string',
|
||||
format: 'binary'
|
||||
},
|
||||
400: fastifyErrors[400],
|
||||
404: fastifyErrors[404],
|
||||
500: fastifyErrors[500]
|
||||
}
|
||||
} as const
|
||||
|
||||
export const uploadsService: FastifyPluginAsync = async (fastify) => {
|
||||
@ -24,4 +35,24 @@ export const uploadsService: FastifyPluginAsync = async (fastify) => {
|
||||
return await reply.sendFile(path.join('users', image))
|
||||
}
|
||||
})
|
||||
|
||||
fastify.route<{ Params: ParametersUploadsSchemaType }>({
|
||||
method: 'GET',
|
||||
url: '/uploads/guilds/:image',
|
||||
schema: getUploadsSchema,
|
||||
handler: async (request, reply) => {
|
||||
const { image } = request.params
|
||||
return await reply.sendFile(path.join('guilds', image))
|
||||
}
|
||||
})
|
||||
|
||||
fastify.route<{ Params: ParametersUploadsSchemaType }>({
|
||||
method: 'GET',
|
||||
url: '/uploads/messages/:image',
|
||||
schema: getUploadsSchema,
|
||||
handler: async (request, reply) => {
|
||||
const { image } = request.params
|
||||
return await reply.sendFile(path.join('messages', image))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -5,7 +5,11 @@ import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
import fastifyMultipart from 'fastify-multipart'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { uploadImage } from '../../../../tools/utils/uploadImage.js'
|
||||
import { uploadFile } from '../../../../tools/utils/uploadFile.js'
|
||||
import {
|
||||
maximumImageSize,
|
||||
supportedImageMimetype
|
||||
} from '../../../../tools/configurations/index.js'
|
||||
|
||||
const putServiceSchema: FastifySchema = {
|
||||
description: 'Edit the current connected user logo',
|
||||
@ -44,19 +48,21 @@ export const putCurrentUserLogo: FastifyPluginAsync = async (fastify) => {
|
||||
if (request.user == null) {
|
||||
throw fastify.httpErrors.forbidden()
|
||||
}
|
||||
const logo = await uploadImage({
|
||||
const file = await uploadFile({
|
||||
fastify,
|
||||
request,
|
||||
folderInUploadsFolder: 'users'
|
||||
folderInUploadsFolder: 'users',
|
||||
maximumFileSize: maximumImageSize,
|
||||
supportedFileMimetype: supportedImageMimetype
|
||||
})
|
||||
await prisma.user.update({
|
||||
where: { id: request.user.current.id },
|
||||
data: { logo }
|
||||
data: { logo: file.pathToStoreInDatabase }
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return {
|
||||
user: {
|
||||
logo
|
||||
logo: file.pathToStoreInDatabase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user