-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathroute.ts
124 lines (95 loc) · 2.72 KB
/
route.ts
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { auth } from '@/app/(auth)/auth';
import type { ArtifactKind } from '@/components/artifact';
import {
deleteDocumentsByIdAfterTimestamp,
getDocumentsById,
getChatById,
saveDocument,
} from '@/lib/db/queries';
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const id = searchParams.get('id');
if (!id) {
return new Response('Missing id', { status: 400 });
}
const documents = await getDocumentsById({ id });
const [document] = documents;
if (!document) {
return new Response('Not Found', { status: 404 });
}
const session = await auth();
if (session?.user && document.userId === session.user.id) {
return Response.json(documents, { status: 200 });
}
const chat = await getChatById({ id: document.chatId });
if (!chat) {
return new Response('Not Found', { status: 404 });
}
if (chat.visibility === 'public') {
return Response.json(documents, { status: 200 });
}
return new Response('Unauthorized', { status: 401 });
}
export async function POST(request: Request) {
const { searchParams } = new URL(request.url);
const id = searchParams.get('id');
if (!id) {
return new Response('Missing id', { status: 400 });
}
const session = await auth();
if (!session?.user?.id) {
return new Response('Unauthorized', { status: 401 });
}
const {
content,
title,
kind,
chatId,
}: {
content: string;
title: string;
kind: ArtifactKind;
chatId: string;
} = await request.json();
const documents = await getDocumentsById({ id: id });
if (documents.length > 0) {
const [document] = documents;
if (document.userId !== session.user.id) {
return new Response('Forbidden', { status: 403 });
}
}
const document = await saveDocument({
id,
content,
title,
kind,
userId: session.user.id,
chatId,
});
return Response.json(document, { status: 200 });
}
export async function DELETE(request: Request) {
const { searchParams } = new URL(request.url);
const id = searchParams.get('id');
const timestamp = searchParams.get('timestamp');
if (!id) {
return new Response('Missing id', { status: 400 });
}
if (!timestamp) {
return new Response('Missing timestamp', { status: 400 });
}
const session = await auth();
if (!session?.user?.id) {
return new Response('Unauthorized', { status: 401 });
}
const documents = await getDocumentsById({ id });
const [document] = documents;
if (document.userId !== session.user.id) {
return new Response('Unauthorized', { status: 401 });
}
await deleteDocumentsByIdAfterTimestamp({
id,
timestamp: new Date(timestamp),
});
return new Response('Deleted', { status: 200 });
}