File size: 1,169 Bytes
0c4cf03
1eff97d
0c4cf03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1eff97d
0c4cf03
 
 
 
 
 
 
 
 
 
 
 
 
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
import { collections } from "$lib/server/database";
import { hashConv } from "$lib/utils/hashConv.js";
import { error } from "@sveltejs/kit";
import { ObjectId } from "mongodb";

export async function GET({ params, locals }) {
	const searchId = new ObjectId(params.id);

	const search = await collections.webSearches.findOne({
		_id: searchId,
	});

	if (!search) {
		throw error(404, "Search query not found");
	}

	const conv = await collections.conversations.findOne({
		_id: search.convId,
	});

	if (!conv) {
		throw error(404, "Conversation not found");
	}

	// there's no better way to see if a conversation has been shared, so we hash the messages and see if there's a shared conversation with the same hash
	const hash = await hashConv(conv);
	const sharedConv = await collections.sharedConversations.findOne({
		hash: hash,
	});

	const userShouldSeeConv =
		(conv.userId && locals.user?._id.toString() === conv.userId.toString()) || sharedConv !== null;

	if (!userShouldSeeConv) {
		throw error(403, "You don't have access to the conversation here.");
	}

	return new Response(JSON.stringify(search), { headers: { "Content-Type": "application/json" } });
}