File size: 1,038 Bytes
ee47ff3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { collections } from "$lib/server/database";
import { authCondition } from "$lib/server/auth";
import { z } from "zod";
import { ObjectId } from "mongodb";

export async function GET({ locals, params }) {
	const id = z.string().parse(params.id);
	const convId = new ObjectId(id);

	if (locals.user?._id || locals.sessionId) {
		const conv = await collections.conversations.findOne({
			_id: convId,
			...authCondition(locals),
		});

		if (conv) {
			const res = {
				id: conv._id,
				title: conv.title,
				updatedAt: conv.updatedAt,
				modelId: conv.model,
				messages: conv.messages.map((message) => ({
					content: message.content,
					from: message.from,
					id: message.id,
					createdAt: message.createdAt,
					updatedAt: message.updatedAt,
					webSearch: message.webSearch,
				})),
			};
			return Response.json(res);
		} else {
			return Response.json({ message: "Conversation not found" }, { status: 404 });
		}
	} else {
		return Response.json({ message: "Must have session cookie" }, { status: 401 });
	}
}