small fixes so calls make sense
This commit is contained in:
56
server/api/channels/[id]/index.get.ts
Normal file
56
server/api/channels/[id]/index.get.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
if (!event.context.user.authenticated) return {
|
||||
message: 'You must be logged in to view a channel.'
|
||||
}
|
||||
|
||||
if (!event.context.params.id) {
|
||||
event.node.res.statusCode = 400;
|
||||
return {
|
||||
message: 'A channelId is required'
|
||||
}
|
||||
}
|
||||
|
||||
const channel = await prisma.channel.findFirst({
|
||||
where: {
|
||||
id: event.context.params.id
|
||||
},
|
||||
include: {
|
||||
messages: true,
|
||||
}
|
||||
})
|
||||
|
||||
if (!channel) {
|
||||
event.node.res.statusCode = 404;
|
||||
return {
|
||||
message: `Channel with id "${event.context.params.id}" not found`
|
||||
}
|
||||
}
|
||||
|
||||
if (channel.id && !channel.DM) {
|
||||
const server = await prisma.server.findFirst({
|
||||
where: {
|
||||
id: channel.id
|
||||
},
|
||||
include: {
|
||||
participants: true
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
const userInServer = server.participants.filter((e) => e.id === event.context.user.id)
|
||||
|
||||
if (!userInServer) {
|
||||
event.node.res.statusCode = 401;
|
||||
return {
|
||||
message: `You mus be in the server to access a channel in that server`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
channel
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user