huggi / src /routes /settings /+server.ts
nsarrazin's picture
nsarrazin HF staff
Reworked settings menu (#591)
2cb745f unverified
raw
history blame
1.13 kB
import { collections } from "$lib/server/database";
import { z } from "zod";
import { models, validateModel } from "$lib/server/models";
import { authCondition } from "$lib/server/auth";
import { DEFAULT_SETTINGS } from "$lib/types/Settings";
export async function POST({ request, locals }) {
const body = await request.json();
const { ethicsModalAccepted, ...settings } = z
.object({
shareConversationsWithModelAuthors: z
.boolean()
.default(DEFAULT_SETTINGS.shareConversationsWithModelAuthors),
hideEmojiOnSidebar: z.boolean().default(DEFAULT_SETTINGS.hideEmojiOnSidebar),
ethicsModalAccepted: z.boolean().optional(),
activeModel: validateModel(models).default(DEFAULT_SETTINGS.activeModel),
customPrompts: z.record(z.string()).default({}),
})
.parse(body);
await collections.settings.updateOne(
authCondition(locals),
{
$set: {
...settings,
...(ethicsModalAccepted && { ethicsModalAcceptedAt: new Date() }),
updatedAt: new Date(),
},
$setOnInsert: {
createdAt: new Date(),
},
},
{
upsert: true,
}
);
// return ok response
return new Response();
}