работаем бля работаем
This commit is contained in:
@@ -2,6 +2,8 @@ import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
|
||||
import * as fs from 'node:fs'
|
||||
import * as path from 'node:path'
|
||||
import { Type } from 'typebox'
|
||||
import { GetAttachmentParamsSchema } from '../plugins/schemas/attachment.ts'
|
||||
import { TypeboxRef } from '../utils/typebox-ref.ts'
|
||||
|
||||
const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
const uploadDir = path.join(process.cwd(), 'uploads')
|
||||
@@ -18,6 +20,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
tags: ['Attachment'],
|
||||
operationId: 'attachment.upload',
|
||||
description: 'Pass file to multipart/form-data',
|
||||
consumes: ['multipart/form-data'],
|
||||
response: {
|
||||
200: Type.String({ format: 'uuid', description: 'Attachment UUID' }),
|
||||
},
|
||||
@@ -62,9 +65,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
summary: 'Get attachment',
|
||||
tags: ['Attachment'],
|
||||
operationId: 'attachment.get',
|
||||
params: Type.Object({
|
||||
id: Type.String({ format: 'uuid' }),
|
||||
}),
|
||||
params: TypeboxRef(GetAttachmentParamsSchema),
|
||||
response: {
|
||||
200: Type.Any({ description: 'Attachment content' }),
|
||||
},
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
|
||||
import bcrypt from 'bcrypt'
|
||||
import { Type } from 'typebox'
|
||||
import { CreateUserSchema, UserSchema } from '../schemas/auth.ts'
|
||||
import { CreateUserPayloadSchema, LoginPayloadSchema, UserSchema } from '../plugins/schemas/auth.ts'
|
||||
import { TypeboxRef } from '../utils/typebox-ref.ts'
|
||||
|
||||
const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
fastify.post(
|
||||
@@ -11,9 +11,9 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
summary: 'Register',
|
||||
tags: ['Auth'],
|
||||
operationId: 'auth.register',
|
||||
body: CreateUserSchema,
|
||||
body: TypeboxRef(CreateUserPayloadSchema),
|
||||
response: {
|
||||
200: UserSchema,
|
||||
200: TypeboxRef(UserSchema),
|
||||
},
|
||||
},
|
||||
config: {
|
||||
@@ -54,12 +54,9 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
summary: 'Login',
|
||||
tags: ['Auth'],
|
||||
operationId: 'auth.login',
|
||||
body: Type.Object({
|
||||
username: Type.String({ minLength: 1 }),
|
||||
password: Type.String({ minLength: 1 }),
|
||||
}),
|
||||
body: TypeboxRef(LoginPayloadSchema),
|
||||
response: {
|
||||
200: UserSchema,
|
||||
200: TypeboxRef(UserSchema),
|
||||
},
|
||||
},
|
||||
config: {
|
||||
@@ -107,7 +104,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
tags: ['Auth'],
|
||||
operationId: 'auth.me',
|
||||
response: {
|
||||
200: UserSchema,
|
||||
200: TypeboxRef(UserSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
90
server/routes/channel.ts
Normal file
90
server/routes/channel.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
|
||||
import { Type } from 'typebox'
|
||||
import { ChannelSchema, CreateChannelPayloadSchema } from '../plugins/schemas/channel.ts'
|
||||
import { PrismaClientKnownRequestError } from '../prisma/generated-client/internal/prismaNamespace.ts'
|
||||
import { TypeboxRef } from '../utils/typebox-ref.ts'
|
||||
|
||||
const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
fastify.get(
|
||||
'/channels',
|
||||
{
|
||||
schema: {
|
||||
summary: 'Get channel list',
|
||||
tags: ['Channel'],
|
||||
operationId: 'channel.list',
|
||||
response: {
|
||||
200: Type.Array(TypeboxRef(ChannelSchema)),
|
||||
},
|
||||
},
|
||||
},
|
||||
async () => {
|
||||
return await fastify.prisma.channel.findMany()
|
||||
},
|
||||
)
|
||||
|
||||
fastify.post(
|
||||
'/channels',
|
||||
{
|
||||
schema: {
|
||||
summary: 'Create channel',
|
||||
tags: ['Channel'],
|
||||
operationId: 'channel.create',
|
||||
body: TypeboxRef(CreateChannelPayloadSchema),
|
||||
response: {
|
||||
200: TypeboxRef(ChannelSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
async (req, reply) => {
|
||||
const user = req.user!
|
||||
|
||||
const channel = await fastify.prisma.channel.create({
|
||||
data: {
|
||||
name: req.body.name,
|
||||
ownerId: user.id,
|
||||
persistent: req.body.persistent,
|
||||
},
|
||||
})
|
||||
|
||||
if (!channel) {
|
||||
return reply.unprocessableEntity()
|
||||
}
|
||||
|
||||
fastify.bus.emit('channel:created', channel)
|
||||
|
||||
return channel
|
||||
},
|
||||
)
|
||||
|
||||
fastify.delete(
|
||||
'/channels/:id',
|
||||
{
|
||||
schema: {
|
||||
summary: 'Delete channel',
|
||||
tags: ['Channel'],
|
||||
operationId: 'channel.delete',
|
||||
params: Type.Object({
|
||||
id: Type.String(),
|
||||
}),
|
||||
},
|
||||
},
|
||||
async (req, reply) => {
|
||||
const user = req.user!
|
||||
|
||||
try {
|
||||
const channel = await fastify.prisma.channel.delete({
|
||||
where: { id: req.params.id, ownerId: user.id },
|
||||
})
|
||||
|
||||
fastify.bus.emit('channel:removed', channel)
|
||||
}
|
||||
catch (e) {
|
||||
if (e instanceof PrismaClientKnownRequestError && e.code === 'P2025') {
|
||||
return reply.notFound()
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
export default plugin
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
|
||||
import { Type } from 'typebox'
|
||||
import { ChatMessageSchema, NewChatMessageSchema } from '../schemas/chat.ts'
|
||||
import { ChatMessageSchema, NewChatMessagePayloadSchema } from '../plugins/schemas/chat.ts'
|
||||
|
||||
const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
fastify.post(
|
||||
@@ -10,7 +10,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
summary: 'Send message',
|
||||
tags: ['Chat'],
|
||||
operationId: 'chat.send',
|
||||
body: NewChatMessageSchema,
|
||||
body: NewChatMessagePayloadSchema,
|
||||
response: {
|
||||
200: ChatMessageSchema,
|
||||
},
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'
|
||||
import { Type } from 'typebox'
|
||||
import { UserSchema } from '../schemas/auth.ts'
|
||||
import { UpdateUserPreferencesSchema, UserPreferencesSchema } from '../schemas/user.ts'
|
||||
import { UserSchema } from '../plugins/schemas/auth.ts'
|
||||
import { GetUserQuerySchema, UpdateUserPayloadSchema, UpdateUserPreferencesPayloadSchema, UserPreferencesSchema } from '../plugins/schemas/user.ts'
|
||||
import { TypeboxRef } from '../utils/typebox-ref.ts'
|
||||
|
||||
const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
fastify.get(
|
||||
@@ -11,11 +11,9 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
summary: 'Get user',
|
||||
tags: ['User'],
|
||||
operationId: 'user.get',
|
||||
querystring: Type.Partial(Type.Object({
|
||||
username: Type.String(),
|
||||
})),
|
||||
querystring: TypeboxRef(GetUserQuerySchema),
|
||||
response: {
|
||||
200: UserSchema,
|
||||
200: TypeboxRef(UserSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -49,11 +47,11 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
tags: ['User'],
|
||||
operationId: 'user.getPreferences',
|
||||
response: {
|
||||
200: UserPreferencesSchema,
|
||||
200: TypeboxRef(UserPreferencesSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
async (req, reply) => {
|
||||
async (req) => {
|
||||
const user = req.user!
|
||||
|
||||
const preferences = await fastify.prisma.userPreferences.upsert({
|
||||
@@ -62,10 +60,6 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
update: {},
|
||||
})
|
||||
|
||||
if (!preferences) {
|
||||
return reply.notFound('User preferences not found')
|
||||
}
|
||||
|
||||
return {
|
||||
toggleInputHotkey: preferences.toggleInputHotkey || '',
|
||||
toggleOutputHotkey: preferences.toggleOutputHotkey || '',
|
||||
@@ -80,7 +74,7 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
summary: 'Update preferences',
|
||||
tags: ['User'],
|
||||
operationId: 'user.updatePreferences',
|
||||
body: UpdateUserPreferencesSchema,
|
||||
body: TypeboxRef(UpdateUserPreferencesPayloadSchema),
|
||||
},
|
||||
},
|
||||
async (req) => {
|
||||
@@ -104,11 +98,9 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
|
||||
summary: 'Update profile',
|
||||
tags: ['User'],
|
||||
operationId: 'user.updateProfile',
|
||||
body: Type.Object({
|
||||
displayName: Type.String(),
|
||||
}),
|
||||
body: TypeboxRef(UpdateUserPayloadSchema),
|
||||
response: {
|
||||
200: UserSchema,
|
||||
200: TypeboxRef(UserSchema),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user