Spaces:
Paused
Paused
File size: 1,670 Bytes
ded72f6 | 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 | import { collections } from "$lib/server/database";
import { ObjectId } from "mongodb";
import { authCondition } from "$lib/server/auth";
import { convertLegacyConversation } from "$lib/utils/tree/convertLegacyConversation";
import { error } from "@sveltejs/kit";
/**
* Resolve a conversation by ID.
* - 7-char IDs → shared conversation lookup
* - ObjectId strings → owned conversation lookup with auth check
*
* Returns the conversation with legacy fields converted and a `shared` flag.
*/
export async function resolveConversation(
id: string,
locals: App.Locals,
fromShare?: string | null
) {
let conversation;
let shared = false;
if (id.length === 7) {
// shared link of length 7
conversation = await collections.sharedConversations.findOne({
_id: id,
});
shared = true;
if (!conversation) {
error(404, "Conversation not found");
}
} else {
try {
new ObjectId(id);
} catch {
error(400, "Invalid conversation ID format");
}
conversation = await collections.conversations.findOne({
_id: new ObjectId(id),
...authCondition(locals),
});
if (!conversation) {
const conversationExists =
(await collections.conversations.countDocuments({
_id: new ObjectId(id),
})) !== 0;
if (conversationExists) {
error(
403,
"You don't have access to this conversation. If someone gave you this link, ask them to use the 'share' feature instead."
);
}
error(404, "Conversation not found.");
}
if (fromShare && conversation.meta?.fromShareId === fromShare) {
shared = true;
}
}
return {
...conversation,
...convertLegacyConversation(conversation),
shared,
};
}
|