Spaces:
Running
Running
File size: 1,574 Bytes
98051f8 d72b096 d7b4e1d fa09831 98051f8 e3c83d9 d7b4e1d e3c83d9 98051f8 e3c83d9 fa09831 e3c83d9 cdc7c53 e3c83d9 7cb5b36 e3c83d9 98051f8 e3c83d9 cdc7c53 98051f8 c554635 e3c83d9 cdc7c53 e3c83d9 7cb5b36 e3c83d9 98051f8 e3c83d9 7cb5b36 d72b096 7cb5b36 |
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 |
import { base } from "$app/paths";
import { PUBLIC_ORIGIN, PUBLIC_SHARE_PREFIX } from "$env/static/public";
import { authCondition } from "$lib/server/auth";
import { collections } from "$lib/server/database";
import type { SharedConversation } from "$lib/types/SharedConversation";
import { hashConv } from "$lib/utils/hashConv.js";
import { error } from "@sveltejs/kit";
import { ObjectId } from "mongodb";
import { nanoid } from "nanoid";
export async function POST({ params, url, locals }) {
const conversation = await collections.conversations.findOne({
_id: new ObjectId(params.id),
...authCondition(locals),
});
if (!conversation) {
throw error(404, "Conversation not found");
}
const hash = await hashConv(conversation);
const existingShare = await collections.sharedConversations.findOne({ hash });
if (existingShare) {
return new Response(
JSON.stringify({
url: getShareUrl(url, existingShare._id),
}),
{ headers: { "Content-Type": "application/json" } }
);
}
const shared: SharedConversation = {
_id: nanoid(7),
createdAt: new Date(),
messages: conversation.messages,
hash,
updatedAt: new Date(),
title: conversation.title,
model: conversation.model,
};
await collections.sharedConversations.insertOne(shared);
return new Response(
JSON.stringify({
url: getShareUrl(url, shared._id),
}),
{ headers: { "Content-Type": "application/json" } }
);
}
function getShareUrl(url: URL, shareId: string): string {
return `${PUBLIC_SHARE_PREFIX || `${PUBLIC_ORIGIN || url.origin}${base}`}/r/${shareId}`;
}
|