File size: 1,054 Bytes
c3e8f3d
d0a1b70
c3e8f3d
 
 
d0a1b70
 
 
 
 
 
f80b091
 
 
 
 
 
 
c3e8f3d
d0a1b70
 
 
 
 
 
c3e8f3d
f80b091
c3e8f3d
d0a1b70
 
 
 
 
 
 
 
f80b091
 
 
 
 
 
 
 
c3e8f3d
d0a1b70
c3e8f3d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
}