Spaces:
Sleeping
Sleeping
import { auth } from '@/auth'; | |
import { ChatEntity, MessageBase } from '@/lib/types'; | |
import { nanoid } from '@/lib/utils'; | |
import { kv } from '@vercel/kv'; | |
/** | |
* TODO: this should be replaced with actual upload to S3 | |
* @param req | |
* @returns | |
*/ | |
export async function POST(req: Request): Promise<Response> { | |
const session = await auth(); | |
const email = session?.user?.email; | |
if (!email) { | |
return new Response('Unauthorized', { | |
status: 401, | |
}); | |
} | |
const { url, initMessages } = (await req.json()) as { | |
url?: string; | |
file?: File; | |
base64?: string; | |
initMessages?: MessageBase[]; | |
}; | |
const id = nanoid(); | |
const payload: ChatEntity = { | |
url: url!, // TODO can be uploaded as well | |
id, | |
user: email, | |
messages: initMessages ?? [], | |
}; | |
await kv.hmset(`chat:${id}`, payload); | |
await kv.zadd(`user:chat:${email}`, { | |
score: Date.now(), | |
member: `chat:${id}`, | |
}); | |
await kv.zadd('user:chat:all', { | |
score: Date.now(), | |
member: `chat:${id}`, | |
}); | |
return Response.json(payload); | |
} | |