feat(services): add PUT /guilds/[guildId]/icon
This commit is contained in:
80
src/services/guilds/[guildId]/icon/put.ts
Normal file
80
src/services/guilds/[guildId]/icon/put.ts
Normal file
@ -0,0 +1,80 @@
|
||||
import { Static, Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
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 { guildSchema } from '../../../../models/Guild.js'
|
||||
|
||||
const parametersSchema = Type.Object({
|
||||
guildId: guildSchema.id
|
||||
})
|
||||
|
||||
type Parameters = Static<typeof parametersSchema>
|
||||
|
||||
const putServiceSchema: FastifySchema = {
|
||||
description: 'Edit the icon of the guild with its id',
|
||||
tags: ['guilds'] 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({
|
||||
guild: Type.Object({
|
||||
icon: Type.String()
|
||||
})
|
||||
}),
|
||||
400: fastifyErrors[400],
|
||||
401: fastifyErrors[401],
|
||||
403: fastifyErrors[403],
|
||||
404: fastifyErrors[404],
|
||||
431: fastifyErrors[431],
|
||||
500: fastifyErrors[500]
|
||||
}
|
||||
} as const
|
||||
|
||||
export const putGuildIconById: FastifyPluginAsync = async (fastify) => {
|
||||
await fastify.register(authenticateUser)
|
||||
|
||||
await fastify.register(fastifyMultipart)
|
||||
|
||||
fastify.route<{
|
||||
Params: Parameters
|
||||
}>({
|
||||
method: 'PUT',
|
||||
url: '/guilds/:guildId/icon',
|
||||
schema: putServiceSchema,
|
||||
handler: async (request, reply) => {
|
||||
if (request.user == null) {
|
||||
throw fastify.httpErrors.forbidden()
|
||||
}
|
||||
const { guildId } = request.params
|
||||
const guild = await prisma.guild.findUnique({ where: { id: guildId } })
|
||||
if (guild == null) {
|
||||
throw fastify.httpErrors.notFound()
|
||||
}
|
||||
const icon = await uploadImage({
|
||||
fastify,
|
||||
request,
|
||||
folderInUploadsFolder: 'guilds'
|
||||
})
|
||||
await prisma.guild.update({
|
||||
where: { id: guildId },
|
||||
data: { icon }
|
||||
})
|
||||
reply.statusCode = 200
|
||||
return {
|
||||
guild: {
|
||||
icon
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
import { FastifyPluginAsync } from 'fastify'
|
||||
|
||||
import { postGuilds } from './post.js'
|
||||
import { putGuildIconById } from './[guildId]/icon/put.js'
|
||||
|
||||
export const guildsService: FastifyPluginAsync = async (fastify) => {
|
||||
await fastify.register(postGuilds)
|
||||
await fastify.register(putGuildIconById)
|
||||
}
|
||||
|
@ -7,7 +7,8 @@ import authenticateUser from '../../tools/plugins/authenticateUser.js'
|
||||
import { guildSchema } from '../../models/Guild.js'
|
||||
import { channelSchema } from '../../models/Channel.js'
|
||||
import { memberSchema } from '../../models/Member.js'
|
||||
import { userPublicSchema } from '../../models/User.js'
|
||||
import { userPublicWithoutSettingsSchema } from '../../models/User.js'
|
||||
import { parseStringNullish } from '../../tools/utils/parseStringNullish.js'
|
||||
|
||||
const bodyPostServiceSchema = Type.Object({
|
||||
name: guildSchema.name,
|
||||
@ -33,7 +34,7 @@ const postServiceSchema: FastifySchema = {
|
||||
members: Type.Array(
|
||||
Type.Object({
|
||||
...memberSchema,
|
||||
user: Type.Object(userPublicSchema)
|
||||
user: Type.Object(userPublicWithoutSettingsSchema)
|
||||
})
|
||||
)
|
||||
})
|
||||
@ -59,7 +60,9 @@ export const postGuilds: FastifyPluginAsync = async (fastify) => {
|
||||
throw fastify.httpErrors.forbidden()
|
||||
}
|
||||
const { name, description } = request.body
|
||||
const guild = await prisma.guild.create({ data: { name, description } })
|
||||
const guild = await prisma.guild.create({
|
||||
data: { name, description: parseStringNullish(description) }
|
||||
})
|
||||
const channel = await prisma.channel.create({
|
||||
data: { name: 'general', guildId: guild.id }
|
||||
})
|
||||
|
@ -10,7 +10,7 @@ const parametersGetUserSchema = Type.Object({
|
||||
userId: userPublicSchema.id
|
||||
})
|
||||
|
||||
export type ParametersGetUser = Static<typeof parametersGetUserSchema>
|
||||
type ParametersGetUser = Static<typeof parametersGetUserSchema>
|
||||
|
||||
const getServiceSchema: FastifySchema = {
|
||||
description: 'GET the public user informations with its id',
|
||||
|
@ -1,19 +1,11 @@
|
||||
import fs from 'node:fs'
|
||||
import { URL } from 'node:url'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
|
||||
import { Type } from '@sinclair/typebox'
|
||||
import { FastifyPluginAsync, FastifySchema } from 'fastify'
|
||||
|
||||
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
|
||||
import { fastifyErrors } from '../../../../models/utils.js'
|
||||
import fastifyMultipart, { Multipart } from 'fastify-multipart'
|
||||
import {
|
||||
maximumImageSize,
|
||||
supportedImageMimetype,
|
||||
ROOT_URL
|
||||
} from '../../../../tools/configurations'
|
||||
import fastifyMultipart from 'fastify-multipart'
|
||||
import prisma from '../../../../tools/database/prisma.js'
|
||||
import { uploadImage } from '../../../../tools/utils/uploadImage.js'
|
||||
|
||||
const putServiceSchema: FastifySchema = {
|
||||
description: 'Edit the current connected user logo',
|
||||
@ -52,36 +44,11 @@ export const putCurrentUserLogo: FastifyPluginAsync = async (fastify) => {
|
||||
if (request.user == null) {
|
||||
throw fastify.httpErrors.forbidden()
|
||||
}
|
||||
let files: Multipart[] = []
|
||||
try {
|
||||
files = await request.saveRequestFiles({
|
||||
limits: {
|
||||
files: 1,
|
||||
fileSize: maximumImageSize * 1024 * 1024
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
throw fastify.httpErrors.requestHeaderFieldsTooLarge(
|
||||
`body.logo should be less than ${maximumImageSize}mb.`
|
||||
)
|
||||
}
|
||||
if (files.length !== 1) {
|
||||
throw fastify.httpErrors.badRequest('You must upload at most one file.')
|
||||
}
|
||||
const image = files[0]
|
||||
if (!supportedImageMimetype.includes(image.mimetype)) {
|
||||
throw fastify.httpErrors.badRequest(
|
||||
`The file must have a valid type (${supportedImageMimetype.join(
|
||||
', '
|
||||
)}).`
|
||||
)
|
||||
}
|
||||
const splitedMimetype = image.mimetype.split('/')
|
||||
const imageExtension = splitedMimetype[1]
|
||||
const logoPath = `uploads/users/${randomUUID()}.${imageExtension}`
|
||||
const logoURL = new URL(logoPath, ROOT_URL)
|
||||
const logo = `/${logoPath}`
|
||||
await fs.promises.copyFile(image.filepath, logoURL)
|
||||
const logo = await uploadImage({
|
||||
fastify,
|
||||
request,
|
||||
folderInUploadsFolder: 'users'
|
||||
})
|
||||
await prisma.user.update({
|
||||
where: { id: request.user.current.id },
|
||||
data: { logo }
|
||||
|
@ -10,6 +10,7 @@ import { userCurrentSchema, userSchema } from '../../../models/User.js'
|
||||
import { sendEmail } from '../../../tools/email/sendEmail.js'
|
||||
import { HOST, PORT } from '../../../tools/configurations/index.js'
|
||||
import { Language, Theme } from '../../../models/UserSettings.js'
|
||||
import { parseStringNullish } from '../../../tools/utils/parseStringNullish.js'
|
||||
|
||||
const bodyPutServiceSchema = Type.Object({
|
||||
name: Type.Optional(userSchema.name),
|
||||
@ -117,9 +118,12 @@ export const putCurrentUser: FastifyPluginAsync = async (fastify) => {
|
||||
where: { id: request.user.current.id },
|
||||
data: {
|
||||
name: name ?? request.user.current.name,
|
||||
status: status ?? request.user.current.status,
|
||||
biography: biography ?? request.user.current.biography,
|
||||
website: website ?? request.user.current.website
|
||||
status: parseStringNullish(request.user.current.status, status),
|
||||
biography: parseStringNullish(
|
||||
request.user.current.biography,
|
||||
biography
|
||||
),
|
||||
website: parseStringNullish(request.user.current.website, website)
|
||||
}
|
||||
})
|
||||
reply.statusCode = 200
|
||||
|
Reference in New Issue
Block a user